Skip to content

Commit

Permalink
fix: correct circulation policy
Browse files Browse the repository at this point in the history
* Corrects item types and patron_types of circulation policy with pid = 10 and 11.
* Adds test to check data consistency when ci-po are created.
* Adds new patron type.
* Closes rero#626.
* Closes rero#213.

Co-Authored-by: Alicia Zangger <alicia.zangger@rero.ch>
  • Loading branch information
Alicia Zangger committed Nov 20, 2019
1 parent f9a2c1f commit 797ee0c
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 32 deletions.
10 changes: 5 additions & 5 deletions data/circulation_policies.json
Expand Up @@ -331,15 +331,15 @@
"$ref": "https://ils.rero.ch/api/patron_types/1"
},
"item_type": {
"$ref": "https://ils.rero.ch/api/item_types/7"
"$ref": "https://ils.rero.ch/api/item_types/8"
}
},
{
"patron_type": {
"$ref": "https://ils.rero.ch/api/patron_types/2"
},
"item_type": {
"$ref": "https://ils.rero.ch/api/item_types/7"
"$ref": "https://ils.rero.ch/api/item_types/8"
}
}
]
Expand All @@ -359,18 +359,18 @@
"settings": [
{
"patron_type": {
"$ref": "https://ils.rero.ch/api/patron_types/3"
"$ref": "https://ils.rero.ch/api/patron_types/6"
},
"item_type": {
"$ref": "https://ils.rero.ch/api/item_types/8"
"$ref": "https://ils.rero.ch/api/item_types/9"
}
},
{
"patron_type": {
"$ref": "https://ils.rero.ch/api/patron_types/4"
},
"item_type": {
"$ref": "https://ils.rero.ch/api/item_types/8"
"$ref": "https://ils.rero.ch/api/item_types/9"
}
}
]
Expand Down
9 changes: 9 additions & 0 deletions data/patron_types.json
Expand Up @@ -43,5 +43,14 @@
"organisation": {
"$ref": "https://ils.rero.ch/api/organisations/3"
}
},
{
"$schema": "https://ils.rero.ch/schema/patron_types/patron_type-v0.0.1.json",
"pid": "6",
"name": "Children",
"description": "Children and teenagers (< 16 years old).",
"organisation": {
"$ref": "https://ils.rero.ch/api/organisations/2"
}
}
]
22 changes: 22 additions & 0 deletions rero_ils/modules/circ_policies/api.py
Expand Up @@ -59,6 +59,28 @@ class CircPolicy(IlsRecord):
fetcher = circ_policy_id_fetcher
provider = CircPolicyProvider

def extended_validation(self, **kwargs):
"""Returns reasons for validation failures, otherwise True.
Override this function for classes that require extended validations
"""
from ..patron_types.api import PatronType
from ..item_types.api import ItemType

org = self.get('organisation')
for setting in self.replace_refs().get('settings', []):
patron_type = PatronType.get_record_by_pid(setting.get(
'patron_type', {}).get('pid')
)
item_type = ItemType.get_record_by_pid(setting.get(
'item_type', {}).get('pid')
)
if patron_type.get('organisation') != org or item_type.get(
'organisation'
) != org:
return False
return True

