-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
279 lines (230 loc) · 7.3 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
from flask import Flask, render_template, url_for, request, redirect
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
from flask import jsonify
from flask import json
import random
# from flask_cors import CORS
from flask_cors import CORS, cross_origin
app = Flask(__name__)
cors = CORS(app)
# CORS(app)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
db = SQLAlchemy(app)
class Todo(db.Model):
id = db.Column(db.Integer, primary_key=True)
content = db.Column(db.String(200), nullable=False)
date_created = db.Column(db.DateTime, default=datetime.utcnow)
def __repr__(self):
return '<Task %r>' % self.id
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
FName = db.Column(db.String(200), nullable=False)
LName = db.Column(db.String(200), nullable=False)
PName = db.Column(db.String(200), nullable=False)
PType = db.Column(db.String(200), nullable=False)
Email = db.Column(db.String(200), nullable=False)
Password = db.Column(db.String(200), nullable=False)
# content = db.Column(db.String(200), nullable=False)
date_created = db.Column(db.DateTime, default=datetime.utcnow)
def __repr__(self):
return '<User %r>' % self.id
@app.route('/', methods=['POST', 'GET'])
def index():
# return "Hello World, The Code is working"
if request.method == 'POST':
task_content = request.form['content']
new_task = Todo(content=task_content)
try:
db.session.add(new_task)
db.session.commit()
return redirect('/')
except:
return 'There was an issue adding your task'
else:
tasks = Todo.query.order_by(Todo.date_created).all()
return render_template('index.html', tasks=tasks)
@app.route('/delete/<int:id>')
def delete(id):
task_to_delete = Todo.query.get_or_404(id)
try:
db.session.delete(task_to_delete)
db.session.commit()
return redirect('/')
except:
return 'There was a problem deleting that task'
@app.route('/update/<int:id>', methods=['GET', 'POST'])
def update(id):
task = Todo.query.get_or_404(id)
if request.method == 'POST':
task.content = request.form['content']
try:
db.session.commit()
return redirect('/')
except:
return 'There was an issue updating your task'
else:
return render_template('update.html', task=task)
@app.route('/banners')
def banner():
# return render_template('banners.json')
data=open("banners.json")
dataj=json.load(data)
response = app.response_class(
response=json.dumps(dataj),
status=200,
mimetype='application/json; charset=utf-8'
)
return response
print(type(json.dumps(dataj)))
return json.dumps(dataj)
@app.route('/categories')
def categories():
# return render_template('categories.json')
data=open("categories.json")
dataj=json.load(data)
response = app.response_class(
response=json.dumps(dataj),
status=200,
mimetype='application/json; charset=utf-8'
)
return response
@app.route('/products')
def products():
data=open("products.json")
dataj=json.load(data)
response = app.response_class(
response=json.dumps(dataj),
status=200,
mimetype='application/json; charset=utf-8'
)
return response
# return jsonify(json.load(open("products.json
@app.route('/recommendations')
def recommend():
data=open("products.json")
dataj=json.load(data)
response = app.response_class(
response=json.dumps(dataj),
status=200,
mimetype='application/json; charset=utf-8'
)
return response
# return jsonify(json.load(open("products.json")))
@app.route('/recommendations/<string:pt>')
def recommend_user(pt):
# print(id) Pet Name
data=open("products.json")
dataj=json.load(data)
# print(dataj[0]["name"])
# User_id = User.query.get_or_404(pt)
# animal=User_id.PType
# print(type(dataj))
l=list()
cat=["CATS","CAT","NEKOCHAN"]
dog=["DOGS","DOG","INU"]
pt=pt.upper()
if pt in cat:
pt="CAT"
# pts="CATS"
elif pt in dog:
pt="DOG"
# pts="DOGS"
else:
pt
for i in dataj:
s=i["name"].upper().split()
if (pt in s) :
# print(i["name"])
# print("*"*50)
l.append(i)
# print(l)
random.shuffle(l)
response = app.response_class(
response=json.dumps(l),
status=200,
mimetype='application/json; charset=utf-8'
)
return response
# return jsonify(json.load(open("products.json")))
@app.route('/user/order', methods=['POST', 'GET'])
def User_Checkout():
dataj={'Data':"Data"}
response = app.response_class(
response=json.dumps(dataj),
# response="",
status=200,
mimetype='application/json; charset=utf-8'
)
return response
@app.route('/user', methods=['POST', 'GET'])
def User_Interaction():
# return "Hello World, The Code is working"
if request.method == 'POST':
# print("Inside post")
fn = request.form['fn']
ln = request.form['ln']
pn = request.form['pn']
pt = request.form['pt']
em = request.form['em']
ps = request.form['ps']
new_user = User(FName=fn,
LName=ln,
PName=pn,
PType=pt,
Email=em,
Password=ps,
)
try:
db.session.add(new_user)
db.session.commit()
# print("Done")
return redirect('/user')
except:
return 'There was an issue adding your User'
else:
users = User.query.all()
# print(users)
return render_template('uindex.html', users=users)
@app.route('/user/delete/<int:id>')
def user_delete(id):
task_to_delete = User.query.get_or_404(id)
try:
db.session.delete(task_to_delete)
db.session.commit()
return redirect('/user')
except:
return 'There was a problem deleting that task'
@app.route('/user/pass/<string:email>')
def user_pasword(email):
task_to_delete = User.query.filter_by(Email=email).first()
if task_to_delete==None:
return "Wrong Username or Password"
return task_to_delete.Password
@app.route('/user/update/<int:id>', methods=['GET', 'POST'])
def user_update(id):
task = User.query.get_or_404(id)
if request.method == 'POST':
task.content = request.form['FName']
try:
db.session.commit()
return redirect('/user')
except:
return 'There was an issue updating your task'
else:
return render_template('update.html', task=task)
@app.route('/user/session')
def session():
print(session["email"])
if __name__ == "__main__":
app.run(debug=True)
# {
# "name": "Recommendations especially for you ! ",
# "key": "fruit-and-veg",
# "description": "The best place out there for recommendations and pet supplies catered for your needs.",
# "enabled": true,
# "order": 6,
# "imageUrl": "/static/images/category/recommendation.png",
# "id": "5b6899953d1a866534f516e4"
# }
# ]