|
| 1 | +Canvas Canvas |
| 2 | +You are an expert in Python, Django, scalable web development, and Django SuperApp structure. |
| 3 | + |
| 4 | +## Key Principles |
| 5 | +- Prioritize readability, maintainability, and Django best practices (PEP 8 compliance). |
| 6 | +- Modular structure: organize code using Django apps within SuperApp for clear separation and reuse. |
| 7 | +- Leverage built-in Django features; avoid raw SQL, prefer Django ORM. |
| 8 | + |
| 9 | +## Django SuperApp Structure |
| 10 | +- Quickly bootstrap projects using pre-built standalone apps. |
| 11 | +- Each app includes independent `settings.py` and `urls.py` automatically integrated by the SuperApp system. |
| 12 | + |
| 13 | +### App Settings Integration |
| 14 | +`superapp/apps/<app_name>/settings.py` |
| 15 | +```python |
| 16 | +def extend_superapp_settings(main_settings): |
| 17 | + main_settings['INSTALLED_APPS'] += ['superapp.apps.sample_app'] |
| 18 | +``` |
| 19 | + |
| 20 | +### App URLs Integration |
| 21 | +`superapp/apps/<app_name>/urls.py` |
| 22 | +```python |
| 23 | +from django.urls import path |
| 24 | +from superapp.apps.sample_app.views import hello_world |
| 25 | + |
| 26 | +def extend_superapp_urlpatterns(main_urlpatterns): |
| 27 | + main_urlpatterns += [path('hello_world/', hello_world)] |
| 28 | +``` |
| 29 | + |
| 30 | +### App-Specific Requirements |
| 31 | +Each app can define its own Python dependencies in: |
| 32 | +`superapp/apps/<app_name>/requirements.txt` |
| 33 | + |
| 34 | +These requirements should be installed during deployment or development setup if the app is used. The SuperApp system may include tooling to auto-install these when apps are enabled. |
| 35 | + |
| 36 | +## Django Guidelines |
| 37 | +- Use CBVs for complex logic, FBVs for simple tasks. |
| 38 | +- Keep business logic in models/forms; views should handle requests/responses only. |
| 39 | +- Utilize built-in authentication and forms/models validation. |
| 40 | +- Follow strict MVT separation. |
| 41 | +- Implement middleware strategically for authentication, logging, caching. |
| 42 | +- Error handling via built-in mechanisms; customize error pages. |
| 43 | +- Leverage signals for decoupled logging/error handling. |
| 44 | +- Do not update migrations in the `migrations` folder. Update migrations only if there are exceptions or issues inside of it. |
| 45 | + |
| 46 | +## Admin Integration |
| 47 | + |
| 48 | +### Use `django-unfold` and SuperApp admin site with Sidebar Navigation |
| 49 | + |
| 50 | +- Admins must live in: `superapp/apps/<app_name>/admin/<model_name_slug>.py` |
| 51 | +- Models must live in: `superapp/apps/<app_name>/models/<model_name_slug>.py` |
| 52 | +- Signals must live in: `superapp/apps/<app_name>/signals/<model_name_slug>.py` |
| 53 | +- Services must live in: `superapp/apps/<app_name>/services/<service_name>.py` |
| 54 | +- Views must live in: `superapp/apps/<app_name>/views/<view_name>.py` |
| 55 | +- Tasks must live in: `superapp/apps/<app_name>/tasks/<task_name>.py` |
| 56 | +- Other things must live in: `superapp/apps/<app_name>/<other>/<view_name>.py` |
| 57 | +- Usually each of the above folders will have an `__init__.py` file to make them packages or to export `__all__` so make sure to keep it up to date. |
| 58 | +- Register using `superapp_admin_site` from `superapp.apps.admin_portal.sites` |
| 59 | +- Use `SuperAppModelAdmin` based on `unfold.admin.ModelAdmin` |
| 60 | +- Prefer `autocomplete_fields` for `ForeignKey` and `ManyToManyField` |
| 61 | + |
| 62 | +### Add Admin Navigation |
| 63 | + |
| 64 | +Each app should configure sidebar entries under `UNFOLD['SIDEBAR']['navigation']` in its `settings.py`. |
| 65 | + |
| 66 | +Example: |
| 67 | +```python |
| 68 | +from django.utils.translation import gettext_lazy as _ |
| 69 | +from django.urls import reverse_lazy |
| 70 | + |
| 71 | +def extend_superapp_settings(main_settings): |
| 72 | + main_settings['INSTALLED_APPS'] += ['superapp.apps.sample_app'] |
| 73 | + |
| 74 | + main_settings['UNFOLD']['SIDEBAR']['navigation'] = [ |
| 75 | + { |
| 76 | + "title": _("Sample App"), |
| 77 | + "icon": "extension", |
| 78 | + "items": [ |
| 79 | + { |
| 80 | + "title": lambda request: _("Sample Models"), |
| 81 | + "icon": "table_rows", |
| 82 | + "link": reverse_lazy("admin:sample_app_samplemodel_changelist"), |
| 83 | + "permission": lambda request: request.user.has_perm("sample_app.view_samplemodel"), |
| 84 | + }, |
| 85 | + ] |
| 86 | + }, |
| 87 | + ] |
| 88 | +``` |
| 89 | +Place this logic inside `superapp/apps/<app_name>/settings.py` within `extend_superapp_settings()`. |
| 90 | + |
| 91 | +Example: `superapp/apps/sample_app/admin/sample_model.py` |
| 92 | +```python |
| 93 | +from superapp.apps.admin_portal.admin import SuperAppModelAdmin |
| 94 | +from superapp.apps.admin_portal.sites import superapp_admin_site |
| 95 | +from .models import SampleModel |
| 96 | + |
| 97 | +@admin.register(SampleModel, site=superapp_admin_site) |
| 98 | +class SampleModelAdmin(SuperAppModelAdmin): |
| 99 | + list_display = ['slug', 'name', 'created_at', 'updated_at'] |
| 100 | + search_fields = ['name__slug', 'slug'] |
| 101 | + autocomplete_fields = ['name'] |
| 102 | +``` |
| 103 | + |
| 104 | +## Dependencies |
| 105 | +- Django |
| 106 | +- Django REST Framework (APIs) |
| 107 | +- Celery (background tasks) |
| 108 | +- Redis (caching/task queues) |
| 109 | +- PostgreSQL/MySQL (production databases) |
| 110 | +- django-unfold (admin UI) |
| 111 | + |
| 112 | +## Performance & Security |
| 113 | +- Optimize ORM queries (`select_related`, `prefetch_related`). |
| 114 | +- Use caching framework (Redis/Memcached). |
| 115 | +- Apply async views/background tasks (Celery). |
| 116 | +- Enforce Django security best practices (CSRF, XSS, SQL injection protections). |
| 117 | + |
| 118 | +## Translation Convention |
| 119 | +- Add translation items explicitly in `AppTranslations` from `backend/superapp/apps/easywindow/enums.py`. |
| 120 | + |
| 121 | +Follow Django and SuperApp documentation for detailed practices. |
| 122 | + |
| 123 | +## Environment Variables |
| 124 | +Use `.env.local.example` and `.env.example` files to manage environment variables for local development, deployment templates, and Docker containers, respectively. The AI agent should take these into account when generating or configuring project settings. |
| 125 | + |
0 commit comments