def _common_comments(exercise_id=None, user_id=None): """ Most common comments throughout all exercises. Filter by exercise id when specified. """ query = CommentText.filter(**{ CommentText.flake8_key.name: None, }).select(CommentText.id, CommentText.text).join(Comment) if exercise_id is not None: query = ( query .join(SolutionFile) .join(Solution) .join(Exercise) .where(Exercise.id == exercise_id) ) if user_id is not None: query = ( query .filter(Comment.commenter == user_id) ) query = ( query .group_by(CommentText.id) .order_by(fn.Count(CommentText.id).desc()) .limit(5) ) return tuple(query.dicts())
def _common_comments(exercise_id=None, user_id=None): """ Most common comments throughout all exercises. Filter by exercise id when specified. """ is_moderator_comments = ( (Comment.commenter.role == Role.get_staff_role().id) | (Comment.commenter.role == Role.get_admin_role().id), ) query = (CommentText.select( CommentText.id, CommentText.text).join(Comment).join(User).join(Role).where( CommentText.flake8_key.is_null(True), is_moderator_comments, ).switch(Comment)) if exercise_id is not None: query = (query.join(SolutionFile).join(Solution).join(Exercise).where( Exercise.id == exercise_id)) if user_id is not None: query = (query.filter(Comment.commenter == user_id)) query = (query.group_by(CommentText.id).order_by( fn.Count(CommentText.id).desc()).limit(5)) return tuple(query.dicts())
def comment(staff_user, solution): return Comment.create_comment( commenter=staff_user, file=solution.solution_files.get(), comment_text=CommentText.create_comment(text='very good!'), line_number=1, is_auto=False, )[0]
def create_note( creator: User, user: User, note_text: CommentText, privacy: int, ): new_note_id = CommentText.create_comment(text=note_text).id privacy_level = Note.get_privacy_level(privacy) return Note.get_or_create( creator=creator, user=user, note=new_note_id, exercise=None, privacy=privacy_level, )
def _create_comment( user_id: int, file: SolutionFile, kind: str, line_number: int, comment_text: Optional[str] = None, # set when kind == text comment_id: Optional[int] = None, # set when kind == id ): user = User.get_or_none(User.id == user_id) if user is None: # should never happen, we checked session_id == solver_id return fail(404, 'No such user.') if (not kind) or (kind not in ('id', 'text')): return fail(400, 'Invalid kind.') if line_number <= 0: return fail(422, f'Invalid line number: {line_number}.') if kind == 'id': new_comment_id = comment_id elif kind == 'text': if not comment_text: return fail(422, 'Empty comments are not allowed.') new_comment_id = CommentText.create_comment(text=comment_text).id else: # should never happend, kind was checked before return fail(400, 'Invalid kind.') solutions.notify_comment_after_check(user, file.solution) comment_ = Comment.create( commenter=user, line_number=line_number, comment=new_comment_id, file=file, ) return jsonify({ 'success': 'true', 'text': comment_.comment.text, 'author_name': user.fullname, 'author_role': user.role.id, 'is_auto': False, 'id': comment_.id, 'line_number': line_number, })
def create( user: User, note_text: str, note_exercise: str, privacy: str, ) -> None: if not note_text: raise UnprocessableRequest('Empty notes are not allowed.', 422) new_note_id = CommentText.create_comment(text=note_text).id Note.create( creator=User.get_or_none(User.id == current_user.id), user=user, note=new_note_id, exercise=Exercise.get_or_none(Exercise.subject == note_exercise), privacy=Note.get_privacy_level(int(privacy)), )
def _create_comment( user: User, file: SolutionFile, kind: str, line_number: int, comment_text: Optional[str] = None, # set when kind == text comment_id: Optional[int] = None, # set when kind == id ): if user is None: # should never happen, we checked session_id == solver_id raise ResourceNotFound('No such user.', 404) if (not kind) or (kind not in ('id', 'text')): raise NotValidRequest('Invalid kind.', 400) if line_number <= 0: raise UnprocessableRequest(f'Invalid line number: {line_number}.', 422) if kind == 'id': new_comment_id = comment_id elif kind == 'text': if not comment_text: raise UnprocessableRequest('Empty comments are not allowed.', 422) new_comment_id = CommentText.create_comment(text=comment_text).id else: # should never happend, kind was checked before raise NotValidRequest('Invalid kind.', 400) solutions.notify_comment_after_check(user, file.solution) return Comment.create( commenter=user, line_number=line_number, comment=new_comment_id, file=file, )