def confirm_ill_request(ill_loan_cycle, supplier_id, comments=''): """Confirm the given inter library loan request. The ill_loan_cycles current_status will be set to 'ordered'. :param supplier_id: Id of the chosen IllSupplier. :raise: ValidationExceptions """ try: try_confirm_ill_request(ill_loan_cycle) except ValidationExceptions as e: raise e ill_loan_cycle.current_status = IllLoanCycle.STATUS_ORDERED ill_loan_cycle.supplier_id = supplier_id ill_loan_cycle.comments = comments ill_loan_cycle.save() create_event(ill_loan_cycle_id=ill_loan_cycle.id, event=IllLoanCycle.EVENT_ILL_CLC_ORDERED) email_notification('ill_ordered', '*****@*****.**', ill_loan_cycle.user.email, ill_loan_cycle=ill_loan_cycle)
def request_ill(user, record, start_date, end_date, delivery=None, comments='', type=None): """Request inter library loan for the given record to the given user. :param record: Invenio Record. :param user: CirculationUser. :param start_date: Start date of the loan (without time). :param end_date: End date of the loan (without time). :param delivery: 'pick_up' or 'internal_mail' :param comments: Comments regarding the inter library loan. :return: Created IllLoanCycle """ delivery = IllLoanCycle.DELIVERY_DEFAULT if delivery is None else delivery type = IllLoanCycle.TYPE_BOOK if type is None else type item = _create_ill_temporary_item(record) ill_clc = IllLoanCycle.new(current_status=IllLoanCycle.STATUS_REQUESTED, item=item, user=user, start_date=start_date, end_date=end_date, delivery=delivery, comments=comments, type=type) create_event(user_id=user.id, item_id=item.id, ill_loan_cycle_id=ill_clc.id, event=IllLoanCycle.EVENT_ILL_CLC_REQUEST) email_notification('ill_request', '*****@*****.**', user.email, name=user.name, action='requested', items=item) return ill_clc
def deliver_ill(ill_loan_cycle): """Deliver the given inter library loan. The items current_status will be set to 'on_loan'. The ill_loan_cycles current_status will be set to 'on_loan'. :raise: ValidationExceptions """ try: try_deliver_ill(ill_loan_cycle) except ValidationExceptions as e: raise e import invenio_circulation.models as models ill_loan_cycle.current_status = IllLoanCycle.STATUS_ON_LOAN ill_loan_cycle.save() ill_loan_cycle.item.current_status = models.CirculationItem.STATUS_ON_LOAN ill_loan_cycle.item.save() create_event(ill_loan_cycle_id=ill_loan_cycle.id, event=IllLoanCycle.EVENT_ILL_CLC_DELIVERED) email_notification('ill_delivery', '*****@*****.**', ill_loan_cycle.user.email, ill_loan_cycle=ill_loan_cycle)
def decline_ill_extension(ill_loan_cycle, reason=''): """Decline the requested extension for the given inter library loan. 'extension_requested' will be removed from additional_statuses. The desired_end_date will be set to None. :raise: ValidationExceptions """ try: try_decline_ill_extension(ill_loan_cycle) except ValidationExceptions as e: raise e ser = IllLoanCycle.STATUS_EXTENSION_REQUESTED ill_loan_cycle.desired_end_date = None ill_loan_cycle.additional_statuses.remove(ser) ill_loan_cycle.save() create_event(ill_loan_cycle_id=ill_loan_cycle.id, event=IllLoanCycle.EVENT_ILL_CLC_EXTENSION_DECLINED, description=reason) email_notification('ill_extension_request_declined', '*****@*****.**', ill_loan_cycle.user.email, loan_cycle=ill_loan_cycle)
def confirm_ill_extension(ill_loan_cycle): """Confirm the requested extension for the given inter library loan. The end_date and desired_end_date attributes will be adjusted. 'extension_requested' will be removed from additional_statuses. :raise: ValidationExceptions """ try: try_confirm_ill_extension(ill_loan_cycle) except ValidationExceptions as e: raise e ser = IllLoanCycle.STATUS_EXTENSION_REQUESTED ill_loan_cycle.end_date = ill_loan_cycle.desired_end_date ill_loan_cycle.desired_end_date = None ill_loan_cycle.additional_statuses.remove(ser) ill_loan_cycle.save() create_event(ill_loan_cycle_id=ill_loan_cycle.id, event=IllLoanCycle.EVENT_ILL_CLC_EXTENSION_CONFIRMED) email_notification('ill_extension_request_confirmed', '*****@*****.**', ill_loan_cycle.user.email, loan_cycle=ill_loan_cycle)
def request_acquisition(user, record, acquisition_type, copies, payment_method, budget_code='', price='', delivery=None, comments=''): if not delivery: delivery = AcquisitionLoanCycle.DELIVERY_DEFAULT item = _create_acquisition_temporary_item(record) acquisition_clc = AcquisitionLoanCycle.new( current_status=AcquisitionLoanCycle.STATUS_REQUESTED, item=item, user=user, acquisition_type=acquisition_type, copies=copies, payment_method=payment_method, budget_code=budget_code, price=price, delivery=delivery, comments=comments, issued_date=datetime.datetime.now()) if acquisition_type == 'acquisition': event = AcquisitionLoanCycle.EVENT_ACQUISITION_REQUEST elif acquisition_type == 'purchase': event = AcquisitionLoanCycle.EVENT_PURCHASE_REQUEST create_event(user_id=user.id, item_id=item.id, acquisition_loan_cycle_id=acquisition_clc.id, event=event) email_notification('acquisition_request', '*****@*****.**', user.email, name=user.name, action='requested', items=item) return acquisition_clc
def lose_items(items): """Lose the given items. This sets the current_status to missing. All CirculationLoanCycles with current_status 'on_loan' or 'requested' associated with the given item will be canceled. :raise: ValidationExceptions """ try: try_lose_items(items) except ValidationExceptions as e: raise e CLC = models.CirculationLoanCycle for item in items: item.current_status = models.CirculationItem.STATUS_MISSING item.save() create_event(item_id=item.id, event=models.CirculationItem.EVENT_MISSING) query = 'item_id:{0} current_status:{1}' statuses = [models.CirculationLoanCycle.STATUS_REQUESTED, models.CirculationLoanCycle.STATUS_ON_LOAN] clcs = [x for status in statuses for x in CLC.search(query.format(item.id, status))] cancel_clcs(clcs)
def update(cu, **kwargs): """Update a CirculationLoanUser object.""" current_items, changed = _update(cu, **kwargs) if changed: changes_str = ['{0}: {1} -> {2}'.format(key, current_items[key], changed[key]) for key in changed] create_event(user_id=cu.id, event=models.CirculationUser.EVENT_CHANGE, description=', '.join(changes_str))
def create(code, name, notes): """Create a CirculationLoanLocation object. :raise: ValidationExceptions :return: The newly created object. """ cl = models.CirculationLocation.new(code=code, name=name, notes=notes) create_event(location_id=cl.id, event=models.CirculationLocation.EVENT_CREATE) return cl
def update(clr, **kwargs): """Update a CirculationLoanLoanRuleMatch object.""" current_items, changed = _update(clr, **kwargs) if changed: changes_str = ['{0}: {1} -> {2}'.format(key, current_items[key], changed[key]) for key in changed] create_event(loan_rule_match_id=clr.id, event=models.CirculationLoanRuleMatch.EVENT_CHANGE, description=', '.join(changes_str))
def update(cmt, **kwargs): """Update a CirculationLoanMailTemplate object.""" current_items, changed = _update(cmt, **kwargs) if changed: changes_str = ['{0}: {1} -> {2}'.format(key, current_items[key], changed[key]) for key in changed] create_event(mail_template_id=cmt.id, event=models.CirculationMailTemplate.EVENT_CHANGE, description=', '.join(changes_str))
def cancel_acquisition_request(acquisition_loan_cycle, reason=''): try: try_cancel_acquisition_request(acquisition_loan_cycle) except ValidationExceptions as e: raise e status = AcquisitionLoanCycle.STATUS_CANCELED acquisition_loan_cycle.current_status = status acquisition_loan_cycle.save() create_event(acquisition_loan_cycle_id=acquisition_loan_cycle.id, event=AcquisitionLoanCycle.EVENT_ACQUISITION_CANCELED, description=reason)
def create(template_name, subject, header, content): """Create a CirculationLoanMailTemplate object. :raise: ValidationExceptions :return: The newly created object. """ cmt = models.CirculationMailTemplate.new( template_name=template_name, subject=subject, header=header, content=content) create_event(mail_template_id=cmt.id, event=models.CirculationMailTemplate.EVENT_CREATE) return cmt
def create(loan_rule_id, item_type, patron_type, location_code, active): """Create a CirculationLoanLoanRuleMatch object. :raise: ValidationExceptions :return: The newly created object. """ clr = models.CirculationLoanRuleMatch.new( loan_rule_id=loan_rule_id, item_type=item_type, patron_type=patron_type, location_code=location_code, active=active) create_event(loan_rule_match_id=clr.id, event=models.CirculationLoanRuleMatch.EVENT_CREATE) return clr
def return_processed_items(items): """Return the given processed items. The items current_status will be set to 'on_shelf'. :raise: ValidationExceptions """ try: try_return_processed_items(items) except ValidationExceptions as e: raise e for item in items: item.current_status = models.CirculationItem.STATUS_ON_SHELF item.save() create_event(item_id=item.id, event=models.CirculationItem.EVENT_PROCESS_RETURNED)
def deliver_acquisition(acquisition_loan_cycle): try: try_deliver_acquisition(acquisition_loan_cycle) except ValidationExceptions as e: raise e status = AcquisitionLoanCycle.STATUS_DELIVERED acquisition_loan_cycle.current_status = status acquisition_loan_cycle.save() create_event(acquisition_loan_cycle_id=acquisition_loan_cycle.id, event=AcquisitionLoanCycle.EVENT_ACQUISITION_DELIVERED) email_notification('acquisition_delivery', '*****@*****.**', acquisition_loan_cycle.user.email, acquisition_loan_cycle=acquisition_loan_cycle)
def create(name, type, loan_period, holdable, home_pickup, renewable, automatic_recall): """Create a CirculationLoanLoanRule object. :raise: ValidationExceptions :return: The newly created object. """ clr = models.CirculationLoanRule.new( name=name, type=type, loan_period=loan_period, holdable=holdable, home_pickup=home_pickup, renewable=renewable, automatic_recall=automatic_recall) create_event(loan_rule_id=clr.id, event=models.CirculationLoanRule.EVENT_CREATE) return clr
def overdue_clcs(clcs): """Overdue the given loan cycles. 'overdue' will be added to the attribute additional_statuses. :raise: ValidationExceptions """ try: try_overdue_clcs(clcs) except ValidationExceptions as e: raise e for clc in clcs: clc.additional_statuses.append( models.CirculationLoanCycle.STATUS_OVERDUE) clc.save() create_event(loan_cycle_id=clc.id, event=models.CirculationLoanCycle.EVENT_OVERDUE)
def send_message(users, subject, message): """Send a message with the provided subject to the given users.""" sender = '*****@*****.**' for user in users: msg = Message(sender=sender, recipients=[user.email], subject=subject, body=message) # TODO: socket exception ~.~ try: current_app.extensions['mail'].send(msg) except Exception: print msg create_event(user_id=user.id, event=models.CirculationUser.EVENT_MESSAGED, description='\n'.join([subject, message]))
def cancel_ill_request(ill_loan_cycle, reason=''): """Cancel the given inter library loan. The ill_loan_cycles current_status will be set to 'canceled'. :raise: ValidationExceptions """ try: try_cancel_ill_request(ill_loan_cycle) except ValidationExceptions as e: raise e ill_loan_cycle.current_status = IllLoanCycle.STATUS_CANCELED ill_loan_cycle.save() create_event(ill_loan_cycle_id=ill_loan_cycle.id, event=IllLoanCycle.EVENT_ILL_CLC_CANCELED, description=reason)
def create(invenio_user_id, ccid, name, address, mailbox, email, phone, notes, user_group, division='', cern_group=''): """Create a CirculationLoanUser object. :raise: ValidationExceptions :return: The newly created object. """ cu = models.CirculationUser.new( current_status=models.CirculationUser.STATUS_ACTIVE, invenio_user_id=invenio_user_id, ccid=ccid, name=name, address=address, mailbox=mailbox, email=email, phone=phone, notes=notes, user_group=user_group, division=division, cern_group=cern_group) create_event(user_id=cu.id, event=models.CirculationUser.EVENT_CREATE) return cu
def transform_into_loan(clcs): """Transform the given requests into loans. The items current_status will be set to 'on_loan'. :raise: ValidationExceptions """ try: try_transform_into_loan(clcs) except ValidationExceptions as e: raise e event = models.CirculationLoanCycle.EVENT_TRANSFORMED_REQUEST for clc in clcs: clc.current_status = models.CirculationLoanCycle.STATUS_ON_LOAN clc.save() create_event(loan_cycle_id=clc.id, event=event)
def lose_ill(ill_loan_cycle): """Lose the given inter library loan. The ill_loan_cycles current_status will be set to 'missing'. The items current_status will be set to 'missing'. :raise: ValidationExceptions """ try: try_lose_ill(ill_loan_cycle) except ValidationExceptions as e: raise e ill_loan_cycle.current_status = IllLoanCycle.STATUS_MISSING ill_loan_cycle.save() create_event(ill_loan_cycle_id=ill_loan_cycle.id, event=IllLoanCycle.EVENT_ILL_CLC_LOST) lose_items([ill_loan_cycle.item])
def process_items(items, description): """Process the given items. The items current_status will be set to 'in_process'. :param description: The reason of the processing of the items. :raise: ValidationExceptions """ try: try_process_items(items) except ValidationExceptions as e: raise e for item in items: item.current_status = models.CirculationItem.STATUS_IN_PROCESS item.save() create_event(item_id=item.id, event=models.CirculationItem.EVENT_IN_PROCESS, description=description)
def send_back_ill(ill_loan_cycle): """Send the given inter library loan back. The ill_loan_cycles current_status will be set to 'send_back'. The items current_status will be set to 'unavailable'. :raise: ValidationExceptions """ try: try_send_back_ill(ill_loan_cycle) except ValidationExceptions as e: raise e ill_loan_cycle.current_status = IllLoanCycle.STATUS_SEND_BACK ill_loan_cycle.save() ill_loan_cycle.item.current_status = CirculationItem.STATUS_UNAVAILABLE ill_loan_cycle.item.save() create_event(ill_loan_cycle_id=ill_loan_cycle.id, event=IllLoanCycle.EVENT_ILL_CLC_SEND_BACK)
def cancel_clcs(clcs, reason=''): """Cancelt the given loan cycles. The items current_status will be set to 'on_shelf'. This also calls api.loan_cycle.update_waitlist. :raise: ValidationExceptions """ try: try_cancel_clcs(clcs) except ValidationExceptions as e: raise e for clc in clcs: clc.current_status = models.CirculationLoanCycle.STATUS_CANCELED clc.save() create_event(loan_cycle_id=clc.id, event=models.CirculationLoanCycle.EVENT_CANCELED, description=reason) update_waitlist(clc)
def confirm_acquisition_request(acquisition_loan_cycle, vendor_id, price, currency, comments): try: try_confirm_acquisition_request(acquisition_loan_cycle) except ValidationExceptions as e: raise e acquisition_loan_cycle.current_status = AcquisitionLoanCycle.STATUS_ORDERED acquisition_loan_cycle.vendor_id = vendor_id acquisition_loan_cycle.price = price acquisition_loan_cycle.currency = currency acquisition_loan_cycle.comments = comments acquisition_loan_cycle.save() create_event(acquisition_loan_cycle_id=acquisition_loan_cycle.id, event=AcquisitionLoanCycle.EVENT_ACQUISITION_ORDERED) email_notification('acquisition_ordered', '*****@*****.**', acquisition_loan_cycle.user.email, acquisition_loan_cycle=acquisition_loan_cycle)
def decline_ill_request(ill_loan_cycle, reason=''): """Decline the given inter library loan request. The ill_loan_cycles current_status will be set to 'declined'. :raise: ValidationExceptions """ try: try_decline_ill_request(ill_loan_cycle) except ValidationExceptions as e: raise e ill_loan_cycle.current_status = IllLoanCycle.STATUS_DECLINED ill_loan_cycle.save() create_event(ill_loan_cycle_id=ill_loan_cycle.id, event=IllLoanCycle.EVENT_ILL_CLC_DECLINED, description=reason) email_notification('ill_declined', '*****@*****.**', ill_loan_cycle.user.email, ill_loan_cycle=ill_loan_cycle)
def create(item_id, user_id, current_status, start_date, end_date, desired_start_date, desired_end_date, issued_date, delivery, group_uuid=None): """Create a CirculationLoanCycle object. :raise: ValidationExceptions :return: The newly created object. """ clc = models.CirculationLoanCycle.new( item_id=item_id, user_id=user_id, current_status=current_status, start_date=start_date, end_date=end_date, desired_start_date=desired_start_date, desired_end_date=desired_end_date, issued_date=issued_date, delivery=delivery, group_uuid=group_uuid) create_event(loan_cycle_id=clc.id, event=models.CirculationLoanCycle.EVENT_CREATE) return clc
def create(record_id, location_id, isbn, barcode, collection, shelf_number, volume, description, current_status, item_group): """Create a CirculationItem object. :raise: ValidationExceptions :return: The newly created object. """ try: try_create(current_status) except ValidationExceptions as e: raise e ci = models.CirculationItem.new( record_id=record_id, location_id=location_id, barcode=barcode, isbn=isbn, collection=collection, shelf_number=shelf_number, current_status=current_status, description=description, item_group=item_group) description = 'Created in status: {0}'.format(current_status) create_event(item_id=ci.id, event=models.CirculationItem.EVENT_CREATE, description=description) ci.record = models.CirculationRecord.get(ci.record_id) return ci
def request_ill_extension(ill_loan_cycle, requested_end_date): """Request an extension for the given inter library loan. 'extension_requested' will be added to the attribute additional_statuses. The desired_end_date will be set to requested_end_date. :raise: ValidationExceptions """ try: try_request_ill_extension(ill_loan_cycle) except ValidationExceptions as e: raise e ser = IllLoanCycle.STATUS_EXTENSION_REQUESTED ill_loan_cycle.desired_end_date = requested_end_date ill_loan_cycle.additional_statuses.append(ser) ill_loan_cycle.save() create_event(ill_loan_cycle_id=ill_loan_cycle.id, event=IllLoanCycle.EVENT_ILL_CLC_EXTENSION_REQUESTED) email_notification('ill_extension_request', '*****@*****.**', ill_loan_cycle.user.email, loan_cycle=ill_loan_cycle)
def loan_extension(clcs, requested_end_date): """Extend the given loan cycles. 'overdue' will be removed from the attribute additional_statuses. :raise: ValidationExceptions """ new_end_date = requested_end_date try: try_loan_extension(clcs, requested_end_date) except ValidationExceptions as e: raise e for clc in clcs: try: clc.additional_statuses.remove( models.CirculationLoanCycle.STATUS_OVERDUE) except ValueError: pass clc.desired_end_date = requested_end_date clc.end_date = new_end_date clc.save() create_event(loan_cycle_id=clc.id, event=models.CirculationLoanCycle.EVENT_LOAN_EXTENSION)
def return_ill(ill_loan_cycle): """Return the given inter library loan. The ill_loan_cycles current_status will be set to 'finished'. The items current_status will be set to 'on_shelf'. This function should be called using a signal once the corresponding item is returned. :raise: ValidationExceptions """ try: try_return_ill(ill_loan_cycle) except ValidationExceptions as e: raise e ill_loan_cycle.current_status = IllLoanCycle.STATUS_FINISHED ill_loan_cycle.save() ill_loan_cycle.item.current_status = CirculationItem.STATUS_ON_SHELF ill_loan_cycle.item.save() create_event(ill_loan_cycle_id=ill_loan_cycle.id, event=IllLoanCycle.EVENT_ILL_CLC_FINISHED)