Skip to content

Commit

Permalink
api: test if a PID is existing
Browse files Browse the repository at this point in the history
Method are needed to verify if a PID is already existing as the
system creates or updates a resource, in order to avoid ending
up with two records sharing the same PID and whether a reference
in the record to a different record exists.

* Adds functionality to test pids for existence.
* Test during acq_accounts creation and update existence of pids.
* Test during acq_invoices cration and update existence of pids.
* Test during acq_order_lines creation and update existence of pids.
* Test during acq_orders creation and update existence of pids.
* Test during holdings creation and update existence of pids.
* Test during items creation and update existence of pids.
* Test during loans creation and update existence of pids.
* Test during patron_types creation und update existence of pids.
* closes #850

Co-Authored-by: Peter Weber <[email protected]>
  • Loading branch information
rerowep and rerowep committed May 8, 2020
1 parent 200e42c commit b9ee0a1
Show file tree
Hide file tree
Showing 41 changed files with 686 additions and 227 deletions.
6 changes: 3 additions & 3 deletions data/locations.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@
"code": "HOG-PUBL",
"name": "Public Section",
"is_pickup": true,
"pickup_name": "Public Section",
"library": {
"$ref": "https://ils.rero.ch/api/libraries/5"
},
"pickup_name": "Public Section"
}
},
{
"pid": "18",
Expand Down Expand Up @@ -351,4 +351,4 @@
"pickup_name": "Jedi Archives",
"is_online": false
}
]
]
42 changes: 25 additions & 17 deletions rero_ils/modules/acq_accounts/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@

from functools import partial

from flask import current_app

from .models import AcqAccountIdentifier, AcqAccountMetadata
from ..acq_order_lines.api import AcqOrderLinesSearch
from ..api import IlsRecord, IlsRecordsIndexer, IlsRecordsSearch
from ..fetchers import id_fetcher
from ..libraries.api import Library
from ..minters import id_minter
from ..providers import Provider
from ..utils import get_ref_for_pid

