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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Quick Reference: Appwrite Integration
|
| 2 |
+
|
| 3 |
+
## File Structure
|
| 4 |
+
```
|
| 5 |
+
enise_site/
|
| 6 |
+
├── appwrite_db.py # ← Appwrite wrapper singleton
|
| 7 |
+
├── settings.py # ← Has APPWRITE_* env vars
|
| 8 |
+
└── wsgi.py
|
| 9 |
+
|
| 10 |
+
app_core/
|
| 11 |
+
├── services.py # ← 5 service classes (NEW)
|
| 12 |
+
├── views.py # ← Updated to use services (MODIFIED)
|
| 13 |
+
├── urls.py
|
| 14 |
+
├── models.py # ← Still exists but not used
|
| 15 |
+
└── management/commands/
|
| 16 |
+
├── setup_appwrite_collections.py # ← Create schema (NEW)
|
| 17 |
+
└── seed_appwrite.py # ← Seed data (NEW)
|
| 18 |
+
|
| 19 |
+
templates/
|
| 20 |
+
├── app_core/
|
| 21 |
+
│ ├── index.html
|
| 22 |
+
│ ├── formations.html
|
| 23 |
+
│ └── specialite_detail.html
|
| 24 |
+
|
| 25 |
+
run.sh # ← 6-stage startup (MODIFIED)
|
| 26 |
+
test_appwrite_crud.py # ← Test suite (NEW)
|
| 27 |
+
```
|
| 28 |
+
|
| 29 |
+
## How Data Flows
|
| 30 |
+
|
| 31 |
+
### Reading Data
|
| 32 |
+
```python
|
| 33 |
+
# In views.py
|
| 34 |
+
service = SpecialiteService()
|
| 35 |
+
specialites = service.list_all() # → Appwrite API call → Cloud DB
|
| 36 |
+
```
|
| 37 |
+
|
| 38 |
+
### Service Layer Architecture
|
| 39 |
+
```python
|
| 40 |
+
# app_core/services.py
|
| 41 |
+
SpecialiteService
|
| 42 |
+
├── list_all() # Query with sorting
|
| 43 |
+
├── get_by_slug() # Query with filter
|
| 44 |
+
├── get_by_id() # Direct document fetch
|
| 45 |
+
├── create() # Insert new document
|
| 46 |
+
├── update() # Modify existing
|
| 47 |
+
└── delete() # Remove document
|
| 48 |
+
|
| 49 |
+
ActualiteService, ContactService, PartenairesService, StatistiqueService
|
| 50 |
+
└── (Similar pattern with collection-specific logic)
|
| 51 |
+
```
|
| 52 |
+
|
| 53 |
+
### Appwrite Wrapper Usage
|
| 54 |
+
```python
|
| 55 |
+
# enise_site/appwrite_db.py
|
| 56 |
+
db = get_appwrite_db() # Singleton instance
|
| 57 |
+
db.list_documents('specialites', queries=[Query.order_asc('ordre')])
|
| 58 |
+
db.create_document('contact', data)
|
| 59 |
+
db.update_document('specialites', doc_id, updated_data)
|
| 60 |
+
db.delete_document('contact', doc_id)
|
| 61 |
+
```
|
| 62 |
+
|
| 63 |
+
## Collections Schema
|
| 64 |
+
|
| 65 |
+
### specialites
|
| 66 |
+
- nom: String(100) ✓
|
| 67 |
+
- slug: String(100) ✓
|
| 68 |
+
- description: String(65535) ✓
|
| 69 |
+
- image_url: URL
|
| 70 |
+
- icone: String(50)
|
| 71 |
+
- ordre: Integer
|
| 72 |
+
|
| 73 |
+
### actualites
|
| 74 |
+
- titre: String(200) ✓
|
| 75 |
+
- slug: String(200) ✓
|
| 76 |
+
- contenu: String(65535) ✓
|
| 77 |
+
- image_url: URL
|
| 78 |
+
- date_publication: DateTime ✓
|
| 79 |
+
- est_publie: Boolean ✓
|
| 80 |
+
|
| 81 |
+
### contact
|
| 82 |
+
- nom: String(100) ✓
|
| 83 |
+
- email: Email ✓
|
| 84 |
+
- sujet: String(200) ✓
|
| 85 |
+
- message: String(65535) ✓
|
| 86 |
+
- date_envoi: DateTime ✓
|
| 87 |
+
- traite: Boolean ✓
|
| 88 |
+
|
| 89 |
+
### partenaires
|
| 90 |
+
- nom: String(150) ✓
|
| 91 |
+
- logo_url: URL ✓
|
| 92 |
+
- url: URL
|
| 93 |
+
- type_partenaire: String(50) ✓
|
| 94 |
+
|
| 95 |
+
### statistiques
|
| 96 |
+
- nom: String(100) ✓
|
| 97 |
+
- valeur: String(50) ✓
|
| 98 |
+
- suffixe: String(10)
|
| 99 |
+
- icone: String(50) ✓
|
| 100 |
+
- ordre: Integer ✓
|
| 101 |
+
|
| 102 |
+
## Initial Data
|
| 103 |
+
|
| 104 |
+
### Specialites (3)
|
| 105 |
+
1. Génie Civil - génie-civil
|
| 106 |
+
2. Mécanique - mecanique
|
| 107 |
+
3. Physique - physique
|
| 108 |
+
|
| 109 |
+
### Actualites (3)
|
| 110 |
+
1. Bienvenue à l'ENISE - bienvenue-a-lenise
|
| 111 |
+
2. Événement scientifique 2024 - evenement-scientifique-2024
|
| 112 |
+
3. Internships et stages - internships-et-stages
|
| 113 |
+
|
| 114 |
+
### Statistiques (3)
|
| 115 |
+
1. Étudiants: 1200+
|
| 116 |
+
2. Années d'existence: 50+
|
| 117 |
+
3. Entreprises partenaires: 300+
|
| 118 |
+
|
| 119 |
+
### Partenaires (3)
|
| 120 |
+
1. SNCF - INDUSTRIEL
|
| 121 |
+
2. Université de Lyon - ACADEMIQUE
|
| 122 |
+
3. Région Auvergne-Rhône-Alpes - INSTITUTIONNEL
|
| 123 |
+
|
| 124 |
+
## Environment Variables Required
|
| 125 |
+
|
| 126 |
+
```bash
|
| 127 |
+
# Appwrite Cloud Configuration
|
| 128 |
+
APPWRITE_ENDPOINT=https://fra.cloud.appwrite.io/v1
|
| 129 |
+
APPWRITE_PROJECT_ID=697abaca00272dab718b
|
| 130 |
+
APPWRITE_API_KEY=<your-api-key>
|
| 131 |
+
APPWRITE_DATABASE_ID=697cd79900149b10540c
|
| 132 |
+
|
| 133 |
+
# Django Settings
|
| 134 |
+
DEBUG=False
|
| 135 |
+
SECRET_KEY=<generated>
|
| 136 |
+
ALLOWED_HOSTS=*
|
| 137 |
+
CSRF_TRUSTED_ORIGINS=https://ktongue-enise.hf.space
|
| 138 |
+
```
|
| 139 |
+
|
| 140 |
+
## Testing Locally
|
| 141 |
+
|
| 142 |
+
```bash
|
| 143 |
+
# Run all tests
|
| 144 |
+
python test_appwrite_crud.py
|
| 145 |
+
|
| 146 |
+
# Run specific service tests
|
| 147 |
+
python manage.py shell
|
| 148 |
+
>>> from app_core.services import SpecialiteService
|
| 149 |
+
>>> service = SpecialiteService()
|
| 150 |
+
>>> specialites = service.list_all()
|
| 151 |
+
>>> len(specialites)
|
| 152 |
+
3
|
| 153 |
+
|
| 154 |
+
# Run management commands
|
| 155 |
+
python manage.py setup_appwrite_collections
|
| 156 |
+
python manage.py seed_appwrite
|
| 157 |
+
|
| 158 |
+
# Test views
|
| 159 |
+
python manage.py runserver
|
| 160 |
+
# Visit http://localhost:8000/
|
| 161 |
+
```
|
| 162 |
+
|
| 163 |
+
## Production Deployment
|
| 164 |
+
|
| 165 |
+
```bash
|
| 166 |
+
# run.sh automatically:
|
| 167 |
+
1. Creates migrations
|
| 168 |
+
2. Runs migrations
|
| 169 |
+
3. Calls setup_appwrite_collections
|
| 170 |
+
4. Calls seed_appwrite
|
| 171 |
+
5. Collects static files
|
| 172 |
+
6. Starts Gunicorn
|
| 173 |
+
|
| 174 |
+
# On HF Spaces, restart the space to trigger deployment
|
| 175 |
+
```
|
| 176 |
+
|
| 177 |
+
## Debugging
|
| 178 |
+
|
| 179 |
+
### Check Appwrite Connection
|
| 180 |
+
```python
|
| 181 |
+
from enise_site.appwrite_db import get_appwrite_db
|
| 182 |
+
db = get_appwrite_db()
|
| 183 |
+
db.test_connection() # Returns True/False
|
| 184 |
+
```
|
| 185 |
+
|
| 186 |
+
### List Documents
|
| 187 |
+
```python
|
| 188 |
+
from app_core.services import SpecialiteService
|
| 189 |
+
service = SpecialiteService()
|
| 190 |
+
docs = service.list_all()
|
| 191 |
+
for doc in docs:
|
| 192 |
+
print(doc)
|
| 193 |
+
```
|
| 194 |
+
|
| 195 |
+
### Check Logs
|
| 196 |
+
```bash
|
| 197 |
+
# Local development
|
| 198 |
+
# Check console output for ✅/❌ messages
|
| 199 |
+
|
| 200 |
+
# HF Spaces
|
| 201 |
+
# Check the "Logs" tab in HF Spaces interface
|
| 202 |
+
```
|
| 203 |
+
|
| 204 |
+
## Common Issues
|
| 205 |
+
|
| 206 |
+
### Collections already exist
|
| 207 |
+
- This is expected and handled gracefully
|
| 208 |
+
- The system skips creation and uses existing collections
|
| 209 |
+
|
| 210 |
+
### Data not seeding
|
| 211 |
+
- Check Appwrite API key in environment variables
|
| 212 |
+
- Check database ID is correct
|
| 213 |
+
- Verify collections were created successfully
|
| 214 |
+
|
| 215 |
+
### 404 on specialite detail
|
| 216 |
+
- Verify slug format matches: /specialite/slug-name/
|
| 217 |
+
- Check specialite exists in Appwrite by listing all
|
| 218 |
+
- Verify slug field is populated correctly
|
| 219 |
+
|
| 220 |
+
## Next Steps
|
| 221 |
+
|
| 222 |
+
1. ✅ Appwrite integration complete
|
| 223 |
+
2. ✅ All CRUD operations working
|
| 224 |
+
3. ✅ Data persistence implemented
|
| 225 |
+
4. ⏳ Optional: Debug frontend CSS issues
|
| 226 |
+
5. ⏳ Optional: Migrate admin panel to Appwrite queries
|
| 227 |
+
|
| 228 |
+
## Resources
|
| 229 |
+
|
| 230 |
+
- Appwrite Console: https://console.appwrite.io
|
| 231 |
+
- GitHub Repo: https://github.com/tiffank1802/enise-site-2
|
| 232 |
+
- HF Space: https://huggingface.co/spaces/ktongue/ENISE
|
| 233 |
+
- Documentation: See APPWRITE_INTEGRATION.md
|