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
oct-12-2025-TODOs.txt · 7.2 KB · 252 lines
| 1 | ✅ Successfully published dbbasic-web 0.1.1 to PyPI! |
| 2 | |
| 3 | View it at: https://pypi.org/project/dbbasic-web/0.1.1/ |
| 4 | |
| 5 | What was added: |
| 6 | |
| 7 | 1. POST form data parsing - request['form'] now contains parsed POST data |
| 8 | 2. Cookie parsing - request['cookies'] now contains parsed cookies |
| 9 | 3. Request body - request['body'] contains raw body bytes |
| 10 | |
| 11 | Changes made: |
| 12 | |
| 13 | dbbasic-web/asgi.py: |
| 14 | - Read request body from ASGI before routing |
| 15 | - Pass body to router |
| 16 | |
| 17 | dbbasic-web/router.py: |
| 18 | - Added parse_form_data() - parses application/x-www-form-urlencoded |
| 19 | - Added parse_cookies() - extracts cookies from Cookie header |
| 20 | - Updated route() to accept body parameter |
| 21 | - Add form, cookies, and body to request dict |
| 22 | |
| 23 | Next steps: |
| 24 | |
| 25 | Still on the list: |
| 26 | 1. Add Request helper class (PHP-style properties like request.POST, request.GET) |
| 27 | 2. Test the examples actually work |
| 28 | |
| 29 | The examples should now work since they can access request.get('form') and |
| 30 | request.get('cookies')! Want to test them? |
| 31 | |
| 32 | > so if you thought perl got it right, why did we go the php way,? because its more |
| 33 | natural, easier, faster, unix ? |
| 34 | |
| 35 | ⏺ Great question! Let me think through this... |
| 36 | |
| 37 | Why We Chose PHP's Way Over Perl's |
| 38 | |
| 39 | Perl CGI.pm did ONE thing really well: |
| 40 | my $email = $q->param('email'); # Works for GET or POST |
| 41 | Simple! But it merges GET and POST silently. |
| 42 | |
| 43 | The Problem with Merging: |
| 44 | # Perl-style (merged) |
| 45 | email = request.param('email') # Is this from GET or POST? 🤔 |
| 46 | |
| 47 | This causes security issues: |
| 48 | - GET params are in URLs, logs, browser history |
| 49 | - POST params are in body, not logged |
| 50 | - If you merge them, a GET param can override a POST param! |
| 51 | |
| 52 | PHP's Explicit Approach: |
| 53 | $_POST['email'] // Explicitly POST - for sensitive data |
| 54 | $_GET['email'] // Explicitly GET - for links/filters |
| 55 | $_REQUEST['email'] // Merged - opt-in when you want it |
| 56 | |
| 57 | Why PHP-style is Better for dbbasic-web: |
| 58 | |
| 59 | 1. Security - No Silent Merging |
| 60 | |
| 61 | password = request.form.get('password') # Only from POST body |
| 62 | # NOT from URL where it could be logged! |
| 63 | |
| 64 | 2. Explicit is Unix - Transparency |
| 65 | |
| 66 | request.form # You KNOW this is POST |
| 67 | request.query # You KNOW this is GET |
| 68 | request.cookies # You KNOW this is cookies |
| 69 | Everything is explicit. No magic. You can print(request) and see it all. |
| 70 | |
| 71 | 3. Faster - No Merging Overhead |
| 72 | |
| 73 | # PHP-style: Direct dict access |
| 74 | email = request.form.get('email') # O(1) |
| 75 | |
| 76 | # Perl-style: Would need to check both |
| 77 | email = request.param('email') # Must search form, then query, then merge |
| 78 | |
| 79 | 4. More Unix-like - Separate Data Streams |
| 80 | |
| 81 | Just like Unix has: |
| 82 | - stdin (input) |
| 83 | - stdout (output) |
| 84 | - stderr (errors) |
| 85 | |
| 86 | HTTP has: |
| 87 | - form (POST body) |
| 88 | - query (GET params) |
| 89 | - cookies (session) |
| 90 | |
| 91 | PHP keeps them separate. Perl merges them. |
| 92 | |
| 93 | The Real Perl Win We Should Add: |
| 94 | |
| 95 | Perl's actual genius was: |
| 96 | $q->param('email') # Instead of $q->{'params'}->{'email'} |
| 97 | |
| 98 | That's just shorter syntax. We can add that WITHOUT merging: |
| 99 | |
| 100 | # What we have now (PHP-style) |
| 101 | email = request.get('form', {}).get('email', '') # Verbose! |
| 102 | |
| 103 | # What we should add (Perl-inspired shortcut) |
| 104 | email = request.POST.get('email', '') # Much better! |
| 105 | |
| 106 | So the answer: PHP-style separation + Perl-style convenience = best of both |
| 107 | |
| 108 | Want me to add that Request helper class now with request.POST, request.GET, |
| 109 | request.COOKIES properties? |
| 110 | |
| 111 | > yep, sounds good, then eventually we do teh perl real win? |
| 112 | |
| 113 | ⏺ Exactly! Let's do it in phases: |
| 114 | |
| 115 | Phase 1 (Now): Add PHP-style properties |
| 116 | |
| 117 | request.POST # Instead of request.get('form', {}) |
| 118 | request.GET # Instead of request.get('query', {}) |
| 119 | request.COOKIES # Instead of request.get('cookies', {}) |
| 120 | request.PATH # Instead of request.get('path_parts', []) |
| 121 | request.method # Instead of request.get('method', 'GET') |
| 122 | |
| 123 | This alone cuts the code size down significantly! |
| 124 | |
| 125 | Phase 2 (Later): Add Perl's real win - the convenience method |
| 126 | |
| 127 | request.param('email') # Checks POST first, then GET (explicit order) |
| 128 | # or |
| 129 | request.params['email'] # Same thing, dict-style |
| 130 | |
| 131 | Both explicit AND convenient. We can document the lookup order clearly so there's no |
| 132 | magic. |
| 133 | |
| 134 | Let me add Phase 1 now: |
| 135 | |
| 136 | #### |
| 137 | |
| 138 | |
| 139 | |
| 140 | ⏺ Perfect! Now I can see the remaining repetitive patterns. Here's what could be |
| 141 | simplified: |
| 142 | |
| 143 | Repetitive Patterns Found |
| 144 | |
| 145 | 1. Authentication Check (Lines 11-21 in post.py, 18-28 in follow.py) |
| 146 | |
| 147 | token = request.cookie('session') |
| 148 | if not token: |
| 149 | return redirect('/login') |
| 150 | user_id = get_session(token) |
| 151 | if not user_id: |
| 152 | return redirect('/login') |
| 153 | user = accounts.get_user(user_id=int(user_id)) |
| 154 | if not user: |
| 155 | return redirect('/login') |
| 156 | Repeated in: post.py, follow.py, unfollow.py (10+ lines each) |
| 157 | |
| 158 | 2. Path Parameter Extraction (Lines 11-16 in follow.py, 22-25 in profile.py) |
| 159 | |
| 160 | parts = request.get('path_parts', []) |
| 161 | if len(parts) != 2: |
| 162 | return redirect('/') |
| 163 | username = parts[1] |
| 164 | |
| 165 | 3. JSON Error Responses (Lines 24-25, 31-32 in profile.py) |
| 166 | |
| 167 | import json as json_lib |
| 168 | return json(json_lib.dumps({"error": "Invalid path"}), status=404) |
| 169 | |
| 170 | 4. Timestamp Generation (Lines 25, 34 in various files) |
| 171 | |
| 172 | post_id = str(int(time.time() * 1000)) |
| 173 | 'created': time.strftime('%Y-%m-%d %H:%M') |
| 174 | |
| 175 | Proposed Convenience Methods |
| 176 | |
| 177 | Option 1: Authentication Helper |
| 178 | |
| 179 | # Add to request.py |
| 180 | def require_auth(self, accounts, redirect_to='/login'): |
| 181 | """Get authenticated user or return redirect response""" |
| 182 | token = self.cookie('session') |
| 183 | if not token: |
| 184 | return None, redirect(redirect_to) |
| 185 | |
| 186 | user_id = get_session(token) |
| 187 | if not user_id: |
| 188 | return None, redirect(redirect_to) |
| 189 | |
| 190 | user = accounts.get_user(user_id=int(user_id)) |
| 191 | if not user: |
| 192 | return None, redirect(redirect_to) |
| 193 | |
| 194 | return user, None |
| 195 | |
| 196 | # Usage: |
| 197 | user, error_response = request.require_auth(accounts) |
| 198 | if error_response: |
| 199 | return error_response |
| 200 | Saves: 10 lines → 2 lines |
| 201 | |
| 202 | Option 2: Path Parameter Access |
| 203 | |
| 204 | # Add to request.py |
| 205 | def path_param(self, index, default=None): |
| 206 | """Get path parameter by index. Example: /profile/alice -> path_param(1) = |
| 207 | 'alice'""" |
| 208 | parts = self.PATH |
| 209 | return parts[index] if len(parts) > index else default |
| 210 | |
| 211 | # Usage: |
| 212 | username = request.path_param(1) or return_404() |
| 213 | Saves: 4 lines → 1 line |
| 214 | |
| 215 | Option 3: Response Helpers on Request |
| 216 | |
| 217 | # Add to responses.py |
| 218 | def json_error(message, status=404): |
| 219 | """Return JSON error response""" |
| 220 | import json |
| 221 | return json(json.dumps({"error": message}), status=status) |
| 222 | |
| 223 | # Usage: |
| 224 | return json_error("User not found", 404) |
| 225 | Saves: 2 lines → 1 line |
| 226 | |
| 227 | Option 4: Timestamp Helpers |
| 228 | |
| 229 | # Add to helpers module |
| 230 | def timestamp_id(): |
| 231 | """Generate millisecond timestamp ID""" |
| 232 | return str(int(time.time() * 1000)) |
| 233 | |
| 234 | def timestamp_now(): |
| 235 | """Current time formatted for display""" |
| 236 | return time.strftime('%Y-%m-%d %H:%M') |
| 237 | |
| 238 | # Usage: |
| 239 | post_id = timestamp_id() |
| 240 | created = timestamp_now() |
| 241 | Saves: Clarity + consistency |
| 242 | |
| 243 | Which Ones to Add? |
| 244 | |
| 245 | Most impactful: |
| 246 | 1. ✅ Auth helper - Saves 10 lines per protected route (8+ times!) |
| 247 | 2. ✅ Path params - request.path_param(1) is much cleaner |
| 248 | 3. ✅ json_error() - Common pattern |
| 249 | 4. ⚠️ Timestamps - Nice but not as critical |
| 250 | |
| 251 | Want me to add these? I'd recommend starting with #1 (auth) and #2 (path params) - |
| 252 | they'd cut the most boilerplate. |