forked from evride/repables-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
267 lines (227 loc) · 8.84 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
from typing import Optional
import subprocess
from fastapi import FastAPI, UploadFile, Depends, File, Form, Request
import peewee
import aiofiles
import time
from datetime import datetime
from PIL import Image
from playhouse.shortcuts import *
from pydantic import BaseModel
from fastapi.middleware.cors import CORSMiddleware
from fastapi_mail import FastMail, MessageSchema, ConnectionConfig
from config import DATABASE
from models import User, Item, ItemRevision, ItemUpload, PasswordReset, ItemView
from utils import create_access_token, get_password_hash, verify_password, require_current_user, reset_password_key
app = FastAPI()
origins = [
"https://api.repables.com",
"https://dev.repables.com",
"http://dev.repables.com:3000"
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
class Login(BaseModel):
username_or_email: str
password: str
class ModifyUser(BaseModel):
email: str
username: str
password: str
class ResetKey(BaseModel):
reset_key: str
class ResetPasswordCreds(BaseModel):
email: str
class UserResponse(BaseModel):
id: int
biography: Optional[str]
birthdate: Optional[str]
company: Optional[str]
created_at: datetime
display_birthday: bool
email: Optional[str]
fullname: Optional[str]
location: Optional[str]
username: str
website: Optional[str]
class MeResponse(BaseModel):
id: int
biography: Optional[str]
birthdate: Optional[str]
company: Optional[str]
created_at: datetime
display_birthday: bool
email: str
email_public: bool
fullname: Optional[str]
hide_inappropriate: bool
location: Optional[str]
username: str
website: Optional[str]
class ModifyItem(BaseModel):
name: str
description: str
instructions: str
license: str
version: str
class ModifyProfile(BaseModel):
fullname: str
location: str
company: str
biography: str
class RevisionUpload(BaseModel):
item_revision_id: int
@app.get("/items/{item_id}")
async def read_item(item_id: int):
item = model_to_dict(Item.select().where(Item.id == item_id).get())
return item
@app.post("/items/{item_id}/view")
async def track_item_view(item_id: int, request: Request):
# current_user: User = await get_current_user()
# print(current_user)
item = Item.select().where(Item.id == item_id).get()
if item:
ItemView.create(item_id = item_id, revision_id = item.revision_id, remote_address = request.client.host)
return {'success' : 'true'}
return {'error' : 'Not Found'}
@app.get("/items/")
async def read_items():
items = Item.select()
items = [model_to_dict(item) for item in items]
return items
@app.get('/my-endpoint')
async def my_endpoint(request: Request):
ip = request.client.host
return request.client
return {'ip': ip, 'message': 'ok'}
@app.post("/item-revision")
async def create_item(current_user: User = Depends(require_current_user)):
i = ItemRevision.create( user_id=current_user.id)
return model_to_dict(i)
@app.post("/item-revision/{item_revision_id}/publish")
async def publish_item( item_revision_id: int, current_user: User = Depends(require_current_user)):
item_revision = ItemRevision.select().where(ItemRevision.id == item_revision_id).where(ItemRevision.user_id == current_user.id).get()
existing = Item.select().where(Item.user_id == current_user.id).where(Item.revision_id == item_revision_id).get()
if (existing):
print(existing)
else:
i = Item.create(user_id = current_user.id, revision_id = item_revision_id, name = item_revision.name, description = item_revision.description, instructions = item_revision.instructions, license = item_revision.license, version = item_revision.version ) #revision id needs to be included
return model_to_dict(i)
# @app.post("/user")
# async def create_user(user: ModifyUser):
# try:
# u = User.create(email = user.email, username = user.username, password = get_password_hash(user.password))
# except:
# return { "error" : "User already exist"}
# return model_to_dict(u)
@app.delete("/item/{item_id}")
async def delete_item(item_id: int, current_user: User = Depends(require_current_user)):
i = Item.select().where(Item.id == item_id).where(User.id == user_id).get()
return i.delete_instance()
@app.post("/item-revision/{item_revision_id}")
async def update_item(item_revision_id : int, item: ModifyItem, current_user: User = Depends(require_current_user)):
i = ItemRevision.select().where(ItemRevision.id == item_revision_id).where(ItemRevision.user_id == current_user.id).get()
if(i):
i.name = item.name
i.description = item.description
i.license = item.license
i.version = item.version
i.instructions = item.instructions
i.save()
return model_to_dict(i)
else:
return {'error' : 'Not Found'}
@app.get('/user/{user_id}', response_model = UserResponse)
async def read_user(user_id: int):
u = User.select().where(User.id == user_id).get()
if(u.email_public == False):
u.email = None
return model_to_dict(u)
@app.get("/users/")
async def read_users():
users = User.select()
users = [model_to_dict(user) for user in users]
return users
@app.post("/user")
async def create_user(user: ModifyUser):
try:
u = User.create(email = user.email, username = user.username, password = get_password_hash(user.password))
except:
return { "error" : "User already exist"}
return model_to_dict(u)
@app.put("/user")
async def update_user(user: ModifyProfile, current_user: User = Depends(require_current_user)):
u = User.select().where(User.id == current_user.id).get()
if(u):
u.fullname = user.fullname
u.location = user.location
u.company = user.company
u.biography = user.biography
u.save()
return model_to_dict(u)
else:
return {'error' : 'Not Found'}
@app.get("/me", response_model = MeResponse)
async def read_current_user(current_user: User = Depends(require_current_user)):
return model_to_dict(current_user)
@app.post("/file/{revision_id}")
async def create_file(revision_id : str, file: UploadFile = File(...), current_user: User = Depends(require_current_user)):
file_location = "uploaded_files/" + str(time.time())
try:
async with aiofiles.open(file_location, 'wb') as out_file:
content = await file.read() # async read
size = len(content)
await out_file.write(content) # async write
file_name_split = file.filename.split('.')
file_extension = ''
if (len(file_name_split) >= 2):
file_extension = file_name_split[-1]
if (file.content_type == 'image/png'):
with Image.open(file_location) as im:
im.thumbnail((128, 128))
im.save(file_location + '_thumbnail.png', 'PNG')
if (file_extension.lower() == 'stl'):
subprocess.run('chromium --headless --screenshot http://render-model')
item_upload = ItemUpload(uploader=current_user.id, revision_id = revision_id, filename = file.filename, file_location = file_location, size = size, mimetype = file.content_type, file_extension = file_extension )
item_upload.save()
except Exception as e:
print(e)
return model_to_dict(item_upload)
@app.put("/user/{user_id}")
async def edit_user(user_id : int, user: ModifyUser):
u = User.update(user.dict()).where(User.id == user_id).execute()
updated_u = model_to_dict(User.select().where(User.id == user_id).get())
return updated_u
@app.delete("/user/{user_id}")
async def delete_user(user_id : int):
u = User.get(User.id == user_id)
return u.delete_instance()
@app.post('/login')
async def login_user(creds: Login):
u = User.select().where(User.username == creds.username_or_email).get()
user = model_to_dict(u)
if not user:
return { 'error': 'Username or email does not exist.' }
if not await verify_password(creds.password, user['password']):
return { 'error': 'Password is incorrect.' }
if user['id'] >= 1:
token_data = {'id': user['id']}
token = await create_access_token(token_data);
return {'token': token, 'id': user['id'], 'username': user['username']}
else:
return {'error': 'Username or password is incorrect.'}
@app.post('/reset-password')
async def create_reset_password_key(creds: ResetPasswordCreds):
u = User.select().where(User.email == creds.email).get()
if (u):
key = PasswordReset.create(user_id = u.id, reset_key = reset_password_key)
key.save()
#logic for emailing the key to the user goes here
return {'status' :'success'}
else:
return {'error': 'Email not found.'}