Skip to content

Commit

Permalink
private org/source/collection searchable for creator
Browse files Browse the repository at this point in the history
  • Loading branch information
snyaggarwal committed Feb 3, 2021
1 parent ad73ee5 commit 9294017
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
5 changes: 5 additions & 0 deletions core/collections/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Index:
identifier = fields.ObjectField()
publisher = fields.KeywordField(attr='publisher', normalizer='lowercase')
immutable = fields.KeywordField(attr='immutable')
created_by = fields.KeywordField()

class Django:
model = Collection
Expand Down Expand Up @@ -58,3 +59,7 @@ def prepare_identifier(instance):
value = jsonify_safe(instance.identifier)

return value or {}

@staticmethod
def prepare_created_by(instance):
return instance.created_by.username
18 changes: 16 additions & 2 deletions core/common/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,11 @@ def is_source_child_document_model(self):
from core.mappings.documents import MappingDocument
return self.document_model in [ConceptDocument, MappingDocument]

def is_concept_container_document_model(self):
from core.collections.documents import CollectionDocument
from core.sources.documents import SourceDocument
return self.document_model in [SourceDocument, CollectionDocument]

@cached_property
def __search_results(self): # pylint: disable=too-many-branches
results = None
Expand Down Expand Up @@ -353,8 +358,17 @@ def __search_results(self): # pylint: disable=too-many-branches

if self._should_exclude_retired_from_search_results():
results = results.query('match', retired=False)
if not self._should_include_private():
results = results.query('match', public_can_view=True)

include_private = self._should_include_private()

if not include_private:
from core.orgs.documents import OrganizationDocument
if self.document_model in [OrganizationDocument] and self.request.user.is_authenticated:
results.query(Q('match', public_can_view=False) & Q('match', user=self.request.user.username))
elif self.is_concept_container_document_model() and self.request.user.is_authenticated:
results.query(Q('match', public_can_view=False) & Q('match', created_by=self.request.user.username))
else:
results = results.query('match', public_can_view=True)

if self.is_owner_document_model():
kwargs_filters = self.kwargs
Expand Down
5 changes: 5 additions & 0 deletions core/sources/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Index:
jurisdiction = fields.ObjectField()
publisher = fields.KeywordField(attr='publisher', normalizer='lowercase')
content_type = fields.KeywordField(attr='content_type', normalizer='lowercase')
created_by = fields.KeywordField()

class Django:
model = Source
Expand Down Expand Up @@ -67,3 +68,7 @@ def prepare_jurisdiction(instance):
value = jsonify_safe(instance.jurisdiction)

return value or {}

@staticmethod
def prepare_created_by(instance):
return instance.created_by.username

0 comments on commit 9294017

Please sign in to comment.