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

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

View File

@@ -17,6 +17,24 @@
<hr>
<div class="row justify-content-md-center">
<div class="col-md-3">
<a class="btn btn-warning btn-block"
href="{% url 'people:organisation.relationship.update' pk=relationship.pk %}">Update Relationship
</a>
</div>
{% if relationship.is_current %}
<div class="col-md-3">
<a class="btn btn-danger btn-block"
href="{% url 'people:organisation.relationship.end' pk=relationship.pk %}">End Relationship
</a>
</div>
{% endif %}
</div>
<hr>
<div class="row align-content-center align-items-center">
<div class="col-md-5 text-center">
<h2>Source</h2>
@@ -39,34 +57,40 @@
<hr>
<a class="btn btn-success"
href="{% url 'people:organisation.relationship.update' pk=relationship.pk %}">Update</a>
{% with relationship.current_answers as answer_set %}
<table class="table table-borderless">
<thead>
<tr>
<th>Question</th>
<th>Answer</th>
</tr>
</thead>
{% if answer_set is None %}
<div class="alert alert-warning mt-3">
This relationship has ended. You can start it again by updating it.
</div>
<tbody>
{% for answer in answer_set.question_answers.all %}
{% else %}
<table class="table table-borderless">
<thead>
<tr>
<td>{{ answer.question }}</td>
<td>{{ answer }}</td>
<th>Question</th>
<th>Answer</th>
</tr>
</thead>
{% empty %}
<tr>
<td>No records</td>
</tr>
{% endfor %}
</tbody>
</table>
<tbody>
{% for answer in answer_set.question_answers.all %}
<tr>
<td>{{ answer.question }}</td>
<td>{{ answer }}</td>
</tr>
Last updated: {{ answer_set.timestamp }}
{% empty %}
<tr>
<td>No records</td>
</tr>
{% endfor %}
</tbody>
</table>
<p>
Last updated: {{ answer_set.timestamp }}
</p>
{% endif %}
{% endwith %}
{% endblock %}

View File

@@ -42,6 +42,14 @@
</a>
{% endif %}
</div>
{% if relationship %}
<div class="col-md-3">
<a class="btn btn-danger btn-block"
href="{% url 'people:organisation.relationship.end' pk=relationship.pk %}">End Relationship
</a>
</div>
{% endif %}
</div>
<hr>

View File

@@ -56,7 +56,15 @@
<tbody>
{% for relationship in person.organisation_relationships_as_source.all %}
<tr>
<td>{{ relationship.target }}</td>
<td>
{% if relationship.is_current %}
{{ relationship.target }}
{% else %}
<del>
{{ relationship.target }}
</del>
{% endif %}
</td>
<td>
<a class="btn btn-sm btn-info"
href="{% url 'people:organisation.detail' pk=relationship.target.pk %}">Profile</a>

View File

@@ -17,6 +17,24 @@
<hr>
<div class="row justify-content-md-center">
<div class="col-md-3">
<a class="btn btn-warning btn-block"
href="{% url 'people:relationship.update' pk=relationship.pk %}">Update Relationship
</a>
</div>
{% if relationship.is_current %}
<div class="col-md-3">
<a class="btn btn-danger btn-block"
href="{% url 'people:relationship.end' pk=relationship.pk %}">End Relationship
</a>
</div>
{% endif %}
</div>
<hr>
<div class="row align-content-center align-items-center">
<div class="col-md-5 text-center">
<h2>Source</h2>
@@ -45,34 +63,40 @@
<hr>
<a class="btn btn-success"
href="{% url 'people:relationship.update' pk=relationship.pk %}">Update</a>
{% with relationship.current_answers as answer_set %}
<table class="table table-borderless">
<thead>
<tr>
<th>Question</th>
<th>Answer</th>
</tr>
</thead>
{% if answer_set is None %}
<div class="alert alert-warning mt-3">
This relationship has ended. You can start it again by updating it.
</div>
<tbody>
{% for answer in answer_set.question_answers.all %}
{% else %}
<table class="table table-borderless">
<thead>
<tr>
<td>{{ answer.question }}</td>
<td>{{ answer }}</td>
<th>Question</th>
<th>Answer</th>
</tr>
</thead>
{% empty %}
<tr>
<td>No records</td>
</tr>
{% endfor %}
</tbody>
</table>
<tbody>
{% for answer in answer_set.question_answers.all %}
<tr>
<td>{{ answer.question }}</td>
<td>{{ answer }}</td>
</tr>
Last updated: {{ answer_set.timestamp }}
{% empty %}
<tr>
<td>No records</td>
</tr>
{% endfor %}
</tbody>
</table>
<p>
Last updated: {{ answer_set.timestamp }}
</p>
{% endif %}
{% endwith %}
{% endblock %}

View File

@@ -78,6 +78,10 @@ urlpatterns = [
views.relationship.OrganisationRelationshipUpdateView.as_view(),
name='organisation.relationship.update'),
path('organisation-relationships/<int:pk>/end',
views.relationship.OrganisationRelationshipEndView.as_view(),
name='organisation.relationship.end'),
############
# Data views
path('map',

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