def delete(id, db_session, username=None): logger.info(LogMsg.START) order_item = get(id, db_session) if order_item is None: logger.error(LogMsg.NOT_FOUND, {'order_item_id': id}) raise Http_error(404, Message.NOT_FOUND) order = get_order(order_item.order_id, db_session) if order is None: logger.error(LogMsg.NOT_FOUND, {'order_id': order_item.order_id}) raise Http_error(404, Message.NOT_FOUND) if username is not None: user = check_user(username, db_session) per_data = {} permissions, presses = get_user_permissions(username, db_session) if order_item.creator == username or order.person_id == user.person_id: per_data.update({Permissions.IS_OWNER.value: True}) has_permission([Permissions.ORDER_ITEM_GET_PREMIUM], permissions, None, per_data) logger.debug(LogMsg.PERMISSION_VERIFIED) try: db_session.delete(order_item) logger.debug(LogMsg.ORDER_ITEM_DELETED, id) new_order = calc_total_price_order(order.id, db_session) logger.debug(LogMsg.ORDER_CALC_PRICE, order_to_dict(new_order, db_session, username)) 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 get_orders_items(order_id, db_session, username=None): logger.info(LogMsg.START) result = db_session.query(OrderItem).filter( OrderItem.order_id == order_id).all() order = get_order(order_id, db_session) if order is None: logger.error(LogMsg.NOT_FOUND, {'order_id': order_id}) raise Http_error(404, Message.NOT_FOUND) if username is not None: user = check_user(username, db_session) per_data = {} permissions, presses = get_user_permissions(username, db_session) if order.person_id == user.person_id or ( result is not None and result[0].creator == username): per_data.update({Permissions.IS_OWNER.value: True}) has_permission([Permissions.ORDER_ITEM_GET_PREMIUM], permissions, None, per_data) logger.debug(LogMsg.PERMISSION_VERIFIED) final_res = [] for item in result: final_res.append(item_to_dict(item, db_session)) logger.debug(LogMsg.ORDERS_ITEMS, final_res) logger.info(LogMsg.END) return final_res
def get_person_accounts(person_id, db_session, username): logger.info(LogMsg.START, username) permissions, presses = get_user_permissions(username, db_session) has_permission([Permissions.ACCOUNT_GET_PREMIUM], permissions) rtn = [] try: logger.debug(LogMsg.GETTING_PERSON_ALL_ACCOUNTS, person_id) result = db_session.query(Account).filter( Account.person_id == person_id).all() for item in result: rtn.append(account_to_dict(item)) logger.debug(LogMsg.GET_SUCCESS, rtn) except: logger.error(LogMsg.GET_FAILED, {'person_id': person_id}, exc_info=True) raise Http_error(404, Message.GET_FAILED) logger.info(LogMsg.END) return rtn
def delete(id, db_session, username): logger.info(LogMsg.START, username) group_member = get_model(id, db_session) if group_member is None: logger.error(LogMsg.NOT_FOUND, {'discussion_group_member': id}) raise Http_error(404, Message.NOT_FOUND) user = check_user(username, db_session) per_data = {} permissions, presses = get_user_permissions(username, db_session) if group_member.person_id == user.person_id or is_admin_member( user.person_id, group_member.group_id, db_session): per_data.update({Permissions.IS_OWNER.value: True}) has_permission([Permissions.DISCUSSION_MEMBER_PREMIUM], permissions, None, per_data) logger.debug(LogMsg.PERMISSION_VERIFIED, username) db_session.query(DiscussionMember).filter( DiscussionMember.id == id).delete() logger.debug(LogMsg.DISCUSSION_GROUP_OLD_MEMBERS_DELETED) return Http_response(204, True)
def get_user_direct_counts(person_id, db_session, username, **kwargs): logger.info(LogMsg.START, username) user = check_user(username, db_session) permissions, presses = get_user_permissions(username, db_session) permission_data = {} if user.person_id == person_id: permission_data.update({Permissions.IS_OWNER.value: True}) has_permission([Permissions.CHAT_GET_PREMIUM], permissions, None, permission_data) logger.debug(LogMsg.PERMISSION_VERIFIED, username) try: final_res = {} logger.debug(LogMsg.CHAT_GET_USER_MESSAGES, person_id) result = db_session.query(ChatMessage).filter( and_(ChatMessage.receptor_id == person_id, ChatMessage.group_id == None)).order_by( ChatMessage.creation_date.desc()).all() for item in result: final_res[item.sender_id] = 0 seen = get_receptor_sender_last_seen(person_id, item.sender_id, db_session) if seen.last_seen is None or item.creation_date > seen.last_seen: final_res[item.sender_id] += 1 except: logger.exception(LogMsg.GET_FAILED, exc_info=True) raise Http_error(400, Message.NOT_FOUND) logger.info(LogMsg.END) return final_res
def get_person_profile(id, db_session, username): logger.info(LogMsg.START, username) user = check_user(username, db_session) per_data = {} permissions, presses = get_user_permissions(username, db_session) if user.person_id == id: per_data.update({Permissions.IS_OWNER.value: True}) has_permission([Permissions.PERSON_GET_PREMIUM], permissions, None, per_data) logger.debug(LogMsg.PERMISSION_VERIFIED) logger.debug(LogMsg.MODEL_GETTING) model_instance = db_session.query(Person).filter(Person.id == id).first() if model_instance: result = model_to_dict(model_instance) result['current_book'] = get_current_book( model_instance.current_book_id, db_session) or None result['following_list'] = list(get_following_list_internal(id, db_session)) result['wish_list'] = list(internal_wish_list(db_session, id)) logger.debug(LogMsg.GET_SUCCESS, result) else: logger.error(LogMsg.NOT_FOUND, {"Person_id": id}) raise Http_error(404, Message.NOT_FOUND) logger.info(LogMsg.END) return result
def delete_comments(book_id, db_session, username, **kwargs): logger.info(LogMsg.START, username) logger.debug(LogMsg.COMMENT_DELETING_BOOK_COMMENTS, book_id) book = get_book(book_id, db_session) press = book.get('press', None) permissions, presses = get_user_permissions(username, db_session) has_permit = has_permission_or_not([Permissions.COMMENT_DELETE_PREMIUM], permissions) if not has_permit: if press in presses: has_permission([Permissions.COMMENT_DELETE_PRESS], permissions) else: logger.error(LogMsg.PERMISSION_DENIED, username) raise Http_error(403, Message.ACCESS_DENIED) logger.debug(LogMsg.PERMISSION_VERIFIED, username) delete_book_comments(book_id, db_session) logger.info(LogMsg.END) return Http_response(204, True)
def delete(id, db_session, username, **kwargs): logger.info(LogMsg.START) permission_data = {} logger.debug(LogMsg.RATE_CHECK) model_instance = db_session.query(Rate).filter(Rate.id == id).first() if model_instance: logger.debug(LogMsg.RATE_EXISTS,model_to_dict(model_instance)) else: logger.debug(LogMsg.RATE_NOT_EXISTS,{'rate_id':id}) raise Http_error(404, Message.NOT_FOUND) user = check_user(username, db_session) if user is None: raise Http_error(400, Message.INVALID_USER) if user.person_id is None: logger.error(LogMsg.USER_HAS_NO_PERSON) raise Http_error(400, Message.Invalid_persons) if model_instance.person_id == user.person_id: permission_data = {Permissions.IS_OWNER.value:True} permissions,presses = get_user_permissions(username, db_session) has_permission([Permissions.RATE_DELETE_PREMIUM], permissions,None,permission_data) try: db_session.query(Rate).filter(Rate.id == id).delete() logger.debug(LogMsg.RATE_DELETED,{'rate_id':id}) except: logger.exception(LogMsg.DELETE_FAILED,exc_info=True) raise Http_error(404, Message.DELETE_FAILED) logger.info(LogMsg.END) return {}
def delete(id, db_session, username): logger.info(LogMsg.START, username) user = db_session.query(User).filter(User.id == id).first() if user is None: logger.error(LogMsg.NOT_FOUND, {'user_id': id}) raise Http_error(404, Message.NOT_FOUND) per_data = {} permissions, presses = get_user_permissions(username, db_session) if user.username == username: per_data.update({Permissions.IS_OWNER.value: True}) has_permission([Permissions.USER_DELETE_PREMIUM], permissions, None, per_data) logger.debug(LogMsg.PERMISSION_VERIFIED) try: logger.debug(LogMsg.DELETE_REQUEST, {'user_id': id}) logger.debug(LogMsg.GROUP_DELETE_USER_GROUPS, id) delete_user_from_groups(id, db_session) db_session.delete(user) logger.debug(LogMsg.DELETE_SUCCESS, {'user_id': id}) except: logger.exception(LogMsg.DELETE_FAILED, exc_info=True) raise Http_error(500, LogMsg.DELETE_FAILED) logger.info(LogMsg.END) return {}
def edit(id, data, db_session, username): logger.info(LogMsg.START) permission_data = {} user = check_user(username, db_session) if user is None: raise Http_error(400, Message.INVALID_USER) if user.person_id is None: logger.error(LogMsg.USER_HAS_NO_PERSON,username) raise Http_error(400, Message.Invalid_persons) logger.debug(LogMsg.RATE_CHECK,{'rate_id':id}) model_instance = db_session.query(Rate).filter(Rate.id == id).first() if model_instance: logger.debug(LogMsg.RATE_EXISTS,model_to_dict(model_instance)) else: logger.error(LogMsg.RATE_NOT_EXISTS,{'rate_id':id}) raise Http_error(404, Message.NOT_FOUND) if model_instance.person_id == user.person_id: permission_data = {Permissions.IS_OWNER.value:True} permissions,presses = get_user_permissions(username, db_session) has_permission([Permissions.RATE_EDIT_PREMIUM], permissions,None,permission_data) model_instance.rate = data.get('rate') edit_basic_data(model_instance,username,data.get('tags')) logger.debug(LogMsg.RATE_CHANGED,model_to_dict(model_instance)) logger.info(LogMsg.END) return model_instance
def get_personal_library(data, db_session, username): logger.info(LogMsg.START, username) permissions, presses = get_user_permissions(username, db_session) has_permission([Permissions.LIBRARY_DELETE_PREMIUM], permissions, None, {Permissions.IS_OWNER.value: True}) user = check_user(username, db_session) if user.person_id is None: logger.error(LogMsg.USER_HAS_NO_PERSON, username) raise Http_error(400, Message.Invalid_persons) validate_person(user.person_id, db_session) logger.debug(LogMsg.PERSON_EXISTS) if data.get('sort') is None: data['sort'] = ['creation_date-'] if data.get('filter') is None: data.update({'filter': {'person_id': user.person_id}}) else: data['filter'].update({'person_id': user.person_id}) logger.debug(LogMsg.LIBRARY_GET_PERSON_LIBRARY, username) result = Library.mongoquery( db_session.query(Library)).query(**data).end().all() logger.info(LogMsg.END) return lib_to_dictlist(result, db_session)
def get(id, db_session, username=None): logger.info(LogMsg.START, username) logger.debug(LogMsg.MODEL_GETTING) model_instance = db_session.query(Person).filter(Person.id == id).first() if model_instance: person_dict = person_to_dict(model_instance, db_session) logger.debug(LogMsg.GET_SUCCESS + json.dumps(person_dict)) else: logger.debug(LogMsg.MODEL_GETTING_FAILED) raise Http_error(404, Message.NOT_FOUND) if username is not None: user = check_user(username, db_session) per_data = {} permissions, presses = get_user_permissions(username, db_session) if user.person_id == id: per_data.update({Permissions.IS_OWNER.value: True}) has_permission([Permissions.PERSON_GET_PREMIUM], permissions, None, per_data) logger.debug(LogMsg.PERMISSION_VERIFIED) logger.info(LogMsg.END) return person_dict
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) logger.info(LogMsg.DELETE_REQUEST, {'group_id': id}) user = check_user(username, db_session) per_data = {} permissions, presses = get_user_permissions(username, db_session) if is_admin_member(user.person_id, id, db_session): per_data.update({Permissions.IS_OWNER.value: True}) has_permission([Permissions.DISCUSSION_GROUP_PREMIUM], permissions, None, per_data) logger.debug(LogMsg.PERMISSION_VERIFIED, username) model_instance = db_session.query(DiscussionGroup).filter( DiscussionGroup.id == id).first() if model_instance is None: logger.error(LogMsg.NOT_FOUND, {'group_id': id}) raise Http_error(404, Message.NOT_FOUND) try: delete_group_members(model_instance.id, db_session, username) logger.debug(LogMsg.DISCUSSION_GROUP_OLD_MEMBERS_DELETED) db_session.delete(model_instance) logger.debug(LogMsg.DISCUSSION_GROUP_DELETE, id) except: logger.exception(LogMsg.DELETE_FAILED, exc_info=True) raise Http_error(500, LogMsg.DELETE_FAILED) logger.info(LogMsg.END) return Http_response(204, True)
def delete(id, db_session, username=None): logger.info(LogMsg.START, username) order = internal_get(id, db_session) if order is None: logger.error(LogMsg.NOT_FOUND, {'order_id': id}) raise Http_error(404, Message.NOT_FOUND) permissions, presses = get_user_permissions(username, db_session) per_data = {} if order.creator == username: per_data.update({Permissions.IS_OWNER.value:True}) has_permission( [Permissions.ORDER_DELETE_PREMIUM], permissions,None,per_data) logger.debug(LogMsg.PERMISSION_VERIFIED) if order.status == OrderStatus.Invoiced: logger.error(LogMsg.ORDER_NOT_EDITABLE, order_to_dict(order, db_session, username)) raise Http_error(403, Message.ORDER_INVOICED) try: logger.debug(LogMsg.ORDER_ITEMS_DELETE, {'order_id': id}) delete_orders_items_internal(order.id, db_session) logger.debug(LogMsg.ORDER_DELETE, {'order_id': id}) db_session.delete(order) 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 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) 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_DELETE_PREMIUM], permissions, None, permission_data) if not has_permit: if content.book_press in presses: has_permission([Permissions.BOOK_DELETE_PRESS], permissions) else: logger.error(LogMsg.PERMISSION_DENIED) raise Http_error(403, Message.ACCESS_DENIED) logger.debug(LogMsg.PERMISSION_VERIFIED, username) try: db_session.delete(content) 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 add(data, db_session, username=None): logger.info(LogMsg.START, username) if data.get('credit') and data.get('debit'): logger.error(LogMsg.DATA_MISSING) raise Http_error(400, Message.CREDIT_DEBIT_ERROR) if username is not None: permissions, presses = get_user_permissions(username, db_session) has_permission([Permissions.TRANSACTION_ADD_PREMIUM], permissions) account_id = data.get('account_id') logger.debug(LogMsg.GETTING_ACCOUNT_BY_ID, account_id) account = get_account(account_id, db_session) if account is None: logger.error(LogMsg.NOT_FOUND, {'account_id': account_id}) raise Http_error(404, Message.NOT_FOUND) model_instance = Transaction() populate_basic_data(model_instance, username) model_instance.account_id = account_id model_instance.credit = data.get('credit') model_instance.debit = data.get('debit') model_instance.payment_details = data.get('payment_details') db_session.add(model_instance) logger.debug(LogMsg.TRANSACTION_ADDED, model_to_dict(model_instance)) logger.info(LogMsg.END) return model_instance
def delete(id, db_session, username): logger.info(LogMsg.START, username) permissions, presses = get_user_permissions(username, db_session) has_permission([Permissions.ACCOUNT_DELETE_PREMIUM], permissions) user = check_user(username, db_session) if user is None: logger.error(LogMsg.INVALID_USER, username) raise Http_error(404, Message.INVALID_USER) if user.person_id is None: logger.error(LogMsg.PERSON_NOT_EXISTS, username) raise Http_error(404, Message.Invalid_persons) validate_person(user.person_id, db_session) logger.debug(LogMsg.PERSON_EXISTS, username) try: logger.debug(LogMsg.DELETE_ACCOUNT_BY_ID, id) db_session.query(Account).filter( and_(Account.person_id == user.person_id, Account.id == id)).delete() except: logger.error(LogMsg.DELETE_FAILED, exc_info=True) raise Http_error(404, Message.NOT_FOUND) logger.info(LogMsg.END) return Http_response(204, True)
def add(data, db_session, username): logger.debug(LogMsg.START, username) permissions, presses = get_user_permissions(username, db_session) has_permission([Permissions.ACCOUNT_ADD_PREMIUM], permissions) check_schema(['person_id', 'type'], data.keys()) logger.debug(LogMsg.SCHEMA_CHECKED) check_enum(data.get('type'), AccountTypes) logger.debug(LogMsg.ENUM_CHECK, { 'enum': data.get('type'), 'reference_enum': 'AccountTypes' }) user = check_user(username, db_session) logger.debug(LogMsg.USER_CHECKING, username) if user is None: logger.error(LogMsg.INVALID_USER, username) raise Http_error(404, Message.INVALID_USER) logger.info(LogMsg.USER_XISTS, username) if user.person_id is None: logger.info(LogMsg.PERSON_NOT_EXISTS, username) raise Http_error(404, Message.Invalid_persons) validate_person(user.person_id, db_session) logger.info(LogMsg.PERSON_EXISTS, username) type = data.get('type', 'Main') value = data.get('value', 0.0) person_id = data.get('person_id') logger.info(LogMsg.GETTING_USER_ACCOUNTS, type) account = get(person_id, type, db_session) if account is not None: logger.error(LogMsg.ACCOUNT_BY_TYPE_EXISTS, type) raise Http_error(409, Message.ALREADY_EXISTS) model_instance = Account() populate_basic_data(model_instance, username, data.get('tags')) logger.debug(LogMsg.POPULATING_BASIC_DATA) model_instance.type = type model_instance.person_id = person_id model_instance.value = value db_session.add(model_instance) logger.debug(LogMsg.DB_ADD, account_to_dict(model_instance)) logger.info(LogMsg.END) return account_to_dict(model_instance)
def edit(id, db_session, data, username): logger.info(LogMsg.START, username) # TODO: you never checked version of passed data, we have version field in our # records, to prevent conflict when we received two different edit request # concurrently. check KAVEH codes (edit functions) to better understanding # version field usage logger.debug(LogMsg.EDIT_REQUST, {'discuss_group_id': id, 'data': data}) user = check_user(username, db_session) per_data = {} permissions, presses = get_user_permissions(username, db_session) if is_admin_member(user.person_id, id, db_session): per_data.update({Permissions.IS_OWNER.value: True}) has_permission([Permissions.DISCUSSION_GROUP_PREMIUM], permissions, None, per_data) logger.debug(LogMsg.PERMISSION_VERIFIED, username) if "id" in data.keys(): del data["id"] model_instance = db_session.query(DiscussionGroup).filter( DiscussionGroup.id == id).first() if model_instance: logger.debug(LogMsg.MODEL_GETTING) else: logger.debug(LogMsg.MODEL_GETTING_FAILED, {'discussion_group_id': id}) raise Http_error(404, Message.NOT_FOUND) 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, data.get('tags')) if 'members' in data: members = data.get('members', []) members.append({'person_id': user.person_id, 'type': 'Admin'}) delete_group_members(model_instance.id, db_session, username) logger.debug(LogMsg.DISCUSSION_GROUP_OLD_MEMBERS_DELETED) member_data = {'group_id': model_instance.id, 'members': members} add_disscussuion_members(member_data, db_session, username) logger.debug(LogMsg.DISCUSSION_GROUP_MEMBERS_ADDED, members) result = discuss_group_to_dict(model_instance, db_session) logger.debug(LogMsg.MODEL_ALTERED, result) except: logger.exception(LogMsg.EDIT_FAILED, exc_info=True) raise Http_error(500, Message.EDIT_FAILED) logger.info(LogMsg.END) return result
def get(id, db_session, username=None): logger.info(LogMsg.START, username) if username is not None: permissions, presses = get_user_permissions(username, db_session) has_permission([Permissions.TRANSACTION_GET_PREMIUM], permissions) return db_session.query(Transaction).filter( Transaction.account_id == id).order_by( Transaction.creation_date.desc()).all()
def get_user_library(person_id, db_session, username=None): logger.info(LogMsg.START) if username is not None: permissions, presses = get_user_permissions(username, db_session) has_permission([Permissions.LIBRARY_GET_PREMIUM], permissions) result = db_session.query(Library).filter( Library.person_id == person_id).all() logger.info(LogMsg.END) return lib_to_dictlist(result, db_session)
def delete(id, db_session, username): logger.info(LogMsg.START, username) logger.info(LogMsg.DELETE_REQUEST, {'person_id': id}) model_instance = db_session.query(Person).filter(Person.id == id).first() if model_instance is None: logger.error(LogMsg.NOT_FOUND, {'person_id': id}) raise Http_error(404, Message.NOT_FOUND) user = check_user(username, db_session) per_data = {} permissions, presses = get_user_permissions(username, db_session) if user.person_id == id: per_data.update({Permissions.IS_OWNER.value: True}) has_permission([Permissions.PERSON_DELETE_PREMIUM], permissions, None, per_data) logger.debug(LogMsg.PERMISSION_VERIFIED) if person_has_books(id, db_session): logger.error(LogMsg.PERSON_HAS_BOOKS, {'person_id': id}) raise Http_error(403, Message.PERSON_HAS_BOOKS) try: delete_person_accounts(id, db_session) logger.debug(LogMsg.PERSON_ACCOUNTS_DELETED, {'person_id': id}) db_session.delete(model_instance) logger.debug(LogMsg.PERSON_DELETED, {'person_id': id}) users = db_session.query(User).filter(User.person_id == id).all() logger.debug(LogMsg.PERSON_USERS_GOT, id) if users is not None: for user in users: logger.debug(LogMsg.RELATED_USER_DELETE, {'person_id': id, 'user_id': user.id}) db_session.delete(user) logger.debug(LogMsg.ENTITY_DELETED) else: logger.debug(LogMsg.NOT_RELATED_USER_FOR_PERSON, {"Person.id {}": id}) unique_connector = get_connector(id, db_session) if unique_connector: logger.debug(LogMsg.DELETE_UNIQUE_CONSTRAINT) delete_uniquecode(unique_connector.UniqueCode, db_session) delete_connector(id, db_session) except: logger.exception(LogMsg.DELETE_FAILED, exc_info=True) raise Http_error(500, Message.DELETE_FAILED) logger.info(LogMsg.END) return Http_response(204, True)
def edit(id, db_session, data, username): logger.info(LogMsg.START, username) schema_validate(data, USER_EDIT_SCHEMA_PATH) logger.debug(LogMsg.SCHEMA_CHECKED) old_pass = data.get('old_password', None) if old_pass is not None: user = check_user(username, db_session) if user.password != old_pass: logger.error(LogMsg.INVALID_USER, {'old_password': '******'}) raise Http_error(403, Message.INVALID_PASSWORD) logger.debug(LogMsg.EDIT_REQUST, {'user_id': id, 'data': data}) model_instance = check_by_id(id, db_session) if model_instance: logger.debug(LogMsg.MODEL_GETTING, {'user_id': id}) else: logger.debug(LogMsg.NOT_FOUND, {'user_id': id}) raise Http_error(404, Message.NOT_FOUND) per_data = {} permissions, presses = get_user_permissions(username, db_session) if model_instance.username == username: password = data.get('old_password') if model_instance.password != password: logger.error(LogMsg.INVALID_USER, {'password': '******'}) raise Http_error(403, Message.INVALID_PASSWORD) per_data.update({Permissions.IS_OWNER.value: True}) has_permission([Permissions.USER_EDIT_PREMIUM], permissions, None, per_data) logger.debug(LogMsg.PERMISSION_VERIFIED) if not has_permission_or_not([Permissions.USER_EDIT_PREMIUM], permissions): if "person_id" in data: del data["person_id"] # if "person_id" in data: # user_by_person = db_session.query(User).filter( # User.person_id == data.get('person_id')).first() # if user_by_person is not None and user_by_person.id != model_instance.id: # raise Http_error(409,Message.USER_ALREADY_EXISTS) 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, data.get('tags')) user_dict = user_to_dict(model_instance) logger.debug(LogMsg.EDIT_SUCCESS, user_dict) logger.info(LogMsg.END) return user_dict
def edit(id, data, db_session, username=None): logger.info(LogMsg.START, username) model_instance = internal_get(id, db_session) logger.debug(LogMsg.ORDER_CHECK, {'order_id': id}) if model_instance is None: logger.error(LogMsg.NOT_FOUND, {'order_id': id}) raise Http_error(404, Message.NOT_FOUND) permissions, presses = get_user_permissions(username, db_session) per_data = {} if model_instance.creator == username: per_data.update({Permissions.IS_OWNER.value: True}) has_permission( [Permissions.ORDER_EDIT_PREMIUM], permissions, None, per_data) logger.debug(LogMsg.PERMISSION_VERIFIED) if model_instance.status == OrderStatus.Invoiced: logger.error(LogMsg.ORDER_NOT_EDITABLE, {'order_id': id}) raise Http_error(403, Message.ORDER_INVOICED) user = check_user(username,db_session) if user.person_id is None: logger.error(LogMsg.USER_HAS_NO_PERSON,username) raise Http_error(404,Message.INVALID_USER) if 'id' in data: del data['id'] if 'status' in data: del data['status'] for key, value in data.items(): # TODO if key is valid attribute of class setattr(model_instance, key, value) if 'items' in data: item_data = {} item_data['items'] = data.get('items') if 'person_id' in data: item_data['person_id'] = data.get('person_id') else: item_data['person_id'] = user.person_id logger.debug(LogMsg.ORDER_ITEMS_DELETE, {'order_id': id}) delete_orders_items_internal(model_instance.id, db_session) logger.debug(LogMsg.ORDER_ADD_ITEMS, {'order_id': id}) model_instance.total_price = add_orders_items(model_instance.id , item_data, db_session, username) edit_basic_data(model_instance, username) order_dict = order_to_dict(model_instance, db_session, username) logger.debug(LogMsg.MODEL_ALTERED, order_dict) logger.info(LogMsg.END) return order_dict
def get(id, db_session, username=None): logger.info(LogMsg.START, username) result = db_session.query(Order).filter(Order.id == id).first() permissions, presses = get_user_permissions(username, db_session) per_data = {} if username is not None and result.creator == username: per_data.update({Permissions.IS_OWNER.value: True}) has_permission( [Permissions.ORDER_GET_PREMIUM], permissions, None, per_data) logger.debug(LogMsg.PERMISSION_VERIFIED) return order_to_dict(result, db_session, username)
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 edit(id, data, db_session, username): logger.info(LogMsg.START, username) if "id" in data.keys(): del data["id"] logger.debug(LogMsg.EDIT_REQUST) logger.debug(LogMsg.MODEL_GETTING, id) model_instance = db_session.query(Comment).filter(Comment.id == id).first() if model_instance is None: logger.error(LogMsg.GET_FAILED, id) raise Http_error(404, Message.NOT_FOUND) user = check_user(username, db_session) if user is None: raise Http_error(400, Message.INVALID_USER) if user.person_id is None: logger.error(LogMsg.USER_HAS_NO_PERSON, username) raise Http_error(400, Message.Invalid_persons) permissions, presses = get_user_permissions(username, db_session) per_data = {} if model_instance.person_id == user.person_id: per_data.update({Permissions.IS_OWNER.value: True}) has_permission([Permissions.COMMENT_EDIT_PREMIUM], permissions, None, per_data) logger.debug(LogMsg.PERMISSION_VERIFIED, username) if 'person_id' in data: del data['person_id'] if 'book_id' in data: del data['book_id'] 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, data.get('tags')) logger.debug(LogMsg.MODEL_ALTERED, id) result = comment_to_dict(db_session, model_instance, username) logger.debug(LogMsg.EDIT_SUCCESS, result) logger.info(LogMsg.END) return result
def get_all(data, db_session, username): logger.info(LogMsg.START, username) if data.get('sort') is None: data['sort'] = ['creation_date-'] permissions, presses = get_user_permissions(username, db_session) has_permission([Permissions.PAYMENT_GET_PREMIUM], permissions) logger.debug(LogMsg.PERMISSION_VERIFIED) result = Payment.mongoquery( db_session.query(Payment)).query(**data).end().all() logger.info(LogMsg.END) return result