def dislike(db_session, comment_id, username): logger.info(LogMsg.START, username) logger.debug(LogMsg.ACTION_CHECK_COMMENT, comment_id) comment = get_comment(comment_id, db_session) if comment is None: logger.error(LogMsg.NOT_FOUND, {'comment_id': comment_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) validate_person(user.person_id, db_session) logger.debug(LogMsg.PERSON_EXISTS) action = get_action_like(comment_id, user.person_id, db_session) if action is None: logger.error(LogMsg.ACTION_USER_CANT_DISLIKE) raise Http_error(404, Message.COMMENT_NOT_FOUND) if action.person_id != user.person_id: logger.error(LogMsg.NOT_ACCESSED, username) raise Http_error(403, Message.ACCESS_DENIED) logger.debug(LogMsg.ACTION_DISLIKE_COMMENT, comment_id) delete(action.id, db_session, username) logger.info(LogMsg.END) return Http_response(204, True)
def add(db_session, data, username): logger.info(LogMsg.START, username) schema_validate(data, COMMENT_ADD_SCHEMA_PATH) logger.debug(LogMsg.SCHEMA_CHECKED) book_id = data.get('book_id') logger.debug(LogMsg.COMMENT_VALIDATING_BOOK, book_id) 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) logger.debug(LogMsg.CHECK_USER_EXISTANCE, username) user = check_user(username, db_session) if user is None: logger.error(LogMsg.INVALID_USER, username) 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) logger.debug(LogMsg.PERSON_EXISTS) logger.debug(LogMsg.COMMENT_CHECK_FOR_PARENT) parent_id = data.get('parent_id', None) if parent_id: logger.debug(LogMsg.COMMENT_GETTING_PARENT, parent_id) parent_comment = get_comment(parent_id, db_session) if parent_comment is None: logger.error(LogMsg.COMMENT_PARENT_NOT_FOUND, parent_id) raise Http_error(404, Message.PARENT_NOT_FOUND) if parent_comment.book_id != book_id: logger.error( LogMsg.COMMENT_PARENT_NOT_MATCH, { 'book_id': book_id, 'parent': comment_to_dict(db_session, parent_comment) }) raise Http_error(400, Message.ACCESS_DENIED) model_instance = Comment() logger.debug(LogMsg.POPULATING_BASIC_DATA) populate_basic_data(model_instance, username, data.get('tags')) model_instance.person_id = user.person_id model_instance.book_id = book_id model_instance.body = data.get('body') model_instance.parent_id = data.get('parent_id') logger.debug(LogMsg.DATA_ADDITION) db_session.add(model_instance) logger.debug(LogMsg.DB_ADD) logger.info(LogMsg.END) return comment_to_dict(db_session, model_instance, username)
def report(db_session, comment_id, data, username): logger.info(LogMsg.START, username) logger.debug(LogMsg.ACTION_CHECK_COMMENT, comment_id) comment = get_comment(comment_id, db_session) if comment is None: logger.error(LogMsg.NOT_FOUND, {'comment_id', comment_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) validate_person(user.person_id, db_session) logger.debug(LogMsg.PERSON_EXISTS) logger.debug(LogMsg.ACTION_CHECK_USER_REPORTED, comment_id) action = get_action_report(comment_id, user.person_id, db_session) if action is not None: logger.error(LogMsg.ACTION_ALREADY_REPORTED, comment_id) raise Http_error(409, Message.ALREADY_REPORTED) report = data.get('report', None) if report: logger.debug(LogMsg.ENUM_CHECK, {'report': report}) check_enum(report, ReportComment) model_instance = CommentAction() populate_basic_data(model_instance, username, None) model_instance.comment_id = comment_id model_instance.person_id = user.person_id model_instance.report = report db_session.add(model_instance) logger.debug(LogMsg.DB_ADD) logger.info(LogMsg.END) return model_instance
def like(db_session, comment_id, username): logger.info(LogMsg.START, username) logger.debug(LogMsg.ACTION_CHECK_COMMENT, comment_id) comment = get_comment(comment_id, db_session) if comment is None: logger.error(LogMsg.NOT_FOUND, comment_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) validate_person(user.person_id, db_session) logger.debug(LogMsg.PERSON_EXISTS) logger.debug(LogMsg.ACTION_CHECK_USER_LIKED, comment_id) action = get_action_like(comment_id, user.person_id, db_session) if action is not None: logger.error(LogMsg.ACTION_ALREADY_LIKED) raise Http_error(409, Message.ALREADY_LIKED) logger.debug(LogMsg.ACTION_LIKE_COMMENT, comment_id) model_instance = CommentAction() populate_basic_data(model_instance, username, None) model_instance.comment_id = comment_id model_instance.person_id = user.person_id model_instance.like = True db_session.add(model_instance) logger.debug(LogMsg.DB_ADD) logger.info(LogMsg.END) return model_instance
def get_comment_reports(comment_id, db_session): logger.info(LogMsg.START) result = [] logger.debug(LogMsg.ACTION_CHECK_COMMENT, comment_id) comment = get_comment(comment_id, db_session) if comment is None: logger.error(LogMsg.NOT_FOUND) raise Http_error(404, Message.NOT_FOUND) try: logger.debug(LogMsg.ACTION_GETTING_REPORTS, comment_id) reports = db_session.query(CommentAction).filter( and_(CommentAction.comment_id == comment_id, CommentAction.report != None)).all() for item in reports: result.append(model_to_dict(item)) except: logger.exception(LogMsg.GET_FAILED, exc_info=True) raise Http_error(404, Message.NOT_FOUND) logger.debug(LogMsg.GET_SUCCESS, result) logger.info(LogMsg.END) return result
def add(db_session, data, username): logger.info(LogMsg.START, username) schema_validate(data, ACTION_ADD_SCHEMA_PATH) logger.debug(LogMsg.SCHEMA_CHECKED) report = data.get('report', None) if report: logger.debug(LogMsg.ENUM_CHECK, {'report_comment': report}) check_enum(report, ReportComment) comment_id = data.get('comment_id') logger.debug(LogMsg.ACTION_CHECK_COMMENT, comment_id) comment = get_comment(comment_id, db_session) if comment is None: logger.error(LogMsg.NOT_FOUND, {'comment_id': comment_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) logger.debug(LogMsg.CHECK_USER_EXISTANCE) validate_person(user.person_id, db_session) logger.debug(LogMsg.PERSON_EXISTS) model_instance = CommentAction() populate_basic_data(model_instance, username, data.get('tags')) logger.debug(LogMsg.POPULATING_BASIC_DATA) model_instance.comment_id = comment_id model_instance.person_id = user.person_id model_instance.like = data.get('like') model_instance.report = report db_session.add(model_instance) logger.debug(LogMsg.DB_ADD) logger.info(LogMsg.END) return model_instance
def get_comment_like_count(comment_id, db_session): logger.info(LogMsg.START) likes = 0 logger.debug(LogMsg.ACTION_CHECK_COMMENT, comment_id) comment = get_comment(comment_id, db_session) if comment is None: logger.error(LogMsg.NOT_FOUND) raise Http_error(404, Message.NOT_FOUND) try: logger.debug(LogMsg.ACTION_GETTING_LIKES, comment_id) likes = db_session.query(CommentAction).filter( and_(CommentAction.comment_id == comment_id, CommentAction.like == True)).count() except: logger.exception(LogMsg.GET_FAILED, exc_info=True) raise Http_error(400, Message.NOT_FOUND) logger.debug(LogMsg.GET_SUCCESS, { 'comment_id': comment_id, 'likes': likes }) logger.info(LogMsg.END) return likes