Skip to content

Commit

Permalink
Support looking up and linking to a Campaign even in cases where ther…
Browse files Browse the repository at this point in the history
…e isn't yet a published Campaign or published related object (IOP, etc)
  • Loading branch information
Edward Keeble committed Nov 15, 2023
1 parent 8c0ec84 commit f06a675
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{% block header %}
<div class="col">
{% if campaign %}
<a href="{% url 'campaign-detail' 'campaign' campaign.uuid %}">&lt; Back to {{ campaign.short_name }}</a>
<a href="{% url 'campaign-detail' 'campaign' campaign.uuid %}">&lt; Back to {{ campaign.short_name|default:campaign.update.short_name }}</a>
{% else %}
<a href="{% url 'canonical-list' view_model %}">&lt; Back to {{ object.content_type.model_class | verbose_name_plural | title }}</a>
{% endif %}
Expand Down
4 changes: 4 additions & 0 deletions app/admin_ui/templates/api_app/canonical/change_update.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
TODO:

- Edit does not seem to be saving correctly. Drafts appear in the history but not as the latest draft for an object

{% extends "./change_form.html" %}
{% load static %}
{% load crispy_forms_tags %}
Expand Down
2 changes: 1 addition & 1 deletion app/admin_ui/templates/api_app/canonical/form_capable.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
{% block header %}
<div class="col">
{% if campaign %}
<a href="{% url 'campaign-detail' 'campaign' campaign.uuid %}">&lt; Back to {{ campaign.short_name }}</a>
<a href="{% url 'campaign-detail' 'campaign' campaign.uuid %}">&lt; Back to {{ campaign.short_name|default:campaign.update.short_name }}</a>
{% else %}
<a href="{% url 'canonical-list' view_model %}">&lt; Back to {{ object.content_type.model_class | verbose_name_plural | title }}</a>
{% endif %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{% block header %}
<div class="col">
{% if campaign %}
<a href="{% url 'campaign-detail' 'campaign' campaign.uuid %}">&lt; Back to {{ campaign.short_name }}</a>
<a href="{% url 'campaign-detail' 'campaign' campaign.uuid %}">&lt; Back to {{ campaign.short_name|default:campaign.update.short_name }}</a>
{% else %}
<a href="{% url 'change-history' view_model canonical_uuid %}">&lt; Back to {{ display_name }} History</a>
{% endif %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{% block header %}
<div class="col">
{% if campaign %}
<a href="{% url 'campaign-detail' 'campaign' campaign.uuid %}">&lt; Back to {{ campaign.short_name }}</a>
<a href="{% url 'campaign-detail' 'campaign' campaign.uuid %}">&lt; Back to {{ campaign.short_name|default:campaign.update.short_name }}</a>
{% else %}
<a href="{% url 'canonical-list' view_model %}">&lt; Back to {{ display_name }}</a>
{% endif %}
Expand Down
2 changes: 1 addition & 1 deletion app/admin_ui/templates/api_app/form_capable.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
{% block header %}
<div class="col">
{% if campaign %}
<a href="{% url 'campaign-detail' 'campaign' campaign.uuid %}">&lt; Back to {{ campaign.short_name }}</a>
<a href="{% url 'campaign-detail' 'campaign' campaign.uuid %}">&lt; Back to {{ campaign.short_name|default:campaign.update.short_name }}</a>
{% else %}
<a href="{% url 'change-list' view_model %}">&lt; Back to {{ object.content_type.model_class | verbose_name_plural | title }}</a>
{% endif %}
Expand Down
21 changes: 19 additions & 2 deletions app/admin_ui/views/v2.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Any, Dict

from django.contrib.auth.decorators import login_required
from django.core.exceptions import ObjectDoesNotExist
from django.utils.decorators import method_decorator
from django.views.generic.detail import DetailView
from django.views.generic.base import ContextMixin
Expand Down Expand Up @@ -70,14 +71,30 @@ def redirect_helper(request, canonical_uuid, model):
class CampaignRelatedView(ContextMixin):
def get_context_data(self, **kwargs) -> dict[str, Any]:
context = super().get_context_data(**kwargs)
model_class = None

if self.model is Change or isinstance(self.object, Change):
canonical_uuid = self.kwargs[self.pk_url_kwarg]
change_object = Change.objects.select_related('content_type').get(uuid=canonical_uuid)
model_instance = change_object._get_model_instance()
try:
model_instance = change_object._get_model_instance()
except ObjectDoesNotExist:
model_instance = None
model_class = change_object.content_type.model_class()
else:
model_instance = self.object
if isinstance(model_instance, DeploymentChildMixin) or isinstance(

# if we have a change object with no published record
if issubclass(model_class, DeploymentChildMixin) or model_class is Deployment:
print("Change object")
# check if Campaign exists otherwise look up Change object of type Campaign
try:
context["campaign"] = Campaign.objects.get(uuid=change_object.update["campaign"])
except Campaign.DoesNotExist:
context["campaign"] = Change.objects.of_type(Campaign).get(
uuid=change_object.update["campaign"]
)
elif isinstance(model_instance, DeploymentChildMixin) or isinstance(
model_instance, Deployment
):
context["campaign"] = model_instance.campaign
Expand Down

0 comments on commit f06a675

Please sign in to comment.