forked from CenterForOpenScience/osf.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
institution-user metrics view (with basic filters)
- Loading branch information
Showing
6 changed files
with
229 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from __future__ import annotations | ||
import abc | ||
|
||
import elasticsearch_dsl as edsl | ||
from rest_framework import generics | ||
|
||
from api.base.filters import FilterMixin | ||
from api.base.views import JSONAPIBaseView | ||
|
||
|
||
class ElasticsearchListView(FilterMixin, JSONAPIBaseView, generics.ListAPIView, abc.ABC): | ||
'''use `elasticsearch_dsl.Search` as a queryset-analogue | ||
''' | ||
@property | ||
@abc.abstractmethod | ||
def elasticsearch_document_class(self) -> type[edsl.Document]: | ||
... | ||
|
||
@abc.abstractmethod | ||
def get_default_search(self) -> edsl.Search: | ||
... | ||
|
||
### | ||
# beware! rest_framework shenanigans below | ||
|
||
filter_backends = () # filtering handled in-view to reuse logic from FilterMixin | ||
|
||
# note: because elasticsearch_dsl.Search supports slicing and gives results when iterated on, | ||
# it should work fine with default pagination! | ||
|
||
# override rest_framework.generics.GenericAPIView | ||
def get_queryset(self): | ||
_search = self.get_default_search() | ||
# using parsing logic from FilterMixin (oddly nested dict and all) | ||
for _parsed_param in self.parse_query_params(self.request.query_params).values(): | ||
for _parsed_filter in _parsed_param.values(): | ||
_search = self.__add_search_filter( | ||
_search, | ||
field_name=_parsed_filter['source_field_name'], | ||
operation=_parsed_filter['op'], | ||
value=_parsed_filter['value'], | ||
) | ||
return _search | ||
|
||
def __add_search_filter( | ||
self, | ||
search: edsl.Search, | ||
field_name: str, | ||
operation: str, | ||
value: str, | ||
) -> edsl.Search: | ||
match operation: # operations from FilterMixin | ||
case 'eq': | ||
if value == '': | ||
return search.exclude('exists', field=field_name) | ||
return search.filter('term', **{field_name: value}) | ||
case 'ne': | ||
if value == '': | ||
return search.filter('exists', field=field_name) | ||
return search.exclude('term', **{field_name: value}) | ||
#case ('contains', 'icontains'): | ||
#case ('gt', 'gte', 'lt', 'lte'): | ||
case _: | ||
raise NotImplementedError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters