mirror of
https://github.com/Southampton-RSG/breccia-mapper.git
synced 2026-03-03 03:17:07 +00:00
fix: Fix filters on network view
Filters now use OR for multiple choices in the same field
This commit is contained in:
@@ -35,7 +35,7 @@ class PersonForm(forms.ModelForm):
|
||||
|
||||
|
||||
class DynamicAnswerSetBase(forms.Form):
|
||||
field_class = forms.ChoiceField
|
||||
field_class = forms.ModelChoiceField
|
||||
field_widget = None
|
||||
field_required = True
|
||||
|
||||
@@ -43,11 +43,8 @@ class DynamicAnswerSetBase(forms.Form):
|
||||
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,
|
||||
queryset=question.answers,
|
||||
widget=self.field_widget,
|
||||
required=self.field_required)
|
||||
self.fields['question_{}'.format(question.pk)] = field
|
||||
@@ -85,6 +82,6 @@ class NetworkFilterForm(DynamicAnswerSetBase):
|
||||
"""
|
||||
Form to provide filtering on the network view.
|
||||
"""
|
||||
field_class = forms.MultipleChoiceField
|
||||
field_class = forms.ModelMultipleChoiceField
|
||||
field_widget = Select2MultipleWidget
|
||||
field_required = False
|
||||
|
||||
@@ -4,6 +4,7 @@ Views for displaying networks of :class:`People` and :class:`Relationship`s.
|
||||
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from django.db.models import Q
|
||||
from django.forms import ValidationError
|
||||
from django.utils import timezone
|
||||
from django.views.generic import FormView
|
||||
|
||||
@@ -38,22 +39,22 @@ class NetworkView(LoginRequiredMixin, FormView):
|
||||
Add filtered QuerySets of :class:`Person` and :class:`Relationship` to the context.
|
||||
"""
|
||||
context = super().get_context_data(**kwargs)
|
||||
form = context['form']
|
||||
form: forms.NetworkFilterForm = context['form']
|
||||
if not form.is_valid():
|
||||
raise ValidationError
|
||||
|
||||
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)
|
||||
for field, values in form.cleaned_data.items():
|
||||
if field.startswith('question_') and values:
|
||||
relationship_set = relationship_set.filter(
|
||||
# Time filters must be here
|
||||
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
|
||||
answer_sets__question_answers__in=values
|
||||
)
|
||||
|
||||
context['person_set'] = serializers.PersonSerializer(
|
||||
@@ -62,11 +63,15 @@ class NetworkView(LoginRequiredMixin, FormView):
|
||||
).data
|
||||
|
||||
context['relationship_set'] = serializers.RelationshipSerializer(
|
||||
relationship_set,
|
||||
relationship_set.distinct(),
|
||||
many=True
|
||||
).data
|
||||
|
||||
return context
|
||||
|
||||
def form_valid(self, form):
|
||||
try:
|
||||
return self.render_to_response(self.get_context_data())
|
||||
|
||||
except ValidationError:
|
||||
return self.form_invalid(form)
|
||||
|
||||
Reference in New Issue
Block a user