@classmethod
def exist_name_and_organisation_pid(cls, name, organisation_pid):
"""Check if the policy name is unique on organisation."""
Expand Down
26 changes: 16 additions & 10 deletions rero_ils/modules/cli.py
Expand Up @@ -209,18 +209,22 @@ def init(force):
@click.option('-s', '--schema', 'schema', default=None)
@click.option('-p', '--pid_type', 'pid_type', default=None)
@click.option('-l', '--lazy', 'lazy', is_flag=True, default=False)
@click.option('-o', '--dont-stop', 'dont_stop_on_error',
is_flag=True, default=False)
@click.argument('infile', type=click.File('r'), default=sys.stdin)
@with_appcontext
def create(infile, append, reindex, dbcommit, verbose, schema, pid_type, lazy):
def create(infile, append, reindex, dbcommit, verbose, schema, pid_type, lazy,
dont_stop_on_error):
"""Load REROILS record.
infile: Json file
append: appends pids to database
reindex: reindex record by record
dbcommit: commit record to database
pid_type: record type
schema: recoord schema
lazy: lazy reads file
:param infile: Json file
:param append: appends pids to database
:param reindex: reindex record by record
:param dbcommit: commit record to database
:param pid_type: record type
:param schema: recoord schema
:param lazy: lazy reads file
:param dont_stop_on_error: don't stop on error
"""
click.secho(
'Loading {pid_type} records from {file_name}.'.format(
Expand Down Expand Up @@ -263,15 +267,17 @@ def create(infile, append, reindex, dbcommit, verbose, schema, pid_type, lazy):
except Exception as err:
error_records.append(record)
click.secho(
'{count: <8} {pid_type} creat error {pid}: {err}'.format(
'{count: <8} {pid_type} create error {pid}: {err}'.format(
count=count,
pid_type=pid_type,
pid=record.get('pid', '???'),
err=err
),
err=True,
fg='red'
)
)
if not dont_stop_on_error:
sys.exit(1)

if error_records:
err_file_name = '{pid_type}_error.json'.format(pid_type=pid_type)
Expand Down
33 changes: 18 additions & 15 deletions scripts/setup
Expand Up @@ -25,6 +25,8 @@ DATA_PATH=$(pipenv --where)/data
# used for create only the items and holdings files for the 'big' documents file
# --deployment:
# used for deploy the 'big' files
# --extra_param:
# used for continue script on error

RED='\033[0;31m'
GREEN='\033[0;0;32m'
Expand All @@ -42,9 +44,10 @@ DEPLOYMENT=false
CREATE_ITEMS_HOLDINGS_SMALL=false
CREATE_ITEMS_HOLDINGS_BIG=false
STOP_EXECUTION=true
EXTRA_PARAMETER=""

# options may be followed by one colon to indicate they have a required argument
if ! options=$(getopt -o dsb -l deployment,create_items_holdings_small,create_items_holdings_big,data_path: -- "$@")
if ! options=$(getopt -o dsb -l deployment,create_items_holdings_small,create_items_holdings_big,extra_param,data_path: -- "$@")
then
# something went wrong, getopt will put out an error message for us
exit 1
Expand All @@ -57,6 +60,7 @@ do
-s|--create_items_holdings_small) CREATE_ITEMS_HOLDINGS_SMALL=true ;;
-b|--create_items_holdings_big) CREATE_ITEMS_HOLDINGS_BIG=true ;;
-c|--continue) STOP_EXECUTION=false ;;
-p|--pursue) EXTRA_PARAMETER="--dont-stop" ;;
-D|--data_path) DATA_PATH=$2 ;;
(--) shift; break;;
(-*) display_error_message "$0: error - unrecognized option $1"; exit 1;;
Expand Down Expand Up @@ -140,22 +144,22 @@ pipenv run invenio roles add admin@rero.ch admins
pipenv run invenio roles add admin@rero.ch superusers

display_success_message "Organisations:"
pipenv run invenio fixtures create --pid_type org ${DATA_PATH}/organisations.json --append
pipenv run invenio fixtures create --pid_type org ${DATA_PATH}/organisations.json --append ${EXTRA_PARAMETER}
pipenv run invenio index reindex -t org --yes-i-know
display_success_message "Libraries:"
pipenv run invenio fixtures create --pid_type lib ${DATA_PATH}/libraries.json --append
pipenv run invenio fixtures create --pid_type lib ${DATA_PATH}/libraries.json --append ${EXTRA_PARAMETER}
pipenv run invenio index reindex -t lib --yes-i-know
display_success_message "Locations:"
pipenv run invenio fixtures create --pid_type loc ${DATA_PATH}/locations.json --append
pipenv run invenio fixtures create --pid_type loc ${DATA_PATH}/locations.json --append ${EXTRA_PARAMETER}
pipenv run invenio index reindex -t loc --yes-i-know
display_success_message "Item types:"
pipenv run invenio fixtures create --pid_type itty ${DATA_PATH}/item_types.json --append
pipenv run invenio fixtures create --pid_type itty ${DATA_PATH}/item_types.json --append ${EXTRA_PARAMETER}
pipenv run invenio index reindex -t itty --yes-i-know
display_success_message "Patron types:"
pipenv run invenio fixtures create --pid_type ptty ${DATA_PATH}/patron_types.json --append
pipenv run invenio fixtures create --pid_type ptty ${DATA_PATH}/patron_types.json --append ${EXTRA_PARAMETER}
pipenv run invenio index reindex -t ptty --yes-i-know
display_success_message "Circulation policies:"
pipenv run invenio fixtures create --pid_type cipo ${DATA_PATH}/circulation_policies.json --append
display_success_message "Circulation policies: ${EXTRA_PARAMETER}"
pipenv run invenio fixtures create --pid_type cipo ${DATA_PATH}/circulation_policies.json --append ${EXTRA_PARAMETER}
pipenv run invenio index reindex -t cipo --yes-i-know
pipenv run invenio index run --raise-on-error

