-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
480 lines (393 loc) · 18.5 KB
/
main.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
from flask import *
from flask_sqlalchemy import SQLAlchemy
from werkzeug.security import generate_password_hash, check_password_hash
from flask_login import UserMixin, login_user, LoginManager, login_required, current_user, logout_user
import config
import datetime
correct = True
app = Flask(__name__)
app.config['SECRET_KEY'] = config.secret_key
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///orders.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
login_manager = LoginManager()
login_manager.init_app(app)
class Users(UserMixin, db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(250))
email = db.Column(db.String(100), unique=True)
password = db.Column(db.String(100))
api_key = db.Column(db.String(18), unique=True)
plan = db.Column(db.String(100))
class Business(db.Model):
__tablename__ = 'business'
id = db.Column(db.Integer, primary_key=True)
business_name = db.Column(db.String(250), nullable=False)
business_email = db.Column(db.String(100), nullable=False)
business_phone_no = db.Column(db.String(14), nullable=False)
business_website = db.Column(db.String(250))
api_key = db.Column(db.String(18), nullable=False)
def to_dict(self):
dictionary = {}
for column in self.__table__.columns:
dictionary[column.name] = getattr(self, column.name)
return dictionary
class Orders(db.Model):
__tablename__ = 'orders'
id = db.Column(db.Integer, primary_key=True)
api_key = db.Column(db.String(18), nullable=False)
customer_name = db.Column(db.String(250), nullable=False)
customer_address = db.Column(db.String(200), nullable=False)
product_name = db.Column(db.String(250), nullable=False)
quantity = db.Column(db.Integer, nullable=False)
time = db.Column(db.String(250), nullable=False)
business_name = db.Column(db.String(250), nullable=False)
def to_dict(self):
dictionary = {}
for column in self.__table__.columns:
dictionary[column.name] = getattr(self, column.name)
return dictionary
db.create_all()
@login_manager.user_loader
def load_user(user_id):
return Users.query.get(int(user_id))
@app.route('/')
def home():
return render_template('index.html')
@app.route('/login', methods=['GET', 'POST'])
def login():
error = request.args.get('error')
all_users = db.session.query(Users).all()
if request.method == 'POST':
email = request.form.get('email')
password = request.form.get('pass')
verified_user = Users.query.filter_by(email=email).first()
if verified_user in all_users:
checked_password = check_password_hash(verified_user.password, password)
if checked_password:
login_user(verified_user)
user_name = verified_user.name
api_key = verified_user.api_key
return redirect(url_for('dashboard', logged_in=current_user.is_authenticated,
name=user_name, api_key=api_key))
else:
error = 'Incorrect password. Please try again'
return redirect(url_for('login', error=error))
else:
error = 'That email address does not exist. Please try again'
return redirect(url_for('login', error=error))
return render_template('login.html', error=error)
@app.route('/signup', methods=['GET', 'POST'])
def signup():
error = request.args.get('error')
all_users = db.session.query(Users).all()
if request.method == 'POST':
name = request.form.get('name').title()
email = request.form.get('email')
plan = request.form.get('plan').title()
password = request.form.get('pass')
verify_new_user = Users.query.filter_by(email=email).first()
if verify_new_user in all_users:
error = 'That email address is already used. Log in instead?'
return redirect(url_for('login', error=error))
api_key = config.api_key_generator()
for user in all_users:
if user.api_key == api_key:
api_key = config.api_key_generator()
hashed_password = generate_password_hash(password, method='pbkdf2:sha256', salt_length=8)
new_user = Users(name=name, email=email, password=hashed_password, api_key=api_key, plan=plan)
db.session.add(new_user)
db.session.commit()
return redirect(url_for('login', error=error))
return render_template('signup.html')
@app.route('/logout')
def logout():
logout_user()
return redirect(url_for('home'))
@app.route('/dashboard', methods=['GET', 'POST'])
@login_required
def dashboard():
name = request.args.get('name')
api_key = request.args.get('api_key')
verified_businesses = Business.query.filter_by(api_key=api_key).all()
business_names = [business.business_name for business in verified_businesses]
return render_template('dashboard.html', logged_in=current_user.is_authenticated,
current_user=current_user, name=name, api_key=api_key, businesses=verified_businesses, )
@app.route('/docs')
def docs():
return render_template('docs.html')
@app.route('/contact', methods=['GET', 'POST'])
def contact():
return render_template('contact.html')
@app.route('/listings')
@login_required
def listings():
api_key = request.args.get('api_key')
user = Users.query.filter_by(api_key=api_key).first()
business_name = request.args.get('business_name')
verify_api_key = Orders.query.filter_by(api_key=api_key).all()
orders = [order.to_dict() for order in verify_api_key if order.business_name == business_name]
return render_template('order_listings.html', orders=orders, logged_in=current_user.is_authenticated,
name=user.name, api_key=user.api_key)
@app.route('/del_order')
@login_required
def del_order():
business_name = request.args.get('business_name')
api_key = request.args.get('api_key')
order_id = request.args.get('id')
order_to_delete = Orders.query.get(int(order_id))
db.session.delete(order_to_delete)
db.session.commit()
return redirect(url_for('listings', api_key=api_key, business_name=business_name, logged_in=current_user.is_authenticated))
@app.route('/del_business')
@login_required
def del_business():
business_id = request.args.get('id')
api_key = request.args.get('api_key')
business_name = request.args.get('business_name').title()
business_to_delete = Business.query.get(int(business_id))
db.session.delete(business_to_delete)
db.session.commit()
user = Users.query.filter_by(api_key=api_key).first()
verify_order_id = Orders.query.filter_by(api_key=api_key).all()
orders_to_delete = []
for order in verify_order_id:
if order.business_name == business_name:
orders_to_delete.append(order.id)
for orders in orders_to_delete:
o_t_d = Orders.query.get(orders)
db.session.delete(o_t_d)
db.session.commit()
return redirect(url_for('dashboard', api_key=api_key, name=user.name))
@app.route('/business_setup', methods=['GET', 'POST'])
@login_required
def business_reg():
error = request.args.get('error')
if request.method == 'POST':
busines_name = request.form.get('biz_name').title()
business_email = request.form.get('biz_email')
business_website = request.form.get('biz_website')
business_phone_no = request.form.get('biz_phone_no')
api_key = request.args.get('api_key')
verify_business_name = Business.query.filter_by(api_key=api_key).all()
if not verify_business_name:
new_business = Business(business_name=busines_name, business_email=business_email,
business_phone_no=business_phone_no, business_website=business_website,
api_key=api_key)
db.session.add(new_business)
db.session.commit()
return redirect(url_for('order', api_key=api_key, business_name=busines_name, logged_in=current_user.is_authenticated))
else:
all_business_names = []
for business in verify_business_name:
all_business_names.append(business.business_name)
if request.form.get('biz_name').title() in all_business_names:
return jsonify(error="You already have a business with this name")
else:
new_business = Business(business_name=busines_name, business_email=business_email,
business_phone_no=business_phone_no,
business_website=business_website,
api_key=api_key)
db.session.add(new_business)
db.session.commit()
return redirect(url_for('order', api_key=api_key, business_name=busines_name, logged_in=current_user.is_authenticated))
return render_template('business_signup.html', error=error)
@app.route('/order', methods=['GET', 'POST'])
def order():
message = request.args.get('message')
if request.method == 'POST':
api_key = request.args.get('api_key')
business_name = request.args.get('business_name').title()
customer_name = request.form.get('cus_name')
customer_address = request.form.get('address')
product = request.form.get('product_name')
qty = request.form.get('qty')
time = datetime.datetime.now()
verify_api_key = Users.query.filter_by(api_key=api_key).first()
verify_business = Business.query.filter_by(api_key=api_key).all()
business_names = []
for business in verify_business:
business_names.append(business.business_name)
if api_key == verify_api_key.api_key:
if business_name in business_names:
new_order = Orders(api_key=api_key,
customer_name=customer_name,
customer_address=customer_address,
product_name=product,
quantity=qty,
time=time,
business_name=business_name)
db.session.add(new_order)
db.session.commit()
message = 'Your order has been successfully received.'
return redirect(url_for('order', message=message, logged_in=current_user.is_authenticated))
return render_template('order.html', message=message, logged_in=current_user.is_authenticated)
@app.route('/all_orders')
def get_all_orders():
api_key = request.args.get('api-key')
business_name = request.args.get('business_name').title()
verify_api_key = Users.query.filter_by(api_key=api_key).first()
verify_order = Orders.query.filter_by(api_key=api_key).all()
verify_business = Business.query.filter_by(api_key=api_key).all()
cors = []
for business in verify_business:
cors.append(business.business_name)
if not verify_api_key:
return jsonify(error='Invalid API key')
else:
if business_name in cors:
return jsonify(orders=[order.to_dict() for order in verify_order if order.business_name == business_name])
else:
return jsonify(error='Wrong business name')
@app.route('/all_businesses')
def get_all_businesses():
global correct
api_key = request.args.get('api-key')
verified_businesses = Business.query.filter_by(api_key=api_key).all()
verify_api = Users.query.filter_by(api_key=api_key).first()
all_businesses = []
for business in verified_businesses:
all_businesses.append(business.business_name)
try:
if api_key == verify_api.api_key:
correct = True
except AttributeError:
correct = False
if all_businesses == [] and correct == True:
return jsonify(businesses=[business.to_dict() for business in verified_businesses])
if all_businesses != [] and correct == True:
return jsonify(businesses=[business.to_dict() for business in verified_businesses])
if all_businesses != [] and correct == False:
return jsonify(error='Invalid API key')
if all_businesses == [] and correct == False:
return jsonify(error='Invalid API key')
@app.route('/add_business', methods=['GET', 'POST'])
def add_business():
busines_name = request.args.get('business_name').title()
business_email = request.args.get('business_email')
business_phone_no = request.args.get('business_phone_number')
business_website = request.args.get('business_website')
api_key = request.args.get('api-key')
verify_api_key = Users.query.filter_by(api_key=api_key).all()
verify_business_name = Business.query.filter_by(api_key=api_key).all()
if not verify_api_key:
return jsonify(error={'Invalid API key': 'Your API key is incorrect. Please check again'})
else:
if not verify_business_name:
new_business = Business(business_name=busines_name, business_email=business_email,
business_phone_no=business_phone_no, business_website=business_website,
api_key=api_key)
db.session.add(new_business)
db.session.commit()
return jsonify(response={'Success': 'Successfully added new business'})
else:
all_business_names = []
for business in verify_business_name:
all_business_names.append(business.business_name)
if request.args.get('business_name').title() in all_business_names:
return jsonify(error="You already have a business with this name")
else:
new_business = Business(business_name=busines_name, business_email=business_email,
business_phone_no=business_phone_no,
business_website=business_website,
api_key=api_key)
db.session.add(new_business)
db.session.commit()
return jsonify(response={'Success': 'Successfully added new business'})
@app.route('/add_order', methods=['GET', 'POST'])
def add_order():
busines_name = request.args.get('business_name').title()
api_key = request.args.get('api-key')
customer_name = request.args.get('customer_name')
customer_address = request.args.get('customer_address')
product = request.args.get('product_name')
qty = request.args.get('quantity')
time = datetime.datetime.now()
verify_api = Business.query.filter_by(api_key=api_key).all()
verify_user = Users.query.filter_by(api_key=api_key).first()
business_api = []
business_names = []
for business in verify_api:
business_api.append(business.api_key)
business_names.append(business.business_name)
if api_key == verify_user.api_key:
if busines_name in business_names:
new_order = Orders(api_key=api_key,
customer_name=customer_name,
customer_address=customer_address,
product_name=product,
quantity=qty,
time=time,
business_name=busines_name)
db.session.add(new_order)
db.session.commit()
return jsonify(Success='New order has been successfully added.')
else:
return jsonify(error="You don't have any business with that name")
else:
return jsonify(error='Invalid API key')
@app.route('/delete_order', methods=['GET', 'DELETE'])
def delete_order():
order_id = request.args.get('id')
busines_name = request.args.get('business_name').title()
api_key = request.args.get('api-key')
verified_businesses = Orders.query.filter_by(business_name=busines_name).all()
verify_api = Users.query.filter_by(api_key=api_key).first()
all_order_ids = []
all_business_names = []
apis = []
for business in verified_businesses:
all_order_ids.append(business.id)
all_business_names.append(business.business_name)
apis.append(business.api_key)
try:
if api_key in apis and api_key == verify_api.api_key:
if busines_name in all_business_names:
order_id = int(order_id)
if order_id in all_order_ids:
order_to_delete = Orders.query.get(order_id)
db.session.delete(order_to_delete)
db.session.commit()
return jsonify(Success="The order has been successfully deleted")
else:
return jsonify(error="Your business does'nt have an order with that id")
else:
return jsonify(error='Incorrect business name')
else:
return jsonify(error='Invalid API key or non-existing order ID')
except AttributeError:
return jsonify(error='Invalid API key')
@app.route('/delete_business', methods=['GET', 'DELETE'])
def delete_business():
busines_name = request.args.get('business_name').title()
api_key = request.args.get('api-key')
verified_businesses = Business.query.filter_by(api_key=api_key).all()
verify_api = Users.query.filter_by(api_key=api_key).first()
verify_order_id = Orders.query.filter_by(api_key=api_key).all()
business_names = {}
orders_to_delete = []
for business in verified_businesses:
business_names[business.business_name] = business.id
for order in verify_order_id:
if order.business_name == busines_name:
orders_to_delete.append(order.id)
if not verified_businesses:
return jsonify(error="Invalid API key")
else:
if verify_api.api_key == api_key:
if busines_name in business_names.keys():
business_to_delete = Business.query.get(business_names[busines_name])
db.session.delete(business_to_delete)
db.session.commit()
for orders in orders_to_delete:
o_t_d = Orders.query.get(orders)
db.session.delete(o_t_d)
db.session.commit()
return jsonify(Deleted='Business has been successfully deleted')
else:
return jsonify(error="You don't have any business with the given business name in our database")
else:
return jsonify(error='Invalid API key')
if __name__ == '__main__':
app.run(debug=True)