Skip to content

Commit

Permalink
notifications: create resource data model and api
Browse files Browse the repository at this point in the history
* Adds notification schema, mappings and required objects
* Integrate invenio-records 1.2.1 to have a separate table by resource

Signed-off-by: Aly Badr <aly.badr@rero.ch>
  • Loading branch information
Aly Badr committed Jun 5, 2019
1 parent b72f68c commit 1e8dde0
Show file tree
Hide file tree
Showing 26 changed files with 1,332 additions and 84 deletions.
2 changes: 1 addition & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ invenio-oaiserver = ">=1.0.3,<1.1.0"
invenio-pidstore = ">=1.0.0,<1.1.0"
invenio-records-rest = ">=1.4.0,<1.5.0"
invenio-records-ui = ">=1.0.1,<1.1.0"
invenio-records = ">=1.1.0,<1.2.0"
invenio-records = "<=1.2.0"
Werkzeug = ">=0.15"
# TODO: to be removed
angular-gettext-babel= ">=0.1"
Expand Down
195 changes: 115 additions & 80 deletions Pipfile.lock

Large diffs are not rendered by default.

42 changes: 41 additions & 1 deletion rero_ils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
get_extension_params, is_item_available_for_checkout, \
loan_satisfy_circ_policies
from .modules.locations.api import Location
from .modules.notifications.api import Notification
from .modules.organisations.api import Organisation
from .modules.patron_types.api import PatronType
from .modules.patrons.api import Patron
Expand Down Expand Up @@ -701,6 +702,44 @@ def _(x):
update_permission_factory_imp=can_update_organisation_records_factory,
delete_permission_factory_imp=can_delete_organisation_records_factory,
),
notif=dict(
pid_type='notif',
pid_minter='notification_id',
pid_fetcher='notification_id',
search_class=RecordsSearch,
search_index='notifications',
indexer_class=IlsRecordIndexer,
search_type=None,
record_serializers={
'application/json': (
'rero_ils.modules.serializers' ':json_v1_response'
),
'application/can-delete+json': (
'rero_ils.modules.serializers' ':can_delete_json_v1_response'
)
},
search_serializers={
'application/json': (
'invenio_records_rest.serializers' ':json_v1_search'
),
'application/can-delete+json': (
'rero_ils.modules.serializers' ':can_delete_json_v1_search'
),
},
record_loaders={
'application/json': lambda: Notification(request.get_json()),
},
list_route='/notifications/',
record_class='rero_ils.modules.notifications.api:Notification',
item_route='/notifications/<pid(notif, record_class="rero_ils.modules.notifications.api:Notification"):pid_value>',
default_media_type='application/json',
max_result_window=10000,
search_factory_imp='rero_ils.query:organisation_search_factory',
read_permission_factory_imp=can_access_organisation_records_factory,
create_permission_factory_imp=can_create_organisation_records_factory,
update_permission_factory_imp=can_update_organisation_records_factory,
delete_permission_factory_imp=can_delete_organisation_records_factory,
),
)

SEARCH_UI_SEARCH_INDEX = 'documents'
Expand Down Expand Up @@ -861,7 +900,8 @@ def _(x):
'loc': '/locations/location-v0.0.1.json',
'org': '/organisations/organisation-v0.0.1.json',
'ptrn': '/patrons/patron-v0.0.1.json',
'ptty': '/patron_types/patron_type-v0.0.1.json'
'ptty': '/patron_types/patron_type-v0.0.1.json',
'notif': '/notifications/notification-v0.0.1.json',
}


Expand Down
4 changes: 3 additions & 1 deletion rero_ils/modules/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,18 @@
from .loans.listener import enrich_loan_data
from .locations.listener import enrich_location_data
from .mef_persons.receivers import publish_api_harvested_records
from .notifications.listener import enrich_notification_data
from .patrons.listener import enrich_patron_data, listener_item_at_desk
from ..filter import admin_menu_is_visible, format_date_filter, jsondumps, \
resource_can_create, text_to_id, to_pretty_json
from ..permissions import can_access_item, can_edit


class REROILSAPP(object):
"""rero-ils extension."""

def __init__(self, app=None):
"""RERO ILS App module."""
from ..permissions import can_access_item, can_edit
if app:
self.init_app(app)
app.add_template_filter(format_date_filter, name='format_date')
Expand Down Expand Up @@ -100,6 +101,7 @@ def register_signals(self):
before_record_index.connect(enrich_item_data)
before_record_index.connect(enrich_patron_data)
before_record_index.connect(enrich_location_data)
before_record_index.connect(enrich_notification_data)

item_at_desk.connect(listener_item_at_desk)

Expand Down
38 changes: 38 additions & 0 deletions rero_ils/modules/items/jsonresolver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# -*- coding: utf-8 -*-
#
# This file is part of RERO ILS.
# Copyright (C) 2017 RERO.
#
# RERO ILS is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# RERO ILS 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
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with RERO ILS; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307, USA.
#
# In applying this license, RERO does not
# waive the privileges and immunities granted to it by virtue of its status
# as an Intergovernmental Organization or submit itself to any jurisdiction.

