feat(people): Allow people to describe relationships

Creating a relationship requires answering each of the
relationship questions

resolve #2
partial #5
This commit is contained in:
James Graham
2020-02-20 14:46:51 +00:00
parent e1df999108
commit 5a1b043862
7 changed files with 147 additions and 1 deletions

View File

@@ -2,6 +2,7 @@
Views for displaying or manipulating models in the 'people' app.
"""
from django.http import HttpResponseRedirect
from django.views.generic import CreateView, DetailView, ListView
from . import forms, models
@@ -58,3 +59,48 @@ class RelationshipDetailView(DetailView):
"""
model = models.Relationship
template_name = 'people/relationship/detail.html'
class RelationshipCreateView(CreateView):
"""
View for creating a :class:`Relationship`.
Displays / processes a form containing the :class:`RelationshipQuestion`s.
"""
model = models.Relationship
template_name = 'people/relationship/create.html'
form_class = forms.RelationshipForm
def get(self, request, *args, **kwargs):
self.person = models.Person.objects.get(pk=self.kwargs.get('person_pk'))
return super().get(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
self.person = models.Person.objects.get(pk=self.kwargs.get('person_pk'))
return super().post(request, *args, **kwargs)
def get_initial(self):
initial = super().get_initial()
initial['source'] = self.request.user.person
initial['target'] = self.person
return initial
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['person'] = self.person
return context
def form_valid(self, form):
"""
Form is valid - create :class:`Relationship` and save answers to questions.
"""
self.object = form.save()
return HttpResponseRedirect(self.object.get_absolute_url())