def sign_up(): sign_up_form = forms.SignUpForm() if sign_up_form.validate_on_submit(): cur = db.cursor() # insert user sql = '''INSERT INTO user (email, password) VALUES (?, ?)''' cur.execute( sql, (sign_up_form.data['email'], str(sign_up_form.data['password']))) db.commit() # get new user id user_id = cur.execute('''SELECT user_id FROM user WHERE email=?''', (sign_up_form.data['email'], )) user_id = user_id.fetchone()['user_id'] # create interests sql = '''INSERT INTO interests (user_id) VALUES (?)''' cur.execute(sql, (user_id, )) db.commit() cur.close() # redirect to survey return redirect(url_for('survey', user_id=user_id)) print(sign_up_form.errors) return render_template('signup.html', form=sign_up_form)
def unique_email(form, field): db.row_factory = arr_factory cur = db.cursor() res = cur.execute('''SELECT email FROM user''') emails = res.fetchall() cur.close() db.row_factory = dict_factory if emails and field.data in emails: raise ValidationError( 'This email is already associated with an account!')
def results(user_id): cur = db.cursor() interests = cur.execute( '''SELECT seminars, workshops, job_networking, workouts, social_events, arts FROM interests WHERE user_id=?''', (user_id, )) interests = interests.fetchone() interest_list = [] for key in interests.keys(): if interests[key] == 1: interest_list.append(key) results = recommend(interest_list) return render_template('results.html', events=results)
def survey(user_id): form = forms.SurveyForm() if form.validate_on_submit(): cur = db.cursor() cur.execute( '''UPDATE user SET college=?, secondary_college=?, degree_level=? WHERE user_id=?''', (form.data['college'], form.data['college_secondary'], form.data['standing'], user_id)) update = '''UPDATE interests SET seminars=?, workshops=?, job_networking=?, workouts=?, social_events=?, arts=? WHERE user_id=?''' params = tuple(create_interests(form.data['interests'])) + (user_id, ) cur.execute(update, params) db.commit() return redirect(url_for('results', user_id=user_id)) return render_template('survey.html', form=form)
def home_page(): sign_in_form = forms.SignInForm() if sign_in_form.validate_on_submit(): # locate user email in database cur = db.cursor() user = cur.execute('''SELECT * FROM user WHERE email=?''', (sign_in_form.data['email'], )) user = user.fetchone() # see if password in the database matches the given password if user: if str(user['password']) == str(sign_in_form.data['password']): return redirect(url_for('results', user_id=user['user_id'])) sign_in_form.email.errors.append(u'Invalid Credentials!') # return login page return render_template('login.html', form=sign_in_form)
def populate_events(): print(db) with open('../taged_events_no_duplicates.csv') as csvfile: reader = csv.DictReader(csvfile) cur = db.cursor() for row in reader: sql = '''INSERT INTO events (doc_id,title,url,event_type,sponsor,location,date_,speaker,originating_calendar,topics,cost,contact,e_mail,phone,registration,description,seminars,workshops,job_networking,workouts,social_events,arts) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)''' params = (row['doc_id'], row['title'], row['url'], row['event_type'], row['sponsor'], row['location'], row['date'], row['speaker'], row['originating_calendar'], row['topics'], row['cost'], row['contact'], row['e-mail'], row['phone'], row['registration'], row['description'], row['seminars'], row['workshops'], row['job_networking'], row['workouts'], row['social_events'], row['arts']) cur.execute(sql, params) db.commit()
def populate_documents(): with open('../events.csv') as csvfile: reader = csv.DictReader(csvfile) column_names = words.copy() column_names[4] += '_' column_names[5] += '_' for row in reader: doc_id = row['doc_id'] word_count = get_word_counts(row['description']) sql = "INSERT INTO documents (doc_id, {0}, title, url, event_type, sponsor, location, date_, speaker, originating_calendar, topics, cost, contact, email, phone, registration) values ({1})".format( ', '.join(column_names), ','.join(['?'] * 54)) print(sql) cur = db.cursor() params = (doc_id, ) + tuple(word_count) + ( row['title'], row['url'], row['event_type'], row['sponsor'], row['location'], row['date'], row['speaker'], row['originating_calendar'], row['topics'], row['cost'], row['contact'], row['e-mail'], row['phone'], row['registration']) cur.execute(sql, params) db.commit()