diff --git a/people/forms.py b/people/forms.py index 9d7d3b6..e883607 100644 --- a/people/forms.py +++ b/people/forms.py @@ -23,9 +23,28 @@ class PersonForm(forms.ModelForm): 'country_of_residence': Select2Widget(), 'themes': Select2MultipleWidget(), } + + +class DynamicAnswerSetBase(forms.Form): + field_class = forms.ChoiceField + field_widget = None + field_required = True + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + for question in models.RelationshipQuestion.objects.all(): + # Get choices from model and add default 'not selected' option + choices = question.choices + [['', '---------']] + + field = self.field_class(label=question, + choices=choices, + widget=self.field_widget, + required=self.field_required) + self.fields['question_{}'.format(question.pk)] = field -class RelationshipAnswerSetForm(forms.ModelForm): +class RelationshipAnswerSetForm(forms.ModelForm, DynamicAnswerSetBase): """ Form to allow users to describe a relationship. @@ -37,17 +56,6 @@ class RelationshipAnswerSetForm(forms.ModelForm): 'relationship', ] - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - - for question in models.RelationshipQuestion.objects.all(): - # Get choices from model and add default 'not selected' option - choices = question.choices + [['', '---------']] - - field = forms.ChoiceField(label=question, - choices=choices) - self.fields['question_{}'.format(question.pk)] = field - def save(self, commit=True) -> models.RelationshipAnswerSet: # Save Relationship model self.instance = super().save(commit=commit) @@ -62,3 +70,12 @@ class RelationshipAnswerSetForm(forms.ModelForm): self.instance.question_answers.add(answer) return self.instance + + +class NetworkFilterForm(DynamicAnswerSetBase): + """ + Form to provide filtering on the network view. + """ + field_class = forms.MultipleChoiceField + field_widget = Select2MultipleWidget + field_required = False diff --git a/people/migrations/0017_answerset_replaced_timestamp.py b/people/migrations/0017_answerset_replaced_timestamp.py new file mode 100644 index 0000000..01bf966 --- /dev/null +++ b/people/migrations/0017_answerset_replaced_timestamp.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2.10 on 2020-03-09 15:23 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('people', '0016_add_answer_set'), + ] + + operations = [ + migrations.AddField( + model_name='relationshipanswerset', + name='replaced_timestamp', + field=models.DateTimeField(blank=True, editable=False, null=True), + ), + ] diff --git a/people/models/relationship.py b/people/models/relationship.py index 68e2eaf..9065ac8 100644 --- a/people/models/relationship.py +++ b/people/models/relationship.py @@ -152,4 +152,11 @@ class RelationshipAnswerSet(models.Model): question_answers = models.ManyToManyField(RelationshipQuestionChoice) #: When were these answers collected? - timestamp = models.DateTimeField(auto_now_add=True) + timestamp = models.DateTimeField(auto_now_add=True, + editable=False) + + replaced_timestamp = models.DateTimeField(blank=True, null=True, + editable=False) + + def get_absolute_url(self): + return self.relationship.get_absolute_url() diff --git a/people/templates/people/network.html b/people/templates/people/network.html index 4c6757a..e0e3deb 100644 --- a/people/templates/people/network.html +++ b/people/templates/people/network.html @@ -9,6 +9,27 @@
+
+ {% csrf_token %} + +
+
+

Filter Relationships

+ {% load bootstrap4 %} + {% bootstrap_form form %} +
+ +
+

Filter People

+
+ +
+ {% buttons %} + + {% endbuttons %} +
+
@@ -16,88 +37,28 @@ {% endblock %} {% block extra_script %} + + {{ person_set|json_script:'person-set-data' }} + + {{ relationship_set|json_script:'relationship-set-data' }} +