From 74fffb0cac6426b4f0342deade7744f8a4b6003e Mon Sep 17 00:00:00 2001 From: James Graham Date: Fri, 19 Mar 2021 12:41:36 +0000 Subject: [PATCH] refactor: views to handled ended relationships Add ending of organisation relationships --- people/models/relationship.py | 22 ++++-- .../organisation-relationship/detail.html | 68 +++++++++++++------ .../templates/people/organisation/detail.html | 8 +++ .../person/includes/relationships_full.html | 10 ++- .../templates/people/relationship/detail.html | 68 +++++++++++++------ people/urls.py | 4 ++ people/views/organisation.py | 24 +++++-- people/views/relationship.py | 8 +++ 8 files changed, 156 insertions(+), 56 deletions(-) diff --git a/people/models/relationship.py b/people/models/relationship.py index 5123764..ea68d0e 100644 --- a/people/models/relationship.py +++ b/people/models/relationship.py @@ -61,9 +61,14 @@ class Relationship(models.Model): @property def current_answers(self) -> typing.Optional['RelationshipAnswerSet']: - answer_set = self.answer_sets.latest() - if answer_set.is_current: - return answer_set + try: + answer_set = self.answer_sets.latest() + if answer_set.is_current: + return answer_set + + except RelationshipAnswerSet.DoesNotExist: + # No AnswerSet created yet + pass return None @@ -145,9 +150,14 @@ class OrganisationRelationship(models.Model): @property def current_answers(self) -> typing.Optional['OrganisationRelationshipAnswerSet']: - answer_set = self.answer_sets.latest() - if answer_set.is_current: - return answer_set + try: + answer_set = self.answer_sets.latest() + if answer_set.is_current: + return answer_set + + except OrganisationRelationshipAnswerSet.DoesNotExist: + # No AnswerSet created yet + pass return None diff --git a/people/templates/people/organisation-relationship/detail.html b/people/templates/people/organisation-relationship/detail.html index d4ea1ec..0d10870 100644 --- a/people/templates/people/organisation-relationship/detail.html +++ b/people/templates/people/organisation-relationship/detail.html @@ -17,6 +17,24 @@
+
+ + + {% if relationship.is_current %} + + {% endif %} +
+ +
+

Source

@@ -39,34 +57,40 @@
- Update - {% with relationship.current_answers as answer_set %} - - - - - - - + {% if answer_set is None %} +
+ This relationship has ended. You can start it again by updating it. +
- - {% for answer in answer_set.question_answers.all %} + {% else %} +
QuestionAnswer
+ - - + + + - {% empty %} - - - - {% endfor %} - -
{{ answer.question }}{{ answer }}QuestionAnswer
No records
+ + {% for answer in answer_set.question_answers.all %} + + {{ answer.question }} + {{ answer }} + - Last updated: {{ answer_set.timestamp }} + {% empty %} + + No records + + {% endfor %} + + + +

+ Last updated: {{ answer_set.timestamp }} +

+ {% endif %} {% endwith %} {% endblock %} diff --git a/people/templates/people/organisation/detail.html b/people/templates/people/organisation/detail.html index 807f769..1dd9065 100644 --- a/people/templates/people/organisation/detail.html +++ b/people/templates/people/organisation/detail.html @@ -42,6 +42,14 @@ {% endif %}
+ + {% if relationship %} + + {% endif %}

diff --git a/people/templates/people/person/includes/relationships_full.html b/people/templates/people/person/includes/relationships_full.html index cc67d8b..e1a1aa3 100644 --- a/people/templates/people/person/includes/relationships_full.html +++ b/people/templates/people/person/includes/relationships_full.html @@ -56,7 +56,15 @@ {% for relationship in person.organisation_relationships_as_source.all %} - {{ relationship.target }} + + {% if relationship.is_current %} + {{ relationship.target }} + {% else %} + + {{ relationship.target }} + + {% endif %} + Profile diff --git a/people/templates/people/relationship/detail.html b/people/templates/people/relationship/detail.html index 71db9d0..70ff1a0 100644 --- a/people/templates/people/relationship/detail.html +++ b/people/templates/people/relationship/detail.html @@ -17,6 +17,24 @@
+
+ + + {% if relationship.is_current %} + + {% endif %} +
+ +
+

