Skip to content
Snippets Groups Projects
Commit 34992ebf authored by Henrik tom Wörden's avatar Henrik tom Wörden
Browse files

MAINT: refactor set_location

parent 2f8cd6f7
No related branches found
No related tags found
1 merge request!2MAINT: refactor set_location
Pipeline #56762 failed
......@@ -28,30 +28,23 @@ from .box_loan import (main, get_loan, F_LOAN, assert_loan_state,
get_borrower_names, set_property)
def _accept_loan_request(data):
"""Update a loan Record and add the `accepted` Property."""
loan = get_loan(data[F_LOAN])
def _accept_loan_request(loan):
"""Update a loan Record: add the `accepted` Property."""
assert_loan_state(loan, S_LOAN_REQUESTED)
# This changes the state from "loan_requested" to "loan_accepted".
set_property(loan, LOAN_ACCEPTED, get_timestamp())
# To be sure that it worked:
assert_loan_state(loan, S_LOAN_ACCEPTED)
db.Container().extend([
loan
]).update()
return loan
db.Container().extend([loan]).update()
def accept_loan_request(data):
"""Accept a loan request.
I.e. update the `Loan` Record and add the `accepted` Property.
I.e. add the `accepted` Property to the `Loan` Record.
"""
loan = _accept_loan_request(data)
loan = get_loan(data[F_LOAN])
_accept_loan_request(loan)
fn, ln = get_borrower_names(loan)
print_success('Thank you for accepting the loan request by {fn} {ln}. See '
......
......@@ -27,41 +27,36 @@ from caosadvancedtools.serverside.helper import print_success, get_timestamp
from .box_loan import (BOX_BORROWED, CONTENT, main, get_loan, set_property,
F_LOAN, assert_loan_state, RETURN_ACCEPTED,
RETURNLOCATION, S_RETURN_ACCEPTED, S_RETURN_REQUESTED,
get_borrower_names, set_location)
get_borrower_names, set_location_of_borrowed_items)
def _accept_return_request(data):
def _accept_return_request(loan):
"""Update a loan Record and add the `accepted` Property."""
loan = get_loan(data[F_LOAN])
assert_loan_state(loan, S_RETURN_REQUESTED)
# This changes the state from "return_requested" to "return_accepted".
set_property(loan, RETURN_ACCEPTED, get_timestamp())
# To be sure that it worked:
assert_loan_state(loan, S_RETURN_ACCEPTED)
box = set_location(loan, RETURNLOCATION, update=False)
items = set_location_of_borrowed_items(loan, RETURNLOCATION)
if loan.get_property(CONTENT) is not None and loan.get_property(CONTENT).value:
if box.get_property(CONTENT) is not None:
box.get_property(CONTENT).value = loan.get_property(CONTENT).value
else:
box.add_property(id=CONTENT.retrieve().id,
value=loan.get_property(CONTENT).value)
db.Container().extend([
box,
loan
]).update()
for item in items:
if item.get_property(CONTENT) is not None:
item.get_property(CONTENT).value = loan.get_property(CONTENT).value
else:
item.add_property(id=CONTENT.retrieve().id,
value=loan.get_property(CONTENT).value)
return loan
items.append(loan)
items.update()
def accept_return_request(data):
"""Accept a return request.
I.e. update the `Loan` Record and add the `returnAccepted` Property.
I.e.add the `returnAccepted` Property to the `Loan` Record.
"""
loan = _accept_return_request(data)
loan = get_loan(data[F_LOAN])
_accept_return_request(loan)
fn, ln = get_borrower_names(loan)
box_id = loan.get_property(BOX_BORROWED).value.split("@")[0]
......
......@@ -515,23 +515,44 @@ def _caller(func, args):
return func(data)
def get_borrowed_items_from(loan)-> db.Container:
"""Retrieves and returns the Records of items that were borrowed
def set_location(loan, kind, update=True):
"""Set the location of the box to a location of the loan.
Paramters
---------
loan: db.Record, borrowed items of this loan are retrieved and returned
kind can be RETURNLOCATION or DESTINATION
Returns
-------
db.Container, borrowed items referenced by the given loan. Return value is a container with
one element, even if only one item is referenced and the property value is not a list.
"""
# @review Timm Fitschen 2022-03-16
borrowed = loan.get_property(BOX_BORROWED)
if borrowed is None:
raise RuntimeError(
f"Loan has no reference to a borrowed item. {BOX_BORROWED} property is missing."
f"Loan ID: {loan.id}")
elif not isinstance(borrowed.value, list):
borrowed = [borrowed.value]
else:
borrowed = borrowed.value
recs = db.Container().extend([db.Record(id=rec_id.split("@")[0]) for rec_id in borrowed])
return recs.retrieve()
if loan.get_property(kind) is not None:
box_id = loan.get_property(BOX_BORROWED).value.split("@")[0]
box = db.Record(id=box_id).retrieve()
set_property(box, LOCATION, loan.get_property(kind).value)
if update:
box.update()
else:
return box
def set_location_of_borrowed_items(loan, kind)-> list(db.Record):
"""Retrieve Records of borrowed items, sets the location and returns the container
Parameters
----------
kind: db.Property, should be RETURNLOCATION or DESTINATION
"""
if loan.get_property(kind) is None:
raise RuntimeError(
f"Cannot set {kind} because the loan does not have one. Loan ID: {laon.id}")
borrowed = get_borrowed_items_from(loan)
for item in borrowed:
set_property(item, LOCATION, loan.get_property(kind).value)
return borrowed
def main(func, args=None):
# configure_server_side_logging(LOGGER_NAME)
......
......@@ -27,7 +27,7 @@ from caosadvancedtools.serverside.helper import get_timestamp, print_success
from .box_loan import (BOX, BOX_BORROWED, DESTINATION, F_LOAN, LENT, S_LENT,
S_LOAN_ACCEPTED, assert_loan_state, get_borrower_names,
get_loan, main, set_location, set_property)
get_loan, main, set_location_of_borrowed_items, set_property)
def _set_lent_box(loan):
......@@ -43,7 +43,8 @@ def _set_lent_box(loan):
def set_loan_location(loan):
"""Set the location of the box to the return location of the loan. """
set_location(loan, DESTINATION)
items = set_location_of_borrowed_items(loan, DESTINATION)
items.update()
def _confirm_loan(data):
......
......@@ -28,7 +28,7 @@ from caosadvancedtools.serverside.helper import get_timestamp, print_success
from .box_loan import (BOX, BOX_BORROWED, BOX_RETURNED, CONTENT, F_LOAN,
RETURNED, RETURNLOCATION, S_RETURN_ACCEPTED, S_RETURNED,
assert_loan_state, get_borrower_names, get_loan, main,
set_location, set_property)
set_location_of_borrowed_items, set_property)
def _set_returned_box(loan):
......@@ -45,7 +45,8 @@ def _set_returned_box(loan):
def set_return_location(loan):
"""Set the location of the box to the return location of the loan. """
set_location(loan, RETURNLOCATION)
items = set_location_of_borrowed_items(loan, RETURNLOCATION)
items.update()
def set_content(loan):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment