Skip to content

Commit

Permalink
Unify model_name_for_url into a property on both Change and BaseModel…
Browse files Browse the repository at this point in the history
… classes. Remove condition in object_header_tabs and expand type hint to include BaseModel for the change variable.
  • Loading branch information
Edward Keeble committed Nov 15, 2023
1 parent e3c16ea commit 6d459f6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
16 changes: 5 additions & 11 deletions app/admin_ui/templatetags/template_extras.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from django import template
from django.db.models import Q
from typing import Optional
from typing import Optional, Union

from api_app.models import Change
from api_app.urls import camel_to_snake
from data_models.models import BaseModel

register = template.Library()

Expand Down Expand Up @@ -47,7 +47,7 @@ def classname(obj):


@register.inclusion_tag('snippets/object_header_tabs.html', takes_context=True)
def object_header_tabs(context, change: Change, canonical_change: Optional[Change] = None):
def object_header_tabs(context, change: Union[Change, BaseModel], canonical_change: Optional[Change] = None):
"""
Reusable header for canonical object.
"""
Expand Down Expand Up @@ -89,13 +89,7 @@ def object_header_tabs(context, change: Change, canonical_change: Optional[Chang
)

draft_status_class = f"draft-status-{draft_status.lower().replace(' ', '-')}"

view_model = (
change.model_name_for_url
if isinstance(change, Change)
else camel_to_snake(change.__class__.__name__)
)


return {
"object": change,
"draft_status": draft_status,
Expand All @@ -104,5 +98,5 @@ def object_header_tabs(context, change: Change, canonical_change: Optional[Chang
"has_progress_draft": has_progress_draft,
"has_published_draft": has_published_draft,
"request": context.get("request"),
"view_model": view_model,
"view_model": change.model_name_for_url,
}
6 changes: 6 additions & 0 deletions app/data_models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.contrib.postgres.search import SearchQuery, SearchVector
from django.db import models


# TODO: Mv to config
FRONTEND_URL = "https://airborne-inventory.surge.sh/"
NOTES_INTERNAL_HELP_TEXT = "Free text notes for ADMG staff, this is NOT visible to the public."
Expand Down Expand Up @@ -69,6 +70,11 @@ def search(cls, params):

return queryset.filter(**params)

@property
def model_name_for_url(self):
from api_app.urls import camel_to_snake
return camel_to_snake(self.__class__.__name__)

def __str__(self):
return self.short_name

Expand Down

0 comments on commit 6d459f6

Please sign in to comment.