askrobots / dbbasic-forms
Public
A form builder that keeps forms and responses in TSV files: git-friendly, no database, CSV export.
edit.py · 2.7 KB · 93 lines
| 1 | """ |
| 2 | Edit existing form (admin interface) |
| 3 | GET /admin/forms/{form_id}/edit - Show form editor |
| 4 | POST /admin/forms/{form_id}/edit - Update form |
| 5 | """ |
| 6 | |
| 7 | import json |
| 8 | from pathlib import Path |
| 9 | from dbbasic_forms import FormBuilder |
| 10 | |
| 11 | |
| 12 | def handle(request): |
| 13 | """Handle form editing - GET shows editor, POST updates form""" |
| 14 | |
| 15 | # Extract form_id from path |
| 16 | # Path format: /admin/forms/{form_id}/edit |
| 17 | path_parts = request.path.strip("/").split("/") |
| 18 | if len(path_parts) < 3: |
| 19 | return { |
| 20 | "status": 400, |
| 21 | "headers": {"Content-Type": "application/json"}, |
| 22 | "body": json.dumps({"error": "Invalid path"}), |
| 23 | } |
| 24 | |
| 25 | form_id = path_parts[2] # forms/{form_id}/edit |
| 26 | |
| 27 | if request.method == 'POST': |
| 28 | # Update form |
| 29 | # Parse request body |
| 30 | try: |
| 31 | data = json.loads(request.body) |
| 32 | except (json.JSONDecodeError, AttributeError): |
| 33 | return { |
| 34 | "status": 400, |
| 35 | "headers": {"Content-Type": "application/json"}, |
| 36 | "body": json.dumps({"error": "Invalid JSON"}), |
| 37 | } |
| 38 | |
| 39 | # Load form |
| 40 | builder = FormBuilder() |
| 41 | form = builder.get_form(form_id) |
| 42 | |
| 43 | if not form: |
| 44 | return { |
| 45 | "status": 404, |
| 46 | "headers": {"Content-Type": "application/json"}, |
| 47 | "body": json.dumps({"error": "Form not found"}), |
| 48 | } |
| 49 | |
| 50 | # Update form |
| 51 | try: |
| 52 | form.update_form( |
| 53 | name=data.get("name"), |
| 54 | description=data.get("description"), |
| 55 | fields=data.get("fields"), |
| 56 | settings=data.get("settings"), |
| 57 | ) |
| 58 | |
| 59 | return { |
| 60 | "status": 200, |
| 61 | "headers": {"Content-Type": "application/json"}, |
| 62 | "body": json.dumps({"success": True, "form_id": form_id}), |
| 63 | } |
| 64 | except Exception as e: |
| 65 | return { |
| 66 | "status": 500, |
| 67 | "headers": {"Content-Type": "application/json"}, |
| 68 | "body": json.dumps({"error": str(e)}), |
| 69 | } |
| 70 | |
| 71 | # GET - Show form editor interface |
| 72 | builder = FormBuilder() |
| 73 | form = builder.get_form(form_id) |
| 74 | |
| 75 | if not form: |
| 76 | return { |
| 77 | "status": 404, |
| 78 | "headers": {"Content-Type": "application/json"}, |
| 79 | "body": json.dumps({"error": "Form not found"}), |
| 80 | } |
| 81 | |
| 82 | # Load editor template |
| 83 | template_path = Path(__file__).parent.parent.parent / "templates" / "forms" / "editor.html" |
| 84 | with open(template_path) as f: |
| 85 | html = f.read() |
| 86 | |
| 87 | # In production, inject form data into template |
| 88 | # For now, return static HTML (data loaded via API) |
| 89 | return { |
| 90 | "status": 200, |
| 91 | "headers": {"Content-Type": "text/html"}, |
| 92 | "body": html, |
| 93 | } |