"""Item resolver."""


import jsonresolver
from invenio_pidstore.models import PersistentIdentifier, PIDStatus


@jsonresolver.route('/api/items/<pid>', host='ils.rero.ch')
def item_resolver(pid):
"""Item resolver."""
persistent_id = PersistentIdentifier.get('item', pid)
if persistent_id.status == PIDStatus.REGISTERED:
return dict(pid=persistent_id.pid_value)
raise Exception('unable to resolve')
25 changes: 25 additions & 0 deletions rero_ils/modules/notifications/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
#
# This file is part of RERO ILS.
# Copyright (C) 2018 RERO.
#
# RERO ILS is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# RERO ILS 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
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with RERO ILS; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307, USA.
#
# In applying this license, RERO does not
# waive the privileges and immunities granted to it by virtue of its status
# as an Intergovernmental Organization or submit itself to any jurisdiction.

"""Notifications Records."""
75 changes: 75 additions & 0 deletions rero_ils/modules/notifications/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# -*- coding: utf-8 -*-
#
# This file is part of RERO ILS.
# Copyright (C) 2018 RERO.
#
# RERO ILS is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# RERO ILS 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
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with RERO ILS; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307, USA.
#
# In applying this license, RERO does not
# waive the privileges and immunities granted to it by virtue of its status
# as an Intergovernmental Organization or submit itself to any jurisdiction.

"""API for manipulating Notifications."""

from __future__ import absolute_import, print_function

from functools import partial

from elasticsearch_dsl import Q
from invenio_search.api import RecordsSearch

from .models import NotificationIdentifier, NotificationMetadata
from ..api import IlsRecord
from ..fetchers import id_fetcher
from ..locations.api import Location
from ..minters import id_minter
from ..providers import Provider

# notif provider
NotificationProvider = type(
'NotificationProvider',
(Provider,),
dict(identifier=NotificationIdentifier, pid_type='notif')
)
# notif minter
notification_id_minter = partial(id_minter, provider=NotificationProvider)
# notif fetcher
notification_id_fetcher = partial(id_fetcher, provider=NotificationProvider)


class NotificationsSearch(RecordsSearch):
"""RecordsSearch for Notifications."""

class Meta:
"""Search only on Notifications index."""

index = 'notifications'


class Notification(IlsRecord):
"""Notifications class."""

minter = notification_id_minter
fetcher = notification_id_fetcher
provider = NotificationProvider
model_cls = NotificationMetadata

@property
def organisation_pid(self):
"""Get organisation pid for notification."""
location_pid = self.replace_refs()['transaction_location']['pid']
location = Location.get_record_by_pid(location_pid)
return location.organisation_pid
25 changes: 25 additions & 0 deletions rero_ils/modules/notifications/jsonschemas/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2018 RERO.
#
# Invenio is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# Invenio 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
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Invenio; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307, USA.
#
# In applying this license, RERO does not
# waive the privileges and immunities granted to it by virtue of its status
# as an Intergovernmental Organization or submit itself to any jurisdiction.

"""Notifications JSON schemas."""
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"title": "Notification",
"description": "JSON schema for notifications.",
"additionalProperties": true,
"required": [
"$schema",
"pid",
"notification_type",
"patron",
"transaction_location"
],
"properties": {
"$schema": {
"title": "Schema",
"description": "Schema to validate notifications records against.",
"type": "string",
"minLength": 9,
"default": "https://ils.rero.ch/schema/notifications/notification-v0.0.1.json"
},
"pid": {
"title": "Notification ID",
"type": "string",
"minLength": 1
},
"notification_date": {
"type": "string",
"format": "date-time",
"title": "Notification creation date"
},
"patron": {
"title": "Patron",
"type": "object",
"properties": {
"$ref": {
"title": "Patron URI",
"type": "string",
"pattern": "^https://ils.rero.ch/api/patrons/.*?$"
}
}
},
"item": {
"title": "Item",
"type": "object",
"properties": {
"$ref": {
"title": "Item URI",
"type": "string",
"pattern": "^https://ils.rero.ch/api/items/.*?$"
}
}
},
"due_date": {
"type": "string",
"format": "date-time",
"title": "Checkout/renewal due date"
},
"reminder_counter": {
"type": "integer",
"title": "Current reminder count"
},
"transaction_location": {
"title": "Transaction location",
"type": "object",
"properties": {
"$ref": {
"title": "Transaction location URI",
"type": "string",
"pattern": "^https://ils.rero.ch/api/locations/.*?$"
}
}
},
"pickup_library": {
"title": "Pickup library",
"type": "object",
"properties": {
"$ref": {
"title": "Pickup library URI",
"type": "string",
"pattern": "^https://ils.rero.ch/api/libaries/.*?$"
}
}
},
"notification_type": {
"title": "Notification type",
"description": "The type of the notification.",
"type": "string",
"enum": [
"due_soon",
"overdue",
"availability",
"recall"
]
}
}
}
Loading

0 comments on commit 1e8dde0

Please sign in to comment.