|
1 | 1 | # Admin Interface |
2 | 2 |
|
3 | | -JsWeb includes a built-in admin interface that allows you to manage your application's data. The admin interface is powered by the `jsweb.admin` module and is highly customizable. |
| 3 | +JsWeb includes a built-in admin interface that allows you to manage your application's data. The admin interface is automatically generated based on your models and is ready for production use out of the box. |
| 4 | + |
| 5 | +## Table of Contents |
| 6 | + |
| 7 | +- [Enabling the Admin Interface](#enabling-the-admin-interface) |
| 8 | +- [Creating Admin Users](#creating-admin-users) |
| 9 | +- [Accessing the Admin Panel](#accessing-the-admin-panel) |
| 10 | +- [Managing Models](#managing-models) |
| 11 | +- [Admin Features](#admin-features) |
| 12 | +- [Security Considerations](#security-considerations) |
| 13 | +- [Customization](#customization) |
| 14 | +- [Best Practices](#best-practices) |
4 | 15 |
|
5 | 16 | ## Enabling the Admin Interface |
6 | 17 |
|
7 | | -To enable the admin interface, you need to create an `Admin` instance and register your models with it. |
| 18 | +To enable the admin interface, create an `Admin` instance and register your models with it: |
8 | 19 |
|
9 | 20 | ```python |
10 | 21 | # app.py |
11 | 22 | from jsweb import JsWebApp |
12 | 23 | from jsweb.admin import Admin |
13 | | -from .models import User, Post |
| 24 | +from .models import User, Post, Category |
14 | 25 | import config |
15 | 26 |
|
16 | 27 | app = JsWebApp(config=config) |
| 28 | + |
| 29 | +# Create admin instance |
17 | 30 | admin = Admin(app) |
18 | 31 |
|
| 32 | +# Register your models |
19 | 33 | admin.register(User) |
20 | 34 | admin.register(Post) |
| 35 | +admin.register(Category) |
21 | 36 | ``` |
22 | 37 |
|
23 | | -This will create an admin interface at the `/admin` URL. |
| 38 | +This will create an admin interface accessible at `/admin`. |
| 39 | + |
| 40 | +!!! tip "Auto-Generated Admin" |
| 41 | + The admin interface is completely auto-generated based on your models. No additional configuration needed! |
24 | 42 |
|
25 | | -## Creating an Admin User |
| 43 | +## Creating Admin Users |
26 | 44 |
|
27 | | -To access the admin interface, you need to create an admin user. You can do this using the `jsweb create-admin` command. |
| 45 | +Before you can access the admin interface, you need to create an admin user: |
28 | 46 |
|
29 | 47 | ```bash |
30 | 48 | jsweb create-admin |
31 | 49 | ``` |
32 | 50 |
|
33 | | -This will prompt you to enter a username, email, and password for the new admin user. |
| 51 | +This command will prompt you to enter: |
| 52 | +- **Username**: Your admin username |
| 53 | +- **Email**: Your admin email address |
| 54 | +- **Password**: A secure password |
| 55 | + |
| 56 | +### Creating Admin Users Programmatically |
| 57 | + |
| 58 | +```python |
| 59 | +from .models import User |
| 60 | + |
| 61 | +# Create admin user with code |
| 62 | +admin_user = User.create( |
| 63 | + username="admin", |
| 64 | + |
| 65 | + password="secure_password", |
| 66 | + is_admin=True |
| 67 | +) |
| 68 | +``` |
| 69 | + |
| 70 | +!!! warning "Security" |
| 71 | + Always use strong passwords. Never hardcode credentials in your application. |
| 72 | + |
| 73 | +## Accessing the Admin Panel |
| 74 | + |
| 75 | +Once an admin user is created, you can access the admin panel: |
| 76 | + |
| 77 | +1. Navigate to `http://your-domain.com/admin` |
| 78 | +2. Log in with your admin credentials |
| 79 | +3. Manage your application's data |
| 80 | + |
| 81 | +## Managing Models |
| 82 | + |
| 83 | +### Viewing Records |
| 84 | + |
| 85 | +The admin interface displays all records in a table format: |
| 86 | +- **List View**: See all records with key fields |
| 87 | +- **Search**: Find specific records |
| 88 | +- **Filtering**: Filter records by field values |
| 89 | +- **Sorting**: Sort by any column |
| 90 | + |
| 91 | +### Creating Records |
| 92 | + |
| 93 | +Click the "Add" or "Create" button to add a new record. A form will appear with all model fields. |
| 94 | + |
| 95 | +### Editing Records |
| 96 | + |
| 97 | +Click on any record in the list to edit its details. The edit form displays: |
| 98 | +- All model fields |
| 99 | +- Field values |
| 100 | +- Validation errors (if any) |
| 101 | + |
| 102 | +### Deleting Records |
| 103 | + |
| 104 | +Click the delete button to remove a record. A confirmation will appear before deletion. |
| 105 | + |
| 106 | +## Admin Features |
| 107 | + |
| 108 | +### Automatic Field Detection |
| 109 | + |
| 110 | +The admin interface automatically detects your model fields and displays appropriate widgets: |
| 111 | + |
| 112 | +| Field Type | Widget | |
| 113 | +|-----------|--------| |
| 114 | +| String | Text input | |
| 115 | +| Integer | Number input | |
| 116 | +| Float | Decimal input | |
| 117 | +| Date | Date picker | |
| 118 | +| DateTime | Date & time picker | |
| 119 | +| Boolean | Checkbox | |
| 120 | +| Text | Textarea | |
| 121 | +| Foreign Key | Dropdown selector | |
| 122 | + |
| 123 | +### Search & Filter |
| 124 | + |
| 125 | +The admin interface provides: |
| 126 | +- **Full-text search** across all fields |
| 127 | +- **Field filtering** by specific values |
| 128 | +- **Boolean filtering** for true/false fields |
| 129 | +- **Relationship filtering** for foreign keys |
| 130 | + |
| 131 | +### Bulk Actions |
| 132 | + |
| 133 | +Perform operations on multiple records: |
| 134 | +- Select multiple records |
| 135 | +- Apply actions (delete, export, etc.) |
| 136 | + |
| 137 | +## Security Considerations |
| 138 | + |
| 139 | +!!! warning "Admin Access Control" |
| 140 | + The admin interface should only be accessible to trusted administrators. Consider: |
| 141 | + |
| 142 | + - Using strong passwords |
| 143 | + - Enabling 2FA (if available) |
| 144 | + - Restricting IP access with a proxy/firewall |
| 145 | + - Using HTTPS in production |
| 146 | + |
| 147 | +!!! danger "Data Protection" |
| 148 | + Be careful when deleting records. Deletions are usually permanent. Consider implementing: |
| 149 | + |
| 150 | + ```python |
| 151 | + # Soft delete approach |
| 152 | + class Post(ModelBase): |
| 153 | + __tablename__ = 'posts' |
| 154 | + title = Column(String(200)) |
| 155 | + deleted_at = Column(DateTime, nullable=True) |
| 156 | + |
| 157 | + @property |
| 158 | + def is_deleted(self): |
| 159 | + return self.deleted_at is not None |
| 160 | + ``` |
| 161 | + |
| 162 | +!!! note "Audit Logging" |
| 163 | + Consider adding audit logs to track admin actions: |
| 164 | + |
| 165 | + ```python |
| 166 | + class AuditLog(ModelBase): |
| 167 | + __tablename__ = 'audit_logs' |
| 168 | + admin_id = Column(Integer, ForeignKey('user.id')) |
| 169 | + action = Column(String(100)) |
| 170 | + model_name = Column(String(100)) |
| 171 | + record_id = Column(Integer) |
| 172 | + timestamp = Column(DateTime, default=datetime.utcnow) |
| 173 | + ``` |
| 174 | + |
| 175 | +## Customization |
| 176 | + |
| 177 | +### Limiting Model Fields |
| 178 | + |
| 179 | +```python |
| 180 | +# Currently not available in base Admin class |
| 181 | +# Feature for future versions |
| 182 | +``` |
| 183 | + |
| 184 | +### Custom Model Lists |
| 185 | + |
| 186 | +```python |
| 187 | +# Custom template override support |
| 188 | +# Feature for future versions |
| 189 | +``` |
| 190 | + |
| 191 | +!!! info "Future Customization" |
| 192 | + More customization options are planned for future versions, such as: |
| 193 | + - Custom column lists |
| 194 | + - Custom filters |
| 195 | + - Inline editing |
| 196 | + - Bulk operations |
| 197 | + |
| 198 | +## Best Practices |
| 199 | + |
| 200 | +!!! tip "Model Organization" |
| 201 | + Keep your models organized in `models.py`: |
| 202 | + |
| 203 | + ```python |
| 204 | + # models.py |
| 205 | + from jsweb.database import ModelBase, Column, Integer, String |
| 206 | + |
| 207 | + class User(ModelBase): |
| 208 | + __tablename__ = 'users' |
| 209 | + # ... fields ... |
| 210 | + |
| 211 | + class Post(ModelBase): |
| 212 | + __tablename__ = 'posts' |
| 213 | + # ... fields ... |
| 214 | + |
| 215 | + # app.py |
| 216 | + from models import User, Post |
| 217 | + admin.register(User) |
| 218 | + admin.register(Post) |
| 219 | + ``` |
| 220 | + |
| 221 | +!!! warning "Register Models Early" |
| 222 | + Register admin models right after creating the Admin instance: |
| 223 | + |
| 224 | + ```python |
| 225 | + admin = Admin(app) |
| 226 | + |
| 227 | + # Register all models |
| 228 | + admin.register(User) |
| 229 | + admin.register(Post) |
| 230 | + admin.register(Category) |
| 231 | + |
| 232 | + # Then define routes |
| 233 | + @app.route("/") |
| 234 | + async def home(req): |
| 235 | + ... |
| 236 | + ``` |
| 237 | + |
| 238 | +!!! success "Use Descriptive Names" |
| 239 | + Use clear, descriptive model names for the admin interface: |
| 240 | + |
| 241 | + ```python |
| 242 | + # Good |
| 243 | + class BlogPost(ModelBase): |
| 244 | + __tablename__ = 'blog_posts' |
| 245 | + |
| 246 | + # Less clear |
| 247 | + class P(ModelBase): |
| 248 | + __tablename__ = 'p' |
| 249 | + ``` |
| 250 | + |
| 251 | +!!! tip "Validation in Models" |
| 252 | + Add validation to your models for better data quality: |
| 253 | + |
| 254 | + ```python |
| 255 | + class User(ModelBase): |
| 256 | + __tablename__ = 'users' |
| 257 | + username = Column(String(80), unique=True, nullable=False) |
| 258 | + email = Column(String(120), unique=True, nullable=False, index=True) |
| 259 | + |
| 260 | + def __repr__(self): |
| 261 | + return f"<User {self.username}>" |
| 262 | + ``` |
34 | 263 |
|
35 | | -## Customizing the Admin Interface |
| 264 | +!!! note "Admin vs Public Interface" |
| 265 | + Keep admin interface separate from public-facing pages: |
| 266 | + |
| 267 | + ``` |
| 268 | + routes/ |
| 269 | + ├── public_routes.py # Public pages |
| 270 | + ├── auth_routes.py # Login/Register |
| 271 | + ├── api_routes.py # API endpoints |
| 272 | + └── admin_routes.py # Admin-only routes (if needed) |
| 273 | + ``` |
36 | 274 |
|
37 | | -The admin interface is automatically generated based on your models. At the moment, there are no specific customization options like `list_display` or `search_fields` available directly in the `Admin` class. However, you can extend the `Admin` class or modify the admin templates to customize the interface. |
|
0 commit comments