-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
444 lines (357 loc) · 12.9 KB
/
main.py
File metadata and controls
444 lines (357 loc) · 12.9 KB
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
from flask import Flask, request, Response
from flask_sqlalchemy import SQLAlchemy
from werkzeug.security import generate_password_hash, check_password_hash
import json, logging, os, stripe
from flask_jwt_extended import (
create_access_token,
get_jwt_identity,
jwt_required,
JWTManager,
create_refresh_token,
)
from datetime import timedelta, datetime
from dotenv import load_dotenv
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)s %(message)s",
handlers=[
logging.FileHandler("ecommerce_error.log"),
logging.StreamHandler(),
],
)
load_dotenv(".env")
stripe.api_key = os.getenv("SECRET_KEY")
app = Flask(__name__)
app.config["TESTING"] = False
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///data.db"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.config["JWT_TOKEN_LOCATION"] = ["headers"]
app.config["JWT_SECRET_KEY"] = "supersecretkey"
app.config["JWT_ACCESS_TOKEN_EXPIRES"] = timedelta(days=30)
db = SQLAlchemy(app)
jwt = JWTManager(app)
class Users(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(250), nullable=False)
password = db.Column(db.String(250), nullable=False)
email = db.Column(db.String(250), unique=True, nullable=False)
is_admin = db.Column(db.Boolean, default=False)
cart = db.relationship("Cart", backref="user", lazy=True)
class Products(db.Model):
id = db.Column(db.Integer, primary_key=True)
product_name = db.Column(db.String(1500))
amount = db.Column(db.Integer)
price = db.Column(db.Float)
currency = db.Column(db.String(10))
createdAt = db.Column(db.DateTime, default=datetime.now)
updatedAt = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)
class Cart(db.Model):
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey("users.id"), nullable=False)
product_id = db.Column(db.Integer, db.ForeignKey("products.id"), nullable=False)
quantity = db.Column(db.Integer, nullable=False)
product = db.relationship("Products", backref="cart_items", lazy=True)
class Payment(db.Model):
id = db.Column(db.Integer, primary_key=True)
product_id = db.Column(db.Integer, db.ForeignKey("products.id"))
user_id = db.Column(db.Integer, db.ForeignKey("users.id"), nullable=False)
total = db.Column(db.Float)
currency = db.Column(db.String(10))
LOW_STOCK = 2
def check_stock(product):
if product.amount <= LOW_STOCK:
logging.warning(
f"Low Stock: {product.product_name} "
f"Remaining: {product.amount}"
)
def json_response(data, status=200):
return Response(
json.dumps(data, indent=4),
status=status,
mimetype="application/json",
)
@app.route("/register", methods=["POST"])
def user_registration():
data = request.get_json()
username = data.get("username")
password = data.get("password")
email = data.get("email")
if not all([username, password, email]):
return json_response({"message": "Missing required fields"}, 400)
if Users.query.filter_by(username=username).first():
return json_response({"message": "Username registered"}, 409)
hashed_password = generate_password_hash(password, method="pbkdf2:sha256")
new_user = Users(username=username, password=hashed_password, email=email)
db.session.add(new_user)
db.session.commit()
token = create_access_token(identity=str(new_user.id))
refresh_token = create_refresh_token(identity=str(new_user.id))
return json_response({"token": token, "refresh_token": refresh_token})
@app.route("/login", methods=["POST"])
def login():
data = request.get_json()
user = Users.query.filter_by(username=data.get("username")).first()
if user and check_password_hash(user.password, data.get("password")):
token = create_access_token(identity=str(user.id))
return json_response({"token": token})
return json_response({"message": "Invalid login details"}, 401)
@app.route("/logout")
@jwt_required()
def logout():
return json_response({"message": "Successfully logged out"})
@app.route("/refresh", methods=["POST"])
@jwt_required(refresh=True)
def refresh():
return json_response(
{"access_token": create_access_token(identity=int(get_jwt_identity()))}
)
@app.route("/cart", methods=["POST"])
@jwt_required()
def add_products():
data = request.get_json()
if not all([data.get("product_id"), data.get("quantity")]):
return json_response({"message": "Missing required fields"}, 400)
db.session.add(
Cart(
user_id=int(get_jwt_identity()),
product_id=data["product_id"],
quantity=data["quantity"],
)
)
db.session.commit()
return json_response({"message": "Product added successfully"})
@app.route("/cart/<id>", methods=["DELETE"])
@jwt_required()
def remove_product(id):
product = Cart.query.get(id)
if not product or product.user_id != int(get_jwt_identity()):
return json_response({"message": "Product not found"}, 404)
db.session.delete(product)
db.session.commit()
return json_response({"message": "Product removed successfully"})
@app.route("/products", methods=["GET"])
def list_products():
search = request.args.get("search", "").strip()
query = Products.query
if search:
query = query.filter(Products.product_name.ilike(f"%{search}%"))
return json_response(
[
{
"id": p.id,
"Name": p.product_name,
"Price": p.price,
"Currency": p.currency,
"In-Stock": p.amount > 0,
}
for p in query.all()
]
)
@app.route("/carts/final", methods=["GET"])
@jwt_required()
def final_cart():
cart_items = Cart.query.filter_by(user_id=int(get_jwt_identity())).all()
if not cart_items:
return json_response({"message": "Cart is empty"}, 400)
items = []
total = 0.0
for item in cart_items:
subtotal = item.quantity * item.product.price
total += subtotal
items.append(
{
"product_id": item.product.id,
"product_name": item.product.product_name,
"quantity": item.quantity,
"price_per_item": item.product.price,
"total": round(subtotal, 2),
}
)
return json_response({"items": items, "total": round(total, 2)})
@app.route("/checkout", methods=["POST"])
@jwt_required()
def checkout_pay():
cart_items = Cart.query.filter_by(user_id=int(get_jwt_identity())).all()
if not cart_items:
return json_response({"message": "Cart is empty"}, 400)
line_items = []
for item in cart_items:
product = item.product
if product.amount < item.quantity:
return json_response(
{"message": f"Insufficient stock for {product.product_name}"},
400,
)
line_items.append(
{
"price_data": {
"currency": product.currency.lower(),
"product_data": {"name": product.product_name},
"unit_amount": int(product.price * 100),
},
"quantity": item.quantity,
}
)
session = stripe.checkout.Session.create(
payment_method_types=["card"],
line_items=line_items,
mode="payment",
success_url="https://127.0.0.1:5000/success",
cancel_url="https://127.0.0.1:5000/cancel",
metadata={"user_id": int(get_jwt_identity())},
)
return json_response({"checkout_url": session.url})
@app.route("/stripe/webhook", methods=["POST"])
def stripe_webhook():
payload = request.data
sig_header = request.headers.get("Stripe-Signature")
try:
event = stripe.Webhook.construct_event(
payload, sig_header, os.environ["STRIPE_WEBHOOK_SECRET"]
)
except Exception as e:
logging.error(f"Webhook error: {e}")
return "", 400
if event["type"] != "checkout.session.completed":
return "", 200
session = event["data"]["object"]
user_id = int(session["metadata"]["user_id"])
cart_items = Cart.query.filter_by(user_id=user_id).all()
total = 0.0
for item in cart_items:
product = item.product
product.amount -= item.quantity
total += item.quantity * product.price
check_stock(product)
db.session.add(
Payment(
user_id=user_id,
total=total,
currency=cart_items[0].product.currency,
)
)
Cart.query.filter_by(user_id=user_id).delete()
db.session.commit()
return "", 200
def admin_required():
user = Users.query.get(int(get_jwt_identity()))
return bool(user and user.is_admin)
# --------------------
# Admin Section
# --------------------
@app.route('/admin/products', methods=['POST'])
@jwt_required()
def add_product():
if not admin_required():
return json_response({"message": "Admin access required"}, 403)
data = request.get_json()
try:
new_product = Products(
product_name=data['product_name'],
amount=data['amount'],
price=data['price'],
currency=data.get('currency', 'DKK')
)
db.session.add(new_product)
db.session.commit()
return json_response({
'id': new_product.id,
'Product Name': new_product.product_name,
'Amount': new_product.amount,
'Price': new_product.price,
'Currency': new_product.currency,
'createdAt': new_product.createdAt.isoformat(),
'updatedAt': new_product.updatedAt.isoformat()
})
except Exception as e:
logging.error(f"Error adding product: {e}")
db.session.rollback()
return json_response(
{'error': 'Product could not be created', 'details': str(e)},
500
)
@app.route('/admin/products', methods=['GET'])
@jwt_required()
def check_inventory():
if not admin_required():
return json_response({"message": "Admin access required"}, 403)
return json_response([
{
'id': p.id,
'Product Name': p.product_name,
'Amount': p.amount,
'Price': p.price,
'Currency': p.currency,
'createdAt': p.createdAt.isoformat(),
'updatedAt': p.updatedAt.isoformat()
}
for p in Products.query.all()
])
@app.route('/admin/low-stock', methods=['GET'])
@jwt_required()
def low_inventory():
if not admin_required():
return json_response({"message": "Admin access required"}, 403)
return json_response([
{
'id': p.id,
'Product Name': p.product_name,
'Amount': p.amount
}
for p in Products.query.filter(Products.amount <= LOW_STOCK).all()
])
@app.route('/admin/products/<id>', methods=['PUT'])
@jwt_required()
def update_product(id):
if not admin_required():
return json_response({"message": "Admin access required"}, 403)
product = Products.query.get(id)
if not product:
return json_response({"message": "Product not found"}, 404)
data = request.get_json()
try:
product.product_name = data.get('product_name', product.product_name)
product.amount = data.get('amount', product.amount)
product.price = data.get('price', product.price)
product.currency = data.get('currency', product.currency)
db.session.commit()
check_stock(product)
return json_response({
'id': product.id,
'Product Name': product.product_name,
'Amount': product.amount,
'Price': product.price,
'Currency': product.currency,
'createdAt': product.createdAt.isoformat(),
'updatedAt': product.updatedAt.isoformat()
})
except Exception as e:
logging.error(f"Error updating product: {e}")
db.session.rollback()
return json_response(
{'error': 'Product could not be updated', 'details': str(e)},
500
)
@app.route('/admin/products/<id>', methods=['DELETE'])
@jwt_required()
def delete_product(id):
if not admin_required():
return json_response({"message": "Admin access required"}, 403)
product = Products.query.get(id)
if not product:
return json_response({"message": "Product not found"}, 404)
try:
db.session.delete(product)
db.session.commit()
return '', 200
except Exception as e:
logging.error(f"Error deleting product: {e}")
db.session.rollback()
return json_response(
{'error': 'Product could not be deleted', 'details': str(e)},
500
)
if __name__ == "__main__":
with app.app_context():
db.create_all()
app.run(debug=True)