Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/jane/documents/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ def get_filtered_queryset_radial_distance(
Distance(km=deg2km(max_radius))))
return queryset

def get_filtered_queryset(self, document_type, queryset=None,
def get_filtered_queryset(self, document_type, queryset=None, negate=False,
**kwargs):
"""
Returns a queryset filtered on the items in the JSON document.
Expand All @@ -329,6 +329,12 @@ def get_filtered_queryset(self, document_type, queryset=None,
queryset is passed.
:param queryset: If no queryset is passed, a new one will be
created, otherwise an existing one will be used and filtered.
:type negate: bool
:param negate: When ``negate=True`` then the resulting queryset is
inverted, i.e. the resulting queryset returns all events that do
not match all given criteria. For example, ``.., negate=True,
max_magnitude=1, site="unterhaching")`` returns all events except
events at site Unterhaching that are below magnitude 1.
:param kwargs: Any additional query parameters.

Assuming a key named ``"example"`` in the JSON file you can search
Expand Down Expand Up @@ -362,6 +368,12 @@ def get_filtered_queryset(self, document_type, queryset=None,
``public=True``
``kwargs={"!public": True}``

The resulting query set can be inverted with ``negate=True``::

* For example, using ``.., negate=True, max_magnitude=1,
site="unterhaching")`` returns all events except for events that are
both declared at site Unterhaching and below magnitude 1.

Please note that as soon as you search for a value, all values that
are null will be discarded from the queryset (even if you search for
!=)! This might be changed in the future.
Expand Down Expand Up @@ -448,6 +460,11 @@ def get_filtered_queryset(self, document_type, queryset=None,
else:
raise NotImplementedError()

# if "negate" option is selected: instead of filtering to given
# criteria, filter out all items that match given criteria
if where != [] and negate:
where = ["NOT ({})".format(" AND ".join(where))]

Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

@megies megies May 3, 2016

Choose a reason for hiding this comment

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

Saw that one, but I didn't know how to use it together with how the json field is set up right now, as I didn't have a template in the source code.
Since these more canonically looking operators aren't used right now (and the rather hacky extra() instead), I figured that maybe they can't be used with the json field right now..

Sorry.. no real django knowledge here, so mostly orienting on the current source..

queryset = queryset.extra(where=where)

if "ordering" in kwargs and kwargs["ordering"] in meta:
Expand Down