Skip to content

Commit

Permalink
OpenConceptLab/ocl_issues#622 | filtering users/orgs with updated sin…
Browse files Browse the repository at this point in the history
…ce in search results and in head call
  • Loading branch information
snyaggarwal committed Mar 16, 2021
1 parent 5c6293f commit f4f9451
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
16 changes: 10 additions & 6 deletions core/common/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
LIMIT_PARAM, NOT_FOUND, MUST_SPECIFY_EXTRA_PARAM_IN_BODY, INCLUDE_RETIRED_PARAM, VERBOSE_PARAM, HEAD
from core.common.mixins import PathWalkerMixin
from core.common.serializers import RootSerializer
from core.common.utils import compact_dict_by_values, to_snake_case, to_camel_case
from core.common.utils import compact_dict_by_values, to_snake_case, to_camel_case, parse_updated_since_param
from core.concepts.permissions import CanViewParentDictionary, CanEditParentDictionary
from core.orgs.constants import ORG_OBJECT_TYPE
from core.users.constants import USER_OBJECT_TYPE
Expand Down Expand Up @@ -46,11 +46,7 @@ def _should_exclude_retired_from_search_results(self):
return not include_retired

def _should_include_private(self):
from core.users.documents import UserProfileDocument
if self.document_model in [UserProfileDocument]:
return True

return self.request.user.is_staff
return self.is_user_document() or self.request.user.is_staff

def is_verbose(self):
return self.request.query_params.get(VERBOSE_PARAM, False) in ['true', True]
Expand Down Expand Up @@ -286,6 +282,10 @@ def get_extras_fields_exists_from_query_params(self):

return []

def is_user_document(self):
from core.users.documents import UserProfileDocument
return self.document_model == UserProfileDocument

def is_owner_document_model(self):
from core.orgs.documents import OrganizationDocument
from core.users.documents import UserProfileDocument
Expand Down Expand Up @@ -330,6 +330,10 @@ def __search_results(self): # pylint: disable=too-many-branches
"query_string", query=self.get_wildcard_search_string(), fields=self.get_searchable_fields()
)

updated_since = parse_updated_since_param(self.request.query_params)
if updated_since:
results = results.query('range', last_update={"gte": updated_since})

if extras_fields:
for field, value in extras_fields.items():
results = results.filter(
Expand Down
23 changes: 15 additions & 8 deletions core/orgs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from core.common.mixins import ListWithHeadersMixin
from core.common.permissions import HasPrivateAccess, CanViewConceptDictionary
from core.common.tasks import delete_organization
from core.common.utils import parse_updated_since_param
from core.common.views import BaseAPIView, BaseLogoView
from core.orgs.constants import DELETE_ACCEPTED
from core.orgs.documents import OrganizationDocument
Expand Down Expand Up @@ -43,15 +44,21 @@ def get_queryset(self):
username = self.kwargs.get('user')
if not username and self.user_is_self:
username = get(self.request.user, 'username')

if username:
return Organization.get_by_username(username)
if self.request.user.is_anonymous:
return Organization.get_public()
if self.request.user.is_superuser or self.request.user.is_staff:
return Organization.objects.filter(is_active=True)

queryset = Organization.get_by_username(self.request.user.username) | Organization.get_public()
return queryset.distinct()
self.queryset = Organization.get_by_username(username)
elif self.request.user.is_anonymous:
self.queryset = Organization.get_public()
elif self.request.user.is_superuser or self.request.user.is_staff:
self.queryset = Organization.objects.filter(is_active=True)
else:
self.queryset = Organization.get_by_username(self.request.user.username) | Organization.get_public()

updated_since = parse_updated_since_param(self.request.query_params)
if updated_since:
self.queryset = self.queryset.filter(updated_at__gte=updated_since)

return self.queryset.distinct()

def get_serializer_class(self):
if self.request.method == 'GET' and self.is_verbose():
Expand Down
1 change: 1 addition & 0 deletions core/users/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Index:
name = 'user_profiles'
settings = {'number_of_shards': 1, 'number_of_replicas': 0}

last_update = fields.DateField(attr='updated_at')
date_joined = fields.DateField(attr='created_at')
username = fields.KeywordField(attr='username', normalizer='lowercase')
location = fields.KeywordField(attr='location', normalizer='lowercase')
Expand Down
7 changes: 7 additions & 0 deletions core/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from rest_framework.response import Response

from core.common.mixins import ListWithHeadersMixin
from core.common.utils import parse_updated_since_param
from core.common.views import BaseAPIView, BaseLogoView
from core.orgs.models import Organization
from core.users.constants import VERIFICATION_TOKEN_MISMATCH, VERIFY_EMAIL_MESSAGE
Expand Down Expand Up @@ -55,6 +56,12 @@ class UserBaseView(BaseAPIView):
is_searchable = True
default_qs_sort_attr = '-date_joined'

def get_queryset(self):
updated_since = parse_updated_since_param(self.request.query_params)
if updated_since:
self.queryset = self.queryset.filter(updated_at__gte=updated_since)
return self.queryset


class UserLogoView(UserBaseView, BaseLogoView):
serializer_class = UserDetailSerializer
Expand Down

0 comments on commit f4f9451

Please sign in to comment.