Repository navigation
OpenConceptLab/ocl_online#736 | Fuzzy concept search keeps to the URL's repository version; a reference's cascade walks only source versions its creator can view #1024
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
eed6957
78d8bbc
0024aca
c9e891c
695c24c
9e1826c
91628a7
3a31969
f03ca38
396efe3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -497,6 +497,11 @@ def get_faceted_fields(self): | |
| ) or field for field, config in es_fields.items() if config.get('facet', False) | ||
| ] | ||
|
|
||
| @property | ||
| def url_username(self): | ||
| """The user the URL names, the requester on the /user/ shortcut.""" | ||
| return self.kwargs.get('user') or (self.request.user.username if self.user_is_self else None) | ||
|
|
||
| def get_facet_filters_from_kwargs(self): | ||
| kwargs = self.kwargs | ||
| filters = {} | ||
|
|
@@ -505,7 +510,7 @@ def get_facet_filters_from_kwargs(self): | |
| if is_collection_specified: | ||
| version = kwargs.get('version') or HEAD | ||
| filters['collection'] = kwargs['collection'] | ||
| collection_owner_url = f'/users/{kwargs["user"]}/' if is_user_specified else f'/orgs/{kwargs["org"]}/' | ||
| collection_owner_url = f'/orgs/{kwargs["org"]}/' if 'org' in kwargs else f'/users/{self.url_username}/' | ||
| filters['collection_owner_url'] = collection_owner_url | ||
| if 'expansion' in self.kwargs: | ||
| filters['expansion_url'] = (f"{collection_owner_url}collections/{filters['collection']}/{version}/" | ||
|
|
@@ -539,7 +544,7 @@ def get_kwargs_filters(self): # pylint: disable=too-many-branches | |
| if is_version_specified: | ||
| container_version = self.kwargs['version'] | ||
| is_latest_released = container_version == LATEST | ||
| params = {'user__username': self.kwargs.get('user'), 'organization__mnemonic': self.kwargs.get('org')} | ||
| params = {'user__username': self.url_username, 'organization__mnemonic': self.kwargs.get('org')} | ||
| if is_latest_released: | ||
| if is_source_specified: | ||
| from core.sources.models import Source | ||
|
|
@@ -891,6 +896,58 @@ def is_canonical_specified(self): | |
| self.is_concept_container_document_model() or self.is_repo_document_model() | ||
| ) and self.request.query_params.get(CANONICAL_URL_REQUEST_PARAM, None) | ||
|
|
||
| def __apply_scope_filters(self, results): # pylint: disable=too-many-branches | ||
| """Limits a search to what the URL names: its owner, repository and version (or the requester's orgs, on a | ||
| members view).""" | ||
| user = self.request.user | ||
| is_authenticated = user.is_authenticated | ||
| username = user.username | ||
|
|
||
| is_members_view = self.is_members_view() | ||
| if self.is_owner_document_model(): | ||
| kwargs_filters = self.kwargs.copy() | ||
| if self.user_is_self and is_authenticated: | ||
| kwargs_filters.pop('user_is_self', None) | ||
| kwargs_filters['user'] = username | ||
| else: | ||
| kwargs_filters = self.get_kwargs_filters() | ||
| if is_members_view: | ||
| kwargs_filters['ownerType'] = 'Organization' | ||
| kwargs_filters['owner'] = self.get_members_view_org_mnemonics() or ['UNKNOWN-ORG-DUMMY'] | ||
| elif self.user_is_self and is_authenticated and 'collection' not in self.kwargs: | ||
| # a collection's concepts can come from any owner's source, so only other routes filter by owner | ||
| kwargs_filters['ownerType'] = 'User' | ||
| kwargs_filters['owner'] = username | ||
|
|
||
| if self.is_url_registry_document() and not is_members_view: | ||
| kwargs_filters = self.kwargs.copy() | ||
| if self.user_is_self and is_authenticated: | ||
| kwargs_filters.pop('user_is_self', None) | ||
| kwargs_filters['ownerType'] = 'User' | ||
| kwargs_filters['owner'] = username | ||
| elif not kwargs_filters: | ||
| kwargs_filters['ownerUrl'] = '/' | ||
| else: | ||
| if 'org' in kwargs_filters: | ||
| kwargs_filters['ownerType'] = 'Organization' | ||
| kwargs_filters['owner'] = kwargs_filters.pop('org') | ||
| elif 'user' in kwargs_filters: | ||
| kwargs_filters['ownerType'] = 'User' | ||
| kwargs_filters['owner'] = kwargs_filters.pop('user') | ||
|
|
||
| for key, value in kwargs_filters.items(): | ||
| attr = to_snake_case(key) | ||
| if value is None: # a version that doesn't resolve, such as `latest` with no release: nothing matches | ||
| results = results.filter(Q('match_none')) | ||
| elif isinstance(value, list): | ||
| criteria = Q('term', **{attr: get(value, '0')}) | ||
| for val in value[1:]: | ||
| criteria |= Q('term', **{attr: val}) | ||
| results = results.filter(criteria) | ||
| else: | ||
| results = results.filter('term', **{attr: value}) | ||
| return results | ||
|
|
||
| def __get_fuzzy_search_results( | ||
| self, source_versions=None, other_filters=None, sort=True | ||
| ): | ||
|
|
@@ -905,6 +962,8 @@ def __get_fuzzy_search_results( | |
| results = results.query( | ||
| self.__get_source_versions_es_criterion(source_versions) | ||
| ) | ||
| else: | ||
| results = self.__apply_scope_filters(results) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit (PR body). Without
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. HEAD URLs are now under the behaviour change in the PR body: a fuzzy search on |
||
| results = results.query(self.get_fuzzy_search_criterion()) | ||
|
|
||
| min_score = self.request.query_params.get('min_score') or None | ||
|
|
@@ -994,50 +1053,7 @@ def __get_search_results(self, ignore_retired_filter=False, sort=True, highlight | |
| for field, value in extras_fields_exact.items(): | ||
| results = results.query("match", **{field: value}, _expand__to_dot=False) | ||
|
|
||
| user = self.request.user | ||
| is_authenticated = user.is_authenticated | ||
| username = user.username | ||
|
|
||
| is_members_view = self.is_members_view() | ||
| if self.is_owner_document_model(): | ||
| kwargs_filters = self.kwargs.copy() | ||
| if self.user_is_self and is_authenticated: | ||
| kwargs_filters.pop('user_is_self', None) | ||
| kwargs_filters['user'] = username | ||
| else: | ||
| kwargs_filters = self.get_kwargs_filters() | ||
| if is_members_view: | ||
| kwargs_filters['ownerType'] = 'Organization' | ||
| kwargs_filters['owner'] = self.get_members_view_org_mnemonics() or ['UNKNOWN-ORG-DUMMY'] | ||
| elif self.user_is_self and is_authenticated: | ||
| kwargs_filters['ownerType'] = 'User' | ||
| kwargs_filters['owner'] = username | ||
|
|
||
| if self.is_url_registry_document() and not is_members_view: | ||
| kwargs_filters = self.kwargs.copy() | ||
| if self.user_is_self and is_authenticated: | ||
| kwargs_filters.pop('user_is_self', None) | ||
| kwargs_filters['ownerType'] = 'User' | ||
| kwargs_filters['owner'] = username | ||
| elif not kwargs_filters: | ||
| kwargs_filters['ownerUrl'] = '/' | ||
| else: | ||
| if 'org' in kwargs_filters: | ||
| kwargs_filters['ownerType'] = 'Organization' | ||
| kwargs_filters['owner'] = kwargs_filters.pop('org') | ||
| elif 'user' in kwargs_filters: | ||
| kwargs_filters['ownerType'] = 'User' | ||
| kwargs_filters['owner'] = kwargs_filters.pop('user') | ||
|
|
||
| for key, value in kwargs_filters.items(): | ||
| attr = to_snake_case(key) | ||
| if isinstance(value, list): | ||
| criteria = Q('term', **{attr: get(value, '0')}) | ||
| for val in value[1:]: | ||
| criteria |= Q('term', **{attr: val}) | ||
| results = results.filter(criteria) | ||
| else: | ||
| results = results.filter('term', **{attr: value}) | ||
| results = self.__apply_scope_filters(results) | ||
|
|
||
| sort_attrs = self._get_sort_attribute() | ||
| if has_search_string and self.is_concept_document() and ( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit (PR body). The How section calls this
CollectionReference._can_view_cascade_version. It also says the URL-scope block moves unchanged out of__get_search_results, but__apply_scope_filtersgains thematch_nonebranch and the'collection' not in self.kwargscondition. Both are listed under "From the review passes", so only the How section is out of date.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in the PR body: the How section names
_get_viewable_cascade_versionand says what__apply_scope_filtersgains on the move (thematch_nonebranch and the'collection' not in self.kwargscondition).