-
Notifications
You must be signed in to change notification settings - Fork 42
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
Add non-searchable tags #398
base: master
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
|
@@ -144,6 +144,10 @@ | |
"type": "keyword" | ||
} | ||
} | ||
}, | ||
"props": { | ||
"type": "object", | ||
"dynamic": "true" | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -144,6 +144,10 @@ | |
"type": "keyword" | ||
} | ||
} | ||
}, | ||
"props": { | ||
"type": "object", | ||
"dynamic": "true" | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -10,11 +10,23 @@ | |||||||||||||||||||
|
||||||||||||||||||||
from invenio_records_permissions.generators import AuthenticatedUser, SystemProcess | ||||||||||||||||||||
|
||||||||||||||||||||
from ...services.permissions import PermissionPolicy | ||||||||||||||||||||
from invenio_vocabularies.services.generators import IfTags | ||||||||||||||||||||
from invenio_vocabularies.services.permissions import PermissionPolicy | ||||||||||||||||||||
|
||||||||||||||||||||
|
||||||||||||||||||||
class NamesPermissionPolicy(PermissionPolicy): | ||||||||||||||||||||
"""Permission policy.""" | ||||||||||||||||||||
"""Names permission policy. | ||||||||||||||||||||
|
||||||||||||||||||||
can_search = [SystemProcess(), AuthenticatedUser()] | ||||||||||||||||||||
can_read = [SystemProcess(), AuthenticatedUser()] | ||||||||||||||||||||
Names endpoints are protected, only authenticated users can access them. | ||||||||||||||||||||
""" | ||||||||||||||||||||
|
||||||||||||||||||||
can_search = [ | ||||||||||||||||||||
SystemProcess(), | ||||||||||||||||||||
IfTags(exclude=["unlisted"], only_authenticated=True), | ||||||||||||||||||||
] | ||||||||||||||||||||
Comment on lines
+23
to
+26
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.
Suggested change
minor: wouldn't that be a bit possible in terms of reusing/composing with existing generators? The |
||||||||||||||||||||
can_read = [SystemProcess(), IfTags(exclude=["unlisted"], only_authenticated=True)] | ||||||||||||||||||||
# this permission is needed for the /api/vocabularies/ endpoint | ||||||||||||||||||||
can_list_vocabularies = [ | ||||||||||||||||||||
SystemProcess(), | ||||||||||||||||||||
IfTags(exclude=["unlisted"], only_authenticated=True), | ||||||||||||||||||||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# -*- coding: utf-8 -*- | ||
Check failure on line 1 in invenio_vocabularies/contrib/names/services.py GitHub Actions / Python / Tests (3.9, postgresql14, opensearch2)
Check failure on line 1 in invenio_vocabularies/contrib/names/services.py GitHub Actions / Python / Tests (3.12, postgresql14, opensearch2)
|
||
# | ||
# Copyright (C) 2021 CERN. | ||
# | ||
|
@@ -19,11 +19,12 @@ | |
class NamesService(record_type.service_cls): | ||
"""Name service.""" | ||
|
||
def resolve(self, identity, id_, id_type): | ||
def resolve(self, identity, id_, id_type, many=False): | ||
"""Get the record with a given identifier. | ||
|
||
This method assumes that the are no duplicates in the system | ||
(i.e. only one name record can have a pair of identifier:scheme). | ||
param id_: The identifier value. | ||
param id_type: The identifier type. | ||
param many: If True, return a list of records. | ||
""" | ||
search_query = dsl.Q( | ||
"bool", | ||
|
@@ -36,20 +37,25 @@ | |
|
||
# max_records = 1, we assume there cannot be duplicates | ||
# the loading process needs to make sure of that | ||
results = self._read_many(identity, search_query, max_records=1) | ||
if many: | ||
results = self._read_many(identity, search_query) | ||
else: | ||
results = self._read_many(identity, search_query, max_records=1) | ||
|
||
# cant use the results_item because it returns dicts intead of records | ||
total = results.hits.total["value"] | ||
if total == 0: | ||
# Not a PID but trated as such | ||
raise PIDDoesNotExistError(pid_type=id_type, pid_value=id_) | ||
|
||
# (0 < #hits <= max_records) = 1 | ||
record = self.record_cls.loads(results[0].to_dict()) | ||
self.require_permission(identity, "read", record=record) | ||
|
||
return self.result_item( | ||
self, | ||
identity, | ||
record, | ||
links_tpl=self.links_item_tpl, | ||
) | ||
if many: | ||
return self.result_list(self, identity, results) | ||
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. is there permission needed to read many? I had this thought seeing the permission required below |
||
else: | ||
record = self.record_cls.loads(results[0].to_dict()) | ||
self.require_permission(identity, "read", record=record) | ||
|
||
return self.result_item( | ||
self, | ||
identity, | ||
record, | ||
links_tpl=self.links_item_tpl, | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (C) 2024 CERN. | ||
# | ||
# Invenio-Vocabularies is free software; you can redistribute it and/or | ||
# modify it under the terms of the MIT License; see LICENSE file for more | ||
# details. | ||
# | ||
|
||
"""Vocabulary generators.""" | ||
|
||
from invenio_access import any_user, authenticated_user | ||
from invenio_records_permissions.generators import Generator | ||
from invenio_search.engine import dsl | ||
|
||
|
||
class IfTags(Generator): | ||
"""Generator to filter based on tags. | ||
|
||
This generator will filter records based on the tags field. | ||
Optionally, it can be configured to only allow authenticated users. | ||
""" | ||
|
||
def __init__(self, include=None, exclude=None, only_authenticated=False): | ||
"""Constructor.""" | ||
self.include = include or [] | ||
self.exclude = exclude or [] | ||
self.only_authenticated = only_authenticated | ||
|
||
def needs(self, **kwargs): | ||
"""Enabling Needs.""" | ||
return [authenticated_user] if self.only_authenticated else [any_user] | ||
|
||
def query_filter(self, **kwargs): | ||
"""Search based on configured tags.""" | ||
must_clauses = [] | ||
must_not_clauses = [] | ||
|
||
if self.include: | ||
must_clauses.append(dsl.Q("terms", tags=self.include)) | ||
|
||
if self.exclude: | ||
must_not_clauses.append(dsl.Q("terms", tags=self.exclude)) | ||
|
||
return dsl.Q( | ||
"bool", | ||
must=must_clauses, | ||
must_not=must_not_clauses, | ||
) |
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.
Is array wanted and maybe copied from somewhere else? It might be difficult to have good search queries with arrays.