|
| 1 | +import flask |
| 2 | +from flask import Flask, redirect, render_template, render_template_string, request |
| 3 | +import os |
| 4 | +from flask_sqlalchemy import SQLAlchemy |
| 5 | +from flask_migrate import Migrate |
| 6 | +import os |
| 7 | +from datetime import datetime |
| 8 | +import sys |
| 9 | +import inspect |
| 10 | +import time |
| 11 | + |
| 12 | +import flask_login |
| 13 | +from flask_login import UserMixin, LoginManager, login_user, logout_user, current_user |
| 14 | + |
| 15 | +app = Flask(__name__) |
| 16 | +app.config["SECRET_KEY"] = "mysecret" |
| 17 | + |
| 18 | + |
| 19 | +basedir = os.path.abspath(os.path.dirname(__file__)) |
| 20 | +app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///" + os.path.join( |
| 21 | + basedir, "data.sqlite" |
| 22 | +) |
| 23 | +app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False |
| 24 | + |
| 25 | +db = SQLAlchemy(app) |
| 26 | +Migrate(app, db) |
| 27 | + |
| 28 | + |
| 29 | +class PrintableMixin: |
| 30 | + def __repr__(self): |
| 31 | + """ |
| 32 | + Gives detailed reprs like |
| 33 | + <Product {'description': 'A small widget', 'name': 'Widget', 'price': 10, 'category': 'Widgets', 'id': 1, 'ingredients': 'Metal, plastic'}> |
| 34 | + """ |
| 35 | + clean_dict = {k: v for (k, v) in self.__dict__.items() if not k.startswith("_")} |
| 36 | + return f"<{self.__class__.__name__} {repr(clean_dict)}>" |
| 37 | + |
| 38 | + |
| 39 | +class Product(PrintableMixin, db.Model): |
| 40 | + __tablename__ = "products" |
| 41 | + |
| 42 | + id = db.Column(db.Integer, primary_key=True) |
| 43 | + name = db.Column(db.String, nullable=True) |
| 44 | + description = db.Column(db.String, nullable=True) |
| 45 | + category = db.Column(db.String, nullable=True) |
| 46 | + price = db.Column(db.Integer, nullable=True) |
| 47 | + ingredients = db.Column(db.String, nullable=True) |
| 48 | + |
| 49 | + order_products = db.relationship("OrderProduct", back_populates="product") |
| 50 | + |
| 51 | + |
| 52 | +class Order(PrintableMixin, db.Model): |
| 53 | + __tablename__ = "orders" |
| 54 | + |
| 55 | + id = db.Column(db.Integer, primary_key=True) |
| 56 | + status = db.Column(db.String, nullable=True) |
| 57 | + order_date = db.Column(db.String, default=datetime.utcnow, nullable=True) |
| 58 | + |
| 59 | + customer_id = db.Column( |
| 60 | + db.Integer, |
| 61 | + db.ForeignKey("customers.id", name="order_customer_fk"), |
| 62 | + nullable=True, |
| 63 | + ) |
| 64 | + customer = db.relationship( |
| 65 | + "Customer", foreign_keys=[customer_id], back_populates="orders" |
| 66 | + ) |
| 67 | + |
| 68 | + order_products = db.relationship("OrderProduct", back_populates="order") |
| 69 | + |
| 70 | + |
| 71 | +class OrderProduct(PrintableMixin, db.Model): |
| 72 | + id = db.Column(db.Integer, primary_key=True) |
| 73 | + quantity = db.Column(db.Integer, nullable=True) |
| 74 | + |
| 75 | + order_id = db.Column( |
| 76 | + db.Integer, |
| 77 | + db.ForeignKey("orders.id", name="orderproduct_order_fk"), |
| 78 | + nullable=True, |
| 79 | + ) |
| 80 | + order = db.relationship( |
| 81 | + "Order", foreign_keys=[order_id], back_populates="order_products" |
| 82 | + ) |
| 83 | + |
| 84 | + product_id = db.Column( |
| 85 | + db.Integer, |
| 86 | + db.ForeignKey("products.id", name="orderproduct_product_fk"), |
| 87 | + nullable=True, |
| 88 | + ) |
| 89 | + product = db.relationship( |
| 90 | + "Product", foreign_keys=[product_id], back_populates="order_products" |
| 91 | + ) |
| 92 | + |
| 93 | + |
| 94 | +class Customer(PrintableMixin, db.Model, UserMixin): |
| 95 | + __tablename__ = "customers" |
| 96 | + |
| 97 | + id = db.Column(db.Integer, primary_key=True) |
| 98 | + first_name = db.Column(db.String, nullable=True) |
| 99 | + last_name = db.Column(db.String, nullable=True) |
| 100 | + email = db.Column(db.String, nullable=True) |
| 101 | + address = db.Column(db.String, nullable=True) |
| 102 | + phone = db.Column(db.String, nullable=True) |
| 103 | + |
| 104 | + orders = db.relationship("Order", back_populates="customer") |
| 105 | + |
| 106 | + |
| 107 | +login_manager = LoginManager() |
| 108 | +login_manager.init_app(app) |
| 109 | + |
| 110 | + |
| 111 | +@login_manager.user_loader |
| 112 | +def load_user(user_id): |
| 113 | + return Customer.query.get_or_404(user_id) |
| 114 | + |
| 115 | + |
| 116 | +@app.route("/login-bob") |
| 117 | +def login_bob(): |
| 118 | + bob = Customer.query.get_or_404(3) |
| 119 | + login_user(bob) |
| 120 | + return redirect("/whoami") |
| 121 | + |
| 122 | + |
| 123 | +@app.route("/whoami") |
| 124 | +def whoami(): |
| 125 | + if current_user.is_authenticated: |
| 126 | + return f"Current user is {current_user.first_name} {current_user.last_name}" |
| 127 | + else: |
| 128 | + return "No user is logged in" |
| 129 | + |
| 130 | + |
| 131 | +@app.route("/logout") |
| 132 | +def logout(): |
| 133 | + logout_user() |
| 134 | + return redirect("/whoami") |
| 135 | + |
| 136 | + |
| 137 | +# prevent browser autofetched favicon request from going to GPT |
| 138 | +@app.route("/favicon.ico") |
| 139 | +def favicon(): |
| 140 | + return ("No favicon", 404) |
| 141 | + |
| 142 | + |
| 143 | +@app.route("/test-frame") |
| 144 | +def test_frame(): |
| 145 | + return render_template("frame-test.html") |
| 146 | + |
| 147 | + |
| 148 | +### |
| 149 | + |
| 150 | +from gpt_http import gpt_hallucinate |
| 151 | + |
| 152 | +gpt_hallucinate(app, get_gbls=lambda: globals()) |
| 153 | + |
| 154 | +if __name__ == "__main__": |
| 155 | + app.run(debug=True, host="0.0.0.0", port=7500) |
0 commit comments