def edit(id, data, db_session, username): logger.info(LogMsg.START, username) content = get_internal(id, db_session) if content is None: logger.error(LogMsg.NOT_FOUND, {'content_id': id}) raise Http_error(404, Message.NOT_FOUND) book = get_book(content.book_id, db_session) if 'book_id' in data.keys(): book = get_book(data.get('book_id'), db_session) if book is None: logger.error(LogMsg.NOT_FOUND, {'book_id': data.get('book_id')}) raise Http_error(404, Message.NOT_FOUND) logger.debug(LogMsg.PERMISSION_CHECK, username) permission_data = {} if content.creator == username: permission_data = {Permissions.IS_OWNER.value: True} permissions, presses = get_user_permissions(username, db_session) has_permit = has_permission_or_not([Permissions.BOOK_EDIT_PREMIUM], permissions, None, permission_data) if not has_permit: if book.get('press') in presses: has_permission([Permissions.BOOK_EDIT_PRESS], permissions) else: logger.error(LogMsg.PERMISSION_DENIED) raise Http_error(403, Message.ACCESS_DENIED) logger.debug(LogMsg.PERMISSION_VERIFIED, username) try: for key, value in data.items(): # TODO if key is valid attribute of class setattr(content, key, value) new_content = get_be_data(content.book_id, content.type, db_session) if new_content is not None and new_content.id != content.id: logger.error(LogMsg.BOOK_CONTENT_ALREADY_EXISTS, content_to_dict(new_content, db_session)) raise Http_error(409, Message.ALREADY_EXISTS) edit_basic_data(content, username, data.get('tags')) content.book_press = book.press except: logger.exception(LogMsg.EDIT_FAILED, exc_info=True) raise Http_error(409, Message.ALREADY_EXISTS) logger.debug(LogMsg.EDIT_SUCCESS, model_to_dict(content)) logger.info(LogMsg.END) return content_to_dict(content, db_session)
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 get(id, db_session, username): logger.info(LogMsg.START, username) content = db_session.query(BookContent).filter( BookContent.id == id).first() if content is None: logger.error(LogMsg.NOT_FOUND, {'book_content_id': id}) raise Http_error(404, Message.NOT_FOUND) book = get_book(content.book_id, db_session) if book is None: logger.error(LogMsg.NOT_FOUND, {'book_id': content.book_id}) raise Http_error(404, Message.NOT_FOUND) permission_data = {} if content.creator == username: permission_data = {Permissions.IS_OWNER.value: True} permissions, presses = get_user_permissions(username, db_session) has_permit = has_permission_or_not([Permissions.BOOK_CONTENT_GET_PREMIUM], permissions, None, permission_data) if not has_permit: if book.press in presses: has_permission([Permissions.BOOK_CONTENT_GET_PRESS], permissions) else: logger.error(LogMsg.PERMISSION_DENIED) raise Http_error(403, Message.ACCESS_DENIED) logger.debug(LogMsg.PERMISSION_VERIFIED, username) return content_to_dict(content, db_session)
def delete(id, db_session, username): logger.info(LogMsg.START, username) content = get_internal(id, db_session) if content is None: logger.error(LogMsg.NOT_FOUND, {'content_id': id}) raise Http_error(404, Message.NOT_FOUND) book = get_book(content.book_id, db_session) logger.debug(LogMsg.PERMISSION_CHECK, username) validate_permissions_and_access(username, db_session, 'BOOK_DELETE', model=content) logger.debug(LogMsg.PERMISSION_VERIFIED) try: db_session.delete(content) book.size = None except: logger.exception(LogMsg.EDIT_FAILED, exc_info=True) raise Http_error(409, Message.DELETE_FAILED) logger.debug(LogMsg.DELETE_SUCCESS, {'book_content_id': id}) logger.info(LogMsg.END) return Http_response(204, True)
def delete(id, db_session, username=None): logger.info(LogMsg.START, username) price = get_by_id(id, db_session) if price is None: logger.error(LogMsg.NOT_FOUND, {'book_price_id': id}) raise Http_error(404, Message.NOT_FOUND) book = get_book(price.book_id, db_session) if username is not None: per_data = {} permissions, presses = get_user_permissions(username, db_session) if book.creator == username: per_data.update({Permissions.IS_OWNER.value: True}) has_permit = has_permission_or_not([Permissions.PRICE_DELETE_PREMIUM], permissions, None, per_data) if not has_permit: if book.press in presses: has_permission([Permissions.PRICE_DELETE_PRESS], permissions) else: logger.error(LogMsg.PERMISSION_DENIED, username) raise Http_error(403, Message.ACCESS_DENIED) logger.debug(LogMsg.PERMISSION_VERIFIED) try: db_session.delete(price) logger.debug(LogMsg.DELETE_SUCCESS, {'book_price_id': id}) except: logger.exception(LogMsg.DELETE_FAILED, exc_info=True) raise Http_error(404, Message.DELETE_FAILED) logger.info(LogMsg.END) return Http_response(204, True)
def edit(id, data, db_session, username): logger.info(LogMsg.START, username) schema_validate(data, CONTENT_EDIT_SCHEMA_PATH) logger.debug(LogMsg.SCHEMA_CHECKED) content = get_internal(id, db_session) if content is None: logger.error(LogMsg.NOT_FOUND, {'content_id': id}) raise Http_error(404, Message.NOT_FOUND) book = get_book(content.book_id, db_session) if 'book_id' in data.keys(): book = get_book(data.get('book_id'), db_session) if book is None: logger.error(LogMsg.NOT_FOUND, {'book_id': data.get('book_id')}) raise Http_error(404, Message.NOT_FOUND) logger.debug(LogMsg.PERMISSION_CHECK, username) validate_permissions_and_access(username, db_session, 'BOOK_EDIT', model=content) logger.debug(LogMsg.PERMISSION_VERIFIED) try: for key, value in data.items(): setattr(content, key, value) new_content = get_be_data(content.book_id, content.type, db_session) if new_content is not None and new_content.id != content.id: logger.error(LogMsg.BOOK_CONTENT_ALREADY_EXISTS, content_to_dict(new_content, db_session)) raise Http_error(409, Message.ALREADY_EXISTS) edit_basic_data(content, username, data.get('tags')) content.book_press = book.press if data.get('content') is not None: content.celery_task_id = None content.content_generated = False except: logger.exception(LogMsg.EDIT_FAILED, exc_info=True) raise Http_error(409, Message.ALREADY_EXISTS) logger.debug(LogMsg.EDIT_SUCCESS, model_to_dict(content)) logger.info(LogMsg.END) return content_to_dict(content, db_session)
def generate_book( data,db_session,username): logger.info(LogMsg.START,username) schema_validate(data,GENERATE_BOOK_SCHEMA_PATH) logger.debug(LogMsg.SCHEMA_CHECKED) content_id = data.get('content_id') content = get_content(content_id, db_session) if content is None: logger.error(LogMsg.NOT_FOUND,{'book_content_id':content_id}) raise Http_error(404,Message.NOT_FOUND) book = get_book(content.book_id,db_session) if book is None: logger.error(LogMsg.NOT_FOUND,{'book_id':content.book_id}) raise Http_error(404,Message.NOT_FOUND) logger.debug(LogMsg.PERMISSION_CHECK, username) per_data ={} if book.creator==username: per_data.update({Permissions.IS_OWNER.value:True}) logger.debug(LogMsg.PERMISSION_CHECK, username) validate_permissions_and_access(username, db_session, 'BOOK_CONTENT_ADD', model=content) logger.debug(LogMsg.PERMISSION_VERIFIED) if content.content_generated: logger.error(LogMsg.CONTENT_ALREADY_GENERATED) raise Http_error(409,Message.ALREADY_EXISTS) elif content.celery_task_id is not None: task_status = check_status(content.celery_task_id) status=task_status.get('inquiry_result') if status=='SUCCESS': book.size = content_size(content_id) content.content_generated = True logger.error(LogMsg.CONTENT_ALREADY_GENERATED) db_session.commit() raise Http_error(409,Message.ALREADY_EXISTS) elif status=='PENDING': logger.error(LogMsg.CONTENT_GENERATING) raise Http_error(409, Message.IN_PROCESS) content_data = return_content_full_path(content.content) content_data.update({'content_id':content_id}) result = generate_book_content.apply_async(args=[content_data], routing_key='book_generate') content.celery_task_id=result.task_id print(result.task_id) # result.get() return {'inquiry_id':result.task_id}
def add(db_session, data, username): logger.info(LogMsg.START, username) check_schema(['book_id', 'type', 'content'], data.keys()) logger.debug(LogMsg.SCHEMA_CHECKED) book_id = data.get('book_id') type = data.get('type') logger.debug(LogMsg.ENUM_CHECK, {'BOOK_CONTENT_TYpe': type}) check_enum(type, BookContentType) book = get_book(book_id, db_session) if not book: logger.error(LogMsg.NOT_FOUND, {'book_id': book_id}) raise Http_error(404, Message.NOT_FOUND) logger.debug(LogMsg.CHECK_UNIQUE_EXISTANCE, data) content = get_be_data(book_id, type, db_session) if content is not None: logger.error(LogMsg.CONTENT_EXIST, data) raise Http_error(409, Message.ALREADY_EXISTS) logger.debug(LogMsg.PERMISSION_CHECK, username) permissions, presses = get_user_permissions(username, db_session) has_permit = has_permission_or_not([Permissions.BOOK_ADD_PREMIUM], permissions) if not has_permit: if book.press in presses: has_permission([Permissions.BOOK_ADD_PRESS], permissions) else: logger.error(LogMsg.PERMISSION_DENIED) raise Http_error(403, Message.ACCESS_DENIED) logger.debug(LogMsg.PERMISSION_VERIFIED, username) model_instance = BookContent() logger.debug(LogMsg.POPULATING_BASIC_DATA) populate_basic_data(model_instance, username, data.get('tags')) model_instance.content = data.get('content') model_instance.book_id = book_id model_instance.type = type model_instance.book_press = book.press db_session.add(model_instance) logger.info(LogMsg.END) return model_instance
def calc_price(data, db_session, username): logger.info(LogMsg.START, username) item_prices = [] total_price = 0.000 check_schema(['items'], data.keys()) logger.debug(LogMsg.SCHEMA_CHECKED) items = data.get('items') for item in items: book_id = item.get('book_id') count = item.get('count', 0) discount = item.get('discount', None) price_object = get_book_price_internal(book_id, db_session) if price_object is None: logger.error(LogMsg.PRICE_NOT_FOUND, {'book_id': book_id}) raise Http_error(404, Message.NO_PRICE_FOUND) book = get_book(book_id, db_session) if (book.type.name in ONLINE_BOOK_TYPES) and (count > 1): logger.error(LogMsg.BOOK_ONLINE_TYPE_COUNT_LIMITATION) raise Http_error(400, Message.ONLINE_BOOK_COUNT_LIMITATION) price = price_object * count net_price = price if discount: if isinstance(discount, float) and discount < 1: effect = 1.000 - discount net_price = price * effect else: logger.error(LogMsg.INVALID_ENTITY_TYPE, {'discount': discount}) raise Http_error(400, Message.DISCOUNT_IS_FLOAT) item_info = { 'book_id': book_id, 'unit_price': price_object, 'count': count, 'discount': discount, 'net_price': net_price } logger.debug(LogMsg.PRICE_ITEM_CALC, item_info) item_prices.append(item_info) total_price += net_price final_res = {'items': item_prices, 'total_price': total_price} logger.debug(LogMsg.PRICE_ALL_CALCED, final_res) logger.info(LogMsg.END) return final_res
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 add(db_session, data, username): logger.info(LogMsg.START, username) schema_validate(data, CONTENT_ADD_SCHEMA_PATH) logger.debug(LogMsg.SCHEMA_CHECKED) book_id = data.get('book_id') type = data.get('type') logger.debug(LogMsg.ENUM_CHECK, {'BOOK_CONTENT_TYpe': type}) check_enum(type, BookContentType) book = get_book(book_id, db_session) if not book: logger.error(LogMsg.NOT_FOUND, {'book_id': book_id}) raise Http_error(404, Message.NOT_FOUND) logger.debug(LogMsg.CHECK_UNIQUE_EXISTANCE, data) content = get_be_data(book_id, type, db_session) if content is not None: logger.error(LogMsg.CONTENT_EXIST, data) raise Http_error(409, Message.ALREADY_EXISTS) logger.debug(LogMsg.PERMISSION_CHECK, username) validate_permissions_and_access(username, db_session, 'BOOK_ADD', model=book) logger.debug(LogMsg.PERMISSION_VERIFIED) model_instance = BookContent() logger.debug(LogMsg.POPULATING_BASIC_DATA) populate_basic_data(model_instance, username, data.get('tags')) model_instance.content = data.get('content') model_instance.book_id = book_id model_instance.type = type model_instance.book_press = book.press db_session.add(model_instance) logger.info(LogMsg.END) return model_instance
def add(data, db_session, username): logger.info(LogMsg.START, username) schema_validate(data,ADD_SCHEMA_PATH) logger.debug(LogMsg.SCHEMA_CHECKED) book_id = data.get('book_id') book = get_book(book_id, db_session) if username is not None: per_data = {} permissions, presses = get_user_permissions(username, db_session) if book.creator == username: per_data.update({Permissions.IS_OWNER.value:True}) has_permit = has_permission_or_not([Permissions.PRICE_ADD_PREMIUM], permissions,None,per_data) if not has_permit: if book.press in presses: has_permission([Permissions.PRICE_ADD_PRESS], permissions) else: logger.error(LogMsg.PERMISSION_DENIED,username) raise Http_error(403,Message.ACCESS_DENIED) logger.debug(LogMsg.PERMISSION_VERIFIED) logger.debug(LogMsg.CHECK_BOOK_PRICE_EXISTANCE, book_id) model_instance = get_by_book(book_id, db_session, username) if model_instance: logger.debug(LogMsg.BOOK_PRICE_EXISTS, book_id) logger.debug(LogMsg.EDIT_PRICE, book_id) model_instance.price = data.get('price') else: logger.debug(LogMsg.ADD_NEW_BOOK_PRICE, book_id) model_instance = Price() populate_basic_data(model_instance, username) model_instance.book_id = book_id model_instance.price = data.get('price') db_session.add(model_instance) logger.info(LogMsg.END) return model_instance
def add(data, db_session, username=None): logging.info(LogMsg.START) # if username is not None: # logger.debug(LogMsg.PERMISSION_CHECK, username) # validate_permissions_and_access(username, db_session, 'LIBRARY_ADD') # logger.debug(LogMsg.PERMISSION_VERIFIED) schema_validate(data, ADD_SCHEMA_PATH) logger.debug(LogMsg.SCHEMA_CHECKED) book_id = data.get('book_id') person_id = data.get('person_id') logger.debug(LogMsg.LIBRARY_CHECK_BOOK_EXISTANCE, { 'book_id': book_id, 'person_id': 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.BOOK_IS_ALREADY_PURCHASED) book = get_book(book_id, db_session) if book.type.name not in ONLINE_BOOK_TYPES: logger.debug(LogMsg.LIBRARY_BOOK_TYPE_NOT_ADDABLE, book.type.name) return {} model_instance = Library() populate_basic_data(model_instance) logger.debug(LogMsg.POPULATING_BASIC_DATA) model_instance.person_id = person_id model_instance.book_id = book_id model_instance.progress = 0.00 model_instance.status = { 'status': 'buyed', 'reading_started': False, 'read_pages': 0, 'read_duration': 0.00, 'is_read': False } db_session.add(model_instance) logger.info(LogMsg.END) return model_instance
def add(data, db_session, username=None): logging.info(LogMsg.START) if username is not None: permissions, presses = get_user_permissions(username, db_session) has_permission([Permissions.LIBRARY_ADD_PREMIUM], permissions) check_schema(['book_id', 'person_id'], data.keys()) logger.debug(LogMsg.SCHEMA_CHECKED) book_id = data.get('book_id') person_id = data.get('person_id') logger.debug(LogMsg.LIBRARY_CHECK_BOOK_EXISTANCE, { 'book_id': book_id, 'person_id': 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.BOOK_IS_ALREADY_PURCHASED) book = get_book(book_id, db_session) if book.type.name not in ONLINE_BOOK_TYPES: logger.debug(LogMsg.LIBRARY_BOOK_TYPE_NOT_ADDABLE, book.type.name) return {} model_instance = Library() populate_basic_data(model_instance) logger.debug(LogMsg.POPULATING_BASIC_DATA) model_instance.person_id = person_id model_instance.book_id = book_id model_instance.progress = 0.00 model_instance.status = { 'status': 'buyed', 'reading_started': False, 'read_pages': 0, 'read_duration': 0.00, 'is_read': False } db_session.add(model_instance) logger.info(LogMsg.END) return model_instance
def get(id, db_session, username): logger.info(LogMsg.START, username) content = db_session.query(BookContent).filter( BookContent.id == id).first() if content is None: logger.error(LogMsg.NOT_FOUND, {'book_content_id': id}) raise Http_error(404, Message.NOT_FOUND) book = get_book(content.book_id, db_session) if book is None: logger.error(LogMsg.NOT_FOUND, {'book_id': content.book_id}) raise Http_error(404, Message.NOT_FOUND) logger.debug(LogMsg.PERMISSION_CHECK, username) validate_permissions_and_access(username, db_session, 'BOOK_CONTENT_GET', model=content) logger.debug(LogMsg.PERMISSION_VERIFIED) return content_to_dict(content, db_session)
def edit(id, data, db_session, username=None): logger.info(LogMsg.START, username) schema_validate(data,EDIT_SCHEMA_PATH) logger.debug(LogMsg.SCHEMA_CHECKED) model_instance = get_by_id(id, db_session) if model_instance is None: logger.error(LogMsg.NOT_FOUND, {'book_price_id': id}) raise Http_error(404, Message.NOT_FOUND) book = get_book(model_instance.book_id, db_session) if username is not None: per_data = {} permissions, presses = get_user_permissions(username, db_session) if book.creator == username: per_data.update({Permissions.IS_OWNER.value: True}) has_permit = has_permission_or_not([Permissions.PRICE_EDIT_PREMIUM], permissions, None, per_data) if not has_permit: if book.press in presses: has_permission([Permissions.PRICE_EDIT_PRESS], permissions) else: logger.error(LogMsg.PERMISSION_DENIED, username) raise Http_error(403, Message.ACCESS_DENIED) logger.debug(LogMsg.PERMISSION_VERIFIED) try: model_instance.price = data.get('price') logger.debug(LogMsg.EDIT_SUCCESS, model_to_dict(model_instance)) except: logger.exception(LogMsg.EDIT_FAILED, exc_info=True) raise Http_error(404, Message.EDIT_FAILED) logger.info(LogMsg.END) return model_instance