-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
notifications: create periodic task to create and send notifications.
* Adds celery task cronned every morning at 4h00 to create and send overdue and due_soon notifications. Signed-off-by: Aly Badr <aly.badr@rero.ch>
- Loading branch information
Aly Badr
committed
Jun 17, 2019
1 parent
fbb79d4
commit 98f89e9
Showing
5 changed files
with
163 additions
and
5 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# -*- 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. | ||
|
||
"""Celery tasks for notification records.""" | ||
|
||
from __future__ import absolute_import, print_function | ||
|
||
from celery import shared_task | ||
from flask import current_app | ||
|
||
from ..loans.api import get_due_soon_loans, get_overdue_loans | ||
|
||
|
||
@shared_task(ignore_result=True) | ||
def create_over_and_due_soon_notifications(): | ||
"""Creates due_soon and overdue notifications.""" | ||
over_due_loans = get_overdue_loans() | ||
for loan in over_due_loans: | ||
loan.create_notification(notification_type='overdue') | ||
|
||
due_soon_loans = get_due_soon_loans() | ||
for loan in due_soon_loans: | ||
loan.create_notification(notification_type='due_soon') |
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,108 @@ | ||
# | ||
# 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. | ||
|
||
"""Tasks tests.""" | ||
|
||
import json | ||
from datetime import datetime, timedelta, timezone | ||
|
||
from flask import url_for | ||
from invenio_accounts.testutils import login_user_via_session | ||
from invenio_circulation.search.api import LoansSearch | ||
from utils import flush_index, get_json | ||
|
||
from rero_ils.modules.loans.api import Loan, LoanAction, get_due_soon_loans, \ | ||
get_overdue_loans | ||
from rero_ils.modules.notifications.api import NotificationsSearch, \ | ||
number_of_reminders_sent | ||
from rero_ils.modules.notifications.tasks import \ | ||
create_over_and_due_soon_notifications | ||
|
||
|
||
def test_create_over_and_due_soon_notifications_task( | ||
client, librarian_martigny_no_email, patron_martigny_no_email, | ||
item_lib_martigny, circ_policy_short_martigny): | ||
"""Test overdue and due_soon loans.""" | ||
login_user_via_session(client, librarian_martigny_no_email.user) | ||
item = item_lib_martigny | ||
item_pid = item.pid | ||
patron_pid = patron_martigny_no_email.pid | ||
# checkout | ||
res = client.post( | ||
url_for('api_item.checkout'), | ||
data=json.dumps( | ||
dict( | ||
item_pid=item_pid, | ||
patron_pid=patron_pid | ||
) | ||
), | ||
content_type='application/json', | ||
) | ||
assert res.status_code == 200 | ||
|
||
data = get_json(res) | ||
loan_pid = data.get('action_applied')[LoanAction.CHECKOUT].get('loan_pid') | ||
loan = Loan.get_record_by_pid(loan_pid) | ||
|
||
# test due_soon notification | ||
end_date = datetime.now(timezone.utc) + timedelta(days=3) | ||
loan['end_date'] = end_date.isoformat() | ||
loan.update(loan, dbcommit=True, reindex=True) | ||
|
||
due_soon_loans = get_due_soon_loans() | ||
|
||
assert due_soon_loans[0].get('loan_pid') == loan_pid | ||
|
||
create_over_and_due_soon_notifications() | ||
flush_index(NotificationsSearch.Meta.index) | ||
flush_index(LoansSearch.Meta.index) | ||
|
||
assert loan.is_notified(notification_type='due_soon') | ||
|
||
# test overdue notification | ||
end_date = datetime.now(timezone.utc) - timedelta(days=7) | ||
loan['end_date'] = end_date.isoformat() | ||
loan.update(loan, dbcommit=True, reindex=True) | ||
|
||
overdue_loans = get_overdue_loans() | ||
assert overdue_loans[0].get('loan_pid') == loan_pid | ||
|
||
create_over_and_due_soon_notifications() | ||
flush_index(NotificationsSearch.Meta.index) | ||
flush_index(LoansSearch.Meta.index) | ||
|
||
assert loan.is_notified(notification_type='overdue') | ||
assert number_of_reminders_sent(loan) == 1 | ||
|
||
# checkin the item to put it back to it's original state | ||
res = client.post( | ||
url_for('api_item.checkin'), | ||
data=json.dumps( | ||
dict( | ||
item_pid=item_pid, | ||
loan_pid=loan_pid | ||
) | ||
), | ||
content_type='application/json', | ||
) | ||
assert res.status_code == 200 |