Skip to content

Commit

Permalink
notification templates: create notification templates
Browse files Browse the repository at this point in the history
* Adds notification templates by type, language and communication channel.

Co-Authored-by: Peter Weber <[email protected]>
Co-Authored-by: Aly Badr <[email protected]>
  • Loading branch information
Aly Badr and rerowep committed Jul 18, 2019
1 parent b05eb74 commit 09e8d1f
Show file tree
Hide file tree
Showing 27 changed files with 342 additions and 51 deletions.
20 changes: 10 additions & 10 deletions data/users.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"postal_code": "11100",
"street": "Via Croix Noire 3",
"communication_channel": "email",
"communication_language": "it"
"communication_language": "ita"
},
{
"$schema": "https://ils.rero.ch/schema/patrons/patron-v0.0.1.json",
Expand All @@ -93,7 +93,7 @@
"postal_code": "11100",
"street": "Panoramica Collinare, 47",
"communication_channel": "email",
"communication_language": "it"
"communication_language": "ita"
},
{
"$schema": "https://ils.rero.ch/schema/patrons/patron-v0.0.1.json",
Expand Down Expand Up @@ -131,7 +131,7 @@
"postal_code": "1950",
"street": "Rue du Nord 7",
"communication_channel": "email",
"communication_language": "fr"
"communication_language": "fre"
},
{
"$schema": "https://ils.rero.ch/schema/patrons/patron-v0.0.1.json",
Expand All @@ -152,7 +152,7 @@
"postal_code": "95054",
"street": "520 Scott Blvd",
"communication_channel": "email",
"communication_language": "en"
"communication_language": "eng"
},
{
"$schema": "https://ils.rero.ch/schema/patrons/patron-v0.0.1.json",
Expand All @@ -173,7 +173,7 @@
"postal_code": "1892",
"street": "Route du Village 6",
"communication_channel": "email",
"communication_language": "fr"
"communication_language": "fre"
},
{
"$schema": "https://ils.rero.ch/schema/patrons/patron-v0.0.1.json",
Expand All @@ -194,7 +194,7 @@
"postal_code": "6500",
"street": "Piazza Collegiata 12",
"communication_channel": "email",
"communication_language": "it"
"communication_language": "ita"
},
{
"$schema": "https://ils.rero.ch/schema/patrons/patron-v0.0.1.json",
Expand All @@ -215,7 +215,7 @@
"postal_code": "1920",
"street": "Gare 45",
"communication_channel": "email",
"communication_language": "en"
"communication_language": "eng"
},
{
"$schema": "https://ils.rero.ch/schema/patrons/patron-v0.0.1.json",
Expand All @@ -236,7 +236,7 @@
"postal_code": "1926",
"street": "Vignettes 25",
"communication_channel": "email",
"communication_language": "de"
"communication_language": "ger"
},
{
"$schema": "https://ils.rero.ch/schema/patrons/patron-v0.0.1.json",
Expand Down Expand Up @@ -275,7 +275,7 @@
"postal_code": "55555",
"street": "Magic Street 3",
"communication_channel": "email",
"communication_language": "en"
"communication_language": "eng"
},
{
"$schema": "https://ils.rero.ch/schema/patrons/patron-v0.0.1.json",
Expand All @@ -295,6 +295,6 @@
"postal_code": "55555",
"street": "Diagon Alley 72",
"communication_channel": "email",
"communication_language": "en"
"communication_language": "eng"
}
]
1 change: 0 additions & 1 deletion rero_ils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,6 @@ def _(x):
'notif': '/notifications/notification-v0.0.1.json',
}


# Login Configuration
# ===================
#: Allow password change by users.
Expand Down
38 changes: 36 additions & 2 deletions rero_ils/modules/notifications/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,18 @@
from __future__ import absolute_import, print_function

from copy import deepcopy
from datetime import datetime
from datetime import datetime, timedelta
from functools import partial

import ciso8601
from invenio_search.api import RecordsSearch

from .dispatcher import Dispatcher
from .models import NotificationIdentifier, NotificationMetadata
from ..api import IlsRecord
from ..documents.api import Document
from ..fetchers import id_fetcher
from ..libraries.api import Library
from ..locations.api import Location
from ..minters import id_minter
from ..patrons.api import Patron
Expand Down Expand Up @@ -84,13 +86,15 @@ def update_process_date(self):

