Skip to content

Commit

Permalink
Circulation Policies: Locate policy using item/patorn types pair
Browse files Browse the repository at this point in the history
* FIX Returns only policies when pair exists in settings

Signed-off-by: Aly Badr <aly.badr@rero.ch>
  • Loading branch information
Aly Badr committed Mar 12, 2019
1 parent 267e183 commit 2288291
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 8 deletions.
22 changes: 14 additions & 8 deletions rero_ils/modules/circ_policies/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,13 @@ def get_circ_policy_by_LPI(
'term',
libraries__pid=library_pid
).source().scan()
try:
return CircPolicy.get_record_by_pid(next(result).pid)
except StopIteration:
return None
# TODO: Better query to search pair in same block
for policy in result:
for settings in policy.settings:
if settings['item_type']['pid'] == item_type_pid and \
settings['patron_type']['pid'] == patron_type_pid:
return CircPolicy.get_record_by_pid(policy.pid)
return None

def get_circ_policy_by_OPI(
patron_type_pid,
Expand All @@ -118,10 +121,13 @@ def get_circ_policy_by_OPI(
'term',
settings__item_type__pid=item_type_pid
).source().scan()
try:
return CircPolicy.get_record_by_pid(next(result).pid)
except StopIteration:
return None
# TODO: Better query to search pair in same block
for policy in result:
for settings in policy.settings:
if settings['item_type']['pid'] == item_type_pid and \
settings['patron_type']['pid'] == patron_type_pid:
return CircPolicy.get_record_by_pid(policy.pid)
return None

def get_default_circ_policy():
"""Return the default circ policy."""
Expand Down
78 changes: 78 additions & 0 deletions tests/ui/circ_policies/test_circ_policies_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# -*- 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.

"""CircPolicy Record tests."""

from __future__ import absolute_import, print_function

from rero_ils.modules.circ_policies.api import CircPoliciesSearch, \
CircPolicy, circ_policy_id_fetcher


def test_circ_policy_search(
app,
circ_policy,
circ_policy_short,
circ_policy_short_library
):
"""Test Find circ policy"""
library_pid = 'lib1'
patron_type_pid = 'ptty1'
item_type_pid = 'itty1'
cipo = CircPolicy.provide_circ_policy(
library_pid,
patron_type_pid,
item_type_pid
)
assert cipo.pid == 'cipo2'
# return library cipo when pair exists.
library_pid = 'lib1'
patron_type_pid = 'ptty2'
item_type_pid = 'itty2'
cipo = CircPolicy.provide_circ_policy(
library_pid,
patron_type_pid,
item_type_pid
)
assert cipo.pid == 'cipo3'
# return default cipo when pair does not exists.
library_pid = 'lib2'
patron_type_pid = 'ptty2'
item_type_pid = 'itty2'
cipo = CircPolicy.provide_circ_policy(
library_pid,
patron_type_pid,
item_type_pid
)
assert cipo.pid == 'cipo1'
# return default when pair is not set anywhere.
library_pid = 'lib1'
patron_type_pid = 'ptty3'
item_type_pid = 'itty2'
cipo = CircPolicy.provide_circ_policy(
library_pid,
patron_type_pid,
item_type_pid
)
assert cipo.pid == 'cipo1'

0 comments on commit 2288291

Please sign in to comment.