-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
195 lines (187 loc) · 6.49 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import os
from flask import Flask, render_template, request, url_for, redirect,session,Response
from flask_sqlalchemy import SQLAlchemy
from flask_session import Session
from sqlalchemy.sql import func
from werkzeug.utils import secure_filename
from werkzeug.datastructures import FileStorage
import base64
basedir = os.path.abspath(os.path.dirname(__file__))
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
db = SQLAlchemy(app)
class products(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80))
description = db.Column(db.String(120))
sizes = db.Column(db.String(80))
price=db.Column(db.Integer)
colors = db.Column(db.String(80))
image=db.Column(db.LargeBinary)
quantity=db.Column(db.Integer)
pv= db.Column(db.String(80))
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(80))
name = db.Column(db.String(120))
password = db.Column(db.String(80))
class Card(db.Model):
id = db.Column(db.Integer, primary_key=True)
pid = db.Column(db.Integer)
email=db.Column(db.Integer)
name = db.Column(db.String(80))
description = db.Column(db.String(120))
sizes = db.Column(db.String(80))
price=db.Column(db.Integer)
colors = db.Column(db.String(80))
image=db.Column(db.LargeBinary)
quantity=db.Column(db.Integer)
pv= db.Column(db.String(80))
def main():
print('i ame the fun')
@app.route("/")
def home():
return render_template('index.html')
@app.route("/card")
def card():
my_p=Card.query.filter_by(email=session['email'])
return render_template('card.html',info=my_p)
@app.route("/register")
def reg():
try:
user =session["email"]
if user:
return render_template('base.html',mess='login')
except :
return render_template("register.html")
return render_template('register.html')
@app.route("/shop-bags")
def shop_bags():
list1=products.query.filter_by(pv='bags').all()
return render_template('shop.html' ,products=list1,items='Bags')
@app.route("/shop-tshirts")
def shop_tshirts():
list1=products.query.filter_by(pv='tshirts').all()
return render_template('shop.html' ,products=list1,items='T-Shirts')
@app.route("/shop-hody")
def shop_hody():
list1=products.query.filter_by(pv='hody').all()
return render_template('shop.html' ,products=list1,items='Hody')
@app.route('/<int:t_id>/viwe/', methods=('GET', 'POST'))
def viwe(t_id):
info=products.query.filter_by(id=t_id).first()
lp=products.query.filter_by(pv=info.pv).all()
return render_template('shop-single.html',info=info,lp=lp)
@app.route('/<int:t_id>/viwe/add', methods=('GET', 'POST'))
def adddd(t_id):
info=products.query.filter_by(id=t_id).first()
lp=products.query.filter_by(pv=info.pv).all()
return render_template('shop-single.html',info=info,lp=lp,mess= 'add')
@app.route("/logout" ,methods=[ "POST"])
def logout():
session['email']= None
return redirect('/')
@app.route("/about")
def about():
return render_template('about.html')
@app.route("/contact")
def contact():
return render_template('contact.html')
@app.route("/login")
def LOGIN():
try:
user =session["email"]
if user:
return render_template('base.html',mess='login')
except :
return render_template("login.html")
return render_template('LOGIN.HTML')
@app.route("/log-m",methods=[ "POST"])
def log():
email = request.form['email']
password = request.form['password']
user = User.query.filter_by(email=email, password=password).first()
if user:
# Login successful
session["email"]=email
return redirect(url_for('home'))
session["pn"] = 0
else:
# Login failed
return render_template('login.html', error='Invalid email or password')
@app.route("/sun-m",methods=[ "POST"])
def sunapp():
name = request.form['name']
email = request.form['email']
password = request.form['password']
cpassword = request.form['password_confirmation']
if password == cpassword :
user=User(name=name,email=email,password=password)
db.session.add(user)
db.session.commit()
session["email"]=email
return redirect('/')
else:
return render_template('register.html', error='The password does not match')
# ...
@app.route("/admin")
def admin():
if session['email']=='admin@gmail.com':
return render_template('madmin.html')
return "404"
@app.route("/<int:t_id>/addtocatd")
def addtocatd(t_id):
return redirect('/')
@app.route('/add_product', methods=['GET', 'POST'])
def add_product():
if request.method == 'POST':
print('ok')
# Retrieve form data
name = request.form['product-name']
price = request.form['price']
description = request.form['description']
file = request.files['image']
quantity = request.form['quantity']
category= request.form.get('category')
colors = " ".join(request.form.getlist('colors'))
# Save product to database
product = products(name=name, price=price,pv =category, description=description, colors=colors,image=file.read(), quantity=quantity)
db.session.add(product)
db.session.commit()
image_data = base64.b64encode(product.image).decode('utf-8')
return render_template('add_product.html',mess='Add product',pname=name,image_data=image_data)
else:
return render_template('add_product.html')
@app.route('/p/<int:img_id>/img_pic')
def img(img_id):
img = products.query.get_or_404(img_id)
return Response(img.image, mimetype='image/jpeg')
@app.route("/addtocard/<int:p_id>")
def addtocard(p_id):
yt=products.query.get(p_id)
new_item=Card(
name=yt.name,
pid=yt.id,
email=session['email'],
description=yt.description,
sizes=yt.sizes,
price=yt.price,
colors=yt.colors,
image=yt.image,
quantity=yt.quantity,
pv=yt.pv)
db.session.add(new_item)
db.session.commit()
session["pn"] +=1
print(session["pn"])
return redirect(f'/{p_id}/viwe/add')
@app.route("/Checkout")
def Checkout():
return render_template('Checkout.html')
if __name__ == '__main__':
print('new user')
app.run(port=200,debug=True)