mirror of
https://github.com/Southampton-RSG/breccia-mapper.git
synced 2026-03-03 03:17:07 +00:00
refactor: allow question prefix on dynamic forms
Setup to support multiple network filters See #54
This commit is contained in:
@@ -41,6 +41,7 @@ class DynamicAnswerSetBase(forms.Form):
|
|||||||
field_widget: typing.Optional[typing.Type[forms.Widget]] = None
|
field_widget: typing.Optional[typing.Type[forms.Widget]] = None
|
||||||
question_model: typing.Type[models.Question]
|
question_model: typing.Type[models.Question]
|
||||||
answer_model: typing.Type[models.QuestionChoice]
|
answer_model: typing.Type[models.QuestionChoice]
|
||||||
|
question_prefix: str = ''
|
||||||
|
|
||||||
def __init__(self, *args, as_filters: bool = False, **kwargs):
|
def __init__(self, *args, as_filters: bool = False, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
@@ -64,7 +65,7 @@ class DynamicAnswerSetBase(forms.Form):
|
|||||||
field_class = forms.ModelMultipleChoiceField
|
field_class = forms.ModelMultipleChoiceField
|
||||||
field_widget = Select2MultipleWidget
|
field_widget = Select2MultipleWidget
|
||||||
|
|
||||||
field_name = f'question_{question.pk}'
|
field_name = f'{self.question_prefix}question_{question.pk}'
|
||||||
|
|
||||||
# If being used as a filter - do we have alternate text?
|
# If being used as a filter - do we have alternate text?
|
||||||
field_label = question.text
|
field_label = question.text
|
||||||
@@ -310,15 +311,33 @@ class OrganisationRelationshipAnswerSetForm(forms.ModelForm,
|
|||||||
return self.instance
|
return self.instance
|
||||||
|
|
||||||
|
|
||||||
class NetworkFilterForm(DynamicAnswerSetBase):
|
class NetworkRelationshipFilterForm(DynamicAnswerSetBase):
|
||||||
"""
|
"""Form to provide filtering on the network view."""
|
||||||
Form to provide filtering on the network view.
|
|
||||||
"""
|
|
||||||
field_class = forms.ModelMultipleChoiceField
|
field_class = forms.ModelMultipleChoiceField
|
||||||
field_widget = Select2MultipleWidget
|
field_widget = Select2MultipleWidget
|
||||||
field_required = False
|
field_required = False
|
||||||
question_model = models.RelationshipQuestion
|
question_model = models.RelationshipQuestion
|
||||||
answer_model = models.RelationshipQuestionChoice
|
answer_model = models.RelationshipQuestionChoice
|
||||||
|
question_prefix = 'relationship_'
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super().__init__(*args, as_filters=True, **kwargs)
|
||||||
|
|
||||||
|
# Add date field to select relationships at a particular point in time
|
||||||
|
self.fields['date'] = forms.DateField(
|
||||||
|
required=False,
|
||||||
|
widget=DatePickerInput(format='%Y-%m-%d'),
|
||||||
|
help_text='Show relationships as they were on this date')
|
||||||
|
|
||||||
|
|
||||||
|
class NetworkPersonFilterForm(DynamicAnswerSetBase):
|
||||||
|
"""Form to provide filtering on the network view."""
|
||||||
|
field_class = forms.ModelMultipleChoiceField
|
||||||
|
field_widget = Select2MultipleWidget
|
||||||
|
field_required = False
|
||||||
|
question_model = models.PersonQuestion
|
||||||
|
answer_model = models.PersonQuestionChoice
|
||||||
|
question_prefix = 'person_'
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, as_filters=True, **kwargs)
|
super().__init__(*args, as_filters=True, **kwargs)
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ class NetworkView(LoginRequiredMixin, FormView):
|
|||||||
View to display relationship network.
|
View to display relationship network.
|
||||||
"""
|
"""
|
||||||
template_name = 'people/network.html'
|
template_name = 'people/network.html'
|
||||||
form_class = forms.NetworkFilterForm
|
form_class = forms.NetworkRelationshipFilterForm
|
||||||
|
|
||||||
def get_form_kwargs(self):
|
def get_form_kwargs(self):
|
||||||
"""
|
"""
|
||||||
@@ -42,7 +42,7 @@ class NetworkView(LoginRequiredMixin, FormView):
|
|||||||
Add filtered QuerySets of :class:`Person` and :class:`Relationship` to the context.
|
Add filtered QuerySets of :class:`Person` and :class:`Relationship` to the context.
|
||||||
"""
|
"""
|
||||||
context = super().get_context_data(**kwargs)
|
context = super().get_context_data(**kwargs)
|
||||||
form: forms.NetworkFilterForm = context['form']
|
form: forms.NetworkRelationshipFilterForm = context['form']
|
||||||
if not form.is_valid():
|
if not form.is_valid():
|
||||||
return context
|
return context
|
||||||
|
|
||||||
@@ -65,7 +65,7 @@ class NetworkView(LoginRequiredMixin, FormView):
|
|||||||
|
|
||||||
# Filter answers to relationship questions
|
# Filter answers to relationship questions
|
||||||
for field, values in form.cleaned_data.items():
|
for field, values in form.cleaned_data.items():
|
||||||
if field.startswith('question_') and values:
|
if field.startswith(f'{form.question_prefix}question_') and values:
|
||||||
relationship_answerset_set = relationship_answerset_set.filter(
|
relationship_answerset_set = relationship_answerset_set.filter(
|
||||||
question_answers__in=values)
|
question_answers__in=values)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user