def replace_pids_and_refs(self):
"""Dumps data."""
from ..items.api import Item
try:
self.init_loan()
data = deepcopy(self.replace_refs())
data['loan'] = self.loan
data['loan']['item'] = self.item.replace_refs().dumps()
# del(data['loan']['item_pid'])
data['loan']['patron'] = self.patron.replace_refs().dumps()
# language = data['loan']['patron']['communication_language']
# del(data['loan']['patron_pid'])
data['loan']['transaction_user'] = \
self.transaction_user.replace_refs().dumps()
Expand All @@ -103,7 +107,37 @@ def replace_pids_and_refs(self):
data['loan']['pickup_location'] = \
pickup_location.replace_refs().dumps()
# del(data['loan']['pickup_location_pid'])
data['loan']['document'] = self.document.replace_refs().dumps()
library_pid = data['loan']['pickup_location']['library']['pid']
library = Library.get_record_by_pid(library_pid)
data['loan']['pickup_location']['library'] = library
keep_until = datetime.now() + timedelta(days=10)
next_open = library.next_open(keep_until)
# language = data['loan']['patron']['communication_language']
next_open = next_open.strftime("%d.%m.%Y")
data['loan']['next_open'] = next_open
else:
data['loan']['pickup_location'] = \
self.transaction_location.replace_refs().dumps()
item_pid = data['loan']['item_pid']
library = Item.get_record_by_pid(item_pid).get_library()
data['loan']['library'] = library

document = self.document.replace_refs().dumps()
data['loan']['document'] = document
authors = document.get('authors', '')
if authors:
author = authors[0].get('name', '')
if not author:
mef_list = ['name_fr', 'name_de', 'name_it', 'name_en',]
for a_name in mef_list:
if authors[0].get(a_name, ''):
author = authors[0].get(a_name)
break
data['loan']['author'] = author
end_date = data.get('loan').get('end_date')
if end_date:
end_date = ciso8601.parse_datetime_as_naive(end_date)
data['loan']['end_date'] = end_date.strftime("%d.%m.%Y")
# del(data['loan']['document_pid'])
return data
except Exception as e:
Expand Down
28 changes: 22 additions & 6 deletions rero_ils/modules/notifications/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

from __future__ import absolute_import, print_function

from flask import current_app
from flask_security.utils import config_value
from invenio_mail.api import TemplatedMessage
from invenio_mail.tasks import send_email
Expand All @@ -38,29 +39,44 @@ def dispatch_notification(self, notification=None):
"""Dispatch the notification."""
if notification:
data = notification.replace_pids_and_refs()
self.send_mail(data=data)
communication_channel = \
data['loan']['patron']['communication_channel']
if communication_channel == 'email':
self.send_mail(data=data)
if communication_channel == 'sms':
pass
if communication_channel == 'whatsapp':
pass
if communication_channel == 'letter':
pass
notification = notification.update_process_date()
return notification

