def add(db_session, data, username): logger.info(LogMsg.START, username) schema_validate(data, ADD_SCHEMA_PATH) logger.debug(LogMsg.CHECKING_VALID_TOKEN, username) current_token = get_current_token(db_session, username) if current_token is not None and \ current_token.expiration_date > (Now()): logger.debug(LogMsg.USER_HAS_VALID_TOKEN, current_token.id) return current_token model_instance = APP_Token() model_instance.id = str(uuid4()) model_instance.username = username model_instance.expiration_date = Now() + int(token_expiration_interval) logger.debug(LogMsg.DATA_ADDITION, data) db_session.add(model_instance) logger.debug(LogMsg.TOKEN_CREATED) logger.info(LogMsg.END) return model_instance
def get_sender_messages(data, db_session, username, **kwargs): logger.info(LogMsg.START, username) user = check_user(username, db_session) if data.get('sort') is None: data['sort'] = ['creation_date-'] if data.get('filter') is None: data.update( {'filter': { 'receptor_id': user.person_id, 'last_seen': Now() }}) else: data['filter'].update({ 'receptor_id': user.person_id, 'last_seen': Now() }) sender_id = data.filter.get('sender_id') permissions, presses = get_user_permissions(username, db_session) permission_data = {Permissions.IS_OWNER.value: True} has_permission([Permissions.CHAT_GET_PREMIUM], permissions, None, permission_data) seen_data = { 'receptor_id': user.person_id, 'sender_id': sender_id, 'last_seen': Now() } logger.debug(LogMsg.PERMISSION_VERIFIED, username) try: logger.debug(LogMsg.CHAT_GET_USER_MESSAGES, { 'receptor': user.person_id, 'sender': sender_id }) result = ChatMessage.mongoquery( db_session.query(ChatMessage)).query(**data).end().all() add_last_seen(seen_data, db_session) except: logger.exception(LogMsg.GET_FAILED, exc_info=True) raise Http_error(400, Message.NOT_FOUND) logger.info(LogMsg.END) return result
def get(data, db_session): if (data.get('post_id') is None) or (data.get('scroll') is None): logging.error(Msg.DATA_MISSING + ' post_id or scroll type') raise Http_error(400, {'data': 'post_id and scroll are required'}) logging.info(Msg.START + "getting like for post_id = {}".format(data['post_id'])) logging.debug(Msg.MODEL_GETTING) 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(Like).filter( and_(Like.post_id == data.get('post_id'), Like.creation_date < data.get('time'))).order_by( Like.creation_date.desc()).limit( data.get('count_number')).all() else: result = db_session.query(Like).filter( and_(Like.post_id == data.get('post_id'), Like.creation_date > data.get('time'))).order_by( Like.creation_date.desc()).limit( data.get('count_number')).all() logging.info(Msg.END) return result
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 add(data, username, db_session): logging.info(Msg.START) required_data = ['action', 'target', 'entity_name', 'entity_id'] check_schema(required_data, data.keys()) logging.debug(Msg.SCHEMA_CHECKED) model_instance = Event() model_instance.creator = username model_instance.id = str(uuid4()) model_instance.creation_date = Now() model_instance.target = data.get('target') model_instance.action = data.get('action') model_instance.entity_id = data.get('entity_id') model_instance.entity_name = data.get('entity_name') model_instance.seen = False logging.debug(Msg.DATA_ADDITION + " || Data :" + json.dumps(data)) db_session.add(model_instance) logging.debug(Msg.DB_ADD) logging.info(Msg.END) return model_instance
def add(data, username, db_session): if username != admin: logging.error(Msg.NOT_ACCESSED) raise Http_error(401, {'username': Msg.NOT_ACCESSED}) logging.info(Msg.START) title = data.get('title') if title is None: logging.error(Msg.DATA_MISSING + ' title') raise Http_error(400, {'title': Msg.DATA_MISSING}) tag = db_session.query(Tag).filter( Tag.title == title).first() if tag: logging.error(Msg.NOT_UNIQUE + ' title already exists') raise Http_error(409, {'title': Msg.NOT_UNIQUE}) model_instance = Tag() model_instance.title = title model_instance.id = str(uuid4()) model_instance.creation_date = Now() model_instance.creator = username logging.debug(Msg.DATA_ADDITION + " || Data :" + json.dumps(data)) db_session.add(model_instance) logging.debug(Msg.DB_ADD) logging.info(Msg.END) return model_instance
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(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 get_new_events(db_session, data, username): logging.info(Msg.START) required = ['scroll'] check_schema(required, data.keys()) 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 + 'new unread Events...') if data.get('scroll') == 'down': result = db_session.query(Event).filter( and_(Event.target == username, Event.seen == False)).filter( 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.seen == False)).filter( Event.creation_date > data.get('time')).order_by( Event.creation_date.desc()).limit( data.get('count_number')).all() logging.debug(Msg.GET_SUCCESS) logging.info(Msg.END) return result
def add(self, db_session, data, username): logger.info(LogMsg.START, username) schema_validate(data, ADD_SCHEMA_PATH) logger.debug(LogMsg.CHECKING_VALID_TOKEN, username) current_token = self.current_token(db_session, username) if current_token is not None and \ current_token.expiration_date > Now(): logger.debug(LogMsg.USER_HAS_VALID_TOKEN, current_token.id) return current_token.to_dict() data = dict(expiration_date=Now() + int(token_expiration_interval), username=username) res = super(TokenController, self).add(data, db_session, username, True) logger.debug(LogMsg.TOKEN_CREATED) logger.info(LogMsg.END) return res.to_dict()
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 add_owner_to_group_users(group_id, user_id, db_session, username): model_instance = GroupUser() model_instance.id = str(uuid4()) model_instance.group_id = group_id model_instance.user_id = user_id model_instance.creation_date = Now() model_instance.creator = username model_instance.version = 1 db_session.add(model_instance) return model_instance
def get(self, id, db_session, username): logger.info(LogMsg.START, username) res = super(TokenController, self).get(id, db_session, username) if res is None: logger.error(LogMsg.MODEL_GETTING_FAILED) raise Http_error(404, Message.TOKEN_INVALID) if res.expiration_date < Now(): logger.error(LogMsg.TOKEN_EXPIRED) raise Http_error(401, Message.TOKEN_EXPIRED) logger.info(LogMsg.END) return res.to_dict()
def update_last_seen(message, person_id, db_session): logger.info(LogMsg.START) last_seen = None if message.receptor_id == person_id or ( message.group_id is not None and is_group_member(person_id, message.group_id, db_session)): data = model_to_dict(message) data.update({'last_seen': Now()}) logger.debug(LogMsg.LAST_SEEN_UPDATE,data) last_seen = add(db_session, data) logger.info(LogMsg.END) return last_seen
def add(db_session, data): logging.info(Msg.START) cell_no = data.get('cell_no') name = data.get('name') user = db_session.query(User).filter(User.username == cell_no).first() if user: logging.error(Msg.USER_XISTS.format(cell_no)) raise Http_error(409, {"cell_no": Msg.USER_XISTS.format(cell_no)}) logging.debug(Msg.CHECK_REDIS_FOR_EXISTANCE) activation_code = redis.get(cell_no) if activation_code is None: logging.error(Msg.REGISTER_KEY_DOESNT_EXIST) raise Http_error(404, {"activation_code": Msg.REGISTER_KEY_DOESNT_EXIST}) activation_code = activation_code.decode("utf-8") if activation_code != data.get('activation_code'): logging.error(Msg.REGISTER_KEY_INVALID) raise Http_error(400, {"activation_code": Msg.REGISTER_KEY_INVALID}) user_by_name = db_session.query(User).filter(User.name == name).first() if user_by_name != None: logging.error(Msg.NAME_NOT_UNIQUE) raise Http_error(409, {"name": Msg.NAME_NOT_UNIQUE}) logging.debug(Msg.USR_ADDING) model_instance = User() model_instance.username = cell_no model_instance.password = data.get('password') model_instance.name = name model_instance.id = str(uuid4()) model_instance.creation_date = Now() model_instance.creator = data.get('cell_no') if data.get('tags') is not None: tags = (data.get('tags')).split(',') for item in tags: item.strip() model_instance.tags = tags logging.debug(Msg.DATA_ADDITION) db_session.add(model_instance) logging.debug(Msg.DB_ADD + json.dumps(model_to_dict(model_instance))) logging.info(Msg.END) return model_instance
def add(db_session, data, username): logging.info(Msg.START) current_token = get_current_token(db_session, username) if current_token is not None and \ current_token.expiration_date > Now() + int(new_token_request_valid_interval): raise Http_error(409, "already have a valid token") model_instance = APP_Token() model_instance.id = str(uuid4()) model_instance.username = username model_instance.expiration_date = Now() + int(token_expiration_interval) logging.debug(Msg.DATA_ADDITION + " || Data :" + json.dumps(data)) db_session.add(model_instance) logging.debug(Msg.TOKEN_CREATED) 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 edit(category_title, data, db_session, username): logging.debug(Msg.EDIT_REQUST) logging.info(Msg.START + ' user is {}'.format(username)) if username != admin: logging.error(Msg.NOT_ACCESSED) raise Http_error(401, {'username': Msg.NOT_ACCESSED}) model_instance = get(category_title, db_session) if model_instance: logging.debug(Msg.MODEL_GETTING) else: logging.debug(Msg.MODEL_GETTING_FAILED) raise Http_error(404, {category_title: Msg.NOT_FOUND}) 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 del data['tags'] if "upload" in data.keys(): upload = data['upload'][0] upload.filename = str(uuid4()) model_instance.image = upload.filename upload.save(save_path) # appends upload.filename automatically del (data['upload']) if 'id' in data.keys(): del data['id'] 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.EDIT_SUCCESS + json.dumps(model_to_dict(model_instance))) logging.info(Msg.END) return model_instance
def edit(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(id, db_session) if model_instance: logging.debug(Msg.MODEL_GETTING) else: logging.debug(Msg.MODEL_GETTING_FAILED) raise Http_error(404, {'comment': Msg.NOT_FOUND}) post = db_session.query(Post).filter( Post.id == model_instance.post_id).first() if post is None: logging.error(Msg.NOT_FOUND + ' post no longer exists') raise Http_error(404, {'post': Msg.NOT_FOUND}) if model_instance.creator != username: logging.error(Msg.ALTERING_AUTHORITY_FAILED) raise Http_error(403, {'creator': Msg.ALTERING_AUTHORITY_FAILED}) 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) event_data = { 'entity_name': 'Post', 'entity_id': post.id, 'action': 'RECOMMENT', 'target': post.creator, } add_event(event_data, username, db_session) logging.debug(Msg.EDIT_SUCCESS + json.dumps(model_to_dict(model_instance))) logging.info(Msg.END) return model_instance
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 add(db_session, data, username): logger.info(LogMsg.START, username) schema_validate(data, ADD_SCHEMA_PATH) logger.debug(LogMsg.SCHEMA_CHECKED) following_id = data.get('following_id') 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) validate_person(user.person_id, db_session) validate_person(following_id, db_session) logger.debug(LogMsg.PERSON_EXISTS) if following_id == user.person_id: logger.error(LogMsg.FOLLOW_SELF_DENIED) raise Http_error(400, Message.FOLLOW_DENIED) logger.debug(LogMsg.FOLLOW_CHECK, data) follow = get(user.person_id, following_id, db_session) if follow is not None: logger.error(LogMsg.FOLLOW_EXISTS, data) raise Http_error(409, Message.ALREADY_FOLLOWS) model_instance = Follow() model_instance.id = str(uuid4()) model_instance.creation_date = Now() model_instance.creator = username model_instance.version = 1 model_instance.tags = data.get('tags') model_instance.following_id = following_id model_instance.follower_id = user.person_id db_session.add(model_instance) logger.debug(LogMsg.FOLLOW_ADD, follow_to_dict(model_instance)) logger.info(LogMsg.END) return model_instance
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 get(id, db_session, username): logging.info(Msg.START + "user is {}".format(username)) model_instance = db_session.query(APP_Token) \ .filter(APP_Token.id == id).first() if model_instance: logging.info(Msg.MODEL_GETTING) else: logging.debug(Msg.MODEL_GETTING_FAILED) raise Http_error(404, Msg.NOT_FOUND) logging.debug(Msg.GET_SUCCESS) if model_instance.expiration_date < Now(): logging.error(Msg.TOKEN_EXPIRED) raise Http_error(401, 'not valid token') logging.info(Msg.END) return model_instance
def get(id, db_session, username): logger.info(LogMsg.START, username) model_instance = db_session.query(APP_Token) \ .filter(APP_Token.id == id).first() if model_instance: logger.info(LogMsg.MODEL_GETTING) else: logger.error(LogMsg.MODEL_GETTING_FAILED) raise Http_error(404, Message.TOKEN_INVALID) logger.debug(LogMsg.GET_SUCCESS) if model_instance.expiration_date < Now(): logger.error(LogMsg.TOKEN_EXPIRED) raise Http_error(401, Message.TOKEN_EXPIRED) logger.info(LogMsg.END) return model_instance
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.POST_ALREADY_LIKED + json.dumps(data)) raise Http_error(404, Msg.POST_NOT_FOUND) instance = db_session.query(Like).filter( Like.post_id == data.get('post_id')).filter( Like.creator == username).first() if instance is not None: logging.error(Msg.POST_ALREADY_LIKED) raise Http_error(409, Msg.POST_ALREADY_LIKED) model_instance = Like() model_instance.post_id = data.get('post_id') model_instance.id = str(uuid4()) model_instance.creation_date = Now() model_instance.creator = username logging.debug(Msg.DATA_ADDITION) db_session.add(model_instance) post.likes += 1 event_data = { 'entity_name': 'Post', 'entity_id': post.id, 'action': 'LIKE', '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 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
def edit(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(id, db_session, username) if model_instance: logging.debug(Msg.MODEL_GETTING) else: logging.debug(Msg.MODEL_GETTING_FAILED) raise Http_error(404, {"id": Msg.NOT_FOUND}) if data.get('tags') is not None: tags = (data.get('tags')).split(',') for item in tags: item.strip() model_instance.tags = tags del data['tags'] 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) # db_session.add(model_instance) logging.debug(Msg.EDIT_SUCCESS + json.dumps(model_to_dict(model_instance))) logging.info(Msg.END) return model_instance
def current_token(self, db_session, username): query_data = dict( filter=dict(username=username, expiration_date={'$gte': Now()})) res = super(TokenController, self).get_by_data(query_data, db_session) return res
def get_current_token(db_session, username): token = db_session.query(APP_Token). \ filter(APP_Token.username == username, APP_Token.expiration_date > Now()).first() return token
def add(data, username, db_session): if username != admin: logging.error(Msg.NOT_ACCESSED) raise Http_error(401, {'username': Msg.NOT_ACCESSED}) logging.info(Msg.START) title = data.get('title') if title is None: logging.error(Msg.DATA_MISSING + ' title') raise Http_error(400, {'title': Msg.DATA_MISSING}) category = db_session.query(Category).filter( Category.title == title).first() if category: logging.error(Msg.NOT_UNIQUE + ' title already exists') raise Http_error(409, {'title': Msg.NOT_UNIQUE}) model_instance = Category() model_instance.title = title model_instance.description = data.get('description') model_instance.id = str(uuid4()) model_instance.creation_date = Now() model_instance.creator = username 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 if ('upload' not in data) or (data['upload'] is None): logging.error(Msg.DATA_MISSING + 'image is not uploaded') raise Http_error(400, {'upload': Msg.DATA_MISSING}) if len(data['upload']) > 1: raise Http_error(400, {'upload': 'just 1 image can be uploaded'}) upload = data['upload'][0] upload.filename = str(uuid4()) model_instance.image = upload.filename print(upload.filename) upload.save(save_path) # appends upload.filename automatically del (data['upload']) logging.debug(Msg.DATA_ADDITION + " || Data :" + json.dumps(data)) db_session.add(model_instance) logging.debug(Msg.DB_ADD) logging.info(Msg.END) return model_instance