def index(): form = NewPaste() if form.validate_on_submit(): # WTForms passes '' for empty text values, # this lambda switches them to None f = lambda s: s if s != '' else None vals = { 'text': form.paste.data, 'title': f(form.title.data), 'language': f(form.language.data), 'password': f(form.password.data), 'unlisted': f(form.unlisted.data) } paste = Paste.new(**vals) if paste is None: return redirect(url_for('pastes.index')) else: authorise_viewing(paste['hash']) if paste['unlisted']: url = url_for('pastes.unlisted', paste_hash=paste['hash']) else: url = url_for('pastes.public', paste_id=paste['id']) return redirect(url) elif request.method == 'POST': # Form submitted but failed validation for field, error in form.errors.items(): errormsg = '{0}: {1}'.format(field, error[0]) flash(errormsg, 'error') return render_template('index.html', form=form)
def index(): form = NewPaste() if form.validate_on_submit(): # WTForms passes '' for empty text values, # this lamba switches them to None f = lambda s: s if s != "" else None vals = { "text": form.paste.data, "title": f(form.title.data), "language": f(form.language.data), "password": f(form.password.data), "unlisted": f(form.unlisted.data), } paste = Paste.new(**vals) if paste is None: return redirect(url_for("pastes.index")) else: authorise_viewing(paste["hash"]) if paste["unlisted"]: url = url_for("pastes.unlisted", paste_hash=paste["hash"]) else: url = url_for("pastes.public", paste_id=paste["id"]) return redirect(url) elif request.method == "POST": # Form submitted but failed validation for field, error in form.errors.items(): errormsg = "{0}: {1}".format(field, error[0]) flash(errormsg, "error") return render_template("index.html", form=form)
def delete_paste(hash): form = DeletePasteForm() if form.validate_on_submit(): if not Paste.delete(form.paste_hash.data): flash('Deletion failed', 'error') return redirect(request.url) return redirect(url_for('pastes.recent')) return render_template('delete.html', form=form, hash=hash)
def logout(): form = LogoutForm() if request.method == 'POST' and form.validate_on_submit(): if session.get('username') == form.username.data: session.pop('username') session.pop('logged_in') session.modified = True return redirect(url_for('pastes.index')) return render_template('logout.html', form=form)
def login(): form = LoginForm() if form.validate_on_submit(): username = form.username.data password = form.password.data if User.password_match(username, password): session['username'] = username session['logged_in'] = True return redirect(url_for('pastes.index')) else: flash('Incorrect username/password', 'error') return render_template('login.html', form=form)