A form builder that keeps forms and responses in TSV files: git-friendly, no database, CSV export.

0 downloads

Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-forms.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-forms.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-forms.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 Fix API handlers for dbbasic-web compatibility (v1.0.1) 143a3f2 1 year, 1 month ago History
create.py · 2.1 KB · 70 lines
1"""
2Create new form (admin interface)
3GET /admin/forms/create - Show form builder
4POST /admin/forms/create - Save new form
5"""
6
7import json
8from pathlib import Path
9from dbbasic_forms import FormBuilder
10
11
12def handle(request):
13 """Handle form creation - GET shows builder, POST saves form"""
14
15 if request.method == 'POST':
16 # Create new form
17 builder = FormBuilder()
18
19 # Parse request body
20 try:
21 data = json.loads(request.body)
22 except (json.JSONDecodeError, AttributeError):
23 return {
24 "status": 400,
25 "headers": {"Content-Type": "application/json"},
26 "body": json.dumps({"error": "Invalid JSON"}),
27 }
28
29 # Validate required fields
30 if not data.get("id") or not data.get("name"):
31 return {
32 "status": 400,
33 "headers": {"Content-Type": "application/json"},
34 "body": json.dumps({"error": "Missing required fields: id, name"}),
35 }
36
37 # Create form
38 try:
39 form = builder.create_form(
40 form_id=data["id"],
41 name=data["name"],
42 description=data.get("description", ""),
43 fields=data.get("fields", []),
44 settings=data.get("settings", {}),
45 )
46
47 return {
48 "status": 201,
49 "headers": {"Content-Type": "application/json"},
50 "body": json.dumps(
51 {"success": True, "form_id": form.form_id, "redirect": f"/admin/forms/{form.form_id}/edit"}
52 ),
53 }
54 except Exception as e:
55 return {
56 "status": 500,
57 "headers": {"Content-Type": "application/json"},
58 "body": json.dumps({"error": str(e)}),
59 }
60
61 # GET - Show form builder interface
62 template_path = Path(__file__).parent.parent.parent / "templates" / "forms" / "editor.html"
63 with open(template_path) as f:
64 html = f.read()
65
66 return {
67 "status": 200,
68 "headers": {"Content-Type": "text/html"},
69 "body": html,
70 }