-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
76 lines (59 loc) · 1.8 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
from typing import Dict, Union
import os
from fastapi import FastAPI, Body
from pydantic import BaseModel
from peewee import (
Model, IntegerField, TextField, CompositeKey, IntegrityError
)
from playhouse.postgres_ext import JSONField
from playhouse.pool import PooledPostgresqlExtDatabase
DB = PooledPostgresqlExtDatabase(
database=os.environ['DATABASE'],
user=os.environ['USER'],
password=os.environ['PASSWORD'],
host=os.environ['HOST'],
max_connections=19,
stale_timeout=10,
)
class Submission_api(BaseModel):
learning_unit: Union[str, int]
slack_id: str = Body(..., min_length=5, max_length=20)
grade: int
metadata: Dict[str, str]
class Submission_db(Model):
learning_unit = IntegerField()
slack_id = TextField()
grade = IntegerField()
metadata = JSONField()
class Meta:
database = DB
primary_key = CompositeKey('learning_unit', 'slack_id')
DB.create_tables([Submission_db], safe=True)
app = FastAPI()
# This hook ensures that a connection is opened to handle any queries
# generated by the request .
@app.on_event("startup")
def startup():
DB.connect(reuse_if_open=True)
# This hook ensures that the connection is closed when we've finished
# processing the request.
@app.on_event("shutdown")
def shutdown():
if not DB.is_closed():
DB.close()
@app.put("/submit")
def submit(
submission_api: Submission_api
):
submission_db = Submission_db(
learning_unit=str(submission_api.learning_unit),
slack_id=submission_api.slack_id,
grade=submission_api.grade,
metadata=submission_api.metadata
)
try:
submission_db.save(force_insert=True)
except IntegrityError:
# no ned to do anyhting, means the student has already submited
DB.rollback()
return 'OK'