triflix commited on
Commit
68d875b
·
verified ·
1 Parent(s): b30d8e3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +110 -0
app.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Request, Form, HTTPException, Depends, Response, BackgroundTasks
2
+ from fastapi.responses import HTMLResponse, RedirectResponse
3
+ from fastapi.staticfiles import StaticFiles
4
+ from fastapi.templating import Jinja2Templates
5
+ import uuid
6
+ import json
7
+ from pathlib import Path
8
+ import os
9
+ from typing import List
10
+
11
+ # Directories
12
+ BASE_DIR = Path(__file__).parent
13
+ DATA_DIR = BASE_DIR / "data"
14
+ FORMS_DIR = DATA_DIR / "forms"
15
+ SUBS_DIR = DATA_DIR / "submissions"
16
+ for d in (FORMS_DIR, SUBS_DIR): d.mkdir(parents=True, exist_ok=True)
17
+
18
+ # Admin key env
19
+ ADMIN_KEY = os.getenv("ADMIN_KEY", "admin123")
20
+
21
+ app = FastAPI()
22
+ app.mount("/static", StaticFiles(directory=BASE_DIR / "static"), name="static")
23
+ templates = Jinja2Templates(directory=BASE_DIR / "templates")
24
+
25
+ # Helper: read/write JSON
26
+
27
+ def save_json(path: Path, data):
28
+ with path.open('w') as f: json.dump(data, f)
29
+
30
+ def load_json(path: Path):
31
+ if not path.exists(): return None
32
+ with path.open() as f: return json.load(f)
33
+
34
+ # Home: show history or welcome
35
+ @app.get("/", response_class=HTMLResponse)
36
+ async def home(request: Request):
37
+ # read cookie
38
+ history = request.cookies.get("form_history", "")
39
+ ids = history.split(',') if history else []
40
+ return templates.TemplateResponse("index.html", {"request": request, "form_ids": ids})
41
+
42
+ # New form builder page
43
+ @app.get("/new", response_class=HTMLResponse)
44
+ async def new_form(request: Request):
45
+ return templates.TemplateResponse("new_form.html", {"request": request})
46
+
47
+ # Create form schema
48
+ @app.post("/create_form")
49
+ async def create_form(request: Request, response: Response,
50
+ title: str = Form(...),
51
+ fields: List[str] = Form(...)):
52
+ form_id = str(uuid.uuid4())
53
+ schema = {"id": form_id, "title": title, "fields": fields}
54
+ save_json(FORMS_DIR / f"{form_id}.json", schema)
55
+ # update history cookie
56
+ history = request.cookies.get("form_history", "")
57
+ ids = history.split(',') if history else []
58
+ ids.append(form_id)
59
+ response = RedirectResponse(url=f"/forms/{form_id}", status_code=302)
60
+ response.set_cookie("form_history", ",".join(ids), httponly=True)
61
+ return response
62
+
63
+ # Render form for users
64
+ @app.get("/forms/{form_id}", response_class=HTMLResponse)
65
+ async def view_form(request: Request, form_id: str):
66
+ schema = load_json(FORMS_DIR / f"{form_id}.json")
67
+ if not schema:
68
+ raise HTTPException(404, "Form not found")
69
+ return templates.TemplateResponse("view_form.html", {"request": request, "schema": schema})
70
+
71
+ # Handle submission
72
+ @app.post("/forms/{form_id}/submit")
73
+ async def submit_form(request: Request, form_id: str):
74
+ schema = load_json(FORMS_DIR / f"{form_id}.json")
75
+ if not schema:
76
+ raise HTTPException(404, "Form not found")
77
+ data = await request.form()
78
+ entry = {field: data.get(field) for field in schema['fields']}
79
+ # append submission
80
+ sub_dir = SUBS_DIR / form_id
81
+ sub_dir.mkdir(exist_ok=True)
82
+ idx = len(list(sub_dir.iterdir())) + 1
83
+ save_json(sub_dir / f"{idx}.json", entry)
84
+ return RedirectResponse(url=f"/forms/{form_id}", status_code=302)
85
+
86
+ # Admin dashboard
87
+ def check_admin(key: str = Depends(lambda request: request.query_params.get('key'))):
88
+ if key != ADMIN_KEY:
89
+ raise HTTPException(401, "Unauthorized")
90
+ return True
91
+
92
+ @app.get("/admin", response_class=HTMLResponse)
93
+ async def admin_dashboard(request: Request, authorized: bool = Depends(check_admin)):
94
+ forms = []
95
+ for p in FORMS_DIR.glob("*.json"):
96
+ schema = load_json(p)
97
+ subs = len(list((SUBS_DIR / schema['id']).glob("*.json"))) if (SUBS_DIR / schema['id']).exists() else 0
98
+ forms.append({"id": schema['id'], "title": schema['title'], "submissions": subs})
99
+ return templates.TemplateResponse("admin.html", {"request": request, "forms": forms})
100
+
101
+ # Delete form (admin)
102
+ @app.post("/admin/delete/{form_id}")
103
+ async def admin_delete(form_id: str, authorized: bool = Depends(check_admin)):
104
+ fpath = FORMS_DIR / f"{form_id}.json"
105
+ if fpath.exists(): fpath.unlink()
106
+ subdir = SUBS_DIR / form_id
107
+ if subdir.exists():
108
+ for f in subdir.iterdir(): f.unlink()
109
+ subdir.rmdir()
110
+ return RedirectResponse(url="/admin?key=" + ADMIN_KEY, status_code=302)