diff --git a/src/registrar/admin.py b/src/registrar/admin.py index 6c2c2e1a6..b2cc8144b 100644 --- a/src/registrar/admin.py +++ b/src/registrar/admin.py @@ -20,6 +20,7 @@ from . import models from auditlog.models import LogEntry # type: ignore from auditlog.admin import LogEntryAdmin # type: ignore from django_fsm import TransitionNotAllowed # type: ignore +from django.utils.safestring import mark_safe logger = logging.getLogger(__name__) @@ -415,7 +416,6 @@ class ContactAdmin(ListHeaderAdmin): "contact", "email", ] - change_form_template = "django/admin/contact_change_form.html" # We name the custom prop 'contact' because linter # is not allowing a short_description attr on it @@ -456,8 +456,7 @@ class ContactAdmin(ListHeaderAdmin): def change_view(self, request, object_id, form_url='', extra_context=None): """Extend the change_view for Contact objects in django admin. Customize to display related objects to the Contact. These will be passed - through the extra_context to the template for display to the user.""" - extra_context = extra_context or {} + through the messages construct to the template for display to the user.""" # Fetch the Contact instance contact = models.Contact.objects.get(pk=object_id) @@ -471,12 +470,14 @@ class ContactAdmin(ListHeaderAdmin): # Check if the related field is not None related_manager = getattr(contact, related_field.name) if related_manager is not None: - # Check if it's a ManyToManyField or a reverse ForeignKey/OneToOneField - # Do this by checking for a method on the related_manager + # Check if it's a ManyToManyField/reverse ForeignKey or a OneToOneField + # Do this by checking for get_queryset method on the related_manager if hasattr(related_manager, 'get_queryset'): + # Handles ManyToManyRel and ManyToOneRel queryset = related_manager.get_queryset() else: - queryset = related_manager.all() + # Handles OneToOne rels, ie. User + queryset = [related_manager] for obj in queryset: # for each object, build the edit url in this view and add as tuple @@ -487,9 +488,13 @@ class ContactAdmin(ListHeaderAdmin): change_url = reverse('admin:%s_%s_change' % (app_label, model_name), args=[obj_id]) related_objects.append((change_url, obj)) - # set the related_objects array in extra_context - extra_context['related_objects'] = related_objects - + if related_objects: + message = f"

Related Objects:

" + message_html = mark_safe(message) + messages.warning(request, message_html,) return super().change_view(request, object_id, form_url, extra_context=extra_context) diff --git a/src/registrar/templates/django/admin/contact_change_form.html b/src/registrar/templates/django/admin/contact_change_form.html deleted file mode 100644 index e3952a1ac..000000000 --- a/src/registrar/templates/django/admin/contact_change_form.html +++ /dev/null @@ -1,24 +0,0 @@ -{% extends 'admin/change_form.html' %} -{% load custom_filters %} - -{% block content %} - - {% if related_objects %} -
-

Related Objects:

- -
- {% endif %} - - {{ block.super }} - -{% endblock %} \ No newline at end of file diff --git a/src/registrar/templatetags/custom_filters.py b/src/registrar/templatetags/custom_filters.py index 5ac290b8b..de2051989 100644 --- a/src/registrar/templatetags/custom_filters.py +++ b/src/registrar/templatetags/custom_filters.py @@ -7,11 +7,6 @@ register = template.Library() logger = logging.getLogger(__name__) -@register.filter -def class_name(value): - return value.__class__.__name__ - - @register.filter(name="extract_value") def extract_value(html_input): match = re.search(r'value="([^"]*)"', html_input)