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_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 get_events(data, db_session, username): if data.get('time') is None: data['time'] = Now() if data.get('count_number') is None: data['count_number'] = 50 final_result = [] logging.debug(Msg.GET_ALL_REQUEST + 'Events...') logging.info(Msg.START + 'getting events for user = {}'.format(username)) logging.debug(Msg.MODEL_GETTING) if data.get('scroll') == 'down': result = db_session.query(Event).filter( and_(Event.target == username, Event.creation_date < data.get('time'))).order_by( Event.creation_date.desc()).limit( data.get('count_number')).all() else: result = db_session.query(Event).filter( and_(Event.target == username, Event.creation_date > data.get('time'))).order_by( Event.creation_date.desc()).limit( data.get('count_number')).all() for event in result: event.seen = True event_creator = get_profile(event.creator, db_session) creator = model_to_dict(event_creator) del creator['password'] new_event = model_to_dict(event) new_event['creator'] = creator final_result.append(new_event) logging.debug(Msg.GET_SUCCESS) logging.info(Msg.END) return final_result
def add(data, username, db_session): logging.info(Msg.START) required_data = ['message', 'reciever'] check_schema(required_data, data.keys()) reciever = get_profile(data['reciever'], db_session) logging.debug(Msg.SCHEMA_CHECKED) model_instance = DirectMessage() model_instance.sender = username model_instance.id = str(uuid4()) model_instance.creation_date = Now() model_instance.reciever = reciever.username model_instance.message = data.get('message') model_instance.seen = False logging.debug(Msg.DATA_ADDITION + " || Data :" + json.dumps(data)) db_session.add(model_instance) event_data = { 'entity_name': 'DirectMessage', 'entity_id': model_instance.id, 'action': 'ADD', 'target': model_instance.reciever, } add_event(event_data, username, db_session) logging.debug(Msg.DB_ADD) logging.info(Msg.END) return model_instance