def edit_address(): """Create and edit an address.""" form = AddressForm(request.form) address_id = request.args.get("id", None, type=int) if address_id: user_address = UserAddress.get_by_id(address_id) form = AddressForm(request.form, obj=user_address) if request.method == "POST" and form.validate_on_submit(): address_data = { "province": form.province.data, "city": form.city.data, "district": form.district.data, "address": form.address.data, "contact_name": form.contact_name.data, "contact_phone": form.contact_phone.data, "user_id": current_user.id } if address_id: UserAddress.update(user_address, **address_data) flash(lazy_gettext("Success edit address."), "success") else: UserAddress.create(**address_data) flash(lazy_gettext("Success add address."), "success") return redirect(url_for("account.index") + "#addresses") else: flash_errors(form) return render_template("account/address_edit.html", form=form, address_id=address_id)
def checkout_shipping(): form = AddressForm(request.form) user_address = None if request.method == "POST": if request.form["address_sel"] != "new": user_address = UserAddress.get_by_id(request.form["address_sel"]) elif request.form["address_sel"] == "new" and form.validate_on_submit( ): user_address = UserAddress.create( province=form.province.data, city=form.city.data, district=form.district.data, address=form.address.data, contact_name=form.contact_name.data, contact_phone=form.contact_phone.data, user_id=current_user.id, ) shipping_method = ShippingMethod.get_by_id( request.form["shipping_method"]) if user_address and shipping_method: cart = Cart.get_current_user_cart() cart.update( shipping_address_id=user_address.id, shipping_method_id=shipping_method.id, ) return redirect(url_for("checkout.checkout_note")) flash_errors(form) shipping_methods = ShippingMethod.query.all() return render_template("checkout/shipping.html", form=form, shipping_methods=shipping_methods)
def set_password(): form = ChangePasswordForm(request.form) if form.validate_on_submit(): current_user.update(password=form.password.data) flash(lazy_gettext("You have changed password."), "success") else: flash_errors(form) return redirect(url_for("account.index"))
def login(): """login page.""" form = LoginForm(request.form) if form.validate_on_submit(): login_user(form.user) redirect_url = request.args.get("next") or url_for("public.home") flash(lazy_gettext("You are log in."), "success") return redirect(redirect_url) else: flash_errors(form) return render_template("account/login.html", form=form)
def signup(): """Register new user.""" form = RegisterForm(request.form) if form.validate_on_submit(): user = User.create( username=form.username.data, email=form.email.data.lower(), password=form.password.data, is_active=True, ) login_user(user) flash(lazy_gettext("You are signed up."), "success") return redirect(url_for("public.home")) else: flash_errors(form) return render_template("account/signup.html", form=form)
def resetpwd(): '''Reset user password''' form = ResetPasswd(request.form) if form.validate_on_submit(): flash(lazy_gettext("Check your e-mail."), "success") user = User.query.filter_by(email=form.username.data).first() new_passwd = id_generator() body = render_template("account/reser_passwd_mail.html", new_passwd=new_passwd) msg = Message(lazy_gettext("Reset Password"), recipients=[form.username.data]) msg.body = lazy_gettext( '''We cannot simply send you your old password.\n A unique password has been generated for you. Change the password after logging in.\n New Password is: %s''' % new_passwd) msg.html = body mail = current_app.extensions.get("mail") mail.send(msg) user.update(password=new_passwd) return redirect(url_for("account.login")) else: flash_errors(form) return render_template("account/login.html", form=form, reset=True)