def create_show_submission(): # called to create new shows in the db, upon submitting new show listing form - done # TODO: insert form data as a new Show record in the db, instead - done form = ShowForm(request.form, meta={'csrf': False}) if form.validate(): try: show = Show(artist_id=form.artist_id.data, venue_id=form.venue_id.data, start_time=form.start_time.data) db.session.add(show) db.session.commit() # on successful db insert, flash success - done flash('Show of date: ' + request.form['start_time'] + ' was successfully listed!') except: # TODO: on unsuccessful db insert, flash an error instead. - done # e.g., flash('An error occurred. Show ' + data.name + ' could not be listed.') # see: http://flask.pocoo.org/docs/1.0/patterns/flashing/ error = True db.session.rollback() flash('An error occurred. Show of date: ' + str(form.start_time.data) + ' could not be listed.') flash(f'{sys.exc_info()}') finally: db.session.close() else: messages = [] for field, errors in form.errors.items(): for error in errors: messages.append(field + ' : ' + error + '\n') flash('The Show data is not valid. Please try again.') for message in messages: flash(message) return render_template('pages/home.html')
def signup(): form = SignupForm(request.form) if request.method == 'POST' and form.validate(): if form.role.data == "learner": user = User(name=form.name.data, email=form.email.data, lang_id=form.language.data) else: user = Teacher(name=form.name.data, title=form.title.data, email=form.email.data, lang_id=form.language.data) user.set_password(form.password.data) try: db.session.add(user) db.session.commit() user = User.query.filter_by(email=form.email.data).first() wallet = Wallet() wallet.balance = 0 # Generate random number for wallet wallet.wallet_id = random.randint(78624, 8123981242) wallet.id = user.id db.session.add(wallet) db.session.commit() response = make_response(redirect(url_for('main.index'))) response.set_cookie("name", form.name.data) return response except IntegrityError: db.session.rollback() flash('ERROR! Unable to register {}. Please check your details are correct and resubmit'.format( form.email.data), 'error') return render_template('signup.html', form=form)
def contact(): form = ContactForm() if request.method == 'POST': if form.validate() == False: flash('All fields are required.') return render_template('contact.html', form = form) else: return render_template('result.html') return render_template('contact.html', form = form)
def login(): form = LoginForm() if request.method == 'POST' and form.validate(): user = User.query.filter_by(email=form.email.data).first() if user is None or not user.check_password(form.password.data): flash('Invalid username or password') return redirect(url_for('auth.login')) login_user(user, remember=form.remember_me.data, duration=timedelta(minutes=1)) flash('Logged in successfully. {}'.format(user.name)) next = request.args.get('next') if not is_safe_url(next): return abort(400) return redirect(next or url_for('main.index')) return render_template('login.html', form=form)
def new_task(): error = None form = AddTaskForm(request.form) if request.method == 'POST': if form.validate(): new_task = Task(form.name.data,form.due_date.data,form.priority.data,datetime.datetime.utcnow(),'1', session['user_id']) db.session.add(new_task) db.session.commit() flash('New entry was successfully posted. Thanks.') return redirect(url_for('tasks')) else: flash('Please ensure all fields are completed') return redirect(url_for('tasks')) return render_template('tasks.html',form=form, error=error, open_task=open(), closed_tasks=closed_tasks())
def register(): error=None form=RegisterForm(request.form) if request.method=='POST'and form.validate(): new_user=User( form.name.data, form.email.data, form.password.data ) db.session.add(new_user) db.session.commit() flash('Thanks for registering. Please login.') return redirect(url_for('login')) return render_template('register.html', form=form, error=error)
def create_artist_submission(): # called upon submitting the new artist listing form # TODO: insert form data as a new Artist record in the db, instead - done # TODO: modify data to be the data object returned from db insertion - done form = ArtistForm(request.form, meta={'csrf': False}) if form.validate(): try: # alternative way to populate fields from line 603: # artist = Artist( # name = form.name.data, # city = form.city.data, # state = form.state.data, # phone = form.phone.data, # genres = form.genres.data, # facebook_link = form.facebook_link.data # ) artist = Artist() form.populate_obj(artist) db.session.add(artist) db.session.commit() # on successful db insert, flash success flash('Artist: ' + request.form['name'] + ' was successfully listed!') except: # TODO: on unsuccessful db insert, flash an error instead. - done # e.g., flash('An error occurred. Artist ' + data.name + ' could not be listed.') # see: http://flask.pocoo.org/docs/1.0/patterns/flashing/ error = True db.session.rollback() flash('An error occurred. Artist ' + form.name.data + ' could not be listed.') flash(f'{sys.exc_info()}') finally: db.session.close() else: messages = [] for field, errors in form.errors.items(): for error in errors: messages.append(field + ' : ' + error + '\n') flash('The Artist data is not valid. Please try again.') for message in messages: flash(message) # on successful db insert, flash success - done # TODO: on unsuccessful db insert, flash an error instead. - done # e.g., flash('An error occurred. Artist ' + data.name + ' could not be listed.') - done return render_template('pages/home.html')
def login(): error = None form = LoginForm(request.form) if request.method=='POST': if form.validate(): user = User.query.filter_by(name=request.form['name']).first() if user is not None and user.password == request.form['password']: session['logged_in'] = True session['user_id'] = user.id flash('Welcome!') return redirect(url_for('tasks')) else: error = 'Invalid username or password.' else: error = 'Both fields are required.' return render_template ('login.html', form=form, error=error)