def post(self, pid, sid): """ Create a new annotation on an existing session """ helpers.abort_if_invalid_parameters(pid, sid) helpers.abort_if_unauthorized(Project.query.get(pid)) # Text and optionally, a list of tags (i.e. codes): json_data = request.get_json(force=True, silent=True) helpers.abort_if_invalid_json(json_data) schema = UserAnnotationSchema() helpers.abort_if_errors_in_validation( errors=schema.validate(json_data)) user = User.query.filter_by(email=get_jwt_identity()).first() user_annotation = UserAnnotationModel( content=json_data['content'], start_interval=json_data['start_interval'], end_interval=json_data['end_interval'], user_id=user.id, session_id=sid) if json_data.get('tags', None): user_annotation.tags.extend([ Tags.query.filter_by(id=cid).first() for cid in json_data['tags'] ]) db.session.add(user_annotation) db.session.commit() InterviewSession.email_participants(user, sid) fcm.notify_participants_user_commented(pid, sid) return custom_response(200, data=schema.dump(user_annotation))
def validate_and_get_data(project_id): """ Helper method as PUT/DELETE required the same validation. """ helpers.abort_if_unauthorized(Project.query.get(project_id)) user = User.query.filter_by(email=get_jwt_identity()).first() helpers.abort_if_unknown_user(user) helpers.abort_if_not_admin_or_staff(user, project_id, "membership.INVITE") data = helpers.jsonify_request_or_abort() return user, data
def get(self, pid, sid): """ Returns a list of all annotations for an existing session """ helpers.abort_if_invalid_parameters(pid, sid) project = Project.query.get(pid) annotations = UserAnnotationModel.query.filter_by(session_id=sid).all() annotations = UserAnnotationSchema(many=True).dump(annotations) if project.is_public: return custom_response(200, data=annotations) helpers.abort_if_unauthorized(project) return custom_response(200, data=annotations)
def create_comment(project_id, session_id, annotation_id, comment_id=None): """ CREATE a comment within a session to an annotation, however, if comment_id is provided, then the comment is a comment on a comment, rather than on an annotation. """ helpers.abort_if_invalid_parameters(project_id, session_id) user = helpers.abort_if_unauthorized(Project.query.get(project_id)) if comment_id: helpers.abort_if_unknown_comment(comment_id, annotation_id) data = helpers.jsonify_request_or_abort() schema = UserAnnotationCommentSchema() helpers.abort_if_errors_in_validation(schema.validate(data)) # Note: comment_id can be null, which represents that it is a parent comment = CommentsModel(data['content'], comment_id, user.id, annotation_id) db.session.add(comment) db.session.commit() # Determine which type of comment the response is to: nested or a root comment if comment_id: _comment = CommentsModel.query.filter_by(parent_id=comment_id).first() else: _comment = RootComment.query.get(annotation_id) parent_user_id = _comment.user_id usr = User.query.get(parent_user_id) if user.id != usr.id: InterviewSession.email_commentor(usr, project_id, session_id) fcm.notify_participants_user_commented(project_id, session_id) return custom_response(200, data=schema.dump(comment))
def post(self, pid): """ Joins a public project for a given user (determined through JWT token) """ project = Project.query.get(pid) user = helpers.abort_if_unauthorized(project) helpers.abort_if_project_member(user, pid) membership = Membership.join_project(user.id, pid) return custom_response(200, data=ProjectMember().dump(membership))
def delete(self, pid): """ Leaves a project for a given user (determined through JWT token) """ project = Project.query.get(pid) user = helpers.abort_if_unauthorized(project) if not user.is_project_member(pid): helpers.abort_if_not_project_member(user, pid) membership = Membership.leave_project(user.id, pid) return custom_response(200, data=ProjectMember().dump(membership))
def delete(self, pid, sid, aid, cid): """ DELETE a comment an session annotation """ helpers.abort_if_invalid_parameters(pid, sid) user = helpers.abort_if_unauthorized(Project.query.get(pid)) helpers.abort_if_unknown_comment(cid, aid) comment = CommentsModel.query.filter_by(id=cid) helpers.abort_if_not_user_made_comment(user.id, comment.first().user_id) comment.update({'is_active': False}) db.session.commit() return custom_response(200)
def delete(self, pid, mid=None): """ Removes a user and emails them that they have been removed from a project and by whom. Mapped to: /api/project/<int:id>/membership/invites/<int:mid> """ if not mid: raise CustomException(400, errors=['membership.NOT_EXISTS']) helpers.abort_if_unauthorized(Project.query.get(pid)) admin = User.query.filter_by(email=get_jwt_identity()).first() helpers.abort_if_unknown_user(admin) helpers.abort_if_not_admin_or_staff(admin, pid, "membership.INVITE") membership = Membership.query.filter_by(id=mid).first() if not membership: raise CustomException(400, errors=['membership.UNKNOWN']) elif membership.deactivated: raise CustomException(400, errors=['membership.USER_DEACTIVATED']) membership.deactivated = True db.session.commit() return custom_response(200, data=ProjectMemberWithAccess().dump(membership))
def delete(self, pid, sid, aid): """ Soft delete an existing annotation """ helpers.abort_if_invalid_parameters(pid, sid) user = helpers.abort_if_unauthorized(Project.query.get(pid)) annotation = UserAnnotationModel.query.get(aid) helpers.abort_if_unknown_annotation(annotation) helpers.abort_if_not_user_made(user.id, annotation.user_id) UserAnnotationModel.query.filter_by(id=aid).update({'is_active': 0}) db.session.commit() return custom_response(200)