def send_message(user, group, request): try: message_text = extract_resource(request, 'body') except: return {}, 400 try: replies_to_message_id = int( extract_resource(request, 'replies_to_message_id')) except: replies_to_message_id = None new_message = Message(sender_user=user, receiver_chat=group.chat, body=message_text) if replies_to_message_id != None: try: replies_to_message = group.chat.received_messages.filter( Message.id == replies_to_message_id).first_or_404() new_message.replies_to_message = replies_to_message except: pass db.session.add(new_message) db.session.commit() return new_message, 201
def publish_post_to_group(user, group, request): try: title = extract_resource(request, 'title') body = extract_resource(request, 'body') except: return {}, 400 new_post = Post(author=user, title=title, body=body, posted_to_board=group) db.session.add(new_post) db.session.commit() return new_post, 201
def publish_post_to_course(user, course_id, request): course = Course.query.filter(Course.id == course_id).first_or_404() try: title = extract_resource(request, 'title') body = extract_resource(request, 'body') except: return {}, 400 new_post = Post(author=user, title=title, body=body, posted_to_board=course) db.session.add(new_post) db.session.commit() return new_post, 201
def save_new_post(user, request): try: title = extract_resource(request, 'title') body = extract_resource(request, 'body') except: return {}, 400 try: board_id = int(extract_resource(request, 'board_id')) except: board_id = None if board_id != None and board_id != 0: board = Board.query.filter(Board.id == board_id).first_or_404() if not board: response_object = { 'status': 'error', 'message': 'invalid board_id supplied', } return response_object, 300 else: if board.type == 'group': accessible, is_owner, group = check_group_accessibility( user, board_id) if not accessible: response_object = { 'status': 'error', 'message': 'Cant post to private group', } return response_object, 401 else: board_id = None author_id = user.id new_post = Post(author_id=author_id, title=title, body=body, posted_to_board_id=board_id) db.session.add(new_post) db.session.commit() whooshee.reindex() return new_post, 201
def change_display_name(user, request): try: new_name = extract_resource(request, 'display_name') except: return {}, 400 user.display_name = new_name db.session.commit() return user, 201
def publish_review_to_course(user, course_id, request): course = Course.query.filter(Course.id == course_id).first_or_404() try: body = extract_resource(request, 'body') score_liking = int(extract_resource(request, 'score_liking')) score_difficulty = int(extract_resource(request, 'score_difficulty')) except: return {}, 400 new_review = Review(author=user, body=body, score_liking=score_liking, score_difficulty=score_difficulty, reviewed_course=course) db.session.add(new_review) db.session.commit() return new_review, 201
def save_new_review(user, request): try: body = extract_resource(request, 'body') reviewed_course_id = int(extract_resource(request, 'reviewed_course_id')) score_liking = int(extract_resource(request, 'score_liking')) score_difficulty = int(extract_resource(request, 'score_difficulty')) except: return {}, 400 if reviewed_course_id is None: response_object = { 'status': 'error', 'message': 'invalid reviewed_course_id supplied', } return response_object, 300 course = Course.query.filter(Course.id == reviewed_course_id).first_or_404() if not course: response_object = { 'status': 'error', 'message': 'invalid reviewed_course_id supplied', } return response_object, 300 if score_liking > 5 or score_liking < 0 or score_difficulty > 5 or score_difficulty < 0: response_object = { 'status': 'error', 'message': 'invalid score supplied. must be a value between 0 and 5', } return response_object, 300 new_review = Review(author=user, body=body, score_liking=score_liking, score_difficulty=score_difficulty, reviewed_course_id=reviewed_course_id) db.session.add(new_review) db.session.commit() return new_review, 201
def save_new_comment(user, request): try: body = extract_resource(request, 'body') except: return { 'status': 'error', 'message': 'Missing expected data: Comment body' }, 452 try: commented_content_id = int( extract_resource(request, 'commented_content_id')) except: return { 'status': 'error', 'message': 'Missing expected data: Comment commented-content id' }, 452 accessible, content = is_content_accessible(user, commented_content_id) if not accessible: response_object = { 'status': 'error', 'message': 'Commented post is private', } return response_object, 401 root_content = content if content.type != 'comment' else content.root_content new_comment = Comment(author=user, body=body, commented_content=content, root_content=root_content) db.session.add(new_comment) db.session.commit() return new_comment, 201
def send_message_to_user(user, user_id, request): other_user = User.query.filter(User.id == user_id).first_or_404() try: message_body = extract_resource(request, 'body') except: return {}, 400 try: chat = other_user.chat_with_user(user).one() except: chat = __make_users_chat(other_user, user) new_message = Message(body=message_body, sender_user=user, receiver_chat=chat) db.session.add(new_message) db.session.commit() return new_message, 201
def register_new_token(user, request): try: token = extract_resource(request, 'token') except Exception: return { 'status': 'error', 'message': 'Missing expected data: Device token' }, 452 device = DeviceToken.query.filter(DeviceToken.token == token).first() if device != None: if device.user != user: device.user = user db.session.commit() return 202 else: return 200 else: db.session.add(DeviceToken(token=token, user=user)) db.session.commit() return 201
def answer_to_invite(user, group_id, request): try: accepted = bool(extract_resource(request, 'answer')) except: return {}, 400 group_invite = user.group_invites.filter( GroupInvite.group_id == group_id).first_or_404() group = group_invite.group db.session.delete(group_invite) if accepted: membership = GroupMember(user=user, group=group, is_owner=False) db.session.add(membership) db.session.commit() return membership, 202 else: db.session.commit() response_object = { 'status': 'success', 'message': 'Successfully declined invite', } return response_object, 201
def create_group(user, request): try: name = extract_resource(request, 'group_name') except: return {}, 400 if name is None or name == "": response_object = { 'status': 'error', 'message': 'Invalid payload. group name needed.', } return response_object, 300 new_group = Group(name=name, chat=GroupChat()) db.session.add(new_group) db.session.add(GroupMember(user=user, group=new_group, is_owner=True)) db.session.commit() try: users_list = extract_object_resource(request, 'invited_members') # TODO # SEND ALL GROUP INVITES IN WORKER THREAD invites = __send_group_invites(inviter_user=user, group=new_group, invited_users_id_list=users_list) if len(invites) > 0: db.session.add_all(invites) db.session.commit() return invites, 202 else: raise Exception() except: print("exception raised") return new_group, 201