def cars(): assigned_cars = json.loads(request.args.get('cars'))['cars'] full_car_info = [] for driver_id in list(assigned_cars.keys()): car_info = {} result = db.select('users', ['first', 'last', 'phone'], ['user_id'], [driver_id], fetchone=True) driver = result car_info['driver'] = driver athletes = [] car_string = '' for athlete in assigned_cars[driver_id]['athletes']: curr_athlete = athlete[0] result = db.select( 'users', ['first', 'last', 'address', 'city', 'state', 'phone'], ['user_id'], [curr_athlete], fetchone=True) athletes.append(result) car_string += ('{} {}: {}, {} - {}\n'.format( result['first'], result['last'], result['address'], result['city'], result['phone'])) car_info['athletes'] = athletes twilio_client.messages.create( to="+1{}".format(driver['phone']), from_=twilio_number, body="Hi {}, your car tomorrow is: {}".format( driver['first'], car_string)) full_car_info.append(car_info) return render_template('cars.html', car_info=full_car_info)
def test_easy_csv(self): clean_up_table('users', 'user_id') path = 'TestResources/easy_test_roster.csv' csv_to_db(path) log.debug('{}'.format(db.select('users', ['ALL']))) result = db.select('users', ['username'], where_cols=['username'], where_params=['jdoe'], operators=['='], fetchone=False) self.assertEqual([{'username': '******'}], result) clean_up_table('users', 'user_id')
def setUp(self): path = 'TestResources/dg_test_data.csv' result = db.select('users', ['ALL'], fetchone=False) if len(result) == 0: csv_to_db(path) result = db.select('users', ['ALL'], fetchone=False) self.athletes = [] self.drivers = [] for user in result: if user['num_seats'] > 0: self.drivers.append(user['user_id']) else: self.athletes.append(user['user_id'])
def recover(): form = web_forms.EnterUserName() if form.validate_on_submit(): # this will exist -- the form validates it username = form.data['username'] user = db.select('users', ['ALL'], ['username'], [username]) email_address = user['email'] subject = "Password Reset Requested" token = ts.dumps(email_address, salt='recover-key') recover_url = url_for('recover_password', token=token, _external=True) html = render_template('recover_email.html', recover_url=recover_url, user=user) # send user email util_basic.send_email(email_address, html, subject) flash( 'Password recovery directions have been sent to your email account.', 'alert-success') return redirect(url_for('new_signup')) return render_template('recover.html', form=form)
def test_duplicate_entry(self): clean_up_table('users', 'user_id') path = 'TestResources/easy_test_roster_w_pn.csv' csv_to_db(path) csv_to_db(path) result = db.select('users', ['phone'], where_cols=['username'], where_params=['jdoe'], operators=['='], fetchone=False) self.assertEqual([{'phone': 5555555555}], result) clean_up_table('users', 'user_id')
def confirm_email(token): try: email = ts.loads(token, salt="email-confirm-key", max_age=86400) except: abort(404) # set "confirmed" to true then return user = db.select('users', ['ALL'], ['email'], [email]) db.update('users', ['confirm_email'], [True], ['user_id'], [user['user_id']]) flash('Your email has been confirmed!', 'alert-success') return redirect(url_for('new_signup'))
def generate_cars(athletes, drivers): """ given an array of user_ids (separated into athletes and drivers), build a data structure with the ids, and coordinates of their location :param athletes: array of user_ids :param drivers: array of user_ids of people driving :return: dictionary of drivers; dictionary of athletes """ drivers = list(map(int, drivers)) athletes = list(map(int, athletes)) # get results from database results = db.select('users', select_cols=['ALL'], where_cols=['user_id'], where_params=[athletes + drivers], fetchone=False) total_seats = 0 # initialize data structures driver_dict = {} athlete_dict = {} # loop through users and build driver and athlete data structures for user in results: curr_id = user['user_id'] num_seats = user['num_seats'] info_dict = { 'x': user['x'], 'y': user['y'], 'id': curr_id, 'num_seats': 0 } if curr_id in drivers: info_dict['num_seats'] = num_seats info_dict['num_assigned'] = 0 info_dict['athletes'] = [] driver_dict[curr_id] = info_dict total_seats += num_seats else: athlete_dict[curr_id] = info_dict if total_seats < len(athletes): return None else: return driver_dict, athlete_dict
def user_from_csv_row(cls, csv_data, active=True): csv_data = {k: v for k, v in csv_data.items() if v is not None} col_names = list(csv_data.keys()) col_values = list(csv_data.values()) username_select = db.select(table_name='users', select_cols=['user_id'], where_cols=['username'], where_params=[csv_data['username']]) if username_select is not None: user_id = username_select['user_id'] db.update('users', col_names, col_values, where_cols=['user_id'], where_params=[user_id], operators=['=']) log.info('Updated {}'.format(user_id)) else: user_id = db.insert('users', col_names, col_values, 'user_id') bio_string = 'Hi, my name is %s!' % csv_data['first'] db.insert('profile', ['bio', 'user_id'], [bio_string, user_id], 'user_id') log.info('Inserted new user {}'.format(user_id))
def clean_up_table(table, pk): # empty users rows = db.select(table, [pk], fetchone=False) for _id in rows: db.delete_entry(table, pk, _id[pk])
def new_signup(): # forms to handle sign up and sign in signup_form = web_forms.SignUpForm() signin_form = web_forms.SignInForm() login = True if request.method == 'POST': if signin_form.data['submit_bttn']: if signin_form.validate_on_submit(): username = signin_form.data['username_field'] password = signin_form.data['password_field'] result = db.select('users', ['password', 'user_id'], ['username'], [username]) log.info('here is result: {}'.format(result)) if result: hash = result['password'] password_match = pbkdf2_sha256.verify(password, hash) if password_match: curr_user = User(result['user_id']) login_user(curr_user) next_url = request.args.get('next') if not is_safe_url(next_url): return abort(400) if not current_user.is_profile_complete(): flash( 'Please complete your profile before continuing!' ) return redirect(next_url or url_for('profile')) return redirect(next_url or url_for('team')) signin_form.username_field.errors.append( "Invalid Username or Password.") elif signup_form.data['submit']: if signup_form.validate(): # create token token = ts.dumps(signup_form.data['email'], salt='email-confirm-key') # build url confirm_url = url_for('confirm_email', token=token, _external=True) # set up html that makes up email html = render_template('signup_email.html', validate_url=confirm_url, user={ 'first': signup_form.data['first'], 'last': signup_form.data['last'] }) # create thread to speed up process subject = "Confirm Your Email" t1 = threading.Thread(target=util_basic.send_email, args=(signup_form.data['email'], html, subject)) t1.start() # create user curr_user = User.user_from_form(signup_form.data) # log user in login_user(curr_user) # wait for thread t1.join() # flash message and redirect user to their new profile page flash( 'Please check your email and follow the instructions to confirm your email address.', 'alert-success') return redirect(url_for('profile')) login = False return render_template( 'signup.html', sign_up=signup_form, sign_in=signin_form, login=login, _url= "https://s3-us-west-2.amazonaws.com/athlessary-images/defaults/login_photo.jpg" )
def get_all_athletes(): users = db.select('users', ['ALL'], fetchone=False) js = json.dumps(users) return Response(js, status=200, mimetype='application/json')
def profile(): form = web_forms.ProfileForm() if form.validate_on_submit(): user_attrs = ['phone', 'team', 'num_seats'] address_attrs = ['address', 'city', 'state', 'zip'] profile_attrs = ['bio'] profile_cols = [] profile_update_values = [] profile_update_col_names = [] for attribute in user_attrs: curr_attr_value = getattr(current_user, attribute) if form.data[attribute] != curr_attr_value: profile_update_values.append(form.data[attribute]) setattr(current_user, attribute, form.data[attribute]) profile_update_col_names.append(attribute) address = verify_user_address('', form.data['address'], form.data['city'], form.data['state'], form.data['zip']) if 'error' in address: flash(address['error'], 'alert-error') print(address['error']) else: for attribute in address_attrs: curr_attr_value = getattr(current_user, attribute) if form.data[attribute] != curr_attr_value: profile_update_values.append(address[attribute]) setattr(current_user, attribute, address[attribute]) profile_update_col_names.append(attribute) # update x and y coordinates current_user.init_coordinates() for attribute in profile_attrs: profile_cols.append((form.data[attribute])) if len(profile_update_values) > 0: db.update('users', profile_update_col_names, profile_update_values, ['user_id'], [current_user.user_id]) db.update('profile', profile_attrs, profile_cols, ['user_id'], [current_user.user_id]) # gather user profile user_profile = db.select('profile', ['ALL'], ['user_id'], [current_user.get_id()]) if current_user.num_seats > 0: form.num_seats.data = int(current_user.num_seats) if form.team: form.team.data = current_user.team if current_user.address: form.address.data = current_user.address if current_user.state: form.state.data = current_user.state if current_user.city: form.city.data = current_user.city if current_user.zip: form.zip.data = current_user.zip if current_user.phone: form.phone.data = current_user.phone form.num_seats.data = current_user.num_seats form.bio.data = user_profile['bio'] return render_template('profile.html', form=form, profile=user_profile, sign_certificate=sign_certificate)