Skip to content
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

US813 #714

Merged
merged 2 commits into from
Jan 27, 2020
Merged

US813 #714

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions data/loans.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"barcode": "reroils1",
"loans": {
"active": 2,
"overdue": 1,
"requested_by_others": 2
},
"requests": {
Expand All @@ -13,7 +14,8 @@
{
"barcode": "2000000001",
"loans": {
"active": 1
"active": 1,
"overdue": 1
},
"requests": {
"requests": 1
Expand All @@ -22,7 +24,8 @@
{
"barcode": "10000001",
"loans": {
"active": 2
"active": 2,
"overdue": 1
},
"requests": {
"requests": 1
Expand All @@ -40,7 +43,8 @@
{
"barcode": "kad001",
"loans": {
"active": 1
"active": 1,
"overdue": 1
},
"requests": {
"requests": 1
Expand All @@ -49,7 +53,8 @@
{
"barcode": "kad002",
"loans": {
"active": 1
"active": 1,
"overdue": 1
},
"requests": {
"requests": 1
Expand Down Expand Up @@ -286,5 +291,17 @@
"rank_1": 1,
"requests": 1
}
},
{
"barcode": "999999",
"loans": {
"active": 1,
"overdue": 1,
"requested_by_others": 1
},
"requests": {
"rank_1": 1,
"rank_2": 1
}
}
]
21 changes: 21 additions & 0 deletions data/users.json
Original file line number Diff line number Diff line change
Expand Up @@ -939,5 +939,26 @@
"street": "Lungolago 118",
"communication_channel": "email",
"communication_language": "ita"
},
{
"$schema": "https://ils.rero.ch/schema/patrons/patron-v0.0.1.json",
"barcode": "999999",
"birth_date": "1970-01-01",
"city": "Flueli-Ranft",
"email": "[email protected]",
"first_name": "Guillaume",
"roles": [
"patron"
],
"last_name": "de Baskerville",
"password": "123456",
"patron_type": {
"$ref": "https://ils.rero.ch/api/patron_types/5"
},
"phone": "+41324993585",
"postal_code": "6073",
"street": "Ranftweg 1",
"communication_channel": "email",
"communication_language": "eng"
}
]
26 changes: 22 additions & 4 deletions rero_ils/modules/fees/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from ..fetchers import id_fetcher
from ..locations.api import Location
from ..minters import id_minter
from ..organisations.api import Organisation
from ..providers import Provider

# fee provider
Expand Down Expand Up @@ -73,6 +74,20 @@ def transaction_location_pid(self):
"""Return transaction location pid."""
return self.replace_refs().get('location').get('pid')

@property
def loan_pid(self):
"""Return the loan pid of the fee ."""
from ..notifications.api import Notification
notification_pid = self.replace_refs().get('notification').get('pid')
return Notification.get_record_by_pid(notification_pid).loan_pid

@property
def patron_pid(self):
"""Return the patron pid of the fee ."""
from ..notifications.api import Notification
notification_pid = self.replace_refs().get('notification').get('pid')
return Notification.get_record_by_pid(notification_pid).patron_pid

@classmethod
def create_fee_from_notification(cls, notification):
"""Create a new fee."""
Expand Down Expand Up @@ -118,10 +133,6 @@ def create_fee_from_notification(cls, notification):
holding_circulation_category_pid
)
data['amount'] = cipo.get('reminder_fee_amount')
currency = current_app.config.get('RERO_ILS_DEFAULT_CURRENCY')
if notification.organisation:
currency = notification.organisation.get('default_currency')
data['currency'] = currency
data['status'] = 'open'
record = cls.create(
data,
Expand All @@ -130,3 +141,10 @@ def create_fee_from_notification(cls, notification):
delete_pid=True
)
return record

@property
def currency(self):
"""Return fee currency."""
organisation_pid = self.organisation_pid
return Organisation.get_record_by_pid(organisation_pid).get(
'default_currency')
1 change: 0 additions & 1 deletion rero_ils/modules/fees/jsonschemas/fees/fee-v0.0.1.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"fee_type",
"location",
"amount",
"currency",
"status"
],
"properties": {
Expand Down
8 changes: 8 additions & 0 deletions rero_ils/modules/fees/listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,11 @@ def enrich_fee_data(sender, json=None, record=None, index=None,
json['organisation'] = {
'pid': org_pid
}
if fee.loan_pid:
json['loan'] = {
'pid': fee.loan_pid
}
if fee.patron_pid:
json['patron'] = {
'pid': fee.patron_pid
}
16 changes: 15 additions & 1 deletion rero_ils/modules/fees/mappings/v6/fees/fee-v0.0.1.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@
"status": {
"type": "keyword"
},
"loan": {
"properties": {
"pid": {
"type": "keyword"
}
}
},
"patron": {
"properties": {
"pid": {
"type": "keyword"
}
}
},
"_created": {
"type": "date"
},
Expand All @@ -53,4 +67,4 @@
}
}
}
}
}
74 changes: 49 additions & 25 deletions rero_ils/modules/items/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ def item_link_to_holding(self):
self.commit()
self.dbcommit(reindex=True, forceindex=True)

