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
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
def recalc_order_price(order_id, db_session): logger.info(LogMsg.START) items = get_orders_items_internal(order_id, db_session) order_price = 0.0 for item in items: book_price = get_book_price_internal(item.book_id, db_session) if item.unit_price != book_price: item.unit_price = book_price item.net_price = calc_net_price(book_price, item.count, item.discount) order_price += item.net_price logger.info(LogMsg.END) return order_price
def book_to_dict(db_session, book): if not isinstance(book, Book): raise Http_error(500, LogMsg.NOT_RIGTH_ENTITY_PASSED.format('Book')) result = { 'creation_date': book.creation_date, 'creator': book.creator, 'edition': book.edition, 'genre': str_genre(book.genre), 'id': book.id, 'images': book.images, 'language': book.language, 'modification_date': book.modification_date, 'modifier': book.modifier, 'pub_year': book.pub_year, 'tags': book.tags, 'title': book.title, 'version': book.version, 'roles': append_book_roles_dict(book.id, db_session), 'files': book.files, 'description': book.description, 'pages': book.pages, 'duration': book.duration, 'size': book.size, 'isben': book.isben, 'from_editor': book.from_editor, 'press': book.press, 'price': get_book_price_internal(book.id, db_session) } if isinstance(book.type, str): result['type'] = book.type else: result['type'] = book.type.name rate = book_average_rate(book.id, db_session) result['rate'] = rate.get('rate_average') result['rate_no'] = rate.get('rate_no') return result
def edit(id, data, db_session, username=None): logger.info(LogMsg.START) model_instance = get(id, db_session) logger.debug(LogMsg.MODEL_GETTING, {'order_item_id': id}) if model_instance is None: logger.error(LogMsg.NOT_FOUND, {'order_item_id': id}) raise Http_error(404, Message.NOT_FOUND) order = get_order(model_instance.order_id, db_session) if order is None: logger.error(LogMsg.NOT_FOUND, {'order_id': model_instance.order_id}) raise Http_error(404, Message.NOT_FOUND) if username is not None: logger.debug(LogMsg.PERMISSION_CHECK, username) validate_permissions_and_access(username, db_session, 'ORDER_ITEM_GET', model=model_instance) logger.debug(LogMsg.PERMISSION_VERIFIED, username) permissions, presses = get_user_permissions(username, db_session) if Permissions.ORDER_ITEM_EDIT_PREMIUM not in permissions: if 'unit_price' in data: del data['unit_price'] if 'net_price' in data: del data['net_price'] if 'order_id' in data: del data['order_id'] if 'book_id' in data: del data['book_id'] try: for key, value in data.items(): # TODO if key is valid attribute of class setattr(model_instance, key, value) edit_basic_data(model_instance, username) 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) except: logger.exception(LogMsg.EDIT_FAILED, exc_info=True) raise Http_error(404, Message.DELETE_FAILED) order = calc_total_price_order(model_instance.order_id, db_session) logger.debug(LogMsg.ORDER_CALC_PRICE, order_to_dict(order, db_session, username)) logger.info(LogMsg.END) return model_instance