askrobots / dbbasic-forms
Public
A form builder that keeps forms and responses in TSV files: git-friendly, no database, CSV export.
responses.py · 1.2 KB · 45 lines
| 1 | """ |
| 2 | View form responses (admin interface) |
| 3 | GET /admin/forms/{form_id}/responses - Show responses page |
| 4 | """ |
| 5 | |
| 6 | import json |
| 7 | from pathlib import Path |
| 8 | from dbbasic_forms import FormBuilder |
| 9 | |
| 10 | |
| 11 | def handle(request): |
| 12 | """Show form responses interface""" |
| 13 | # Extract form_id from path |
| 14 | # Path format: /admin/forms/{form_id}/responses |
| 15 | path_parts = request.path.strip("/").split("/") |
| 16 | if len(path_parts) < 3: |
| 17 | return { |
| 18 | "status": 400, |
| 19 | "headers": {"Content-Type": "application/json"}, |
| 20 | "body": json.dumps({"error": "Invalid path"}), |
| 21 | } |
| 22 | |
| 23 | form_id = path_parts[2] # forms/{form_id}/responses |
| 24 | |
| 25 | # Load form |
| 26 | builder = FormBuilder() |
| 27 | form = builder.get_form(form_id) |
| 28 | |
| 29 | if not form: |
| 30 | return { |
| 31 | "status": 404, |
| 32 | "headers": {"Content-Type": "application/json"}, |
| 33 | "body": json.dumps({"error": "Form not found"}), |
| 34 | } |
| 35 | |
| 36 | # Load responses template |
| 37 | template_path = Path(__file__).parent.parent.parent / "templates" / "forms" / "responses.html" |
| 38 | with open(template_path) as f: |
| 39 | html = f.read() |
| 40 | |
| 41 | return { |
| 42 | "status": 200, |
| 43 | "headers": {"Content-Type": "text/html"}, |
| 44 | "body": html, |
| 45 | } |