def dumps_for_circulation(self):
def dumps_for_circulation(self, sort_by=None):
"""Enhance item information for api_views."""
item = self.replace_refs()
data = item.dumps()
Expand All @@ -315,7 +315,7 @@ def dumps_for_circulation(self):
data['actions'] = list(self.actions)
data['available'] = self.available
# data['number_of_requests'] = self.number_of_requests()
for loan in self.get_requests():
for loan in self.get_requests(sort_by=sort_by):
data.setdefault('pending_loans',
[]).append(loan.dumps_for_circulation())
return data
Expand Down Expand Up @@ -394,44 +394,59 @@ def get_item_by_barcode(cls, barcode=None):
return None

@classmethod
def get_pendings_loans(cls, library_pid):
"""Returns list of pending loand for a given library."""
# check library exists
def get_pendings_loans(cls, library_pid=None, sort_by='transaction_date'):
"""Return list of sorted pending loans for a given library.

default sort is set to transaction_date
"""
# check if library exists
lib = Library.get_record_by_pid(library_pid)
if not lib:
raise Exception('Invalid Library PID')

results = current_circulation.loan_search\
# the '-' prefix means a desc order.
sort_by = sort_by or 'transaction_date'
order_by = 'asc'
if sort_by.startswith('-'):
sort_by = sort_by[1:]
order_by = 'desc'
search = current_circulation.loan_search\
.source(['pid'])\
.params(preserve_order=True)\
.filter('term', state='PENDING')\
.filter('term', library_pid=library_pid)\
.sort({'transaction_date': {'order': 'asc'}})\
.scan()
.sort({sort_by: {"order": order_by}})
results = search.scan()
for loan in results:
yield Loan.get_record_by_pid(loan.pid)

@classmethod
def get_checked_out_loans(cls, patron_pid):
"""Returns checked out loans for a given patron."""
def get_checked_out_loans(
cls, patron_pid=None, sort_by='transaction_date'):
"""Returns sorted checked out loans for a given patron."""
# check library exists
patron = Patron.get_record_by_pid(patron_pid)
if not patron:
raise InvalidRecordID('Invalid Patron PID')
results = current_circulation.loan_search\
.source(['pid'])\
# the '-' prefix means a desc order.
sort_by = sort_by or 'transaction_date'
order_by = 'asc'
if sort_by.startswith('-'):
sort_by = sort_by[1:]
order_by = 'desc'

results = current_circulation.loan_search.source(['pid'])\
.params(preserve_order=True)\
.filter('term', state='ITEM_ON_LOAN')\
.filter('term', patron_pid=patron_pid)\
.sort({'transaction_date': {'order': 'asc'}})\
.scan()
.sort({sort_by: {"order": order_by}}).scan()
for loan in results:
yield Loan.get_record_by_pid(loan.pid)

