def home(): if 'email' not in session: return redirect(url_for('login')) email = session['email'] form = InfoForm() key = '' value = '' user = db_session.query(User).filter(User.email == email).first() if request.method == 'POST': if form.validate() == False: return render_template('home.html', form=form) else: key = form.key.data value = form.value.data newinfo = Info(form.key.data, form.value.data, user.uid) db_session.add(newinfo) db_session.commit() return render_template('home.html', form=form, key=key, value=value) elif request.method == 'GET': return render_template("home.html", form=form, key=key, value=value, user=user)
def get_lucky_num(): """takes data from form, makes api call, returns lucky number information. Returns JSON {'num': {fact, num} 'year': {fact, year}} """ data = request.get_json() form = InfoForm(MultiDict(mapping=data['data']), csrf_enabled=False) #essentially make a fake form that the JSON data gets assigned to and is then validated. Disable CSRF so the "form" goes through validation print(form) if form.validate(): num = randint(0, 100) resp = { "num": { "fact": requests.get(f"http://numbersapi.com/{num}/trivia").text, "num": num }, "year": { "fact": requests.get( f"http://numbersapi.com/{data['data']['year']}/year").text, "year": data['data']['year'] } } return jsonify(resp) else: resp = {'errors': {}} for error in form.errors: resp['errors'][error] = eval(f'form.{error}.errors')[0] return jsonify(resp)
def get_lucky_num(): data = request.get_json() form = InfoForm(MultiDict(mapping=data['data']), csrf_enabled=False) if form.validate(): print(form.data) from random import randint num = randint(0, 100) resp = { "num": { "fact": requests.get(f"http://numbersapi.com/{num}/trivia").text, "num": num }, "year": { "fact": requests.get( f"http://numbersapi.com/{data['data']['year']}/year").text, "year": data['data']['year'] } } return jsonify(resp) else: resp = {'errors': {}} for error in form.errors: resp['errors'][error] = eval(f'form.{error}.errors')[0] return jsonify(resp)
def new_album(): """ Add a new info """ form = InfoForm(request.form) if request.method == 'POST' and form.validate(): # save the info info = Info() save_changes(info, form, new=True) flash('info created successfully!') return redirect('/') return render_template('new_album.html', form=form)
def edit(id): """ Add / edit an item in the database """ qry = db_session.query(Info).filter( Info.id==id) info = qry.first() if info: form = InfoForm(formdata=request.form, obj=info) if request.method == 'POST' and form.validate(): # save edits save_changes(info, form) flash('info updated successfully!') return redirect('/') return render_template('edit_album.html', form=form) else: return 'Error loading #{id}'.format(id=id)
def wnew(): form = InfoForm() if request.method == "POST": if form.validate() == False: flash('All fields are required') return render_template("/whats_new.html", form=form) else: msg = Message("Interest in Sewing Classes", sender=form.email.data, recipients=['*****@*****.**']) msg.body = """From: %s %s <%s> %s""" % ( form.first_name.data, form.last_name.data, form.email.data, form.experience.data) mail.send(msg) return redirect("/signup.html") elif request.method == "GET": return render_template("/whats_new.html", form=form)
def delete(id): """ Delete the item in the database that matches the specified id in the URL """ qry = db_session.query(Info).filter(Info.id==id) info = qry.first() if info: form = InfoForm(formdata=request.form, obj=info) if request.method == 'POST' and form.validate(): # delete the item from the database db_session.delete(info) db_session.commit() flash('info deleted successfully!') return redirect('/') return render_template('delete_album.html', form=form) else: return 'Error deleting #{id}'.format(id=id)