def linkedin_login(): """ Login with auth code and exchanges auth code for auth token. If the auth token is expired, update the Student object. Finds student with corresponding linkedin_token and returns serialized Student. :return: { linkedin_token: <token>, profile: <serialized_student> } """ req = request.get_json() try: token = linkedin_token(req['code'])['access_token'] # get new token # TODO: perhaps use token expiration date for fail-safe log in except KeyError: # no access token print 'error: {}'.format(linkedin_token(req['code'])) return dumps({ 'reason': linkedin_token(req['code'])['error_description'], 'uri': linkedin_redirect_uri() }), 404 stu = Student.query.filter(Student.linkedin_token == token).first() if stu: # if token is not expired/invalid, return student profile = student_to_dict(stu) print 'student found: \n {}'.format(profile) return dumps({'linkedin_token': token, 'profile': profile}), 200 else: # update token, return student profile_dict = linkedin_basic_profile(token) account_exists = Student.query.filter( Student.username == profile_dict['emailAddress']).first() if account_exists: account_exists.linkedin_token = token # update token profile = student_to_dict(account_exists) print 'student found: \n {}'.format(profile) return dumps({'linkedin_token': token, 'profile': profile}), 200 else: # if linkedin_token doesn't exist in db, create student new_student = li_to_student(profile_dict) new_student.linkedin_token = token new_student.skills = linkedin_to_skills_list(profile_dict) new_student.looking_for = ['Internship', 'Full-Time'] new_student.save() stu = Student.query.filter(Student.linkedin_token == token).first() profile = student_to_dict(stu) print 'student created: \n {}'.format(profile) return dumps({'linkedin_token': token, 'profile': profile}), 200
def login(): """ Login with username and password. Returns a JSON serialized Student or Employer object. :return: { first_name: <first_name> ... account_type: <Student or Employer> } """ if request.method == 'POST': data = request.get_json() username = data['username'] password = data['password'] # check if username is in db st_obj = Student.query.filter(Student.username == username, Student.password == password).first() if st_obj is None: em_obj = Employer.query.filter( Employer.username == username, Employer.password == password).first() if em_obj: employer_dict = employer_to_dict(em_obj) if 'password' in employer_dict: del employer_dict['password'] print employer_dict return dumps(employer_dict), 200 else: return dumps({"reason": "No account exists for this username"}), 404 else: student_dict = student_to_dict(st_obj) if 'password' in student_dict: del student_dict['password'] print student_dict return dumps(student_dict), 200
def get_student_profile(username): st_obj = Student.query.filter(Student.username == username).first() if st_obj is not None: student_dict = student_to_dict(st_obj) return dumps(student_dict), 200 else: return dumps({}), 404
def edit_student_profile(username): new_data = request.get_json() # dictionary with data from user st_obj = Student.query.filter(Student.username == username).first() if st_obj is not None: for key in new_data: setattr(st_obj, key, new_data[key]) st_obj.save() return dumps(student_to_dict(st_obj)), 200 else: return dumps({'reason': 'Student account does not exist'}), 404
def listing_matches_to_dict(match_list): """ Similar to job_matches_to_dict, but for listing matches :param match_list: Listing student_matches field :return: dictionary of students, in order of highest to lowest match """ if len(match_list) == 0: return [] sorted_matches = sorted(match_list, key=lambda x: x[1], reverse=True) out_json = [] for m in sorted_matches: student = Student.query.filter(Student.username == m[0]) if student: student_dict = student_to_dict(student) student_dict['_id'] = str(student.username) out_json.append(student_dict) return out_json
def create_student_profile(): req_data = request.get_json() if 'skills' in req_data: skills = [i for i in req_data['skills']] req_data['skills'] = skills student_obj = dict_to_student(req_data) account_exists = Student.query.filter( Student.username == req_data['username']).first() if account_exists: return dumps({'reason': 'Student account already exists for email'}), 404 employer_exists = Employer.query.filter( Employer.username == req_data['username']).first() if employer_exists: return dumps({'reason': 'Employer account already exists for email'}), 404 else: student_obj.save() st_dict = student_to_dict(student_obj) return dumps(st_dict), 200