def place_order(): user_id = flask_jwt_extended.get_jwt_identity() cart_list = db_session.query( models.Cart).filter(models.Cart.user_id == user_id).all() try: address_id = request.form["address"] created_at = datetime.datetime.now() for cart in cart_list: create_order = models.Orders( order_id=address_id, user_id=user_id, product_id=cart.product_id, status='pending', created_at=created_at, ) db_session.add(create_order) db_session.flush() except Exception as e: print(e) return redirect(url_for("view.cart")) try: db_session.commit() except Exception as e: print(e) db_session.rollback() return redirect(url_for("view.cart")) return redirect(url_for("view.orders"))
def delete(self): """ 从数据库中删除对象 :return: """ try: db_session.delete(self) db_session.commit() except: db_session.rollback() raise finally: db_session.close() return self
def save(self): """ 保存对象到数据库 :return: """ try: db_session.add(self) db_session.commit() except: db_session.rollback() raise finally: db_session.close() model_saved.send(app._get_current_object()) return self
def reset_password(): try: email = request.form["email"] password_hash = request.form["password"] user = (db_session.query( models.User).filter(models.User.email == email).one_or_none()) user.password_hash = ph.hash(password_hash) except Exception as e: print(e) return {"message": "Failed to get user"} try: db_session.commit() session["logged_in"] = True return redirect(url_for("view.products")) except Exception as e: print(e) db_session.rollback() return redirect(url_for("view.user"))
def add_address(): try: user_id = flask_jwt_extended.get_jwt_identity() address_name = request.form["address_name"] first_name = request.form["first_name"] last_name = request.form["last_name"] email = request.form["email"] phone_number = request.form["phone_number"] address = request.form["address"] country = request.form["country"] state = request.form["state"] city = request.form["city"] postal_code = request.form["postal_code"] add_address = models.UserAddress(address_name=address_name, user_id=user_id, first_name=first_name, last_name=last_name, email=email, phone_number=phone_number, address=address, country=country, state=state, city=city, postal_code=postal_code) db_session.add(add_address) db_session.flush() except Exception as e: print(e) return redirect(url_for("view.checkout")) try: db_session.commit() address_id = add_address.id except Exception as e: print(e) db_session.rollback() return redirect(url_for("view.checkout")) return redirect(url_for("view.cart"))
def edit_delivery_address(): address_id = request.form["address_id"] address = db_session.query(models.UserAddress).filter( models.UserAddress.id == int(address_id)).one_or_none() address.address_name = request.form["address_name"] address.first_name = request.form["first_name"] address.last_name = request.form["last_name"] address.email = request.form["email"] address.phone_number = request.form["phone_number"] address.address = request.form["address"] address.country = request.form["country"] address.state = request.form["state"] address.city = request.form["city"] address.postal_code = request.form["postal_code"] try: db_session.commit() except Exception as e: print(e) db_session.rollback() return redirect(url_for("view.checkout")) return redirect(url_for("view.cart"))
def registration(): try: name = request.form["name"] email = request.form["email"] password_hash = request.form["password"] user_type_id = 1 created_at = datetime.datetime.now() create_user = models.User( name=name, email=email, password_hash=ph.hash(password_hash), email_validated=False, user_type_id=user_type_id, created_at=created_at, ) db_session.add(create_user) db_session.flush() except Exception as e: print(e) print("failed to create user") return redirect(url_for("view.register")) try: db_session.commit() res = verification_mail.send_mail(create_user.id, create_user.name, create_user.email, email_type="new-user") if not res: print("invalid email or server failed to send verification mail") return redirect(url_for("view.register")) return redirect(url_for("view.user")) except Exception as e: print(e) print("failed to store user in database") db_session.rollback() return redirect(url_for("view.register"))