-
Notifications
You must be signed in to change notification settings - Fork 11
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
project: add collection
resource
#566
Merged
+2,034
−5,126
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Swiss Open Access Repository | ||
# Copyright (C) 2021 RERO | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as published by | ||
# the Free Software Foundation, version 3 of the License. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
"""Resource.""" |
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,92 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Swiss Open Access Repository | ||
# Copyright (C) 2021 RERO | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as published by | ||
# the Free Software Foundation, version 3 of the License. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
"""Record API.""" | ||
|
||
from functools import partial | ||
|
||
from ..api import SonarIndexer, SonarRecord, SonarSearch | ||
from ..fetchers import id_fetcher | ||
from ..providers import Provider | ||
from .config import Configuration | ||
from .minters import id_minter | ||
|
||
# provider | ||
RecordProvider = type('RecordProvider', (Provider, ), | ||
dict(pid_type=Configuration.pid_type)) | ||
# minter | ||
pid_minter = partial(id_minter, provider=RecordProvider) | ||
# fetcher | ||
pid_fetcher = partial(id_fetcher, provider=RecordProvider) | ||
|
||
|
||
class Record(SonarRecord): | ||
"""Record.""" | ||
|
||
minter = pid_minter | ||
fetcher = pid_fetcher | ||
provider = RecordProvider | ||
schema = Configuration.schema | ||
|
||
@classmethod | ||
def create(cls, data, id_=None, dbcommit=False, with_bucket=True, | ||
**kwargs): | ||
"""Create record. | ||
|
||
:param dict data: Metadata of the new record | ||
:param str id_: UUID to use if not generated | ||
:param bool dbcommit: Wether to commit into DB during creation | ||
:param bool with_bucket: Wether to create a bucket for record | ||
:return: New record instance | ||
:rtype: Record | ||
""" | ||
return super().create(data, | ||
id_=id_, | ||
dbcommit=dbcommit, | ||
with_bucket=with_bucket, | ||
**kwargs) | ||
|
||
@classmethod | ||
def get_pid_by_hash_key(cls, hash_key): | ||
"""Get a record by a hash key. | ||
|
||
:param str hash_key: Hash key to find. | ||
:return: The record found. | ||
:rtype: SonarRecord. | ||
""" | ||
result = RecordSearch().filter( | ||
'term', hashKey=hash_key).source(includes='pid').scan() | ||
try: | ||
return next(result).pid | ||
except StopIteration: | ||
return None | ||
|
||
|
||
class RecordSearch(SonarSearch): | ||
"""Record search.""" | ||
|
||
class Meta: | ||
"""Search only on item index.""" | ||
|
||
index = Configuration.index | ||
doc_types = [] | ||
|
||
|
||
class RecordIndexer(SonarIndexer): | ||
"""Indexing documents in Elasticsearch.""" | ||
|
||
record_cls = Record |
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,107 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Swiss Open Access Repository | ||
# Copyright (C) 2021 RERO | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as published by | ||
# the Free Software Foundation, version 3 of the License. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
"""Configuration.""" | ||
|
||
from sonar.modules.permissions import record_permission_factory | ||
|
||
# Resource name | ||
RESOURCE_NAME = 'collections' | ||
|
||
# JSON schema name | ||
JSON_SCHEMA_NAME = RESOURCE_NAME[:-1] | ||
|
||
# Module path | ||
MODULE_PATH = f'sonar.modules.{RESOURCE_NAME}' | ||
|
||
# PID type | ||
PID_TYPE = 'coll' | ||
|
||
|
||
class Configuration: | ||
"""Resource configuration.""" | ||
|
||
index = f'{RESOURCE_NAME}' | ||
schema = f'{RESOURCE_NAME}/{JSON_SCHEMA_NAME}-v1.0.0.json' | ||
pid_type = PID_TYPE | ||
resolver_url = f'/api/{RESOURCE_NAME}/<pid>' | ||
rest_endpoint = { | ||
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. I don't like PEP-8 80 characters line limit. Not easy to read :-) 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. Completely agree with you, we should increase this limit. |
||
'pid_type': | ||
PID_TYPE, | ||
'pid_minter': | ||
f'{RESOURCE_NAME}_id', | ||
'pid_fetcher': | ||
f'{RESOURCE_NAME}_id', | ||
'default_endpoint_prefix': | ||
True, | ||
'record_class': | ||
f'{MODULE_PATH}.api:Record', | ||
'search_class': | ||
f'{MODULE_PATH}.api:RecordSearch', | ||
'indexer_class': | ||
f'{MODULE_PATH}.api:RecordIndexer', | ||
'search_index': | ||
RESOURCE_NAME, | ||
'search_type': | ||
None, | ||
'record_serializers': { | ||
'application/json': (f'{MODULE_PATH}.serializers' | ||
':json_v1_response'), | ||
}, | ||
'search_serializers': { | ||
'application/json': (f'{MODULE_PATH}.serializers' | ||
':json_v1_search'), | ||
}, | ||
'record_loaders': { | ||
'application/json': (f'{MODULE_PATH}.loaders' | ||
':json_v1'), | ||
}, | ||
'list_route': | ||
f'/{RESOURCE_NAME}/', | ||
'item_route': | ||
f'/{RESOURCE_NAME}/<pid({PID_TYPE}, record_class="{MODULE_PATH}.api:Record"):pid_value>', | ||
'default_media_type': | ||
'application/json', | ||
'max_result_window': | ||
10000, | ||
'search_factory_imp': | ||
f'{MODULE_PATH}.query:search_factory', | ||
'create_permission_factory_imp': | ||
lambda record: record_permission_factory( | ||
action='create', cls=f'{MODULE_PATH}.permissions:RecordPermission' | ||
), | ||
'read_permission_factory_imp': | ||
lambda record: record_permission_factory( | ||
action='read', | ||
record=record, | ||
cls=f'{MODULE_PATH}.permissions:RecordPermission'), | ||
'update_permission_factory_imp': | ||
lambda record: record_permission_factory( | ||
action='update', | ||
record=record, | ||
cls=f'{MODULE_PATH}.permissions:RecordPermission'), | ||
'delete_permission_factory_imp': | ||
lambda record: record_permission_factory( | ||
action='delete', | ||
record=record, | ||
cls=f'{MODULE_PATH}.permissions:RecordPermission'), | ||
'list_permission_factory_imp': | ||
lambda record: record_permission_factory( | ||
action='list', | ||
record=record, | ||
cls=f'{MODULE_PATH}.permissions:RecordPermission') | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 this necessary if your
create
method did nothing more than class parent ?Into RERO-ILS project, we skip this declaration into children classes if there are no specific process.
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.
Yes it's mandatory because I need to set the default value for
with_bucket
toTrue
.