refactor: views to handled ended relationships

Add ending of organisation relationships
This commit is contained in:
James Graham
2021-03-19 12:41:36 +00:00
parent 42d95beb5a
commit 74fffb0cac
8 changed files with 156 additions and 56 deletions

View File

@@ -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

View File

@@ -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`."""