# provider
AcqAccountProvider = type(
Expand Down Expand Up @@ -58,6 +57,15 @@ class AcqAccount(IlsRecord):
fetcher = acq_account_id_fetcher
provider = AcqAccountProvider
model_cls = AcqAccountMetadata
pids_exist_check = {
'required': {
'lib': 'library',
'budg': 'budget'
},
'not_required': {
'org': 'organisation'
}
}

@classmethod
def create(cls, data, id_=None, delete_pid=False,
Expand All @@ -68,28 +76,28 @@ def create(cls, data, id_=None, delete_pid=False,
data, id_, delete_pid, dbcommit, reindex, **kwargs)
return record

def update(self, data, dbcommit=False, reindex=False):
"""Update acq account record."""
self._acq_account_build_org_ref(data)
super(AcqAccount, self).update(data, dbcommit, reindex)
return self

@classmethod
def _acq_account_build_org_ref(cls, data):
"""Build $ref for the organisation of the acq account."""
library_pid = data.get('library', {}).get('pid')
if not library_pid:
library_pid = data.get('library').get(
'$ref').split('libraries/')[1]
org_pid = Library.get_record_by_pid(library_pid).organisation_pid
base_url = current_app.config.get('RERO_ILS_APP_BASE_URL')
url_api = '{base_url}/api/{doc_type}/{pid}'
org_ref = {
'$ref': url_api.format(
base_url=base_url,
doc_type='organisations',
pid=org_pid or cls.organisation_pid)
}
data['organisation'] = org_ref
library = data.get('library', {})
library_pid = library.get('pid') or \
library.get('$ref').split('libraries/')[1]
data['organisation'] = {'$ref': get_ref_for_pid(
'org',
Library.get_record_by_pid(library_pid).organisation_pid
)}
return data

@property
def library_pid(self):
"""Shortcut for acq account library pid."""
return self.replace_refs().get('library').get('pid')
return self.replace_refs()['library']['pid']

def get_number_of_acq_order_lines(self):
"""Get number of acquisition order lines linked to this account."""
Expand Down
9 changes: 9 additions & 0 deletions rero_ils/modules/acq_invoices/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ class AcquisitionInvoice(IlsRecord):
fetcher = acq_invoice_id_fetcher
provider = AcquisitionInvoiceProvider
model_cls = AcquisitionInvoiceMetadata
pids_exist_check = {
'required': {
'lib': 'library',
'vndr': 'vendor'
},
'not_required': {
'org': 'organisation'
}
}

@classmethod
def create(cls, data, id_=None, delete_pid=False,
Expand Down
45 changes: 25 additions & 20 deletions rero_ils/modules/acq_order_lines/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@

"""API for manipulating Acquisition Order Line."""

from copy import deepcopy
from functools import partial

from flask import current_app

from .models import AcqOrderLineIdentifier, AcqOrderLineMetadata
from ..api import IlsRecord, IlsRecordsIndexer, IlsRecordsSearch
from ..fetchers import id_fetcher
from ..minters import id_minter
from ..providers import Provider
from ..utils import get_ref_for_pid

# provider
AcqOrderLineProvider = type(
Expand Down Expand Up @@ -56,6 +56,16 @@ class AcqOrderLine(IlsRecord):
fetcher = acq_order_line_id_fetcher
provider = AcqOrderLineProvider
model_cls = AcqOrderLineMetadata
pids_exist_check = {
'required': {
'doc': 'document',
'acac': 'acq_account',
'acor': 'acq_order'
},
'not_required': {
'org': 'organisation'
}
}

@classmethod
def create(cls, data, id_=None, delete_pid=False,
Expand All @@ -69,30 +79,25 @@ def create(cls, data, id_=None, delete_pid=False,

def update(self, data, dbcommit=True, reindex=True):
"""Update Acquisition Order Line record."""
self._build_total_amount_for_order_line(data)
super(AcqOrderLine, self).update(data, dbcommit, reindex)
new_data = deepcopy(dict(self))
new_data.update(data)
self._acq_order_line_build_org_ref(new_data)
self._build_total_amount_for_order_line(new_data)
super(AcqOrderLine, self).update(new_data, dbcommit, reindex)
return self

@classmethod
def _acq_order_line_build_org_ref(cls, data):
"""Build $ref for the organisation of the acquisition order."""
from ..acq_orders.api import AcqOrder

order_pid = data.get('acq_order', {}).get('pid')
if not order_pid:
order_pid = data.get('acq_order').get(
'$ref').split('acq_orders/')[1]

org_pid = AcqOrder.get_record_by_pid(order_pid).organisation_pid
base_url = current_app.config.get('RERO_ILS_APP_BASE_URL')
url_api = '{base_url}/api/{doc_type}/{pid}'
org_ref = {
'$ref': url_api.format(
base_url=base_url,
doc_type='organisations',
pid=org_pid or cls.organisation_pid)
}
data['organisation'] = org_ref
order = data.get('acq_order', {})
order_pid = order.get('pid') or \
order.get('$ref').split('acq_orders/')[1]
data['organisation'] = {'$ref': get_ref_for_pid(
'org',
AcqOrder.get_record_by_pid(order_pid).organisation_pid
)}
return data

@classmethod
def _build_total_amount_for_order_line(cls, data):
Expand Down
42 changes: 25 additions & 17 deletions rero_ils/modules/acq_orders/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@

from functools import partial

from flask import current_app

from .models import AcqOrderIdentifier, AcqOrderMetadata
from ..acq_order_lines.api import AcqOrderLinesSearch
from ..api import IlsRecord, IlsRecordsIndexer, IlsRecordsSearch
from ..fetchers import id_fetcher
from ..libraries.api import Library
from ..minters import id_minter
from ..providers import Provider
from ..utils import get_ref_for_pid

# provider
AcqOrderProvider = type(
Expand Down Expand Up @@ -58,6 +57,15 @@ class AcqOrder(IlsRecord):
fetcher = acq_order_id_fetcher
provider = AcqOrderProvider
model_cls = AcqOrderMetadata
pids_exist_check = {
'required': {
'lib': 'library',
'vndr': 'vendor'
},
'not_required': {
'org': 'organisation'
}
}

@classmethod
def create(cls, data, id_=None, delete_pid=False,
Expand All @@ -68,23 +76,23 @@ def create(cls, data, id_=None, delete_pid=False,
data, id_, delete_pid, dbcommit, reindex, **kwargs)
return record

def update(self, data, dbcommit=False, reindex=False):
"""Update acq order record."""
self._acq_order_build_org_ref(data)
super(AcqOrder, self).update(data, dbcommit, reindex)
return self

@classmethod
def _acq_order_build_org_ref(cls, data):
"""Build $ref for the organisation of the acquisition order."""
library_pid = data.get('library', {}).get('pid')
if not library_pid:
library_pid = data.get('library').get(
'$ref').split('libraries/')[1]
org_pid = Library.get_record_by_pid(library_pid).organisation_pid
base_url = current_app.config.get('RERO_ILS_APP_BASE_URL')
url_api = '{base_url}/api/{doc_type}/{pid}'
org_ref = {
'$ref': url_api.format(
base_url=base_url,
doc_type='organisations',
pid=org_pid or cls.organisation_pid)
}
data['organisation'] = org_ref
library = data.get('library', {})
library_pid = library.get('pid') or \
library.get('$ref').split('libraries/')[1]
data['organisation'] = {'$ref': get_ref_for_pid(
'org',
Library.get_record_by_pid(library_pid).organisation_pid
)}
return data

@property
def organisation_pid(self):
Expand All @@ -94,7 +102,7 @@ def organisation_pid(self):
@property
def library_pid(self):
"""Shortcut for acquisition order library pid."""
return self.replace_refs().get('library').get('pid')
return self.replace_refs()['library']['pid']

def get_number_of_acq_order_lines(self):
"""Get number of acquisition order lines."""
Expand Down
Loading

0 comments on commit b9ee0a1

Please sign in to comment.