-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmain.py
361 lines (283 loc) · 12.1 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
from fastapi import (FastAPI, status, Request,
HTTPException, Depends, Query)
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
# database
from tortoise.contrib.fastapi import register_tortoise
from models import (User, Business, Product,
user_pydantic, user_pydanticIn, user_pydanticOut,
business_pydantic, business_pydanticIn,
product_pydantic, product_pydanticIn)
from datetime import datetime
# authentication
from authentication import (get_hashed_password,
very_token, very_token_email,
is_not_email, token_generator)
from fastapi.security import (OAuth2PasswordBearer, OAuth2PasswordRequestForm)
# signal
from tortoise.signals import post_save
from tortoise import BaseDBAsyncClient
from typing import List, Optional, Type
# email
from emails import send_mail
# images
from fastapi import File, UploadFile
from fastapi.staticfiles import StaticFiles
from PIL import Image
import secrets
# env file
from config import get_settings
SITE_URL = get_settings().SITE_URL
app = FastAPI(title="E-commerce API", version="0.1.1",
description=" E-commerce API created with FastAPI and jwt Authenticated")
oauth_scheme = OAuth2PasswordBearer(tokenUrl="token")
# static file setup config
app.mount("/static", StaticFiles(directory="static"), name="static")
@app.post("/token", tags=["User"])
async def generate_token(request_form: OAuth2PasswordRequestForm = Depends()):
token = await token_generator(request_form.username, request_form.password)
return {"access_token": token, "token_type": "bearer"}
async def get_current_user(token: str = Depends(oauth_scheme)):
return await very_token(token)
@app.post("/users/me", tags=["User"])
async def client_data(user: user_pydanticIn = Depends(get_current_user)):
business = await Business.get(owner=user)
logo = business.logo
logo = f'{SITE_URL}{logo}'
return {
"status": "ok",
"data": {
"username": user.username,
"email": user.email,
"is_verified": user.is_verified,
"join_date": user.join_date.strftime("%b %d %Y"),
"logo": logo,
"business": await business_pydantic.from_tortoise_orm(business)
}
}
@app.get("/users/", tags=["User"], response_model=List[user_pydanticOut])
async def get_users(user: user_pydanticIn = Depends(get_current_user),
limit: int = Query(100, le=100),
skip: int = Query(0, ge=0)
):
users = await User.filter(id__gt=skip, id__lte=skip+limit)
return users
@post_save(User)
async def create_business(
sender: "Type[User]",
instance: User,
created: bool,
using_db: "Optional[BaseDBAsyncClient]",
update_fields: List[str]) -> None:
if created:
business_obj = await Business.create(
business_name=instance.username,
owner=instance)
await business_pydantic.from_tortoise_orm(business_obj)
# send the email
await send_mail([instance.email], instance)
@app.put("/business/{id}", response_model=business_pydantic, tags=["Business"])
async def update_business(id: int,
update_business: business_pydanticIn,
user: user_pydantic = Depends(get_current_user)):
business = await Business.get(id=id)
owner = await business.owner
update_business = update_business.dict()
if user == owner:
await business.update_from_dict(update_business)
await business.save()
return await business_pydantic.from_tortoise_orm(business)
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Not authenticated to perform this action",
headers={"WWW-Authenticate": "Bearer"}
)
@app.post("/users/", tags=["User"], status_code=status.HTTP_201_CREATED, response_model=user_pydanticOut)
async def user_registration(user: user_pydanticIn):
user_info = user.dict(exclude_unset=True)
# This is a bad way to do it:
# TODO fix this, create custom 'user_pydanticIn for validate data '
if len(user_info["password"]) < 8:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST, detail="Password must be longer than 8 characters")
if len(user_info["username"]) < 5:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST, detail="Username must be longer than 5 characters")
if is_not_email(user_info["email"]):
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST, detail="This is not a valid email")
if await User.exists(username=user_info.get("username")):
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST, detail="Username already exists")
if await User.exists(email=user_info.get("email")):
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST, detail="Email already exists")
user_info["password"] = get_hashed_password(user_info["password"])
user_obj = await User.create(**user_info)
user_new = await user_pydanticOut.from_tortoise_orm(user_obj)
return user_new
template = Jinja2Templates(directory="templates")
@app.get("/verification/email", response_class=HTMLResponse, tags=["User"])
async def email_verification(request: Request, token: str):
user = await very_token_email(token)
if user:
if not user.is_verified:
user.is_verified = True
await user.save()
context = {
"request": request,
"is_verified": user.is_verified,
"username": user.username
}
return template.TemplateResponse("verification.html", context)
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid Token or expired token",
headers={"WWW-Authenticate": "Bearer"}
)
@app.post("/uploadfile/profile", tags=["User"])
async def upload_profile_image(file: UploadFile = File(..., max_lenght=10485760),
user: user_pydantic = Depends(get_current_user)):
FILEPATH = "./static/images/"
file_name = file.filename
try:
extension = file_name.split(".")[1]
finally:
if extension not in ["png", "jpg", "jpeg"]:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
detail="File extension not allowed")
token_name = "logo" + secrets.token_hex(10) + "." + extension
generated_name = FILEPATH + token_name
file_content = await file.read()
with open(generated_name, "wb") as f:
f.write(file_content)
# PILLOW
img = Image.open(generated_name)
img = img.resize(size=(200, 200))
img.save(generated_name)
business = await Business.get(owner=user)
owner = await business.owner
if owner == user:
business.logo = generated_name[1:]
await business.save()
return await business_pydantic.from_tortoise_orm(business)
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Not authenticated to perform this action",
headers={"WWW-Authenticate": "Bearer"}
)
@app.post("/uploadfile/product/{id}", tags=["Product"])
async def upload_product_image(
id: int,
file: UploadFile = File(..., max_lenght=10485760),
user: user_pydantic = Depends(get_current_user)):
FILEPATH = "./static/images/"
file_name = file.filename
try:
extension = file_name.split(".")[1]
finally:
if extension not in ["png", "jpg", "jpeg"]:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
detail="File extension not allowed")
token_name = "product" + secrets.token_hex(10) + "." + extension
generated_name = FILEPATH + token_name
file_content = await file.read()
with open(generated_name, "wb") as f:
f.write(file_content)
product = await Product.get_or_none(id=id)
if product:
business = await product.business
owner = await business.owner
else:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Product Not Found"
)
if owner == user:
product.product_image = generated_name[1:]
await product.save()
return await product_pydantic.from_tortoise_orm(product)
else:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Not authenticated to perform this action",
headers={"WWW-Authenticate": "Bearer"}
)
@app.post("/products/", tags=["Product"], response_model=product_pydantic)
async def add_new_product(product: product_pydanticIn,
user: user_pydantic = Depends(get_current_user)):
product = product.dict(exclude_unset=True)
if product["original_price"] > 0:
product["percentage_discount"] = (
(product["original_price"] - product["new_price"]) / product["original_price"]) * 100
product_obj = await Product.create(**product, business=user)
product_obj = await product_pydantic.from_tortoise_orm(product_obj)
return product_obj
else:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
detail="The original price must be greater than 0")
@app.delete("/products/{id}", tags=["Product"], status_code=status.HTTP_204_NO_CONTENT)
async def delete_product(id: int, user: user_pydantic = Depends(get_current_user)):
product = await Product.get(id=id)
business = await product.business
owner = await business.owner
if user == owner:
await product.delete()
return
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Not authenticated to perform this action",
headers={"WWW-Authenticate": "Bearer"}
)
@app.put("/products/{id}", tags=["Product"])
async def update_product(id: int,
updated_product: product_pydanticIn,
user: user_pydantic = Depends(get_current_user)):
product = await Product.get(id=id)
business = await product.business
owner = await business.owner
updated_product = updated_product.dict(exclude_unset=True)
updated_product["percentage_discount"] = (
(updated_product["original_price"] - updated_product["new_price"]) / updated_product["original_price"]) * 100
if user == owner and updated_product["original_price"] > 0:
await product.update_from_dict(updated_product)
await product.save()
return await product_pydantic.from_tortoise_orm(product)
raise HTTPException(
status_code=status.HTTP_400_UNAUTHORIZED,
detail="Not authenticated to perform this action or Invalid user input",
headers={"WWW-Authenticate": "Bearer"}
)
@app.get("/products", tags=["Product"], response_model=List[product_pydantic])
async def get_product_list(limit: int = Query(100, le=100),
skip: int = Query(0, ge=0)):
response = await product_pydantic.from_queryset(Product.filter(id__gt=skip, id__lte=skip+limit))
return response
@app.get("/products/{id}", tags=["Product"])
async def get_product_detail(id: int):
product = await Product.get(id=id)
business = await product.business
owner = await business.owner
response = await product_pydantic.from_queryset_single(Product.get(id=id))
response = response.dict()
response["product_image"] = f'{SITE_URL}{response["product_image"]}'
return {
"product_details": response,
"business_detaild": {
"name": business.business_name,
"city": business.city,
"region": business.region,
"description": business.business_description,
"logo": f'{SITE_URL}{business.logo}',
"owner_id": owner.id,
"email": owner.email,
"join_date": owner.join_date.strftime("%b %d %Y")
}
}
register_tortoise(
app,
db_url="sqlite://db.sqlite3",
modules={"models": ["models"]},
generate_schemas=True,
add_exception_handlers=True
)