def like_match(): """ Check if two user have a match. :return: True if both users have a match, false otherwise. """ # Retrieve query parameters user_from = request.args.get('user_from') user_to = request.args.get('user_to') # Query into like database first_match = db_session().query(Like).filter_by(user_from=user_from, user_to=user_to).first() second_match = db_session().query(Like).filter_by( user_to=user_from, user_from=user_to).first() return jsonify(match=bool(first_match) and bool(second_match)), 200
def match_put_status(): # Retrieve request body. body = request.json # Check parameters. required_parameters = ['id', 'status'] if not all(x in body for x in required_parameters): return jsonify(success=False), 202 # Check match existence. match = db_session().query(Match).filter_by(id=body['id']).first() if not match: return jsonify(success=False), 201 # Set parameters. if body['status'] in ['PENDING', 'ACCEPTED', 'DECLINED']: match.status = body['status'] db_session().commit() return jsonify(success=True), 200 else: return jsonify(success=False), 202
def like_get_to_user(): """ Retrieve the likes that a user received. :return: List of likes. """ # Retrieve query parameters username = request.args.get('username') # Query into like database like_list = db_session().query(Like).filter_by(user_to=username).all() if like_list: return jsonify(likes=[like.serialize() for like in like_list]), 200 else: return jsonify(likes=[]), 200
def project_get_by_user(): """ Retrieve project information by user given its username. :return: Project information. """ # Retrieve query parameters username = request.args.get('username') # Query into project database project = db_session().query(Project).filter_by( project_username=username).first() if project: return jsonify(project=project.serialize()), 200 else: return jsonify(project=None), 200
def match_get_by_specialization(): """ Retrieve the matches by specialization. :return: List of matches. """ # Retrieve query parameters specialization = request.args.get('specialization') # Query into match database match_list = db_session().query(Match).filter_by( specialization=specialization).all() if match_list: return jsonify(matches=[match.serialize() for match in match_list]), 200 else: return jsonify(matches=[]), 200
def match_get_by_user(): """ Retrieve the matches by user. :return: List of matches. """ # Retrieve query parameters username = request.args.get('username') # Query into match database match_list = db_session().query(Match).filter( or_(Match.user_student == username, Match.user_teacher == username)).all() if match_list: return jsonify(matches=[match.serialize() for match in match_list]), 200 else: return jsonify(matches=[]), 200
def project_post(): """ Create a project given some parameters. :return: Project created. """ # Retrieve request body. body = request.json # Check parameters. required_parameters = ['title', 'description', 'tags', 'project_username'] if not all(x in body for x in required_parameters): return jsonify(success=False), 202 # Check user existence user = db_session().query(User).filter_by( username=body['project_username']).first() if not user: return jsonify(success=False), 202 # Check project existence project = db_session().query(Project).filter_by( project_username=body['project_username']).first() if project: return jsonify(success=False), 202 # Create and add project to the database. project = Project(id=str(uuid.uuid4()), title=body['title'], description=body['description'], tags=body['tags'], project_username=body['project_username']) db_session().add(project) db_session().commit() # Return result. return jsonify(success=True), 201
def like_post(): """ Create a like between two given users. :return: Like created. """ # Retrieve request body. body = request.json # Check parameters. required_parameters = ['user_from', 'user_to'] if not all(x in body for x in required_parameters): return jsonify(match=False), 202 # Check like existence. like = db_session().query(Like).filter_by(user_from=body['user_from'], user_to=body['user_to']).first() if like: return jsonify(match=False), 202 # Create and add like to the database. like = Like(id=str(uuid.uuid4()), user_from=body['user_from'], user_to=body['user_to']) db_session().add(like) db_session().commit() # Check if there is a match. like = db_session().query(Like).filter_by( user_to=body['user_from'], user_from=body['user_to']).first() if not like: return jsonify(match=False), 201 else: # Initialize user student and teacher. user_destination = db_session().query(User).filter_by( username=body['user_from']).first() user_origin = db_session().query(User).filter_by( username=body['user_to']).first() if user_destination.account_type == 'Student': user_student = user_destination.username user_teacher = user_origin.username else: user_student = user_origin.username user_teacher = user_destination.username # Initialize project identifier. project_student = db_session().query(Project).filter_by( project_username=user_student).first() project_teacher = db_session().query(Project).filter_by( project_username=user_teacher).first() project_id = None if project_student and not project_teacher: project_id = project_student.id elif not project_student and project_teacher: project_id = project_teacher.id elif project_student and project_teacher: project_id = project_teacher.id # Create and add match to the database. match = Match(id=str(uuid.uuid4()), user_student=user_student, user_teacher=user_teacher, project_id=project_id, specialization=user_origin.specialization, status='PENDING') db_session().add(match) db_session().commit() # Return result. return jsonify(match=True), 201