-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
552 lines (372 loc) · 13.6 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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
#!/usr/bin/env python3
import os
import uuid
from datetime import date, datetime
from enum import Enum
from math import ceil
from typing import Any, Dict, List, Optional, TypeVar
from deta import Deta
from deta.base import Base
from fastapi import FastAPI, HTTPException, Query, status
from fastapi.encoders import jsonable_encoder
from passlib.context import CryptContext
from pydantic import BaseModel
from pydantic.errors import NoneIsNotAllowedError
from pydantic.fields import PrivateAttr
try:
from keys import PROJECT_KEY
except:
# When running on CI services.
PROJECT_KEY = os.getenv("DETA_PROJECT_KEY", default="PROJECT_KEY")
HASHED_PASSWORD = "$2b$12$VOGTaA8tXdYoAU4Js6NBXO9uL..rXITV.WMiF/g8MEmCtdoMjLkOK"
pwd_context = CryptContext(schemes=["bcrypt"])
app = FastAPI()
EPOCH = datetime.utcfromtimestamp(0)
#### ---- Datebases ---- ####
deta = Deta(PROJECT_KEY) # no key needed with using Deta Micro
# coffee_bag_db = deta.Base("coffee_bag_db-TEST")
# coffee_use_db = deta.Base("coffee_use_db-TEST")
# meta_db = deta.Base("meta_db-TEST")
coffee_bag_db = deta.Base("coffee_bag_db")
coffee_use_db = deta.Base("coffee_use_db")
meta_db = deta.Base("meta_db")
#### ---- Dates and Times ---- ####
def today_at_midnight() -> datetime:
return datetime.combine(date.today(), datetime.min.time())
def unix_time_millis(dt: datetime = datetime.now()) -> float:
return (dt - EPOCH).total_seconds() * 1000.0
#### ---- Models ---- ####
def make_key() -> str:
return str(uuid.uuid4())
class KeyedModel(BaseModel):
_key: str = PrivateAttr(default_factory=make_key)
class CoffeeBag(KeyedModel):
brand: str
name: str
weight: float = 340.0
start: Optional[date] = date.today()
finish: Optional[date] = None
active: bool = True
def __init__(self, **data):
super().__init__(**data)
key = data.get("key")
if key is not None:
self._key = key
class CoffeeUse(KeyedModel):
bag_id: str
datetime: datetime
_seconds: float = PrivateAttr(0)
def __init__(self, **data):
super().__init__(**data)
self._seconds = unix_time_millis(self.datetime)
key = data.get("key")
if key is not None:
self._key = key
KeyedObjectType = TypeVar("KeyedObjectType", CoffeeBag, CoffeeUse)
#### ---- Database interface helpers ---- ####
def convert_info_to_bag(info: Dict[str, Any]) -> CoffeeBag:
return CoffeeBag(**info)
def convert_bag_to_info(bag: CoffeeBag) -> Dict[str, Any]:
info = jsonable_encoder(bag)
info["key"] = bag._key
return info
def convert_info_to_use(info: Dict[str, Any]) -> CoffeeUse:
return CoffeeUse(**info)
def convert_use_to_info(use: CoffeeUse) -> Dict[str, Any]:
info = jsonable_encoder(use)
info["_seconds"] = use._seconds
info["key"] = use._key
return info
def keyedlist_to_dict(x: List[KeyedObjectType]) -> Dict[str, KeyedObjectType]:
return {y._key: y for y in x}
def get_all_detabase_info(db: Base, n_items: int):
n_buffer = 100
n_pages = ceil(n_items / n_buffer) + 1 # add one just in case
pages = db.fetch(query=None, buffer=n_buffer, pages=n_pages)
info: List[Dict[str, Any]] = []
for page in pages:
info += page
return info
def get_all_coffee_bag_info() -> List[Dict[str, Any]]:
num_bags = num_coffee_bags()
return get_all_detabase_info(coffee_bag_db, n_items=num_bags)
def coffee_bag_list() -> List[CoffeeBag]:
return [convert_info_to_bag(info) for info in get_all_coffee_bag_info()]
def coffee_bag_dict() -> Dict[str, CoffeeBag]:
return {x._key: x for x in coffee_bag_list()}
def get_all_coffee_use_info() -> List[Dict[str, Any]]:
num_uses = num_coffee_uses()
return get_all_detabase_info(coffee_use_db, n_items=num_uses)
def coffee_use_dict() -> Dict[str, CoffeeUse]:
uses = [convert_info_to_use(info) for info in get_all_coffee_use_info()]
return keyedlist_to_dict(uses)
def sort_coffee_bags(bags: List[CoffeeBag]):
def f(b: CoffeeBag) -> date:
if b.start is None:
return date.today()
else:
return b.start
bags.sort(key=f)
return None
#### ---- Meta DB ---- ####
META_DB_KEY = "KEY"
class MetaDataField(str, Enum):
bag_count = "bag_count"
use_count = "use_count"
def initialize_meta_db(bag_count: int = 0, use_count: int = 0):
meta_db.put(
{MetaDataField.bag_count: bag_count, MetaDataField.use_count: use_count},
key=META_DB_KEY,
)
def increment_meta_count(field: MetaDataField, by: int):
try:
meta_db.update({field: meta_db.util.increment(by)}, key=META_DB_KEY)
except:
initialize_meta_db(**{field.value: by})
return None
def increment_coffee_bag(by: int = 1):
increment_meta_count(MetaDataField.bag_count, by=by)
def increment_coffee_use(by: int = 1):
increment_meta_count(MetaDataField.use_count, by=by)
def reset_coffee_bag_count():
meta_db.update({MetaDataField.bag_count: 0}, key=META_DB_KEY)
def reset_coffee_use_count():
meta_db.update({MetaDataField.use_count: 0}, key=META_DB_KEY)
def num_coffee_bags() -> int:
res: Optional[Dict[str, Any]] = meta_db.get(key=META_DB_KEY)
if res is None:
return 0
return res[MetaDataField.bag_count]
def num_coffee_uses() -> int:
res: Optional[Dict[str, Any]] = meta_db.get(key=META_DB_KEY)
if res is None:
return 0
return res[MetaDataField.use_count]
#### ---- Security ---- ####
def compare_password(password: str) -> bool:
return pwd_context.verify(password, HASHED_PASSWORD)
def verify_password(password: str) -> bool:
if not compare_password(password):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED, detail="Incorrect password."
)
return True
#### ---- Error messages ---- ####
def raise_bag_not_found(bag_id: str) -> None:
raise HTTPException(
status.HTTP_404_NOT_FOUND, detail=f"Bag with key '{bag_id}' not found."
)
def raise_server_error(err: Exception) -> None:
raise HTTPException(status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(err))
def raise_invalid_field(field: str):
raise HTTPException(
status.HTTP_404_NOT_FOUND, detail=f"Field '{field}' is not a valid field."
)
#### ---- Response Models ---- ####
BagResponse = Dict[str, CoffeeBag]
UseResponse = Dict[str, CoffeeUse]
#### ---- Start Page ---- ####
@app.get("/", response_description="A 'Welcome to the API' message!")
async def root():
return {"message": "Coffee Counter API"}
#### ---- Getters ---- ####
@app.get("/bags/", response_model=BagResponse)
def get_bags() -> BagResponse:
return coffee_bag_dict()
@app.get("/number_of_bags/", response_model=int)
def get_number_of_bags() -> int:
return num_coffee_bags()
@app.get("/bag/{bag_id}", response_model=BagResponse)
def get_bag_info(bag_id: str) -> BagResponse:
bag_info = coffee_bag_db.get(bag_id)
if bag_info is None:
raise_bag_not_found(bag_id)
bag = convert_info_to_bag(bag_info)
return {bag._key: bag}
@app.get("/active_bags/", response_model=BagResponse)
def get_active_bags(n_last: Optional[int] = Query(None, ge=1)) -> BagResponse:
n_bags = num_coffee_bags()
n_buffer = 100
n_pages = ceil(n_bags / n_buffer) + 1
bags: List[CoffeeBag] = []
for page in coffee_bag_db.fetch(
query={"active": True}, buffer=n_buffer, pages=n_pages
):
bags = [convert_info_to_bag(i) for i in page]
sort_coffee_bags(bags)
if n_last is not None:
bags = bags[-n_last:]
return keyedlist_to_dict(bags)
def query_coffee_uses_db(
n_last: Optional[int] = None,
since: Optional[datetime] = None,
bag_id: Optional[str] = None,
) -> UseResponse:
if n_last is None:
n_last = num_coffee_uses()
buffer_size = 300
pages = ceil(n_last / buffer_size)
uses: List[CoffeeUse] = []
query_prep: Dict[str, Any] = {}
if bag_id is not None:
query_prep["bag_id"] = bag_id
if since is not None:
query_prep["_seconds?gt"] = unix_time_millis(since)
query: Optional[Dict[str, Any]] = None
if len(query_prep.keys()) > 0:
query = query_prep
print(query)
if since:
print(unix_time_millis(since) < unix_time_millis())
for page in coffee_use_db.fetch(query=query, buffer=300, pages=pages):
uses += [convert_info_to_use(i) for i in page]
uses.sort(key=lambda x: x.datetime)
if len(uses) > n_last:
uses = uses[-n_last:]
return keyedlist_to_dict(uses)
@app.get("/uses/", response_model=UseResponse)
def get_uses(
n_last: int = Query(100, ge=1, le=10000),
since: Optional[datetime] = None,
bag_id: Optional[str] = None,
) -> UseResponse:
return query_coffee_uses_db(n_last=n_last, since=since, bag_id=bag_id)
@app.get("/number_of_uses/", response_model=int)
def get_number_of_uses(
since: Optional[datetime] = None, bag_id: Optional[str] = None
) -> int:
if since is None and bag_id is None:
return num_coffee_uses()
uses = query_coffee_uses_db(since=since, bag_id=bag_id)
return len(uses.keys())
#### ---- Setters ---- ####
@app.put("/new_bag/", response_model=Dict[str, CoffeeBag])
def add_new_bag(bag: CoffeeBag, password: str) -> Dict[str, CoffeeBag]:
verify_password(password)
try:
coffee_bag_db.put(convert_bag_to_info(bag))
increment_coffee_bag(1)
except Exception as err:
raise_server_error(err)
return {bag._key: bag}
@app.put("/new_use/{bag_id}", response_model=UseResponse)
def add_new_use(
bag_id: str, password: str, when: datetime = datetime.now()
) -> UseResponse:
verify_password(password)
bag_info = coffee_bag_db.get(bag_id)
if bag_info is None:
raise_bag_not_found(bag_id)
new_coffee_use = CoffeeUse(bag_id=bag_id, datetime=when)
try:
coffee_use_db.put(convert_use_to_info(new_coffee_use))
increment_coffee_use(1)
except Exception as err:
raise_server_error(err)
return {new_coffee_use._key: new_coffee_use}
@app.patch("/deactivate/{bag_id}", response_model=BagResponse)
def deactivate_bag(
bag_id: str, password: str, when: date = date.today()
) -> BagResponse:
verify_password(password)
bag_info = coffee_bag_db.get(key=bag_id)
if bag_info is None:
raise_bag_not_found(bag_id)
if bag_info["finish"] is None and bag_info["active"]:
bag_info["finish"] = when
bag_info["active"] = False
coffee_bag_db.update(
updates={"finish": jsonable_encoder(when), "active": False},
key=bag_info["key"],
)
return {bag_id: convert_info_to_bag(bag_info)}
else:
raise HTTPException(
status.HTTP_400_BAD_REQUEST,
detail=f"Bag with key '{bag_id}' is not active (cannot deactivate).",
)
@app.patch("/activate/{bag_id}", response_model=BagResponse)
def activate_bag(bag_id: str, password: str) -> BagResponse:
verify_password(password)
bag_info = coffee_bag_db.get(key=bag_id)
if bag_info is None:
raise_bag_not_found(bag_id)
bag = convert_info_to_bag(bag_info)
if bag.active:
raise HTTPException(
status.HTTP_400_BAD_REQUEST,
detail=f"Bag with key '{bag_id}' is already active (cannot activate).",
)
bag_info["finish"] = None
bag_info["active"] = True
coffee_bag_db.update(
updates={"finish": None, "active": True},
key=bag_id,
)
return {bag_id: convert_info_to_bag(bag_info)}
@app.patch("/update_bag/{bag_id}", response_model=BagResponse)
def update_bag(bag_id: str, field: str, value: Any, password: str) -> BagResponse:
verify_password(password)
if field.startswith("_"):
# Cannot change private fields.
raise_invalid_field(field)
bag_info = coffee_bag_db.get(bag_id)
if bag_info is None:
raise_bag_not_found(bag_id)
if field not in bag_info.keys():
# Not a viable field in CoffeeBag model.
raise_invalid_field(field)
bag_info[field] = value
try:
bag = convert_info_to_bag(bag_info)
except:
raise HTTPException(
status.HTTP_400_BAD_REQUEST,
detail="Unable to convert data into CoffeeBag object.",
)
coffee_bag_db.update({field: value}, key=bag_id)
return {bag._key: bag}
def _delete_coffee_bag(bag_id: str):
if coffee_bag_db.get(bag_id) is not None:
coffee_bag_db.delete(bag_id)
increment_coffee_bag(by=-1)
@app.delete("/delete_bag/{bag_id}")
def delete_bag(bag_id: str, password: str):
verify_password(password)
_delete_coffee_bag(bag_id=bag_id)
return None
@app.delete("/delete_bags/")
def delete_bags(bag_ids: List[str], password: str):
verify_password(password)
for id in bag_ids:
_delete_coffee_bag(bag_id=id)
return None
@app.delete("/delete_all_bags/")
def delete_all_bags(password: str):
verify_password(password)
for bag_info in get_all_coffee_bag_info():
coffee_bag_db.delete(bag_info["key"])
reset_coffee_bag_count()
return None
def _delete_coffee_use(id: str):
if coffee_use_db.get(id) is not None:
coffee_use_db.delete(id)
increment_coffee_use(by=-1)
@app.delete("/delete_use/{id}")
def delete_use(id: str, password: str):
verify_password(password)
_delete_coffee_use(id)
return None
@app.delete("/delete_uses/")
def delete_uses(ids: List[str], password: str):
verify_password(password)
for id in ids:
_delete_coffee_use(id)
return None
@app.delete("/delete_all_uses/")
def delete_all_uses(password: str):
verify_password(password)
for use_info in get_all_coffee_use_info():
coffee_use_db.delete(use_info["key"])
reset_coffee_use_count()
return None