|
28 | 28 | ) |
29 | 29 | from py4web.core import DAL, Fixture, Reloader, Session, dumps, error_logger, safely |
30 | 30 | from py4web.utils.factories import ActionFactory |
| 31 | +from py4web.utils.grid import Grid |
| 32 | +from yatl.helpers import A |
31 | 33 |
|
32 | 34 | from .diff2kryten import diff2kryten |
33 | 35 | from .utils import * |
@@ -156,10 +158,51 @@ def logout(): |
156 | 158 | session["user"] = None |
157 | 159 | return dict() |
158 | 160 |
|
159 | | - @action("dbadmin") |
| 161 | + @action("tickets/search") |
160 | 162 | @action.uses(Logged(session), "dbadmin.html") |
161 | 163 | def dbadmin(): |
162 | | - return dict(languages=dumps(getattr(T.local, "language", {}))) |
| 164 | + db = error_logger.database_logger.db |
| 165 | + |
| 166 | + def make_grid(): |
| 167 | + make_safe(db) |
| 168 | + table = db.py4web_error |
| 169 | + columns = [field for field in table if not field.name == "snapshot"] |
| 170 | + return Grid(table, columns=columns) |
| 171 | + |
| 172 | + grid = action.uses(db)(make_grid)() |
| 173 | + return dict(grid=grid) |
| 174 | + |
| 175 | + @action("dbadmin/<app_name>/<db_name>/<table_name>") |
| 176 | + @action.uses(Logged(session), "dbadmin.html") |
| 177 | + def dbadmin(app_name, db_name, table_name): |
| 178 | + module = Reloader.MODULES.get(app_name) |
| 179 | + db = getattr(module, db_name) |
| 180 | + |
| 181 | + def make_grid(): |
| 182 | + make_safe(db) |
| 183 | + table = db[table_name] |
| 184 | + for field in table: |
| 185 | + field.readable = True |
| 186 | + field.writable = True |
| 187 | + columns = [ |
| 188 | + field |
| 189 | + for field in table |
| 190 | + if field.type |
| 191 | + in ( |
| 192 | + "id", |
| 193 | + "string", |
| 194 | + "integer", |
| 195 | + "double", |
| 196 | + "time", |
| 197 | + "date", |
| 198 | + "datetime", |
| 199 | + "boolean", |
| 200 | + ) |
| 201 | + ] |
| 202 | + return Grid(table, columns=columns) |
| 203 | + |
| 204 | + grid = action.uses(db)(make_grid)() |
| 205 | + return dict(grid=grid) |
163 | 206 |
|
164 | 207 | @action("info") |
165 | 208 | @session_secured |
@@ -350,69 +393,25 @@ def api(path): |
350 | 393 |
|
351 | 394 | if not module: |
352 | 395 | raise HTTP(404) |
353 | | - |
354 | | - def url(*args): |
355 | | - return request.url + "/" + "/".join(args) |
356 | | - |
357 | 396 | databases = [ |
358 | 397 | name for name in dir(module) if isinstance(getattr(module, name), DAL) |
359 | 398 | ] |
360 | | - if len(args) == 1: |
361 | | - |
362 | | - def tables(name): |
363 | | - db = getattr(module, name) |
364 | | - make_safe(db) |
365 | | - return [ |
366 | | - { |
367 | | - "name": t._tablename, |
368 | | - "fields": t.fields, |
369 | | - "link": url(name, t._tablename) + "?model=true", |
370 | | - } |
371 | | - for t in getattr(module, name) |
372 | | - ] |
373 | | - |
374 | | - return { |
375 | | - "databases": [ |
376 | | - {"name": name, "tables": tables(name)} for name in databases |
377 | | - ] |
378 | | - } |
379 | | - elif len(args) > 2 and args[1] in databases: |
380 | | - db = getattr(module, args[1]) |
| 399 | + |
| 400 | + def tables(name): |
| 401 | + db = getattr(module, name) |
381 | 402 | make_safe(db) |
382 | | - id = args[3] if len(args) == 4 else None |
383 | | - policy = Policy() |
384 | | - for table in db: |
385 | | - policy.set( |
386 | | - table._tablename, |
387 | | - "GET", |
388 | | - authorize=True, |
389 | | - allowed_patterns=["**"], |
390 | | - allow_lookup=True, |
391 | | - fields=table.fields, |
392 | | - ) |
393 | | - policy.set(table._tablename, "PUT", authorize=True, fields=table.fields) |
394 | | - policy.set( |
395 | | - table._tablename, "POST", authorize=True, fields=table.fields |
396 | | - ) |
397 | | - policy.set(table._tablename, "DELETE", authorize=True) |
398 | | - |
399 | | - def make_writable(tablename): |
400 | | - if tablename in db: |
401 | | - for field in db[tablename]: |
402 | | - field.writable = True |
403 | | - |
404 | | - # must wrap into action uses to make sure it closes transactions |
405 | | - data = action.uses(db)( |
406 | | - lambda: make_writable(args[2]) |
407 | | - or RestAPI(db, policy)( |
408 | | - request.method, args[2], id, request.query, request.json |
409 | | - ) |
410 | | - )() |
411 | | - else: |
412 | | - data = {} |
413 | | - if "code" in data: |
414 | | - response.status = data["code"] |
415 | | - return data |
| 403 | + return [ |
| 404 | + { |
| 405 | + "name": t._tablename, |
| 406 | + "fields": t.fields, |
| 407 | + "link": URL("dbadmin", app_name, name, t._tablename), |
| 408 | + } |
| 409 | + for t in getattr(module, name) |
| 410 | + ] |
| 411 | + |
| 412 | + return { |
| 413 | + "databases": [{"name": name, "tables": tables(name)} for name in databases] |
| 414 | + } |
416 | 415 |
|
417 | 416 |
|
418 | 417 | if MODE == "full": |
|
0 commit comments