Esempio n. 1
0
def add(data, db_session, username):
    logger.info(LogMsg.START, username)

    schema_validate(data,ITEM_ADD_SCHEMA_PATH)
    logger.debug(LogMsg.SCHEMA_CHECKED)

    book_id = data.get('book_id')

    person_id = data.get('person_id')
    if person_id is None:
        logger.error(LogMsg.DATA_MISSING, 'person_id')

    if is_book_in_library(person_id, book_id, db_session):
        logger.error(LogMsg.ALREADY_IS_IN_LIBRARY, {'book_id': book_id})
        raise Http_error(409, Message.ALREADY_EXISTS)

    book = get_book(book_id, db_session)
    if book is None:
        logger.error(LogMsg.NOT_FOUND, {'book_id': book_id})
        raise Http_error(404, Message.NOT_FOUND)

    if book.type.name in ONLINE_BOOK_TYPES :
        if data.get('count') > 1:
            logger.error(LogMsg.BOOK_ONLINE_TYPE_COUNT_LIMITATION)
            raise Http_error(400, Message.ONLINE_BOOK_COUNT_LIMITATION)

        content_id = book_has_content(book_id,'Original',db_session)
        if not content_id:
            logger.error(LogMsg.CONTENT_NOT_FOUND,{'book_id':book_id})
            raise Http_error(404,Message.BOOK_HAS_NO_CONTENT)

        if not is_generated(content_id):
            logger.error(LogMsg.CONTENT_NOT_GENERATED, {'content_id': content_id})
            raise Http_error(404, Message.BOOK_NOT_GENERATED)

    model_instance = OrderItem()

    populate_basic_data(model_instance, username)
    logger.debug(LogMsg.POPULATING_BASIC_DATA)
    model_instance.order_id = data.get('order_id')
    model_instance.book_id = book_id
    model_instance.count = data.get('count')
    # TODO discount not imposed yet
    model_instance.discount = data.get('discount', 0.0)
    model_instance.description = data.get('description')
    model_instance.price_detail = data.get('price_detail')

    model_instance.unit_price = get_book_price_internal(model_instance.book_id,
                                                        db_session)
    logger.debug(LogMsg.ORDER_ITEM_UNIT_PRICE, model_instance.unit_price)

    model_instance.net_price = calc_net_price(model_instance.unit_price,
                                              model_instance.count,
                                              model_instance.discount)
    logger.debug(LogMsg.ORDER_ITEM_NET_PRICE, model_instance.net_price)

    db_session.add(model_instance)
    logger.info(LogMsg.END)

    return model_instance
Esempio n. 2
0
def add(data, db_session, username):
    logger.info(LogMsg.START, username)

    check_schema(['book_id', 'count', 'order_id', 'person_id'], data.keys())
    logger.debug(LogMsg.SCHEMA_CHECKED)
    book_id = data.get('book_id')

    person_id = data.get('person_id')
    if person_id is None:
        logger.error(LogMsg.DATA_MISSING, 'person_id')

    if is_book_in_library(person_id, book_id, db_session):
        logger.error(LogMsg.ALREADY_IS_IN_LIBRARY, {'book_id': book_id})
        raise Http_error(409, Message.ALREADY_EXISTS)

    book = get_book(book_id, db_session)
    if book is None:
        logger.error(LogMsg.NOT_FOUND, {'book_id': book_id})
        raise Http_error(404, Message.NOT_FOUND)

    if book.type.name in ONLINE_BOOK_TYPES and data.get('count') > 1:
        logger.error(LogMsg.BOOK_ONLINE_TYPE_COUNT_LIMITATION)
        raise Http_error(400, Message.ONLINE_BOOK_COUNT_LIMITATION)

    model_instance = OrderItem()

    populate_basic_data(model_instance, username)
    logger.debug(LogMsg.POPULATING_BASIC_DATA)
    model_instance.order_id = data.get('order_id')
    model_instance.book_id = book_id
    model_instance.count = data.get('count')
    # TODO discount not imposed yet
    model_instance.discount = data.get('discount', 0.0)
    model_instance.description = data.get('description')
    model_instance.price_detail = data.get('price_detail')

    model_instance.unit_price = get_book_price_internal(
        model_instance.book_id, db_session)
    logger.debug(LogMsg.ORDER_ITEM_UNIT_PRICE, model_instance.unit_price)

    model_instance.net_price = calc_net_price(model_instance.unit_price,
                                              model_instance.count,
                                              model_instance.discount)
    logger.debug(LogMsg.ORDER_ITEM_NET_PRICE, model_instance.net_price)

    db_session.add(model_instance)
    logger.info(LogMsg.END)

    return model_instance