diff --git a/rero_ils/config.py b/rero_ils/config.py index 7e16c7395a..f5836bfecf 100644 --- a/rero_ils/config.py +++ b/rero_ils/config.py @@ -75,6 +75,7 @@ from .modules.notifications.api import Notification from .modules.organisations.api import Organisation from .modules.organisations.permissions import can_update_organisations_factory +from .modules.patron_transactions.api import PatronTransaction from .modules.patron_types.api import PatronType from .modules.patrons.api import Patron from .modules.patrons.permissions import can_delete_patron_factory, \ @@ -521,6 +522,39 @@ def _(x): update_permission_factory_imp=can_update_patron_factory, delete_permission_factory_imp=can_delete_patron_factory, ), + pttr=dict( + pid_type='pttr', + pid_minter='patron_transaction_id', + pid_fetcher='patron_transaction_id', + search_class=RecordsSearch, + search_index='patron_transactions', + search_type=None, + indexer_class=IlsRecordIndexer, + record_serializers={ + 'application/json': ( + 'rero_ils.modules.serializers:json_v1_response' + ) + }, + search_serializers={ + 'application/json': ( + 'rero_ils.modules.serializers:json_v1_search' + ) + }, + record_loaders={ + 'application/json': lambda: PatronTransaction(request.get_json()), + }, + record_class='rero_ils.modules.patron_transactions.api:PatronTransaction', + list_route='/patron_transactions/', + item_route='/patron_transactions/', + default_media_type='application/json', + max_result_window=10000, + search_factory_imp='rero_ils.query:search_factory', + read_permission_factory_imp=allow_all, + list_permission_factory_imp=allow_all, + create_permission_factory_imp=deny_all, + update_permission_factory_imp=deny_all, + delete_permission_factory_imp=deny_all, + ), ptty=dict( pid_type='ptty', pid_minter='patron_type_id', @@ -1236,6 +1270,7 @@ def _(x): 'budg': '/budgets/budget-v0.0.1.json', 'acor': '/acq_orders/acq_order-v0.0.1.json', 'acol': '/acq_order_lines/acq_order_line-v0.0.1.json', + 'pttr': '/patron_transactions/patron_transaction-v0.0.1.json', } # Login Configuration diff --git a/rero_ils/modules/patron_transactions/__init__.py b/rero_ils/modules/patron_transactions/__init__.py new file mode 100644 index 0000000000..1869883559 --- /dev/null +++ b/rero_ils/modules/patron_transactions/__init__.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- +# +# RERO ILS +# Copyright (C) 2019 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 . + +"""PatronTransaction Records.""" diff --git a/rero_ils/modules/patron_transactions/api.py b/rero_ils/modules/patron_transactions/api.py new file mode 100644 index 0000000000..3b3755971f --- /dev/null +++ b/rero_ils/modules/patron_transactions/api.py @@ -0,0 +1,56 @@ +# -*- coding: utf-8 -*- +# +# RERO ILS +# Copyright (C) 2019 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 . + +"""API for manipulating patron_transactions.""" + +from functools import partial + +from .models import PatronTransactionIdentifier +from ..api import IlsRecord, IlsRecordsSearch +from ..fetchers import id_fetcher +from ..minters import id_minter +from ..providers import Provider + +# provider +PatronTransactionProvider = type( + 'PatronTransactionProvider', + (Provider,), + dict(identifier=PatronTransactionIdentifier, pid_type='pttr') +) +# minter +patron_transaction_id_minter = partial( + id_minter, provider=PatronTransactionProvider) +# fetcher +patron_transaction_id_fetcher = partial( + id_fetcher, provider=PatronTransactionProvider) + + +class PatronTransactionsSearch(IlsRecordsSearch): + """PatronTransactionsSearch.""" + + class Meta: + """Search only on patron_transaction index.""" + + index = 'patron_transactions' + + +class PatronTransaction(IlsRecord): + """PatronTransaction class.""" + + minter = patron_transaction_id_minter + fetcher = patron_transaction_id_fetcher + provider = PatronTransactionProvider diff --git a/rero_ils/modules/patron_transactions/jsonresolver.py b/rero_ils/modules/patron_transactions/jsonresolver.py new file mode 100644 index 0000000000..7e873e3050 --- /dev/null +++ b/rero_ils/modules/patron_transactions/jsonresolver.py @@ -0,0 +1,38 @@ +# -*- coding: utf-8 -*- +# +# RERO ILS +# Copyright (C) 2019 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 . + +"""PatronTransaction resolver.""" + +import jsonresolver +from flask import current_app +from invenio_pidstore.models import PersistentIdentifier, PIDStatus + + +@jsonresolver.route('/api/patron_transactions/', host='ils.rero.ch') +def patron_transaction_resolver(pid): + """Resolver for patron_transaction record.""" + persistent_id = PersistentIdentifier.get('pttr', pid) + if persistent_id.status == PIDStatus.REGISTERED: + return dict(pid=persistent_id.pid_value) + current_app.logger.error( + 'record resolver error: /api/patron_transactions/{pid} \ + {persistent_id}'.format( + pid=pid, + persistent_id=persistent_id + ) + ) + raise Exception('unable to resolve') diff --git a/rero_ils/modules/patron_transactions/jsonschemas/__init__.py b/rero_ils/modules/patron_transactions/jsonschemas/__init__.py new file mode 100644 index 0000000000..6950e6e40e --- /dev/null +++ b/rero_ils/modules/patron_transactions/jsonschemas/__init__.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# +# RERO ILS +# Copyright (C) 2019 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 . + +"""JSON schemas.""" + +from __future__ import absolute_import, print_function diff --git a/rero_ils/modules/patron_transactions/jsonschemas/patron_transactions/patron_transaction-v0.0.1.json b/rero_ils/modules/patron_transactions/jsonschemas/patron_transactions/patron_transaction-v0.0.1.json new file mode 100644 index 0000000000..2b6013b902 --- /dev/null +++ b/rero_ils/modules/patron_transactions/jsonschemas/patron_transactions/patron_transaction-v0.0.1.json @@ -0,0 +1,116 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "type": "object", + "title": "Patron Transaction", + "description": "JSON schema for Patron Transaction.", + "additionalProperties": false, + "required": [ + "$schema", + "pid" + ], + "properties": { + "$schema": { + "title": "Schema", + "description": "Schema to validate Patron Transaction records against.", + "type": "string", + "minLength": 9, + "default": "https://ils.rero.ch/schema/patron_transactions/patron_transaction-v0.0.1.json" + }, + "pid": { + "title": "Patron Transaction ID", + "type": "string", + "minLength": 1 + }, + "creation_date": { + "type": "string", + "format": "date-time", + "title": "Patron Transaction creation date" + }, + "note": { + "title": "Patron Transaction Note", + "description": "Additional informations about patron transaction", + "type": "string", + "minLength": 3 + }, + "status": { + "title": "Status", + "type": "string", + "enum": [ + "open", + "closed" + ], + "default": "open", + "form": { + "options": [ + { + "label": "open", + "value": "open" + }, + { + "label": "closed", + "value": "closed" + } + ] + } + }, + "type": { + "title": "Patron Transaction Type", + "type": "string", + "enum": [ + "overdue", + "photocopy", + "subscription" + ], + "default": "overdue", + "form": { + "options": [ + { + "label": "overdue", + "value": "overdue" + }, + { + "label": "photocopy", + "value": "photocopy" + }, + { + "label": "subscription", + "value": "subscription" + } + ] + } + }, + "patron": { + "title": "Patron", + "type": "object", + "properties": { + "$ref": { + "title": "Patron URI", + "type": "string", + "pattern": "^https://ils.rero.ch/api/patrons/.*?$" + } + } + }, + "loan": { + "title": "Loan", + "type": "object", + "properties": { + "$ref": { + "title": "Loan URI", + "type": "string", + "pattern": "^^https://ils.rero.ch/api/loans/.*?$" + } + } + }, + "notification": { + "title": "Notification", + "type": "object", + "properties": { + "$ref": { + "title": "Notification URI", + "type": "string", + "pattern": "^^https://ils.rero.ch/api/notifications/.*?$" + } + } + } + } +} \ No newline at end of file diff --git a/rero_ils/modules/patron_transactions/mappings/__init__.py b/rero_ils/modules/patron_transactions/mappings/__init__.py new file mode 100644 index 0000000000..b2c554268f --- /dev/null +++ b/rero_ils/modules/patron_transactions/mappings/__init__.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# +# RERO ILS +# Copyright (C) 2019 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 . + +"""Elasticsearch mappings.""" + +from __future__ import absolute_import, print_function diff --git a/rero_ils/modules/patron_transactions/mappings/v6/__init__.py b/rero_ils/modules/patron_transactions/mappings/v6/__init__.py new file mode 100644 index 0000000000..b2c554268f --- /dev/null +++ b/rero_ils/modules/patron_transactions/mappings/v6/__init__.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# +# RERO ILS +# Copyright (C) 2019 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 . + +"""Elasticsearch mappings.""" + +from __future__ import absolute_import, print_function diff --git a/rero_ils/modules/patron_transactions/mappings/v6/patron_transactions/patron_transaction-v0.0.1.json b/rero_ils/modules/patron_transactions/mappings/v6/patron_transactions/patron_transaction-v0.0.1.json new file mode 100644 index 0000000000..86eb43ff72 --- /dev/null +++ b/rero_ils/modules/patron_transactions/mappings/v6/patron_transactions/patron_transaction-v0.0.1.json @@ -0,0 +1,60 @@ +{ + "settings": { + "number_of_shards": 1, + "number_of_replicas": 0, + "max_result_window": 20000 + }, + "mappings": { + "patron_transaction-v0.0.1": { + "date_detection": false, + "numeric_detection": false, + "properties": { + "$schema": { + "type": "keyword" + }, + "pid": { + "type": "keyword" + }, + "creation_date": { + "type": "date" + }, + "note": { + "type": "keyword" + }, + "status": { + "type": "keyword" + }, + "type": { + "type": "keyword" + }, + "patron": { + "properties": { + "pid": { + "type": "keyword" + } + } + }, + "loan": { + "properties": { + "pid": { + "type": "keyword" + } + } + }, + "notification": { + "properties": { + "pid": { + "type": "keyword" + } + } + }, + "_created": { + "type": "date" + }, + "_updated": { + "type": "date" + } + } + } + } +} diff --git a/rero_ils/modules/patron_transactions/models.py b/rero_ils/modules/patron_transactions/models.py new file mode 100644 index 0000000000..fe21208bfc --- /dev/null +++ b/rero_ils/modules/patron_transactions/models.py @@ -0,0 +1,35 @@ +# -*- coding: utf-8 -*- +# +# RERO ILS +# Copyright (C) 2019 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 . + +"""Define relation between records and buckets.""" + +from __future__ import absolute_import + +from invenio_db import db +from invenio_pidstore.models import RecordIdentifier + + +class PatronTransactionIdentifier(RecordIdentifier): + """Sequence generator for PatronTransaction identifiers.""" + + __tablename__ = 'patron_transaction_id' + __mapper_args__ = {'concrete': True} + + recid = db.Column( + db.BigInteger().with_variant(db.Integer, 'sqlite'), + primary_key=True, autoincrement=True, + ) diff --git a/setup.py b/setup.py index 8cc0447234..44ad8f8d81 100644 --- a/setup.py +++ b/setup.py @@ -164,6 +164,8 @@ def run(self): 'budgets = rero_ils.modules.budgets.models', 'acq_orders = rero_ils.modules.acq_orders.models', 'acq_order_lines = rero_ils.modules.acq_order_lines.models', + 'patron_transactions = \ + rero_ils.modules.patron_transactions.models', ], 'invenio_pidstore.minters': [ 'organisation_id = \ @@ -192,8 +194,12 @@ def run(self): 'acq_account_id = \ rero_ils.modules.acq_accounts.api:acq_account_id_minter', 'budget_id = rero_ils.modules.budgets.api:budget_id_minter', - 'acq_order_id = rero_ils.modules.acq_orders.api:acq_order_id_minter', - 'acq_order_line_id = rero_ils.modules.acq_order_lines.api:acq_order_line_id_minter', + 'acq_order_id = \ + rero_ils.modules.acq_orders.api:acq_order_id_minter', + 'acq_order_line_id = \ + rero_ils.modules.acq_order_lines.api:acq_order_line_id_minter', + 'patron_transaction_id = \ + rero_ils.modules.patron_transactions.api:patron_transaction_id_minter', ], 'invenio_pidstore.fetchers': [ 'organisation_id = rero_ils.modules.organisations' @@ -225,8 +231,12 @@ def run(self): 'acq_account_id = \ rero_ils.modules.acq_accounts.api:acq_account_id_fetcher', 'budget_id = rero_ils.modules.budgets.api:budget_id_fetcher', - 'acq_order_id = rero_ils.modules.acq_orders.api:acq_order_id_fetcher', - 'acq_order_line_id = rero_ils.modules.acq_order_lines.api:acq_order_line_id_fetcher', + 'acq_order_id = \ + rero_ils.modules.acq_orders.api:acq_order_id_fetcher', + 'acq_order_line_id = \ + rero_ils.modules.acq_order_lines.api:acq_order_line_id_fetcher', + 'patron_transaction_id = \ + rero_ils.modules.patron_transactions.api:patron_transaction_id_fetcher', ], 'invenio_jsonschemas.schemas': [ 'organisations = rero_ils.modules.organisations.jsonschemas', @@ -248,6 +258,8 @@ def run(self): 'budgets = rero_ils.modules.budgets.jsonschemas', 'acq_orders = rero_ils.modules.acq_orders.jsonschemas', 'acq_order_lines = rero_ils.modules.acq_order_lines.jsonschemas', + 'patron_transactions = \ + rero_ils.modules.patron_transactions.jsonschemas', ], 'invenio_search.mappings': [ 'organisations = rero_ils.modules.organisations.mappings', @@ -269,6 +281,8 @@ def run(self): 'budgets = rero_ils.modules.budgets.mappings', 'acq_orders = rero_ils.modules.acq_orders.mappings', 'acq_order_lines = rero_ils.modules.acq_order_lines.mappings', + 'patron_transactions = \ + rero_ils.modules.patron_transactions.mappings', ], 'invenio_search.templates': [ 'base-record = rero_ils.es_templates:list_es_templates' @@ -296,6 +310,8 @@ def run(self): 'budgets = rero_ils.modules.budgets.jsonresolver', 'acq_orders = rero_ils.modules.acq_orders.jsonresolver', 'acq_order_lines = rero_ils.modules.acq_order_lines.jsonresolver', + 'patron_transactions = \ + rero_ils.modules.patron_transactions.jsonresolver', ] }, classifiers=[