second commit
This commit is contained in:
62
main.py
Normal file
62
main.py
Normal file
@@ -0,0 +1,62 @@
|
||||
#Just some generic imports
|
||||
from fastapi import FastAPI
|
||||
from starlette.responses import FileResponse
|
||||
from pysondb import db
|
||||
from pydantic import BaseModel
|
||||
from datetime import datetime
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
# Get handles to database
|
||||
care_db_file_name = "care_status.json"
|
||||
care_transactions_file_name = "care_transactions.json"
|
||||
|
||||
status_db = db.getDb(care_db_file_name)
|
||||
transactions_db = db.getDb(care_transactions_file_name)
|
||||
|
||||
|
||||
|
||||
# Return a html located within the pages folder
|
||||
@app.get("/")
|
||||
def root():
|
||||
return FileResponse('pages/checkboxes_demo.html')
|
||||
|
||||
|
||||
# Return data by just dumping the database content
|
||||
@app.get('/data.json')
|
||||
def get_data():
|
||||
all_rows = status_db.getAll()
|
||||
print("hello!")
|
||||
for x in all_rows:
|
||||
del x['id']
|
||||
return {'result': all_rows}
|
||||
|
||||
|
||||
# This is the "schema" of web request made back and forth between the frontend and backend
|
||||
class UpdateRecordFeedSheet(BaseModel):
|
||||
animal_name: str
|
||||
column_name: str
|
||||
value_set: bool
|
||||
|
||||
|
||||
|
||||
# This updates a record
|
||||
@app.post('/update')
|
||||
def update_data(item: UpdateRecordFeedSheet):
|
||||
|
||||
# Extract the needed values
|
||||
animal_name = item.animal_name
|
||||
column_name = item.column_name
|
||||
value_set = item.value_set
|
||||
|
||||
|
||||
#Find the row to update, and update the new value
|
||||
row_to_update = status_db.reSearch('name',animal_name)[0] #Pick the first row of results. This isn't a proper database
|
||||
status_db.updateById(row_to_update['id'], {column_name:value_set})
|
||||
|
||||
# Add what was done to the transactions database
|
||||
transactions_db.add( {'name': animal_name, 'column_updated': column_name, 'value_set': value_set, 'timestamp':datetime.now().isoformat()})
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user