def link_itinerary(): """Links a user to an existing itienrary and returns data as JSON""" user = helper.get_user_by_email(session['EMAIL']) itinerary_id = request.form['id'] itinerary = helper.get_itinerary_by_id(itinerary_id) crud.create_user_itinerary(user.user_id, itinerary_id) return jsonify({'itinerary_id': itinerary_id, 'trip_name': itinerary.trip_name})
def show_user_profile(fname): """ Show logged in user profile.""" email = session['USERNAME'] user = helper.get_user_by_email(email) user_itins = helper.get_itineraries_by_user(user) return render_template('user_profile.html', user=user, user_itins=user_itins)
def new_itinerary(): """Creates a new itinerary for a user and returns data as JSON.""" user = helper.get_user_by_email(session['EMAIL']) trip_name = request.form['trip_name'] start_date = request.form['start_date'] end_date = request.form['end_date'] num_days = crud.calculate_itinerary_days(start_date, end_date) lat, lng = crud.get_latitude_longitude_for_itinerary(trip_name) new_itinerary = crud.create_itinerary(trip_name, start_date, end_date, num_days, lat, lng) crud.create_user_itinerary(user.user_id, new_itinerary.itinerary_id) return jsonify({'itinerary_id': new_itinerary.itinerary_id, 'trip_name': new_itinerary.trip_name})
def new_user(): """Create new profile.""" email = request.form['email'] password = request.form['password'] fname = request.form['fname'] lname = request.form['lname'] user = helper.get_user_by_email(email) if user != None: return jsonify('This email is already associated with an account. Please log in.') else: crud.create_user(email, password, fname, lname) return jsonify('Your account has been created. Please log in.')
def login_user(): """ Login user and redirect to profile page if successful """ email = request.form['email'] incoming_password = request.form['password'] user = helper.get_user_by_email(email) if user == None: flash('No account with this email exists. Please try again.') return redirect('/') if helper.check_hashed_password(incoming_password, user.password) == True: session['EMAIL'] = user.email session['NAME'] = user.fname session['ID'] = user.user_id return redirect(f'profile/{user.fname}') else: flash('Incorrect Password. Please try again.') return redirect('/')
def login_user(): """ Log in user.""" email = request.form['email'] incoming_password = request.form['password'] user = helper.get_user_by_email(email) if user == None: flash('No account with this email exists. Please try again.') return redirect ('/') else: if argon2.verify(incoming_password, user.password): session['EMAIL'] = user.email session['NAME'] = user.fname session['ID'] = user.user_id return redirect (f'users/profile/{user.fname}') else: flash('Incorrect Password. Please try again.') return redirect ('/')
def login_user(): """ Log in user.""" email = request.form['email'] password = request.form['password'] user = helper.get_user_by_email(email) if user == None: flash('No account with this email exists. Please try again.') return redirect('/') else: if password != user.password: flash('Incorrect Password. Please try again.') return redirect('/') else: session['USERNAME'] = user.email # print(session['USERNAME']) print('session username set') return redirect(f'users/profile/{user.fname}')
def new_itinerary(): """Creates a new itinerary for a user.""" email = session["USERNAME"] user = helper.get_user_by_email(email) trip_name = request.form.get('trip_name') start_date = request.form.get('start_date') end_date = request.form.get('end_date') num_days = crud.calculate_itinerary_days(start_date, end_date) lat_lng = crud.get_latitude_longitude_for_itinerary(trip_name) lat = lat_lng[0] lng = lat_lng[1] new_itinerary = crud.create_itinerary(trip_name, start_date, end_date, num_days, lat, lng) crud.create_user_itinerary(user.user_id, new_itinerary.itinerary_id) json_info = { 'itinerary_id': new_itinerary.itinerary_id, 'trip_name': new_itinerary.trip_name } return jsonify(json_info)
def create_user(): """Create new user account""" email = request.form['email'] password = request.form['password'] fname = request.form['fname'] lname = request.form['lname'] phone = request.form['phone'] user = helper.get_user_by_email(email) if len(phone) != 10: flash('Phone numbers must be 10 digits long') return redirect('/') else: phone = '+1' + phone if user != None: flash( 'This email is already associated with an account. Please log in.') return redirect('/') else: crud.create_user(email, password, fname, lname, phone) flash('Your account has been created. Please log in.') return redirect('/')
def get_user_information(): user = helper.get_user_by_email(session['EMAIL']) user_itins = helper.get_itineraries_by_user(user) return jsonify({'fname': user.fname, 'lname': user.lname, 'email': user.email, 'itineraries': user_itins})
def test_get_itineraries_by_user(self): """Test get_itineraries_by_user function""" user = helper.get_user_by_email('*****@*****.**') test = helper.get_itineraries_by_user(user) self.assertIs(type(test), list)
def test_get_user_by_email(self): """Test get_user_by_email function""" u = helper.get_user_by_email('*****@*****.**') self.assertIsNotNone(u.user_id)
def test_get_user_by_email(self): """Tests get user by email database query""" u = helper.get_user_by_email(email='*****@*****.**') self.assertIsNotNone(u.user_id)