def test_good(self): app.app.config['WTF_CSRF_ENABLED'] = False with app.app.test_request_context('/'): r = Record(**test_record) form = EditForm(MultiDict(r.record)) result = form.validate() self.assertEqual(result, True)
def edit(id): #qry=db.session.query(Item).filter(Item.id==id) #items=qry.first() u_items = current_user.items for i in u_items: if i.id == id: items = i if items: form = EditForm(formdata=request.form, obj=items) if request.method == 'GET': form.isold.data = items.isold if request.method == 'POST': if request.form.get('delete'): delete(id) flash('Deleted succesfuly!') return redirect('/showitems') elif form.validate(): #save edited save_changes(items, form) flash('Edited succesfuly!') return redirect('/showitems') return render_template('edititem.html', form=form) else: return 'Error loading #{id}'.format(id=id)
def user_edit(): form = EditForm() if request.method == 'POST' and form.validate(): user = User.objects(id=current_user.id).first() #form_username = User.objects(username=form.username.data).first() form_email = User.objects(email=form.email.data).first() # if form_username != None and user.id != form_username.id: # flash('"{}" has already been registered'.format(form_username)) # return redirect(url_for('user_edit')) if form_email != None and user.id != form_email.id: flash('"{}" has already been registered'.format(form_email)) return redirect(url_for('user_edit')) # user.username = form.username.data user.email = form.email.data if form.password.data != None: user.reset_password(form.password.data) user.save() flash('Your changes have been saved!') return redirect(url_for('user', username=current_user.username)) else: user = User.objects(email=current_user.email).first() # form.username.data = user.username form.email.data = user.email # form.about_me.data = user.about_me return render_template('user_edit.html', form=form)
def edit(): form = EditForm() if request.method == 'POST' and form.validate(): user = User.objects(id=current_user.id).first() form_username = User.objects(username=form.username.data).first() form_email = User.objects(email=form.email.data).first() if form_username != None and user.id != form_username.id: flash('Ya existe un usuario con el mismo nombre, seleccione otro') return redirect(url_for('edit')) if form_email != None and user.id != form_email.id: flash('Ya existe un usuario con el mismo correo, seleccione otro') return redirect(url_for('edit')) user.username = form.username.data user.email = form.email.data user.about_me = form.about_me.data user.save() flash('Los cambios han sido guardados.') return redirect(url_for('edit')) else: user = User.objects(email=current_user.email).first() form.username.data = user.username form.email.data = user.email form.about_me.data = user.about_me return render_template('edit.html.j2', form=form)
def validate(self, record={}): """ Validates a single record. Returns a list of issues found, which can be empty. """ form = EditForm(MultiDict(record), meta={'csrf': False}) result = form.validate() errors = form.errors return result, errors, form
def test_should_fail_validation_when_text_is_too_long(app): app.config['MAX_PASTE_LENGTH'] = 4 with app.app_context(): form = EditForm( MultiDict([('text', 'abcde'), ('extension', ''), ('delete_after', '4')])) assert form.validate() is False assert len(form.errors) == 1 assert form.text.errors[ 0] == 'Your snippet is too long. Max 4 characters.'
def user(username): user = User.query.filter_by(username=username).first_or_404() cancelform = CancelForm(prefix="a") editform = EditForm(request.form, prefix="b") if cancelform.submit.data and cancelform.validate(): order = user.orders.filter_by( order_name=cancelform.confirm.data).first_or_404() order.order_flag = "cancelled" db.session.add(order) db.session.commit() send_mail( f"{cancelform.confirm.data} cancelled", current_user.email, ["*****@*****.**"], f"{cancelform.confirm.data} has been cancelled", ) if editform.submit2.data and editform.validate(): file = request.files.get(editform.profileimg.name) print(file) if file: filename = secure_filename(file.filename) file_extensions = [".jpg", ".jpeg", ".png"] good = False for file_extension in file_extensions: if filename.endswith(file_extension): good = True if good: file.save(app.config["UPLOAD_FOLDER"] + filename) user.profile_img = "/static/profile_imgs/" + filename db.session.add(user) db.session.commit() else: flash("Not an image!") if editform.name.data: user_exists = User.query.filter_by( username=editform.name.data).first() if not user_exists: user.username = editform.name.data db.session.add(user) db.session.commit() return redirect(f"/account/{user.username}") else: flash("Username exists!") else: print("no!") ordered = user.orders.filter_by(order_flag="open") cancels = user.orders.filter_by(order_flag="cancelled") completed = user.orders.filter_by(order_flag="completed") if current_user.email in [ "*****@*****.**", "*****@*****.**", "*****@*****.**", ]: return render_template( "user.html", user=user, opens=ordered, cancels=cancels, completed=completed, cancel_form=cancelform, edit_form=editform, isAdmin=True, ) else: return render_template( "user.html", user=user, opens=ordered, cancels=cancels, completed=completed, cancel_form=cancelform, edit_form=editform, isAdmin=False, )
def test_should_fail_validation_when_all_required_missing(app): with app.app_context(): form = EditForm() assert form.validate() is False assert len(form.errors) == 3
def test_should_fail_validation_when_some_required_missing(app): with app.app_context(): form = EditForm(MultiDict([('text', 'abcde'), ('delete_after', '4')])) assert form.validate() is False assert len(form.errors) == 1