Skip to content

Commit

Permalink
feat(api): Add item metadata to markdown field for renderable items
Browse files Browse the repository at this point in the history
  • Loading branch information
jon-nfc committed Jan 7, 2025
1 parent f341ada commit b65da4e
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions app/api/react_ui_metadata.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import re

from django.conf import settings
from django.utils.encoding import force_str

Expand Down Expand Up @@ -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<model_type>[a-z_]+)-(?P<model_id>\d+)[\s|\n]?', value)
linked_tickets = re.findall(r'(?P<ticket>#(?P<number>\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


Expand Down

0 comments on commit b65da4e

Please sign in to comment.