diff --git a/app/core/lib/slash_commands/__init__.py b/app/core/lib/slash_commands/__init__.py index a4928ba1..ef680d6a 100644 --- a/app/core/lib/slash_commands/__init__.py +++ b/app/core/lib/slash_commands/__init__.py @@ -2,11 +2,13 @@ import re from .duration import Duration from .related_ticket import CommandRelatedTicket +from .linked_model import CommandLinkedModel class SlashCommands( Duration, - CommandRelatedTicket + CommandRelatedTicket, + CommandLinkedModel, ): """Slash Commands Base Class @@ -38,6 +40,8 @@ class SlashCommands( markdown = re.sub(self.time_spent, self.command_duration, markdown) + markdown = re.sub(self.linked_item, self.command_linked_model, markdown) + markdown = re.sub(self.related_ticket, self.command_related_ticket, markdown) return markdown diff --git a/app/core/lib/slash_commands/linked_model.py b/app/core/lib/slash_commands/linked_model.py new file mode 100644 index 00000000..dbd40c49 --- /dev/null +++ b/app/core/lib/slash_commands/linked_model.py @@ -0,0 +1,148 @@ +import re + + +class CommandLinkedModel: + # This summary is used for the user documentation + """Link an item to the current ticket. Supports all ticket +relations: blocked by, blocks and related. +The command keyword is `link` along with the model reference, i.e. `$-`. + +Valid commands are as follows: + +- /link $device-1 + +- /link $cluster-55 + +Available model types for linking are as follows: + +- cluster + +- config_group + +- device + +- operating_system + +- service + +- software + +For this command to process the following conditions must be met: + +- There is either a `` (`\\n`) or a `` char immediatly before the slash `/` + +- There is a `` char after the command keyword, i.e. `/link$device-101` +""" + + + linked_item: str = r'[\s|\n]\/(?P[link]+)\s\$(?P[a-z_]+)-(?P\d+)[\s|\n]?' + + + def command_linked_model(self, match) -> str: + """/link processor + + Slash command usage within a ticket description will add an action comment with the + time spent. For a ticket comment, it's duration field is set to the duration valuee calculated. + + Args: + match (re.Match): Named group matches + + Returns: + str: The matched string if the duration calculation is `0` + None: On successfully processing the command + """ + + a = 'a' + + command = match.group('command') + + model_type:int = str(match.group('type')) + model_id:int = int(match.group('id')) + + + try: + + from core.models.ticket.ticket_linked_items import TicketLinkedItem + + if model_type == 'cluster': + + from itim.models.clusters import Cluster + + model = Cluster + + item_type = TicketLinkedItem.Modules.CLUSTER + + elif model_type == 'config_group': + + from config_management.models.groups import ConfigGroups + + model = ConfigGroups + + item_type = TicketLinkedItem.Modules.CONFIG_GROUP + + elif model_type == 'device': + + from itam.models.device import Device + + model = Device + + item_type = TicketLinkedItem.Modules.DEVICE + + elif model_type == 'operating_system': + + from itam.models.operating_system import OperatingSystem + + model = OperatingSystem + + item_type = TicketLinkedItem.Modules.OPERATING_SYSTEM + + elif model_type == 'service': + + from itim.models.services import Service + + model = Service + + item_type = TicketLinkedItem.Modules.SERVICE + + elif model_type == 'software': + + from itam.models.software import Software + + model = Software + + item_type = TicketLinkedItem.Modules.SOFTWARE + + else: + + return str(match.string[match.start():match.end()]) + + + if str(self._meta.verbose_name).lower() == 'ticket': + + ticket = self + + elif str(self._meta.verbose_name).lower() == 'comment': + + ticket = self.ticket + + + if model: + + item = model.objects.get( + pk = model_id + ) + + TicketLinkedItem.objects.create( + organization = self.organization, + ticket = ticket, + item_type = item_type, + item = item.id + ) + + return None + + except Exception as e: + + return str(match.string[match.start():match.end()]) + + return None diff --git a/app/core/models/ticket/__init__.py b/app/core/models/ticket/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/docs/projects/centurion_erp/development/models.md b/docs/projects/centurion_erp/development/models.md index ab101d86..a142cde1 100644 --- a/docs/projects/centurion_erp/development/models.md +++ b/docs/projects/centurion_erp/development/models.md @@ -49,7 +49,9 @@ All models must meet the following requirements: This section details the additional items that may need to be done when adding a new model: -- If the model is a primary model, add to model reference rendering in `app/core/lib/markdown_plugins/model_reference.py` function `tag_html` +- If the model is a primary model, add it to model reference rendering in `app/core/lib/markdown_plugins/model_reference.py` function `tag_html` + +- If the model is a primary model, add it to the model link slash command in `app/core/lib/slash_commands/linked_model.py` function `command_linked_model` ## History diff --git a/docs/projects/centurion_erp/user/core/tickets.md b/docs/projects/centurion_erp/user/core/tickets.md index b6c40e04..b3da5eb4 100644 --- a/docs/projects/centurion_erp/user/core/tickets.md +++ b/docs/projects/centurion_erp/user/core/tickets.md @@ -13,6 +13,12 @@ The ticketing system within Centurion ERP is common to all ticket types. The dif - Commenting +- Linked Items to ticket + +- Milestone + +- Project + - Related Tickets - Slash commands @@ -47,6 +53,8 @@ Comment types are: Slash commands are a quick action that is specified after a slash command. As the name implies, the command starts with a slash `/`. The following slash commands are available: +- Linked Item `link` + - Related `/blocked_by`, `/blocks` and `/relate` - Time Spent `/spend`, `/spent` @@ -63,6 +71,17 @@ Slash commands are a quick action that is specified after a slash command. As th summary: true +### Linked Items + +::: app.core.lib.slash_commands.CommandLinkedModel + options: + inherited_members: false + members: [] + show_bases: false + show_submodules: false + summary: true + + ### Related Tickets ::: app.core.lib.slash_commands.CommandRelatedTicket diff --git a/mkdocs.yml b/mkdocs.yml index fa8915c9..49c686c1 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -93,6 +93,8 @@ nav: - projects/centurion_erp/development/api/models/access_organization_permission_checking.md + - projects/centurion_erp/development/api/models/ticket.md + - projects/centurion_erp/development/api/models/tenancy_object.md - projects/centurion_erp/development/api/common_views.md @@ -229,6 +231,12 @@ nav: - projects/centurion_erp/user/project_management/project.md + - projects/centurion_erp/user/project_management/project_state.md + + - projects/centurion_erp/user/project_management/project_task.md + + - projects/centurion_erp/user/project_management/project_type.md + - Settings: - projects/centurion_erp/user/settings/index.md