Expand Down Expand Up @@ -183,9 +187,9 @@ else
HOLDINGS=${DATA_PATH}/holdings_small.json
fi

display_success_message "Documents:"
display_success_message "Documents: ${EXTRA_PARAMETER}"
echo -e ${DOCUMENTS}
pipenv run invenio fixtures create --pid_type doc --schema 'http://ils.rero.ch/schema/documents/document-v0.0.1.json' ${DOCUMENTS} --append
pipenv run invenio fixtures create --pid_type doc --schema 'http://ils.rero.ch/schema/documents/document-v0.0.1.json' ${DOCUMENTS} --append ${EXTRA_PARAMETER}

if $CREATE_ITEMS_HOLDINGS_SMALL
then
Expand All @@ -209,23 +213,22 @@ then
fi
fi

display_success_message "Holdings:"
display_success_message "Holdings: ${EXTRA_PARAMETER}"
echo -e ${HOLDINGS}
pipenv run invenio fixtures create --pid_type hold --schema 'http://ils.rero.ch/schema/holdings/holding-v0.0.1.json' ${HOLDINGS} --append
pipenv run invenio fixtures create --pid_type hold --schema 'http://ils.rero.ch/schema/holdings/holding-v0.0.1.json' ${HOLDINGS} --append ${EXTRA_PARAMETER}
pipenv run invenio index reindex -t hold --yes-i-know
pipenv run invenio index run -c 4 --raise-on-error

display_success_message "Items:"
display_success_message "Items: ${EXTRA_PARAMETER}"
echo -e ${ITEMS}
pipenv run invenio fixtures create --pid_type item --schema 'http://ils.rero.ch/schema/items/item-v0.0.1.json' ${ITEMS} --append
pipenv run invenio fixtures create --pid_type item --schema 'http://ils.rero.ch/schema/items/item-v0.0.1.json' ${ITEMS} --append ${EXTRA_PARAMETER}
pipenv run invenio index reindex -t item --yes-i-know
pipenv run invenio index run -c 4 --raise-on-error

display_success_message "Index Documents:"
pipenv run invenio index reindex -t doc --yes-i-know
pipenv run invenio index run -c 4 --raise-on-error

set +e
display_success_message "Circulation transactions:"
pipenv run invenio fixtures create_loans --fee ${DATA_PATH}/loans.json

Expand Down
41 changes: 39 additions & 2 deletions tests/ui/circ_policies/test_circ_policies_api.py
Expand Up @@ -20,12 +20,13 @@
from __future__ import absolute_import, print_function

from copy import deepcopy

import pytest
import mock
from utils import get_mapping

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


def test_no_default_policy(app):
Expand All @@ -34,7 +35,15 @@ def test_no_default_policy(app):
assert not cipo


def test_circ_policy_create(db, circ_policy_martigny_data_tmp):
def test_circ_policy_create(circ_policy_martigny_data_tmp,
org_martigny,
lib_martigny, lib_saxon,
patron_type_children_martigny,
item_type_standard_martigny,
patron_type_adults_martigny,
item_type_specific_martigny,
item_type_regular_sion,
patron_type_youngsters_sion):
"""Test circulation policy creation."""
cipo = CircPolicy.create(circ_policy_martigny_data_tmp, delete_pid=True)
assert cipo == circ_policy_martigny_data_tmp
Expand All @@ -53,6 +62,34 @@ def test_circ_policy_create(db, circ_policy_martigny_data_tmp):
assert cipo.get('$schema')
assert cipo.get('pid') == '2'

cipo_data = {
'$schema': 'https://ils.rero.ch/schema/'
'circ_policies/circ_policy-v0.0.1.json',
'pid': 'cipo_test',
'name': 'test',
'organisation': {
'$ref': 'https://ils.rero.ch/api/organisations/org1'
},
'is_default': False,
'settings': [{
'patron_type': {
'$ref': 'https://ils.rero.ch/api/patron_types/ptty3'
},
'item_type': {
'$ref': 'https://ils.rero.ch/api/item_types/itty1'
}
}, {
'patron_type': {
'$ref': 'https://ils.rero.ch/api/patron_types/ptty2'
},
'item_type': {
'$ref': 'https://ils.rero.ch/api/item_types/itty4'
}
}]
}
with pytest.raises(RecordValidationError):
cipo = CircPolicy.create(cipo_data, delete_pid=False)


def test_circ_policy_exist_name_and_organisation_pid(
circ_policy_default_martigny):
Expand Down

0 comments on commit 797ee0c

Please sign in to comment.