def send_mail(self, data):
"""Send email."""
data['loan']['profile_url'] = \
'https://ils.test.rero.ch/patrons/profile'
notification_type = data.get('notification_type')
language = 'eng'
template = 'email/{type}_{lang}'.format(
language = data['loan']['patron']['communication_language']
template = 'email/{type}/{lang}'.format(
type=notification_type,
lang=language
)
recipient = data['loan']['patron']['email']
msg = TemplatedMessage(
# template_html='{template}.html'.format(template=template),
template_body='{template}.txt'.format(template=template),
subject=notification_type,
sender=config_value('EMAIL_SENDER'),
recipients=[recipient],
ctx=data['loan']
)

text = msg.body.split('\n')
msg.subject = text[0]
msg.body = '\n'.join(text[1:])
try:
send_email.delay(msg.__dict__)
send_email.run(msg.__dict__)
# TODO: investigate why delay does not work
# send_email.delay(msg.__dict__)
# current_app.extensions['mail'].send(msg)
except Exception as e:
raise(e)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Invitation to pick up a document
Dear patron,

The document you requested is now available. You can pick it up at the loan desk of the library mentioned below.

{% if author %}Author : {{ author }}{% endif %}
Title: {{ document.title }}
Pick up location: {{ pickup_location.library.name }}
To pick up until: {{ next_open }}

Should the document not be picked up within the given period, it will be made available for other people.
You can consult your account and extend the loan period of your documents at: {{ profile_url }}

Best regards

{{ pickup_location.library.name }}
{{ pickup_location.library.address }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Invitation à retirer un document
Chère lectrice, cher lecteur,

Le document que vous avez demandé est maintenant disponible. Vous pouvez venir le retirer au bureau de prêt de la bibliothèque mentionnée ci-dessous.

{% if author %}Auteur : {{ author }}{% endif %}
Titre : {{ document.title }}
Lieu de retrait : {{ pickup_location.library.name }}
A retirer jusqu'au : {{ next_open }}

Si le document n'est pas retiré dans les délais, il sera remis en circulation pour d'autres personnes.
Vous pouvez consulter votre compte et prolonger la durée de prêt de vos documents à l'adresse : {{ profile_url }}

Avec nos compliments

{{ pickup_location.library.name }}
{{ pickup_location.library.address }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Abholeinladung
Sehr geehrte Leserin, sehr geehrter Leser,

Das von Ihnen bestellte Dokument ist nun verfügbar und kann an der Ausleihtheke der nachstehend genannten Bibliothek abgeholt werden.

{% if author %}Autor : {{ author }}{% endif %}
Titel: {{ document.title }}
Abholort: {{ pickup_location.library.name }}
Abholen bis: {{ next_open }}

Wenn das Dokument innerhalb der gegebenen Frist nicht abgeholt wird, wird es anderen Personen zur Verfügung gestellt.
Unter folgender Adresse können Sie Ihr Konto einsehen und die Ausleihfrist Ihrer Dokumente verlängern: {{ profile_url }}

Freundliche Grüsse

{{ pickup_location.library.name }}
{{ pickup_location.library.address }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Invito a ritirare un documento
Cara lettrice, caro lettore,

Il documento che Lei ha domandato è ora disponibile. Lei può ritirarlo al servizio prestiti della biblioteca sotto indicata.

{% if author %}Autore : {{ author }}{% endif %}
Titolo: {{ document.title }}
Punto di ritiro: {{ pickup_location.library.name }}
Ritirare entro: {{ next_open }}

Se il documento non è ritirato entro detto termine, esso sarà rimesso in circolazione per altre persone.
Lei può consultare il Suo conto et prorogare la durata di prestito dei Suoi documenti al seguente indirizzo: {{ profile_url }}

Cordiali saluti

{{ pickup_location.library.name }}
{{ pickup_location.library.address }}

This file was deleted.

16 changes: 16 additions & 0 deletions rero_ils/modules/notifications/templates/email/due_soon/eng.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Expiry notice
Dear patron,

The loan period of following documents is expiring:

{% if author %}Author : {{ author }}{% endif %}
Title: {{ document.title }}
Due date: {{ end_date }}
Note: Non extendable

You can consult your account and extend the loan period of your documents at: {{ profile_url }}

Best regards

{{ library.name }}
{{ library.address }}
16 changes: 16 additions & 0 deletions rero_ils/modules/notifications/templates/email/due_soon/fre.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Avis d'échéance
Chère lectrice, cher lecteur,

Le délai de prêt des documents mentionnés ci-dessous arrive à échéance :

{% if author %}Auteur : {{ author }}{% endif %}
Titre : {{ document.title }}
Echéance : {{ end_date }}
Note : Non extendable

Vous pouvez consulter votre compte et prolonger la durée de prêt de vos documents à l'adresse : {{ profile_url }}

Avec nos compliments

{{ library.name }}
{{ library.address }}
16 changes: 16 additions & 0 deletions rero_ils/modules/notifications/templates/email/due_soon/ger.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Ablauf der Ausleihfrist
Sehr geehrte Leserin, sehr geehrter Leser,

Die Ausleihfrist der folgenden Dokumente läuft ab:

{% if author %}Autor : {{ author }}{% endif %}
Titel: {{ document.title }}
Rückgabedatum: {{ end_date }}
Anmerkung: Non extendable

Unter folgender Adresse können Sie Ihr Konto einsehen und die Ausleihfrist Ihrer Dokumente verlängern: {{ profile_url }}

Freundliche Grüsse

{{ library.name }}
{{ library.address }}
16 changes: 16 additions & 0 deletions rero_ils/modules/notifications/templates/email/due_soon/ita.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Avviso di scadenza
Cara lettrice, caro lettore,

La durata di prestito dei seguenti documenti sta per scadere:

{% if author %}Autore : {{ author }}{% endif %}
Titolo: {{ document.title }}
Scadenza: {{ end_date }}
Nota: Non extendable

Lei può consultare il Suo conto et prorogare la durata di prestito dei Suoi documenti al seguente indirizzo: {{ profile_url }}

Cordiali saluti

{{ library.name }}
{{ library.address }}

This file was deleted.

Loading

0 comments on commit 09e8d1f

Please sign in to comment.