@classmethod
def get_checked_out_items(cls, patron_pid):
"""Return checked out items for a given patron."""
loans = cls.get_checked_out_loans(patron_pid)
def get_checked_out_items(cls, patron_pid=None, sort_by=None):
"""Return sorted checked out items for a given patron."""
loans = cls.get_checked_out_loans(
patron_pid=patron_pid, sort_by=sort_by)
returned_item_pids = []
for loan in loans:
item_pid = loan.get('item_pid')
Expand All @@ -441,23 +456,32 @@ def get_checked_out_items(cls, patron_pid):
returned_item_pids.append(item_pid)
yield item, loan

def get_requests(self):
"""Return any pending, item_on_transit, item_at_desk loans."""
def get_requests(self, sort_by=None):
"""Return sorted pending, item_on_transit, item_at_desk loans.

default sort is transaction_date.
"""
search = search_by_pid(
item_pid=self.pid, filter_states=[
'PENDING',
'ITEM_AT_DESK',
'ITEM_IN_TRANSIT_FOR_PICKUP'
]).params(preserve_order=True)\
.source(['pid'])\
.sort({'transaction_date': {'order': 'asc'}})
]).params(preserve_order=True).source(['pid'])
order_by = 'asc'
sort_by = sort_by or 'transaction_date'
if sort_by.startswith('-'):
sort_by = sort_by[1:]
order_by = 'desc'
search = search.sort({sort_by: {'order': order_by}})
for result in search.scan():
yield Loan.get_record_by_pid(result.pid)

@classmethod
def get_requests_to_validate(cls, library_pid):
def get_requests_to_validate(
cls, library_pid=None, sort_by=None):
"""Returns list of requests to validate for a given library."""
loans = cls.get_pendings_loans(library_pid)
loans = cls.get_pendings_loans(
library_pid=library_pid, sort_by=sort_by)
returned_item_pids = []
for loan in loans:
item_pid = loan.get('item_pid')
Expand Down
16 changes: 10 additions & 6 deletions rero_ils/modules/items/api_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,14 @@ def extend_loan(item, data):
@check_authentication
@jsonify_error
def requested_loans(library_pid):
"""HTTP GET request for requested loans for a library."""
items_loans = Item.get_requests_to_validate(library_pid)
"""HTTP GET request for sorted requested loans for a library."""
sort_by = flask_request.args.get('sort')
items_loans = Item.get_requests_to_validate(
library_pid=library_pid, sort_by=sort_by)
metadata = []
for item, loan in items_loans:
metadata.append({
'item': item.dumps_for_circulation(),
'item': item.dumps_for_circulation(sort_by=sort_by),
'loan': loan.dumps_for_circulation()
})
return jsonify({
Expand All @@ -235,11 +237,13 @@ def requested_loans(library_pid):
@check_authentication
@jsonify_error
def loans(patron_pid):
"""HTTP GET request for requested loans for a library."""
items_loans = Item.get_checked_out_items(patron_pid)
"""HTTP GET request for sorted loans for a patron pid."""
sort_by = flask_request.args.get('sort')
items_loans = Item.get_checked_out_items(
patron_pid=patron_pid, sort_by=sort_by)
metadata = []
for item, loan in items_loans:
item_dumps = item.dumps_for_circulation()
item_dumps = item.dumps_for_circulation(sort_by=sort_by)
metadata.append({
'item': item_dumps,
'loan': loan.dumps_for_circulation()
Expand Down
15 changes: 0 additions & 15 deletions rero_ils/modules/loans/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,18 +323,3 @@ def get_overdue_loans():
if now > due_date + timedelta(days=days_after):
overdue_loans.append(loan)
return overdue_loans


def get_item_on_loan_loans():
"""Return item on loan."""
item_on_loan_loans = []
results = current_circulation.loan_search\
.source(['pid'])\
.params(preserve_order=True)\
.filter('term', state='ITEM_ON_LOAN')\
.sort({'transaction_date': {'order': 'asc'}})\
.scan()
for record in results:
loan = Loan.get_record_by_pid(record.pid)
item_on_loan_loans.append(loan)
return item_on_loan_loans
Loading