mirror of
https://github.com/Southampton-RSG/breccia-mapper.git
synced 2026-03-03 11:27:09 +00:00
@@ -3,9 +3,10 @@ Views for displaying or manipulating models in the 'people' app.
|
||||
"""
|
||||
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.db.models import Q
|
||||
from django.urls import reverse
|
||||
from django.views.generic import CreateView, DetailView, ListView, TemplateView, UpdateView
|
||||
from django.utils import timezone
|
||||
from django.views.generic import CreateView, DetailView, FormView, ListView, UpdateView
|
||||
|
||||
from rest_framework.views import APIView, Response
|
||||
|
||||
@@ -174,11 +175,18 @@ class RelationshipUpdateView(permissions.UserIsLinkedPersonMixin, CreateView):
|
||||
|
||||
def form_valid(self, form):
|
||||
"""
|
||||
Don't rebind self.object to be the result of the form - it is a :class:`RelationshipAnswerSet`.
|
||||
Mark any previous answer sets as replaced.
|
||||
"""
|
||||
form.save()
|
||||
|
||||
return HttpResponseRedirect(self.relationship.get_absolute_url())
|
||||
previous_valid_answer_sets = self.relationship.answer_sets.filter(replaced_timestamp__isnull=True)
|
||||
|
||||
response = super().form_valid(form)
|
||||
|
||||
# Shouldn't be more than one after initial updates after migration
|
||||
for answer_set in previous_valid_answer_sets:
|
||||
answer_set.replaced_timestamp = timezone.now()
|
||||
answer_set.save()
|
||||
|
||||
return response
|
||||
|
||||
|
||||
class PersonApiView(APIView):
|
||||
@@ -207,8 +215,62 @@ class RelationshipApiView(APIView):
|
||||
return Response(serializer.data)
|
||||
|
||||
|
||||
class NetworkView(LoginRequiredMixin, TemplateView):
|
||||
class NetworkView(LoginRequiredMixin, FormView):
|
||||
"""
|
||||
View to display relationship network.
|
||||
"""
|
||||
template_name = 'people/network.html'
|
||||
form_class = forms.NetworkFilterForm
|
||||
|
||||
def get_form_kwargs(self):
|
||||
"""
|
||||
Add GET params to form data.
|
||||
"""
|
||||
kwargs = super().get_form_kwargs()
|
||||
|
||||
if self.request.method == 'GET':
|
||||
if 'data' in kwargs:
|
||||
kwargs['data'].update(self.request.GET)
|
||||
|
||||
else:
|
||||
kwargs['data'] = self.request.GET
|
||||
|
||||
return kwargs
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
"""
|
||||
Add filtered QuerySets of :class:`Person` and :class:`Relationship` to the context.
|
||||
"""
|
||||
context = super().get_context_data(**kwargs)
|
||||
form = context['form']
|
||||
|
||||
at_time = timezone.now()
|
||||
|
||||
relationship_set = models.Relationship.objects.all()
|
||||
|
||||
# Filter answers to relationship questions
|
||||
for key, value in form.data.items():
|
||||
if key.startswith('question_') and value:
|
||||
question_id = key.replace('question_', '', 1)
|
||||
answer = models.RelationshipQuestionChoice.objects.get(pk=value,
|
||||
question__pk=question_id)
|
||||
relationship_set = relationship_set.filter(
|
||||
Q(answer_sets__replaced_timestamp__gt=at_time) | Q(answer_sets__replaced_timestamp__isnull=True),
|
||||
answer_sets__timestamp__lte=at_time,
|
||||
answer_sets__question_answers=answer
|
||||
)
|
||||
|
||||
context['person_set'] = serializers.PersonSerializer(
|
||||
models.Person.objects.all(),
|
||||
many=True
|
||||
).data
|
||||
|
||||
context['relationship_set'] = serializers.RelationshipSerializer(
|
||||
relationship_set,
|
||||
many=True
|
||||
).data
|
||||
|
||||
return context
|
||||
|
||||
def form_valid(self, form):
|
||||
return self.render_to_response(self.get_context_data())
|
||||
|
||||
Reference in New Issue
Block a user