def search_permission(data, db_session, username=None): logger.info(LogMsg.START, username) if data.get('sort') is None: data['sort'] = ['creation_date-'] logger.debug(LogMsg.PERMISSION_CHECK, username) permission_result = validate_permissions_and_access( username, db_session, 'PERMISSION_GET') logger.debug(LogMsg.PERMISSION_VERIFIED, username) access_type = permission_result.get('access_type') if access_type == 'Normal': logger.error(LogMsg.PERMISSION_DENIED, '{} is a normal_user'.format(username)) raise Http_error(403, Message.ACCESS_DENIED) result = [] permissions = Permission.mongoquery( db_session.query(Permission)).query(**data).end().all() if access_type == 'Press': for model in permissions: if not 'PREMIUM' in model.permission: result.append(model_to_dict(model)) return result for permission in permissions: result.append(model_to_dict(permission)) logger.debug(LogMsg.GET_SUCCESS, result) logger.info(LogMsg.END) return result
def edit(id, data, db_session, username): logger.info(LogMsg.START) permission_data = {} schema_validate(data, EDIT_SCHEMA_PATH) user = check_user(username, db_session) logger.debug(LogMsg.RATE_CHECK, {'rate_id': id}) model_instance = db_session.query(Rate).filter( Rate.id == id, Rate.person_id == user.person_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) 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 add(data, db_session, username): logger.info(LogMsg.START, username) user = check_user(username, db_session) if user is None: logger.error(LogMsg.INVALID_USER, username) model_instance = DiscussionGroup() populate_basic_data(model_instance, username, data.get('tags')) logger.debug(LogMsg.POPULATING_BASIC_DATA) model_instance.title = data.get('title') model_instance.description = data.get('description') model_instance.image = data.get('image') model_instance.status = 'Created' db_session.add(model_instance) members = data.get('members', []) members.append({'person_id': user.person_id, 'type': 'Admin'}) member_data = {'group_id': model_instance.id, 'members': members} logger.debug(LogMsg.DISCUSSION_GROUP_ADD, model_to_dict(model_instance)) discuss_members = add_disscussuion_members(member_data, db_session, username) logger.debug(LogMsg.DISCUSSION_GROUP_MEMBERS_ADDED, members) result = model_to_dict(model_instance) result['members'] = discuss_members logger.info(LogMsg.END) return result
def edit(id, db_session, data, username): logging.info(Msg.START + "user is {} ".format(username) + "post_id = {}".format(id)) model_instance = get(id, db_session, username) if model_instance: logging.debug(Msg.MODEL_GETTING + json.dumps(model_to_dict(model_instance))) else: logging.debug(Msg.MODEL_GETTING_FAILED + json.dumps(data)) raise Http_error(404, Msg.NOT_FOUND) if username != model_instance.creator: logging.error(Msg.ALTERING_AUTHORITY_FAILED) raise Http_error(403, Msg.ALTERING_AUTHORITY_FAILED) if data.get('category') is None: logging.error(Msg.DATA_MISSING + ' category is not in data') raise Http_error(400, {'category': Msg.DATA_MISSING}) if "id" in data.keys(): del data["id"] if data.get('tags') is not None: tags = (data.get('tags')).split(',') for item in tags: item.strip() tag = get_tag(item, db_session) if item != tag.title: logging.error(Msg.INVALID_TAG) raise Http_error(404, {item: Msg.INVALID_TAG}) model_instance.tags = tags categories = (data.get('category')).split(',') for item in categories: item.strip() category = get_category(item, db_session) if item != category.title: logging.error(Msg.INVALID_TAG) raise Http_error(404, {item: Msg.INVALID_CATEGORY}) model_instance.category = categories del (data['category']) del (data['tags']) if "upload" in data.keys(): logging.debug(Msg.UPLOAD_NOT_ALLOWED + " post_id = {}".format(id)) raise Http_error(400, Msg.UPLOAD_NOT_ALLOWED) for key, value in data.items(): # TODO if key is valid attribute of class setattr(model_instance, key, value) model_instance.modification_date = Now() model_instance.modifier = username logging.debug(Msg.MODEL_ALTERED) logging.debug(Msg.DB_ADD + json.dumps(model_to_dict(model_instance))) logging.info(Msg.END) return model_instance
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_user_posts(data, db_session, username=None): logging.info(Msg.START) # result = db_session.query(Post).filter(Post.creator == username).order_by( # Post.creation_date.desc()).all() if data.get('time') is None: data['time'] = Now() if data.get('count') is None: data['count'] = 50 if data.get('scroll') is None: logging.error(Msg.SCROLL_UNDEFINED) raise Http_error(400, {'scroll': Msg.SCROLL_UNDEFINED}) if data.get('creator') is None: logging.error(Msg.DATA_MISSING + 'creator is required') raise Http_error((400, {'creator': Msg.DATA_MISSING})) if data['scroll'] == 'down': result = db_session.query(Post).filter( and_(Post.creator == data.get('creator'), Post.creation_date < data.get('time'))).order_by( Post.creation_date.desc()).limit(data.get('count')).all() else: result = db_session.query(Post).filter( and_(Post.creator == username, Post.creation_date > data.get('time'))).order_by( Post.creation_date.desc()).limit(data.get('count')).all() final_result = [] if result is None: logging.error(Msg.NOT_FOUND) raise Http_error(400, {'post': Msg.NOT_FOUND}) for item in result: post = model_to_dict(item) if username is None: post['user_liked'] = False else: liked = liked_by_user(item.id, username, db_session) if liked: post['user_liked'] = True post_user = get_profile(item.creator, db_session) creator = model_to_dict(post_user) del creator['password'] post['creator'] = creator post['last_comment'] = post_last_comment(post['id'], db_session) final_result.append(post) logging.debug(Msg.GET_SUCCESS) logging.debug(Msg.END) return final_result
def get(id, db_session, username): logger.info(LogMsg.START, username) user = check_user(username, db_session) model_instance = db_session.query(DeviceCode).filter( DeviceCode.id == id).first() if model_instance is None: logger.error(LogMsg.NOT_FOUND, {'device_key': id}) raise Http_error(404, Message.NOT_FOUND) result = model_to_dict(model_instance) logger.debug(LogMsg.GET_SUCCESS, result) per_data = {} if model_instance.user_id == user.id: per_data.update({Permissions.IS_OWNER.value: True}) logger.debug(LogMsg.PERMISSION_CHECK, username) validate_permissions_and_access(username, db_session, 'DEVICE_KEY_GET', per_data, access_level=Access_level.Premium) logger.debug(LogMsg.PERMISSION_VERIFIED) logger.debug(LogMsg.PERMISSION_VERIFIED) logger.info(LogMsg.END) return result
def get(id, db_session, username=None): logger.info(LogMsg.START, username) limited_permissions = False logger.debug(LogMsg.PERMISSION_CHECK, username) permission_result = validate_permissions_and_access( username, db_session, 'PERMISSION_GET') logger.debug(LogMsg.PERMISSION_VERIFIED, username) access_type = permission_result.get('access_type') if access_type == 'Normal': logger.error(LogMsg.PERMISSION_DENIED, '{} is a normal_user'.format(username)) raise Http_error(403, Message.ACCESS_DENIED) logger.debug(LogMsg.MODEL_GETTING) model_instance = db_session.query(Permission).filter( Permission.id == id).first() if model_instance: logger.debug(LogMsg.GET_SUCCESS, model_to_dict(model_instance)) else: logger.debug(LogMsg.MODEL_GETTING_FAILED) raise Http_error(404, Message.NOT_FOUND) if access_type == 'Press': if 'PREMIUM' in model_instance.permission: logger.error(LogMsg.PERMISSION_DENIED, {'premium_permission_id': id}) raise Http_error(403, Message.ACCESS_DENIED) logger.info(LogMsg.END) return model_instance
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 add(data, db_session, username=None): logger.info(LogMsg.START, username) # schema_validate(data,ADD_SCHEMA_PATH) if username is not None: logger.debug(LogMsg.PERMISSION_CHECK, username) validate_permissions_and_access(username, db_session, 'TRANSACTION_ADD', access_level=Access_level.Premium) logger.debug(LogMsg.PERMISSION_VERIFIED) 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 get_persons(person_list, db_session): result = db_session.query(Person).filter(Person.id.in_( set(person_list))).all() res = [] for item in result: res.append(model_to_dict(item)) return res
def edit(id, db_session, data, username): logger.info(LogMsg.START, username) logger.debug(LogMsg.EDIT_REQUST, {'permission_id': id, 'data': data}) if username not in ADMINISTRATORS: logger.error(LogMsg.NOT_ACCESSED, {'username': username}) raise Http_error(403, Message.ACCESS_DENIED) if "id" in data.keys(): del data["id"] model_instance = db_session.query(Permission).filter( Permission.id == id).first() if model_instance: logger.debug(LogMsg.MODEL_GETTING) else: logger.debug(LogMsg.MODEL_GETTING_FAILED, {'permission_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')) logger.debug(LogMsg.MODEL_ALTERED, model_to_dict(model_instance)) except: logger.exception(LogMsg.EDIT_FAILED, exc_info=True) raise Http_error(500, Message.DELETE_FAILED) logger.info(LogMsg.END) return model_instance
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 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 search_group(data, db_session, username=None): if username is not None: user = check_user(username, db_session) logger.debug(LogMsg.PERMISSION_CHECK, username) validate_permissions_and_access(username, db_session, 'PERMISSION_GROUP_GET') logger.debug(LogMsg.PERMISSION_VERIFIED, username) if data.get('sort') is None: data['sort'] = ['creation_date-'] result = [] groups = Group.mongoquery( db_session.query(Group)).query(**data).end().all() for group in groups: result.append(model_to_dict(group)) if username is not None and not (user_is_in_group( user.id, group.id, db_session)): del result[-1] logger.debug(LogMsg.GET_SUCCESS, result) logger.info(LogMsg.END) return result
def return_parent(id, db_session, username): logger.info(LogMsg.START, username) if id is None: return None comment = db_session.query(Comment).filter(Comment.id == id).first() result = { 'creation_date': comment.creation_date, 'creator': comment.creator, 'person_id': comment.person_id, 'body': comment.body, 'id': comment.id, 'modification_date': comment.modification_date, 'modifier': comment.modifier, 'parent_id': comment.parent_id, 'book_id': comment.book_id, 'version': comment.version, 'tags': comment.tags, 'person': model_to_dict(comment.person), 'likes': get_comment_like_count(comment.id, db_session), 'reports': len(get_comment_reports(comment.id, db_session)), 'liked_by_user': liked_by_user(db_session, comment.id, username), 'reported_by_user': reported_by_user(db_session, comment.id, username) } logger.info(LogMsg.END) return result
def add_payment(data, db_session, username): logger.info(LogMsg.START, username) schema_validate(data, CHECKOUT_ADD_SCHEMA_PATH) logger.debug(LogMsg.SCHEMA_CHECKED) logger.debug(LogMsg.PERMISSION_CHECK, username) validate_permissions_and_access(username, db_session, 'CHECKOUT_PAYMENT_ADD', access_level=Access_level.Premium) logger.debug(LogMsg.PERMISSION_VERIFIED, username) model_instance = CheckoutPressAccount() populate_basic_data(model_instance, username) logger.debug(LogMsg.POPULATING_BASIC_DATA) model_instance.amount = data.get('amount') model_instance.payer_id = data.get('payer_id') model_instance.receiver_id = data.get('receiver_id') model_instance.receiver_account_id = data.get('receiver_account_id') model_instance.payment_details = data.get('payment_details') db_session.add(model_instance) logger.debug(LogMsg.DB_ADD, model_to_dict(model_instance)) logger.info(LogMsg.END) return model_instance
def get_profile(username, db_session): logger.info(LogMsg.START, username) logger.debug(LogMsg.MODEL_GETTING, {'user.username': username}) model_instance = db_session.query(User).filter( User.username == username).first() if model_instance: profile = dict(get_person_profile(model_instance.person_id, db_session, username)) permissions, groups = get_user_permissions(username, db_session) logger.debug(LogMsg.GET_SUCCESS, profile) else: logger.debug(LogMsg.NOT_FOUND, {'user.username': username}) raise Http_error(404, Message.NOT_FOUND) result = model_to_dict(model_instance) result['person'] = profile result['permissions'] = list(permissions) result['permission_groups'] = list(groups) del result['password'] logger.debug(LogMsg.USER_PROFILE_IS, result) logger.info(LogMsg.END) return result
def edit(direct_id, db_session, data, username): logging.info(Msg.START + " user is {}".format(username)) if "id" in data.keys(): del data["id"] logging.debug(Msg.EDIT_REQUST) model_instance = get(direct_id, db_session, username) if model_instance: logging.debug(Msg.MODEL_GETTING) else: logging.debug(Msg.MODEL_GETTING_FAILED) raise Http_error(404, {'message': Msg.NOT_FOUND}) if model_instance.sender != username: logging.error(Msg.ALTERING_AUTHORITY_FAILED) raise Http_error(403, {'sender': Msg.ALTERING_AUTHORITY_FAILED}) model_instance.modification_date = Now() model_instance.modifier = username model_instance.message = data.get('message') logging.debug(Msg.MODEL_ALTERED) logging.debug(Msg.EDIT_SUCCESS + json.dumps(model_to_dict(model_instance))) logging.info(Msg.END) return model_instance
def add_users_to_groups(data, db_session, username): logger.info(LogMsg.START, username) if username not in ADMINISTRATORS: logger.error(LogMsg.NOT_ACCESSED, {'username': username}) raise Http_error(403, Message.ACCESS_DENIED) users = set(data.get('users')) groups = set(data.get('groups')) validate_users(users, db_session) validate_groups(groups, db_session) final_res = {} for group_id in groups: result = [] for user_id in users: if user_is_in_group(user_id, group_id, db_session): logger.error(LogMsg.GROUP_USER_IS_IN_GROUP, {'user_id': user_id, 'group_id': group_id}) raise Http_error(409, Message.ALREADY_EXISTS) result.append( model_to_dict(add(user_id, group_id, db_session, username))) final_res.update({group_id: result}) logger.info(LogMsg.END) return final_res
def delete(id, db_session, username, **kwargs): logger.info(LogMsg.START) 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) logger.debug(LogMsg.PERMISSION_CHECK, username) validate_permissions_and_access(username, db_session, 'RATE_DELETE', model=model_instance) logger.debug(LogMsg.PERMISSION_VERIFIED) try: db_session.delete(model_instance) 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 follow_to_dict(follow_item): result = { 'creation_date': follow_item.creation_date, 'creator': follow_item.creator, 'id': follow_item.id, 'modification_date': follow_item.modification_date, 'modifier': follow_item.modifier, 'follower_id': follow_item.follower_id, 'following_id': follow_item.following_id, 'version': follow_item.version, 'tags': follow_item.tags, 'follower': model_to_dict(follow_item.follower), 'following': model_to_dict(follow_item.following) } logger.info(LogMsg.END) return result
def get(id, db_session, username): logger.info(LogMsg.START, username) result = db_session.query(Movie).filter(Movie.id == id).first() final_res = model_to_dict(result) logger.debug(LogMsg.GET_SUCCESS, final_res) logger.info(LogMsg.END) return final_res
def edit(id, db_session, data, username): logger.info(LogMsg.START, username) logger.debug(LogMsg.PERMISSION_CHECK, username) validate_permissions_and_access(username, db_session, 'PERMISSION_EDIT', access_level=Access_level.Premium) logger.debug(LogMsg.PERMISSION_VERIFIED, username) logger.debug(LogMsg.EDIT_REQUST, {'permission_id': id, 'data': data}) schema_validate(data, PERMISSION_EDIT_SCHEMA_PATH) logger.debug(LogMsg.SCHEMA_CHECKED) model_instance = db_session.query(Permission).filter( Permission.id == id).first() if model_instance: logger.debug(LogMsg.MODEL_GETTING) else: logger.debug(LogMsg.MODEL_GETTING_FAILED, {'permission_id': id}) raise Http_error(404, Message.NOT_FOUND) try: for key, value in data.items(): setattr(model_instance, key, value) edit_basic_data(model_instance, username, data.get('tags')) logger.debug(LogMsg.MODEL_ALTERED, model_to_dict(model_instance)) except: logger.exception(LogMsg.EDIT_FAILED, exc_info=True) raise Http_error(409, Message.ALREADY_EXISTS) logger.info(LogMsg.END) return model_instance
def get(id, db_session, username=None): logger.info(LogMsg.START, username) logger.debug(LogMsg.MODEL_GETTING) model_instance = db_session.query(GroupPermission).filter( GroupPermission.id == id).first() if model_instance: logger.debug(LogMsg.GET_SUCCESS, model_to_dict(model_instance)) 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 = {} if user_is_in_group(user.id, model_instance.group_id, db_session): per_data.update({Permissions.IS_MEMBER.value: True}) logger.debug(LogMsg.PERMISSION_CHECK, username) validate_permissions_and_access(username, db_session, 'GROUP_PERMISSION_GET', per_data) logger.debug(LogMsg.PERMISSION_VERIFIED, username) logger.info(LogMsg.END) return group_permission_to_dict(model_instance)
def get_user_devices(user_id, db_session, username): logger.info(LogMsg.START, username) user = check_user(username, db_session) logger.debug(LogMsg.PERMISSION_CHECK, {username: Permissions.DEVICE_KEY_GET_PREMIUM}) per_data = {} if user_id == user.id: per_data.update({Permissions.IS_OWNER.value: True}) logger.debug(LogMsg.PERMISSION_CHECK, username) validate_permissions_and_access(username, db_session, 'DEVICE_KEY_GET', per_data, access_level=Access_level.Premium) logger.debug(LogMsg.PERMISSION_VERIFIED) result = db_session.query(DeviceCode).filter( DeviceCode.user_id == user_id).order_by( DeviceCode.creation_date.desc()).all() final_res = [] for item in result: final_res.append(model_to_dict(item)) logger.debug(LogMsg.GET_SUCCESS, final_res) logger.info(LogMsg.END) return final_res
def add(db_session, data, username): logging.info(Msg.START + json.dumps(data)) post = db_session.query(Post).filter( Post.id == data.get('post_id')).first() if post is None: logging.error(Msg.PARENT_INVALID + 'relative post doesnt exist') raise Http_error(404, Msg.PARENT_INVALID) model_instance = Comment() model_instance.id = str(uuid4()) model_instance.message = data.get('message') model_instance.post_id = data.get('post_id') model_instance.creation_date = Now() model_instance.creator = username logging.debug(Msg.DATA_ADDITION) db_session.add(model_instance) event_data = { 'entity_name': 'Post', 'entity_id': post.id, 'action': 'COMMENT', 'target': post.creator, } add_event(event_data, username, db_session) logging.debug(Msg.DB_ADD + json.dumps(model_to_dict(model_instance))) logging.info(Msg.END) return model_instance
def get_messages_by_recipient(db_session, username, data): logging.info(Msg.START) required = ['recipient', 'scroll'] check_schema(required, data.keys()) recipient = data.get('recipient') if data.get('time') is None: data['time'] = Now() if data.get('count_number') is None: data['count_number'] = 50 logging.debug(Msg.GET_ALL_REQUEST + 'DirectMessages...') if data.get('scroll') == 'down': result = db_session.query(DirectMessage).filter( and_(DirectMessage.sender.in_((username, recipient)), DirectMessage.reciever.in_((username, recipient)))).filter( DirectMessage.creation_date < data.get('time')).order_by( DirectMessage.creation_date.desc()).limit( data.get('count_number')).all() else: result = db_session.query(DirectMessage).filter( and_(DirectMessage.sender.in_((username, recipient)), DirectMessage.reciever.in_(username, recipient))).filter( DirectMessage.creation_date > data.get('time')).order_by( DirectMessage.creation_date.desc()).limit( data.get('count_number')).all() final_result = [] for message in result: message.seen = True message_creator = get_profile(message.creator, db_session) creator = model_to_dict(message_creator) del creator['password'] new_message = model_to_dict(message) new_message['creator'] = creator final_result.append(new_message) logging.debug(Msg.GET_SUCCESS) logging.info(Msg.END) return final_result
def get_all(data, db_session): logging.info(Msg.START) final_result = [] logging.debug(Msg.GET_ALL_REQUEST + "Comments...") post_id = data.get('post_id') post = db_session.query(Post).filter(Post.id == post_id).first() if post is None: logging.error(Msg.PARENT_INVALID) raise Http_error(404, Msg.PARENT_INVALID) if data.get('time') is None: data['time'] = Now() if data.get('count_number') is None: data['count_number'] = 50 if data.get('scroll') is None: logging.error(Msg.SCROLL_UNDEFINED) raise Http_error(400, {'scroll': Msg.SCROLL_UNDEFINED}) if data['scroll'] == 'down': result = db_session.query(Comment).filter( and_(Comment.post_id == post_id, Comment.creation_date < data.get('time'))).order_by( Comment.creation_date.desc()).limit( data.get('count_number')).all() else: result = db_session.query(Comment).filter( and_(Comment.post_id == post_id, Comment.creation_date > data.get('time'))).order_by( Comment.creation_date.desc()).limit( data.get('count_number')).all() for item in result: comment = model_to_dict(item) comment_user = get_profile(item.creator, db_session) creator = model_to_dict(comment_user) del creator['password'] comment['creator'] = creator final_result.append(comment) logging.debug(Msg.GET_SUCCESS) logging.info(Msg.END) return final_result
def post_last_comment(post_id, db_session): last_comment = db_session.query(Comment).filter( Comment.post_id == post_id).order_by( Comment.creation_date.desc()).first() comment = model_to_dict(last_comment) if last_comment: comment_creator = db_session.query(User).filter( User.username == last_comment.creator).first() comment['creator'] = model_to_dict(comment_creator) del comment['creator']['password'] logging.info(('post_id = {} , last_comment is : {}').format( post_id, last_comment.message)) return comment