-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
230 lines (189 loc) · 6.65 KB
/
main.py
File metadata and controls
230 lines (189 loc) · 6.65 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
import json
import os
import subprocess
import uuid
from fastapi import FastAPI, Request, UploadFile, Form
from fastapi.responses import JSONResponse
from starlette.responses import HTMLResponse
import db
from Entities.Film import Film, FilmType, FilmFormat
from Entities.FilmRoll import FilmRoll, DevelopmentStatus
from Entities.Picture import Picture
app = FastAPI()
@app.get("/")
async def root():
return HTMLResponse(content="""
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Filmstore</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h1>Filmstore</h1>
<p>Everything should be working</p>
</body>
</html>
""")
@app.get("/api/v1")
async def api_root():
return JSONResponse(status_code=200, content={
"status": 200,
"message": "Everything is working!"
})
@app.get("/api/v1/films")
async def list_films():
try:
films = db.fetch_films()
except Exception as e:
print(e)
return JSONResponse(status_code=500, content={
"status": 500,
"message": str(e)
})
json_films = [film.to_dict() for film in films]
return JSONResponse(status_code=200, content={
"status": 200,
"message": "Successfully fetched films",
"films": json_films
})
@app.get("/api/v1/films/{film_id}")
async def get_film(film_id: int):
try:
film = db.fetch_film(film_id=film_id)
except Exception as e:
print(e)
return JSONResponse(status_code=500, content={
"status": 500,
"message": str(e)
})
return JSONResponse(status_code=200, content={
"status": 200,
"message": "Successfully fetched film",
"film": film.to_dict()
})
@app.get("/api/v1/filmrolls")
async def list_filmrolls():
try:
filmrolls = db.fetch_filmrolls()
except Exception as e:
print(e)
return JSONResponse(status_code=500, content={
"status": 500,
"message": str(e)
})
return JSONResponse(status_code=200, content={
"status": 200,
"message": "Successfully fetched filmrolls",
"filmrolls": [filmroll.to_dict() for filmroll in filmrolls]
})
@app.get("/api/v1/filmrolls/{filmrollid}")
async def get_filmroll(filmrollid: int):
filmroll = db.fetch_filmroll(filmrollid)
return JSONResponse(status_code=200, content={
"status": 200,
"message": "Successfully fetched filmroll",
"id": filmrollid,
"film": filmroll.film.to_dict(),
"pictures": [picture.db_id for picture in filmroll.pictures],
"filmroll_status": filmroll.status,
"camera": filmroll.camera,
"identifier": filmroll.archival_identifier
})
@app.get("/api/v1/pictures/{pictureid}")
async def get_picture(pictureid: int):
return JSONResponse(status_code=200, content={})
@app.post("/api/v1/films")
async def create_film(request: Request):
req_json = await request.json()
film = Film(name=req_json["name"],
iso=req_json["iso"],
development_info=req_json["development_info"],
type=FilmType(req_json["type"]),
format=FilmFormat(req_json["format"]))
try:
db.add_film(film)
except Exception as e:
return JSONResponse(status_code=500, content={
"status": 500,
"message": str(e)
})
return JSONResponse(status_code=200, content={
"status": 200,
"message": "Film created",
"request_json": req_json
})
# Consider switch to base64 in json for simplicity reasons
@app.post("/api/v1/pictures")
async def upload_picture(file: UploadFile, req: str = Form()):
req_json = json.loads(req)
if file.content_type == 'image/jpeg' or file.content_type == 'image/png':
# Run compression script
filename = f"{uuid.uuid4().hex}."
filename += "jpeg" if file.content_type == 'image/jpeg' else "png"
with open(".temp/" + filename, 'wb') as f:
f.write(await file.read())
subprocess.run(
["./scripts/img.sh", f".temp/{filename}", f"./pictures/{filename}"]
)
else: # TODO)) ADD FILM FORMAT
return JSONResponse(status_code=400,
content={
"status": 400,
"message": "Only JPEG and PNG are supported"
})
picture = Picture(thumbnail=filename,
description=req_json["description"],
location=req_json["location"],
aperture=req_json["aperture"],
shutter_speed=req_json["shutter_speed"],
posted=req_json["posted"],
printed=req_json["printed"])
try:
insert_id = db.add_picture(picture)
except Exception as e:
return JSONResponse(status_code=500, content={
"status": 500,
"message": str(e)
})
return JSONResponse(status_code=200, content={
"status": 200,
"message": "Picture stored successfully!",
"picture_id": insert_id
})
@app.post("/api/v1/filmrolls")
async def add_filmroll(request: Request):
req_json = await request.json()
pictures = [Picture(thumbnail="", db_id=id) for id in req_json["pictures"]]
film = Film(db_id=req_json["film"], name="", iso=0, development_info="", type=FilmType.UNDEFINED)
filmroll = FilmRoll(camera=req_json["camera"],
archival_identifier=req_json["identifier"],
status= DevelopmentStatus(req_json["status"]),
pictures=pictures,
film=film)
try:
id = db.add_filmroll(filmroll)
except Exception as e:
return JSONResponse(status_code=500, content={
"status": 500,
"message": str(e)
})
return JSONResponse(status_code=200, content={
"status": 200,
"message": "Filmroll added",
"filmroll_id": id
})
@app.post("/api/v1/films/{id}")
async def update_film(request: Request, id: int):
pass
@app.delete("/api/v1/films/{id}")
async def delete_film(id: int):
resp_json = db.fetch_film(id).to_dict()
pictures_to_delete, deleted_films = db.delete_film_stock(id)
for picture in pictures_to_delete:
os.remove(f"./pictures/{picture}")
return JSONResponse(status_code=200, content={
"status": 200,
"message": f"Film deleted successfully along {deleted_films} film rolls and {len(pictures_to_delete)} pictures",
})