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 @property
def current_answers(self) -> typing.Optional['RelationshipAnswerSet']: def current_answers(self) -> typing.Optional['RelationshipAnswerSet']:
answer_set = self.answer_sets.latest() try:
if answer_set.is_current: answer_set = self.answer_sets.latest()
return answer_set if answer_set.is_current:
return answer_set
except RelationshipAnswerSet.DoesNotExist:
# No AnswerSet created yet
pass
return None return None
@@ -145,9 +150,14 @@ class OrganisationRelationship(models.Model):
@property @property
def current_answers(self) -> typing.Optional['OrganisationRelationshipAnswerSet']: def current_answers(self) -> typing.Optional['OrganisationRelationshipAnswerSet']:
answer_set = self.answer_sets.latest() try:
if answer_set.is_current: answer_set = self.answer_sets.latest()
return answer_set if answer_set.is_current:
return answer_set
except OrganisationRelationshipAnswerSet.DoesNotExist:
# No AnswerSet created yet
pass
return None return None

View File

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

View File

@@ -42,6 +42,14 @@
</a> </a>
{% endif %} {% endif %}
</div> </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> </div>
<hr> <hr>

View File

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

View File

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

View File

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

View File

@@ -2,6 +2,7 @@ import typing
from django.conf import settings from django.conf import settings
from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.mixins import LoginRequiredMixin
from django.core.exceptions import ObjectDoesNotExist
from django.utils import timezone from django.utils import timezone
from django.views.generic import CreateView, DetailView, ListView, UpdateView 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( context['orgs_by_country'] = self.sort_organisation_countries(
orgs_by_country) orgs_by_country)
context['existing_relationships'] = set( existing_relationships = set()
self.request.user.person.organisation_relationship_targets. try:
values_list('pk', flat=True)) 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 return context
@@ -144,8 +155,11 @@ class OrganisationDetailView(LoginRequiredMixin, DetailView):
context['relationship'] = None context['relationship'] = None
try: try:
context['relationship'] = models.OrganisationRelationship.objects.get( relationship = models.OrganisationRelationship.objects.get(
source=self.request.user.person, target=self.object) # yapf: disable source=self.request.user.person, target=self.object)
if relationship.is_current:
context['relationship'] = relationship
except models.OrganisationRelationship.DoesNotExist: except models.OrganisationRelationship.DoesNotExist:
pass pass

View File

@@ -116,6 +116,14 @@ class RelationshipEndView(permissions.UserIsLinkedPersonMixin,
return relationship.target.get_absolute_url() 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, class OrganisationRelationshipDetailView(permissions.UserIsLinkedPersonMixin,
DetailView): DetailView):
"""View displaying details of an :class:`OrganisationRelationship`.""" """View displaying details of an :class:`OrganisationRelationship`."""