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