Skip to content
This repository has been archived by the owner on Apr 22, 2024. It is now read-only.

feat: filter submissions by payload content #412

Merged
merged 5 commits into from
Oct 15, 2018

Conversation

quesebifurcan
Copy link
Contributor

@quesebifurcan quesebifurcan commented Oct 12, 2018

Add a dynamic filter to SubmissionViewSet and EntityViewSet.

Any query parameter prefixed by "payload__" will filter submissions
based on the contents of e.g. Submission.payload.

Example:
The URL "/submissions?payload__a__b__c=1" will yield
the queryset

models.Submission.objects.filter(payload__a__b__c=1)

and return a list of submissions with a JSON payload like

'{"a": {"b": {"c": 1}}}'

Note that it is possible to compare not just strings, but
numbers, lists and objects as well.

Add a dynamic filter to SubmissionViewSet. Any query
parameter prefixed by "payload__" will filter submissions
based on the contents of Submission.payload.

Example:
The URL "/submissions?payload__a__b__c=1" will yield
the queryset

  models.Submission.objects.filter(payload__a__b__c=1)

and return a list of submissions with a JSON payload like

  '{"a": {"b": {"c": 1}}}'

Note that it is possible to compare not just strings, but
numbers, lists and objects as well.
@@ -310,6 +312,16 @@ class SubmissionViewSet(CustomViewSet):
serializer_class = serializers.SubmissionSerializer
filter_class = filters.SubmissionFilter

def get_queryset(self):
queryset = models.Submission.objects.all()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line will remove previous filters executed by the SubmissionFilter class like the project filter 😱
The correct line is queryset = self.queryset 😛

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh thanks, I thought this ran before the other filters. I'll take a look.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you are right, but imagine that the default queryset is something like this:

    queryset = Project.objects \
                      .prefetch_related('xforms', 'xforms__media_files') \
                      .order_by('name')

or

    queryset = models.Project \
                     .objects \
                     .values('id', 'name', 'created') \
                     .annotate(
                         first_submission=Min('submissions__created'),
                         last_submission=Max('submissions__created'),
                         submissions_count=Count('submissions__id', distinct=True),
                         entities_count=Count('submissions__entities__id', distinct=True),
                     )

🤷‍♀️

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, agreed it's much clearer to use self.queryset. Fixing.

@quesebifurcan quesebifurcan changed the title WIP feat: filter submissions by payload content feat: filter submissions by payload content Oct 12, 2018
@quesebifurcan quesebifurcan merged commit b362664 into develop Oct 15, 2018
@quesebifurcan quesebifurcan deleted the feat/generic-json-filters branch October 15, 2018 09:38
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants