askrobots / dbbasic-follows
Public
Follows, friends and connection suggestions for DBBasic apps: a social graph module with an admin interface.
list.py · 5.5 KB · 130 lines
| 1 | """List Follows - Admin interface |
| 2 | |
| 3 | Handles GET /admin/follows |
| 4 | """ |
| 5 | |
| 6 | from dbbasic_follows import Follows |
| 7 | |
| 8 | |
| 9 | def handle(request): |
| 10 | """ |
| 11 | Handle /admin/follows requests. |
| 12 | |
| 13 | Lists all follow relationships in the system. |
| 14 | """ |
| 15 | # Initialize follows API |
| 16 | follows = Follows() |
| 17 | |
| 18 | # Get all follow relationships |
| 19 | all_follows = follows.table.select() |
| 20 | |
| 21 | # Build item data for display |
| 22 | item_data = [] |
| 23 | for follow in all_follows: |
| 24 | item_data.append({ |
| 25 | 'id': follow.get('id', ''), |
| 26 | 'follower_id': follow.get('follower_id', ''), |
| 27 | 'followee_id': follow.get('followee_id', ''), |
| 28 | 'status': follow.get('status', ''), |
| 29 | 'created_at': follow.get('created_at', ''), |
| 30 | }) |
| 31 | |
| 32 | # Build HTML |
| 33 | from dbbasic_admin.admin import build_nav |
| 34 | nav_items = build_nav() |
| 35 | nav_html = "".join(f'<li><a href="{item["href"]}">{item.get("icon", "")} {item["label"]}</a></li>' for item in nav_items) |
| 36 | |
| 37 | # Build table rows |
| 38 | rows_html = "" |
| 39 | for item in item_data: |
| 40 | rows_html += f""" |
| 41 | <tr> |
| 42 | <td><input type="checkbox" value="{item['id']}"></td> |
| 43 | <td>{item['follower_id']}</td> |
| 44 | <td>→</td> |
| 45 | <td>{item['followee_id']}</td> |
| 46 | <td><span style="color: {'green' if item['status'] == 'active' else 'gray'};">●</span> {item['status']}</td> |
| 47 | <td>{item['created_at'][:10] if item['created_at'] else ''}</td> |
| 48 | <td> |
| 49 | <div class="action-buttons"> |
| 50 | <form method="POST" action="/admin/follows/delete/{item['id']}" style="display: inline;"> |
| 51 | <button type="submit" class="btn-icon" title="Delete" onclick="return confirm('Remove this follow relationship?')">🗑️</button> |
| 52 | </form> |
| 53 | </div> |
| 54 | </td> |
| 55 | </tr> |
| 56 | """ |
| 57 | |
| 58 | html_content = f"""<!DOCTYPE html> |
| 59 | <html lang="en"> |
| 60 | <head> |
| 61 | <meta charset="UTF-8"> |
| 62 | <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 63 | <title>Follows - Admin</title> |
| 64 | <style> |
| 65 | * {{ margin: 0; padding: 0; box-sizing: border-box; }} |
| 66 | body {{ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; display: flex; height: 100vh; background: #f5f5f5; }} |
| 67 | .sidebar {{ width: 250px; background: #2c3e50; color: white; padding: 20px; overflow-y: auto; }} |
| 68 | .sidebar h1 {{ font-size: 24px; margin-bottom: 30px; color: #ecf0f1; }} |
| 69 | .sidebar ul {{ list-style: none; }} |
| 70 | .sidebar li {{ margin-bottom: 10px; }} |
| 71 | .sidebar a {{ color: #ecf0f1; text-decoration: none; display: block; padding: 10px; border-radius: 5px; transition: background 0.2s; }} |
| 72 | .sidebar a:hover, .sidebar a.active {{ background: #34495e; }} |
| 73 | .content {{ flex: 1; padding: 40px; overflow-y: auto; }} |
| 74 | .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; }} |
| 75 | .header h2 {{ color: #2c3e50; margin: 0; }} |
| 76 | .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; }} |
| 77 | .btn-primary:hover {{ background: #2980b9; }} |
| 78 | .card {{ background: white; padding: 20px; border-radius: 10px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); margin-bottom: 20px; }} |
| 79 | .table-container {{ overflow-x: auto; }} |
| 80 | table {{ width: 100%; border-collapse: collapse; }} |
| 81 | th, td {{ padding: 12px; text-align: left; border-bottom: 1px solid #ddd; }} |
| 82 | th {{ background: #f8f9fa; font-weight: 600; }} |
| 83 | .action-buttons {{ display: flex; gap: 5px; }} |
| 84 | .btn-icon {{ background: none; border: none; cursor: pointer; font-size: 16px; padding: 5px; }} |
| 85 | .btn-icon:hover {{ opacity: 0.7; }} |
| 86 | .breadcrumb {{ color: #666; font-size: 14px; }} |
| 87 | </style> |
| 88 | </head> |
| 89 | <body> |
| 90 | <div class="sidebar"><h1>Admin</h1><ul>{nav_html}</ul></div> |
| 91 | <div class="content"> |
| 92 | <div class="header"> |
| 93 | <div> |
| 94 | <h2>👥 Social Graph</h2> |
| 95 | <div class="breadcrumb">Home / Follows</div> |
| 96 | </div> |
| 97 | </div> |
| 98 | |
| 99 | <div class="card"> |
| 100 | <h3 style="margin-bottom: 20px;">Follow Relationships ({len(item_data)})</h3> |
| 101 | <div class="table-container"> |
| 102 | <table> |
| 103 | <thead> |
| 104 | <tr> |
| 105 | <th><input type="checkbox"></th> |
| 106 | <th>Follower</th> |
| 107 | <th></th> |
| 108 | <th>Following</th> |
| 109 | <th>Status</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="7" style="text-align: center; padding: 40px; color: #999;">No follow relationships yet</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()]) |