Source

@@ -45,34 +63,40 @@
- Update - {% with relationship.current_answers as answer_set %} - - - - - - - + {% if answer_set is None %} +
+ This relationship has ended. You can start it again by updating it. +
- - {% for answer in answer_set.question_answers.all %} + {% else %} +
QuestionAnswer
+ - - + + + - {% empty %} - - - - {% endfor %} - -
{{ answer.question }}{{ answer }}QuestionAnswer
No records
+ + {% for answer in answer_set.question_answers.all %} + + {{ answer.question }} + {{ answer }} + - Last updated: {{ answer_set.timestamp }} + {% empty %} + + No records + + {% endfor %} + + + +

+ Last updated: {{ answer_set.timestamp }} +

+ {% endif %} {% endwith %} {% endblock %} diff --git a/people/urls.py b/people/urls.py index e38621b..25e526f 100644 --- a/people/urls.py +++ b/people/urls.py @@ -78,6 +78,10 @@ urlpatterns = [ views.relationship.OrganisationRelationshipUpdateView.as_view(), name='organisation.relationship.update'), + path('organisation-relationships//end', + views.relationship.OrganisationRelationshipEndView.as_view(), + name='organisation.relationship.end'), + ############ # Data views path('map', diff --git a/people/views/organisation.py b/people/views/organisation.py index db1b46a..9a2df8e 100644 --- a/people/views/organisation.py +++ b/people/views/organisation.py @@ -2,6 +2,7 @@ import typing from django.conf import settings from django.contrib.auth.mixins import LoginRequiredMixin +from django.core.exceptions import ObjectDoesNotExist from django.utils import timezone from django.views.generic import CreateView, DetailView, ListView, UpdateView @@ -93,9 +94,19 @@ class OrganisationListView(LoginRequiredMixin, ListView): context['orgs_by_country'] = self.sort_organisation_countries( orgs_by_country) - context['existing_relationships'] = set( - self.request.user.person.organisation_relationship_targets. - values_list('pk', flat=True)) + existing_relationships = set() + try: + existing_relationships = set( + self.request.user.person.organisation_relationships_as_source.filter( + answer_sets__replaced_timestamp__isnull=True + ).values_list('target_id', flat=True) + ) + + except ObjectDoesNotExist: + # No linked Person yet + pass + + context['existing_relationships'] = existing_relationships return context @@ -144,8 +155,11 @@ class OrganisationDetailView(LoginRequiredMixin, DetailView): context['relationship'] = None try: - context['relationship'] = models.OrganisationRelationship.objects.get( - source=self.request.user.person, target=self.object) # yapf: disable + relationship = models.OrganisationRelationship.objects.get( + source=self.request.user.person, target=self.object) + + if relationship.is_current: + context['relationship'] = relationship except models.OrganisationRelationship.DoesNotExist: pass diff --git a/people/views/relationship.py b/people/views/relationship.py index 3e638ec..fb6a5ee 100644 --- a/people/views/relationship.py +++ b/people/views/relationship.py @@ -116,6 +116,14 @@ class RelationshipEndView(permissions.UserIsLinkedPersonMixin, return relationship.target.get_absolute_url() +class OrganisationRelationshipEndView(RelationshipEndView): + """View for marking an organisation relationship as ended. + + Sets `replaced_timestamp` on all answer sets where this is currently null. + """ + model = models.OrganisationRelationship + + class OrganisationRelationshipDetailView(permissions.UserIsLinkedPersonMixin, DetailView): """View displaying details of an :class:`OrganisationRelationship`."""