From b65da4efac1ced63b90ac33ebe2d0e1ebdd438ed Mon Sep 17 00:00:00 2001 From: Jon Date: Tue, 7 Jan 2025 13:19:25 +0930 Subject: [PATCH] feat(api): Add item metadata to markdown field for renderable items ref: #466 #467 nofusscomputing/centurion_erp_ui#40 --- app/api/react_ui_metadata.py | 80 ++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/app/api/react_ui_metadata.py b/app/api/react_ui_metadata.py index f7df9b129..bdb6043e5 100644 --- a/app/api/react_ui_metadata.py +++ b/app/api/react_ui_metadata.py @@ -1,3 +1,5 @@ +import re + from django.conf import settings from django.utils.encoding import force_str @@ -270,6 +272,84 @@ def get_field_info(self, field): field.field_name in serializer.included_serializers ) + + if field_info["type"] == 'Markdown': + + linked_models = [] + + linked_tickets = [] + + field_info["render"] = { + 'models': {}, + 'tickets': {}, + } + + + if field.context['view'].kwargs.get('pk', None): + + from core.lib.slash_commands.linked_model import CommandLinkedModel + + value = getattr(list(field.context['view'].queryset)[0], field.source, None) + + if value: + + linked_models = re.findall(r'\s\$(?P[a-z_]+)-(?P\d+)[\s|\n]?', value) + linked_tickets = re.findall(r'(?P#(?P\d+))', value) + + from core.models.ticket.ticket import Ticket + + for ticket, number in linked_tickets: + + try: + + item = Ticket.objects.get( pk = number ) + + field_info["render"]['tickets'].update({ + number: { + 'status': Ticket.TicketStatus.All(item.status).label, + 'ticket_type': Ticket.TicketType(item.ticket_type).label, + 'title': str(item), + 'url': item.get_url() + } + }) + + except Ticket.DoesNotExist as e: + + pass + + + for model_type, model_id in linked_models: + + try: + + model, item_type = CommandLinkedModel().get_model( model_type ) + + if model: + + item = model.objects.get( pk = model_id ) + + item_meta = { + model_id: { + 'title': str(item), + 'url': item.get_url(), + } + } + + if not field_info["render"]['models'].get(model_type, None): + + field_info["render"]['models'].update({ + model_type: item_meta + }) + + else: + + field_info["render"]['models'][model_type].update( item_meta ) + + except Ticket.DoesNotExist as e: + + pass + + return field_info