forked from pynutshell/pynut4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver2.py
57 lines (50 loc) · 1.84 KB
/
server2.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
from decimal import Decimal
from fastapi import FastAPI, Form
from fastapi.responses import HTMLResponse, FileResponse
from mongoengine import connect
from mongoengine.errors import NotUniqueError
from typing import Optional
import json
import uvicorn
from models import PItem, MItem
DATABASE_URI = "mongodb://localhost:27017"
db=DATABASE_URI+"/mydatabase"
connect(host=db)
app = FastAPI()
def save(item):
try:
return item.save(force_insert=True)
except NotUniqueError:
return None
@app.get('/')
def home_page():
"View function to display a simple form."
return FileResponse("index.html")
@app.post("/items/new/form/", response_class=HTMLResponse)
def create_item_from_form(name: str=Form(...),
price: Decimal=Form(...),
description: Optional[str]=Form(""),
tax: Optional[Decimal]=Form(Decimal("0.0"))):
"View function to accept form data and create an item."
mongoitem = MItem(name=name, price=price, description=description, tax=tax)
value = save(mongoitem)
if value:
body = f"Item({name!r}, {price!r}, {description!r}, {tax!r})"
else:
body = f"Item {name!r} already present."
return f"""<html><body><h2>{body}</h2></body></html>"""
@app.post("/items/new/")
def create_item_from_json(item: PItem):
"View function to accept JSON data and create an item."
mongoitem = MItem(**item.dict())
value = save(mongoitem)
if not value:
return f"Primary key {item.name!r} already present"
return item.dict()
@app.get("/items/{name}/")
def retrieve_item(name: str):
"View function to return the JSON contents of an item."
m_item = MItem.objects(name=name).get()
return json.loads(m_item.to_json())
if __name__ == "__main__":
uvicorn.run("__main__:app", host="127.0.0.1", port=8000, reload=True)