feat: add organisation questions to update view

This commit is contained in:
James Graham
2021-02-24 14:59:43 +00:00
parent adf12442a4
commit c8a68d542a
4 changed files with 89 additions and 10 deletions

View File

@@ -1,6 +1,7 @@
import typing
from django.contrib.auth.mixins import LoginRequiredMixin
from django.utils import timezone
from django.views.generic import CreateView, DetailView, ListView, UpdateView
from people import forms, models
@@ -30,6 +31,7 @@ class OrganisationDetailView(LoginRequiredMixin, DetailView):
"""Add map marker to context."""
context = super().get_context_data(**kwargs)
context['answer_set'] = self.object.current_answers
context['map_markers'] = [{
'name': self.object.name,
'lat': self.object.latitude,
@@ -44,7 +46,7 @@ class OrganisationUpdateView(LoginRequiredMixin, UpdateView):
model = models.Organisation
context_object_name = 'organisation'
template_name = 'people/organisation/update.html'
form_class = forms.OrganisationForm
form_class = forms.OrganisationAnswerSetForm
def get_context_data(self,
**kwargs: typing.Any) -> typing.Dict[str, typing.Any]:
@@ -58,3 +60,37 @@ class OrganisationUpdateView(LoginRequiredMixin, UpdateView):
}]
return context
def get_initial(self) -> typing.Dict[str, typing.Any]:
try:
previous_answers = self.object.current_answers.as_dict()
except AttributeError:
previous_answers = {}
previous_answers.update({
'organisation_id': self.object.id,
})
return previous_answers
def get_form_kwargs(self) -> typing.Dict[str, typing.Any]:
"""Remove instance from form kwargs as it's an Organisation, but expects an OrganisationAnswerSet."""
kwargs = super().get_form_kwargs()
kwargs.pop('instance')
return kwargs
def form_valid(self, form):
"""Mark any previous answer sets as replaced."""
response = super().form_valid(form)
now_date = timezone.now().date()
# Saving the form made self.object an OrganisationAnswerSet - so go up, then back down
# Shouldn't be more than one after initial updates after migration
for answer_set in self.object.organisation.answer_sets.exclude(
pk=self.object.pk):
answer_set.replaced_timestamp = now_date
answer_set.save()
return response