askrobots / dbbasic-examples
Public
Example DBBasic apps (an API, a blog and a microblog) and a template for new DBBasic modules.
danq9
Add official module template for creating DBBasic packages
17cf88a
1 year, 1 month ago
History
list.py · 5.4 KB · 130 lines
| 1 | """List THINGS - Admin interface |
| 2 | |
| 3 | Handles GET /admin/module |
| 4 | """ |
| 5 | |
| 6 | from dbbasic_module import Module |
| 7 | |
| 8 | |
| 9 | def handle(request): |
| 10 | """ |
| 11 | Handle /admin/module requests. |
| 12 | |
| 13 | Lists all THINGS with their details. |
| 14 | Customize the columns, filters, and search as needed. |
| 15 | """ |
| 16 | # Initialize module |
| 17 | module = Module() |
| 18 | |
| 19 | # Get all items |
| 20 | items = module.list() |
| 21 | |
| 22 | # Build item data for display |
| 23 | # Customize these fields based on your schema |
| 24 | item_data = [] |
| 25 | for item in items: |
| 26 | item_data.append({ |
| 27 | 'id': item.get('id', ''), |
| 28 | 'name': item.get('name', ''), |
| 29 | 'description': item.get('description', ''), |
| 30 | 'created_at': item.get('created_at', ''), |
| 31 | }) |
| 32 | |
| 33 | # Build HTML |
| 34 | from dbbasic_admin.admin import build_nav |
| 35 | nav_items = build_nav() |
| 36 | nav_html = "".join(f'<li><a href="{item["href"]}">{item.get("icon", "")} {item["label"]}</a></li>' for item in nav_items) |
| 37 | |
| 38 | # Build table rows |
| 39 | # Customize columns and actions as needed |
| 40 | rows_html = "" |
| 41 | for item in item_data: |
| 42 | rows_html += f""" |
| 43 | <tr> |
| 44 | <td><input type="checkbox" value="{item['id']}"></td> |
| 45 | <td>{item['name']}</td> |
| 46 | <td>{item['description']}</td> |
| 47 | <td>{item['created_at']}</td> |
| 48 | <td> |
| 49 | <div class="action-buttons"> |
| 50 | <a href="/admin/module/edit/{item['id']}" class="btn-icon" title="Edit">✏️</a> |
| 51 | <form method="POST" action="/admin/module/delete/{item['id']}" style="display: inline;"> |
| 52 | <button type="submit" class="btn-icon" title="Delete" onclick="return confirm('Are you sure?')">🗑️</button> |
| 53 | </form> |
| 54 | </div> |
| 55 | </td> |
| 56 | </tr> |
| 57 | """ |
| 58 | |
| 59 | html_content = f"""<!DOCTYPE html> |
| 60 | <html lang="en"> |
| 61 | <head> |
| 62 | <meta charset="UTF-8"> |
| 63 | <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 64 | <title>Things - Admin</title> |
| 65 | <style> |
| 66 | * {{ margin: 0; padding: 0; box-sizing: border-box; }} |
| 67 | body {{ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; display: flex; height: 100vh; background: #f5f5f5; }} |
| 68 | .sidebar {{ width: 250px; background: #2c3e50; color: white; padding: 20px; overflow-y: auto; }} |
| 69 | .sidebar h1 {{ font-size: 24px; margin-bottom: 30px; color: #ecf0f1; }} |
| 70 | .sidebar ul {{ list-style: none; }} |
| 71 | .sidebar li {{ margin-bottom: 10px; }} |
| 72 | .sidebar a {{ color: #ecf0f1; text-decoration: none; display: block; padding: 10px; border-radius: 5px; transition: background 0.2s; }} |
| 73 | .sidebar a:hover, .sidebar a.active {{ background: #34495e; }} |
| 74 | .content {{ flex: 1; padding: 40px; overflow-y: auto; }} |
| 75 | .header {{ background: white; padding: 30px; border-radius: 10px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); margin-bottom: 20px; display: flex; justify-content: space-between; align-items: center; }} |
| 76 | .header h2 {{ color: #2c3e50; margin: 0; }} |
| 77 | .btn-primary {{ background: #3498db; color: white; border: none; padding: 10px 20px; border-radius: 5px; cursor: pointer; font-size: 14px; text-decoration: none; display: inline-block; }} |
| 78 | .btn-primary:hover {{ background: #2980b9; }} |
| 79 | .card {{ background: white; padding: 20px; border-radius: 10px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); margin-bottom: 20px; }} |
| 80 | .table-container {{ overflow-x: auto; }} |
| 81 | table {{ width: 100%; border-collapse: collapse; }} |
| 82 | th, td {{ padding: 12px; text-align: left; border-bottom: 1px solid #ddd; }} |
| 83 | th {{ background: #f8f9fa; font-weight: 600; }} |
| 84 | .action-buttons {{ display: flex; gap: 5px; }} |
| 85 | .btn-icon {{ background: none; border: none; cursor: pointer; font-size: 16px; padding: 5px; }} |
| 86 | .btn-icon:hover {{ opacity: 0.7; }} |
| 87 | .breadcrumb {{ color: #666; font-size: 14px; }} |
| 88 | </style> |
| 89 | </head> |
| 90 | <body> |
| 91 | <div class="sidebar"><h1>Admin</h1><ul>{nav_html}</ul></div> |
| 92 | <div class="content"> |
| 93 | <div class="header"> |
| 94 | <div> |
| 95 | <h2>Things</h2> |
| 96 | <div class="breadcrumb">Home / Things</div> |
| 97 | </div> |
| 98 | <a href="/admin/module/create" class="btn-primary">+ New Thing</a> |
| 99 | </div> |
| 100 | |
| 101 | <div class="card"> |
| 102 | <h3 style="margin-bottom: 20px;">All Things ({len(item_data)})</h3> |
| 103 | <div class="table-container"> |
| 104 | <table> |
| 105 | <thead> |
| 106 | <tr> |
| 107 | <th><input type="checkbox"></th> |
| 108 | <th>Name</th> |
| 109 | <th>Description</th> |
| 110 | <th>Created</th> |
| 111 | <th>Actions</th> |
| 112 | </tr> |
| 113 | </thead> |
| 114 | <tbody> |
| 115 | {rows_html if rows_html else '<tr><td colspan="5" style="text-align: center; padding: 40px; color: #999;">No items yet. <a href="/admin/module/create">Create your first one</a></td></tr>'} |
| 116 | </tbody> |
| 117 | </table> |
| 118 | </div> |
| 119 | </div> |
| 120 | </div> |
| 121 | </body> |
| 122 | </html>""" |
| 123 | |
| 124 | # Return HTML response using dbbasic_web format |
| 125 | try: |
| 126 | from dbbasic_web.responses import html as html_response |
| 127 | return html_response(html_content) |
| 128 | except ImportError: |
| 129 | # Fallback if dbbasic_web not available |
| 130 | return (200, [('content-type', 'text/html; charset=utf-8')], [html_content.encode()]) |