def get_current_auth_user(): mentor_id = get_jwt_identity() # get mentor response = get_mentor_response_by_id(mentor_id) response_json = response.json() # Validation check if (response.status_code // 100) != 2: return response_json["error"], response.status_code if len(response_json["records"]) == 0: return ( "No Move Up user exists for this Google account! Please add user info to Airtable to log in.", 400, ) # Get object's parameters id_number = response_json["records"][0]["id"] name = response_json["records"][0]["fields"].get("Name") email = response_json["records"][0]["fields"].get("Move Up Email") if name is None or email is None: return "There is no Move Up mentor with that email.", 400 mentor = Mentor(name=name, email=email, id_number=id_number) return jsonify({"user": mentor.serialize()}), 200
def getResponse(): for r in response_json["records"]: id_number = r["id"] name = r["fields"].get("Name") email = r["fields"].get("Move Up Email") if name is not None and email is not None: m = Mentor(name=name, email=email, id_number=id_number) list_of_mentors.append(m.serialize())
def mentorRegister(): if current_user.is_authenticated: return redirect(url_for('index')) form = MentorRegistrationForm() if form.validate_on_submit(): user = Mentor(name=form.name.data, email=form.email.data) user.set_password(form.password.data) db.session.add(user) db.session.commit() flash('Congratulations, you are now a registered user!') return redirect(url_for('mentorLogin')) return render_template('register.html', title='Register', form=form)
def get_mentor_by_id(id): response = requests.get( "https://api.airtable.com/v0/appw4RRMDig1g2PFI/Mentors/{}".format(id), headers={"Authorization": str(os.environ.get("API_KEY"))}, ) if response.status_code == 200: response_json = response.json() name = response_json["fields"].get("Name") email = response_json["fields"].get("Move Up Email") if name is not None: mentor = Mentor(name=name, email=email) return jsonify(mentor.serialize()) else: return "This mentor does not exist in the database."
def get_all_mentors(): response = requests.get( "https://api.airtable.com/v0/appw4RRMDig1g2PFI/Mentors", headers={"Authorization": str(os.environ.get("API_KEY"))}, ) response_json = response.json() list_of_mentors = [] for r in response_json["records"]: name = r["fields"].get("Name") email = r["fields"].get("Move Up Email") if name is not None and email is not None: m = Mentor(name=name, email=email) list_of_mentors.append(m.serialize()) return jsonify(list_of_mentors)
def get_mentor_by_email(email): response = requests.get( "https://api.airtable.com/v0/appw4RRMDig1g2PFI/Mentors?filterByFormula=SEARCH('{}'" .format(email) + ", {Move Up Email})", headers={"Authorization": str(os.environ.get("API_KEY"))}, ) if response.status_code == 200: response_json = response.json() for r in response_json["records"]: name = r["fields"].get("Name") email = r["fields"].get("Move Up Email") if name is not None: m = Mentor(name=name, email=email) return jsonify(m.serialize()) else: return "There is no mentor with that email, please try again."
def login(): # required in body: email: String data = request.get_json(force=True) email = data.get("email") token = data.get("token") try: id_token.verify_oauth2_token(token, Request(), os.getenv("GOOGLE_CLIENT_ID")) except ValueError: return "Invalid Google ID token!", 400 # get mentor response = get_mentor_response_by_email(email) response_json = response.json() # Validation check if (response.status_code // 100) != 2: return response_json["error"], response.status_code if len(response_json["records"]) == 0: return ( "No Move Up user exists for this Google account! Please add user info to Airtable to log in.", 400, ) # Get object's parameters id_number = response_json["records"][0]["id"] name = response_json["records"][0]["fields"].get("Name") email = response_json["records"][0]["fields"].get("Move Up Email") mentor = Mentor(name=name, email=email, id_number=id_number) # create tokens access_token = create_access_token(identity=mentor.id_number) refresh_token = create_refresh_token(identity=mentor.id_number) # set cookies resp = jsonify({"user": mentor.serialize()}) set_access_cookies(resp, access_token) set_refresh_cookies(resp, refresh_token) return resp, 200
def get_mentor_by_email(email): response = get_mentor_response_by_email(email) response_json = response.json() # Validation check if (response.status_code // 100) != 2: return response_json["error"], response.status_code error = handleEmailResponse(response_json["records"]) if error is not None: return error, 422 # Get object's parameters id_number = response_json["records"][0]["id"] name = response_json["records"][0]["fields"].get("Name") email = response_json["records"][0]["fields"].get("Move Up Email") if name is None or email is None: return "There is no mentor with that email. Please try again.", 422 # Create and return object m = Mentor(name=name, email=email, id_number=id_number) return jsonify(m.serialize()), 200
def get_mentor_by_id(id): base_url = current_app.config["DATABASE_URL"] response = requests.get( "{}/Mentors/{}".format(base_url, id), headers={"Authorization": str(os.environ.get("API_KEY"))}, ) # Convert to JSON response_json = response.json() # Validation checks if (response.status_code // 100) != 2: return response_json["error"], response.status_code # Get object's parameters id_number = response_json["id"] name = response_json["fields"].get("Name") email = response_json["fields"].get("Move Up Email") if name is None or email is None: return "There is no mentor with this id. Please try again.", 422 # Create and return object mentor = Mentor(name=name, email=email, id_number=id_number) return jsonify(mentor.serialize()), 200
def test_follow(self): u1 = Mentor(name='john', email='*****@*****.**') u2 = Mentor(name='susan', email='*****@*****.**') db.session.add(u1) db.session.add(u2) db.session.commit() self.assertEqual(u1.followed.all(), []) self.assertEqual(u1.followers.all(), []) u1.follow(u2) db.session.commit() self.assertTrue(u1.is_following(u2)) self.assertEqual(u1.followed.count(), 1) self.assertEqual(u1.followed.first().name, 'susan') self.assertEqual(u2.followers.count(), 1) self.assertEqual(u2.followers.first().name, 'john') u1.unfollow(u2) db.session.commit() self.assertFalse(u1.is_following(u2)) self.assertEqual(u1.followed.count(), 0) self.assertEqual(u2.followers.count(), 0)
def register_user(userType): if current_user.is_admin(): try: userType = int(userType) except: return render_template('404.html') if (userType == 0): form = MenteeRegistrationForm() accessType = 0 elif (userType == 1): form = MentorRegistrationForm() accessType = 1 elif (userType == 3): form = CompanyRegistrationForm() accessType = 3 else: form = MenteeRegistrationForm() accessType = 0 if form.validate_on_submit(): user = User(email=form.email.data, access=accessType) user.set_password(form.password.data) user.set_id() db.session.add(user) db.session.commit() # create mentor if accessType == 1: mentor = Mentor(first_name=form.first_name.data, last_name=form.last_name.data, email=form.email.data, headline=form.headline.data, about_me=form.about.data, avail=form.avail.data, skills=form.skills.data, industry=dict(form.industry.choices).get(form.industry.data), mentor_company=form.company.data, position=form.position.data, university=dict(form.university.choices).get(form.university.data), major=dict(form.major.choices).get(form.major.data), grad_year=form.grad_year.data, linkedin=form.linkedin.data, twitter=form.twitter.data) db.session.add(mentor) db.session.commit() else: mentee = Mentee(first_name=form.first_name.data, last_name=form.last_name.data, email=form.email.data, headline=form.headline.data, about=form.about.data, skills=form.skills.data, university=dict(form.university.choices).get(form.university.data), major=dict(form.major.choices).get(form.major.data), year=dict(form.year.choices).get(form.year.data), company=form.company.data, industry=dict(form.industry.choices).get(form.industry.data), linkedin=form.linkedin.data, twitter=form.twitter.data) db.session.add(mentee) db.session.commit() # CREATE A COMPANY ACCOUNT AND LINK IT TO THIS ACCOUNT IF ACCESSTYPE == 3 if accessType == 3: # add new user email to company if not added previously form_emails = form.team_emails.data if form.email.data not in form_emails: form_emails = form_emails + ', ' + form.email.data company = Company(company=form.company.data, members=form.founder_names.data, email=form_emails, industry=form.industry.data, help_req=form.help_needed.data) db.session.add(company) db.session.commit() email_array = form_emails.split(", ") # links members listed via email to company for mem_email in email_array: user = User.query.filter_by(email=mem_email).first() # make acct a company having acct if user: user.access = 3 mentee = Mentee.query.filter_by(email=mem_email).first() # link acct to company print(mem_email) if mentee: mentee.company_l = company db.session.commit() flash('Congratulations, you have registered ' + form.email.data) return redirect(url_for('login')) elif request.method == 'GET': if accessType == 1: return render_template('register_mentor.html', title='Register Mentor', form=form) elif accessType == 3: return render_template('register_mentee.html', title='Register Company', form=form) else: return render_template('register_mentee.html', title='Register Mentee', form=form) else: return render_template('404.html')
def test_follow_posts(self): # create four users u1 = Mentor(name='john', email='*****@*****.**') u2 = Mentor(name='susan', email='*****@*****.**') u3 = Mentor(name='mary', email='*****@*****.**') u4 = Mentor(name='david', email='*****@*****.**') db.session.add_all([u1, u2, u3, u4]) # create four posts now = datetime.utcnow() p1 = Post(body="post from john", author=u1, timestamp=now + timedelta(seconds=1)) p2 = Post(body="post from susan", author=u2, timestamp=now + timedelta(seconds=4)) p3 = Post(body="post from mary", author=u3, timestamp=now + timedelta(seconds=3)) p4 = Post(body="post from david", author=u4, timestamp=now + timedelta(seconds=2)) db.session.add_all([p1, p2, p3, p4]) db.session.commit() # setup the followers u1.follow(u2) # john follows susan u1.follow(u4) # john follows david u2.follow(u3) # susan follows mary u3.follow(u4) # mary follows david db.session.commit() # check the followed posts of each user f1 = u1.followed_posts().all() f2 = u2.followed_posts().all() f3 = u3.followed_posts().all() f4 = u4.followed_posts().all() self.assertEqual(f1, [p2, p4, p1]) self.assertEqual(f2, [p2, p3]) self.assertEqual(f3, [p3, p4]) self.assertEqual(f4, [p4])
def test_avatar(self): u =Mentor(name='john', email='*****@*****.**') self.assertEqual(u.avatar(128), ('https://www.gravatar.com/avatar/' 'd4c74594d841139328695756648b6bd6' '?d=identicon&s=128'))
def test_password_hashing(self): u = Mentor(id=1) u.set_password('cat') self.assertFalse(u.check_password('dog')) self.assertTrue(u.check_password('cat'))