Starting webapp dev.
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
from fastapi import APIRouter, Header
|
||||
from dataclasses import dataclass, field
|
||||
from typing import List
|
||||
from .auth import AuthRepo
|
||||
from ..data.models import Basket, BasketDonorRel, Donor, Prefix, Ticket
|
||||
from ..data.repos import BasketRepo, DonorRepo, PrefixRepo, TicketRepo
|
||||
|
||||
@dataclass
|
||||
class BackupFile:
|
||||
baskets: List[Basket] = field(default_factory=list)
|
||||
donors: List[Donor] = field(default_factory=list)
|
||||
donor_rels: List[BasketDonorRel] = field(default_factory=list)
|
||||
prefixes: List[Prefix] = field(default_factory=list)
|
||||
tickets: List[Ticket] = field(default_factory=list)
|
||||
|
||||
backup_router = APIRouter(prefix="/api/backup")
|
||||
|
||||
def chunk_list(in_list: list, chunk_size: int):
|
||||
out_list = []
|
||||
for i in range(0, len(in_list), chunk_size):
|
||||
out_list.append([*in_list[i:i+chunk_size]])
|
||||
return out_list
|
||||
|
||||
@backup_router.get("/backup.json")
|
||||
def get_backup_file(tam_auth_key: str = Header("")):
|
||||
auth_repo = AuthRepo()
|
||||
auth_repo.verify_key(tam_auth_key)
|
||||
del auth_repo
|
||||
return BackupFile(
|
||||
baskets=BasketRepo().get_all_baskets(),
|
||||
donors=DonorRepo().get_all_donors(),
|
||||
donor_rels=DonorRepo().get_all_donor_relations(),
|
||||
prefixes=PrefixRepo().get_prefixes(),
|
||||
tickets=TicketRepo().get_all_tickets()
|
||||
)
|
||||
|
||||
@backup_router.post("/restore")
|
||||
def post_backup_file(backup_file: BackupFile, tam_auth_key: str = Header("")):
|
||||
for bs in chunk_list(backup_file.baskets, 300):
|
||||
BasketRepo().post_baskets(bs)
|
||||
for ds in chunk_list(backup_file.donors, 300):
|
||||
DonorRepo().post_donors(ds)
|
||||
for drs in chunk_list(backup_file.donor_rels, 300):
|
||||
DonorRepo().post_donor_relation(drs)
|
||||
for ps in chunk_list(backup_file.prefixes, 300):
|
||||
PrefixRepo().post_prefixes(ps)
|
||||
for ts in chunk_list(backup_file.tickets, 300):
|
||||
TicketRepo().post_tickets(ts)
|
||||
return {"detail": "File uploaded successfully."}
|
||||
@@ -39,9 +39,9 @@ def init_db():
|
||||
donor_business TEXT
|
||||
)""")
|
||||
cur.execute("""CREATE TABLE IF NOT EXISTS r_basket_donor (
|
||||
b_prefix TEXT REFERENCES baskets(prefix),
|
||||
b_id INT REFERENCES baskets(basket_id),
|
||||
d_id INT REFERENCES donors(donor_id),
|
||||
b_prefix TEXT,
|
||||
b_id INT,
|
||||
d_id INT,
|
||||
PRIMARY KEY (b_prefix, b_id, d_id)
|
||||
)""")
|
||||
cur.execute("""CREATE VIEW IF NOT EXISTS winners_by_basket AS
|
||||
|
||||
@@ -29,6 +29,16 @@ class PrefixRepo(RepoTemplate):
|
||||
self.conn.commit()
|
||||
return {"detail": "Prefix posted successfully."}
|
||||
|
||||
def post_prefixes(self, ps: list[Prefix]):
|
||||
"""Posts a list of prefixes."""
|
||||
for p in ps:
|
||||
self.cur.execute(
|
||||
"INSERT INTO prefixes VALUES (?, ?, ?) ON CONFLICT (prefix) DO UPDATE SET color = EXCLUDED.color, weight = EXCLUDED.weight",
|
||||
(p.prefix, p.color, p.weight),
|
||||
)
|
||||
self.conn.commit()
|
||||
return {"detail": "Prefixes posted successfully."}
|
||||
|
||||
def del_prefix(self, prefix: str):
|
||||
"""Deletes a prefix from the database."""
|
||||
self.cur.execute("DELETE FROM prefixes WHERE prefix = ?", (prefix,))
|
||||
@@ -172,6 +182,12 @@ class DonorRepo(RepoTemplate):
|
||||
results = self.cur.fetchall()
|
||||
return [BasketDonorView(*r) for r in results]
|
||||
|
||||
def get_all_donor_relations(self):
|
||||
"""Gets all donor relations, typically for backup purposes."""
|
||||
self.cur.execute("SELECT * FROM r_basket_donor")
|
||||
results = self.cur.fetchall()
|
||||
return [BasketDonorRel(*r) for r in results]
|
||||
|
||||
def post_donors(self, ds: list[Donor]):
|
||||
"""Posts donors"""
|
||||
rtn_lst = []
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
from fastapi import FastAPI
|
||||
|
||||
from .core.auth import auth_router
|
||||
from .core.backuprestore import backup_router
|
||||
from .data import routers
|
||||
from .reports import routers as report_routers
|
||||
from .search import routers as search_routers
|
||||
@@ -15,3 +16,4 @@ def append_routers(app: FastAPI):
|
||||
app.include_router(routers.donor_router)
|
||||
app.include_router(report_routers.reports_router)
|
||||
app.include_router(search_routers.search_router)
|
||||
app.include_router(backup_router)
|
||||
|
||||
Reference in New Issue
Block a user