mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-20 11:51:10 -05:00
Implement historical tracking using django-pghistory; add middleware for context capture and update model architecture
This commit is contained in:
45
memory-bank/decisions/pghistory-integration.md
Normal file
45
memory-bank/decisions/pghistory-integration.md
Normal file
@@ -0,0 +1,45 @@
|
||||
## Decision: Universal Model History via django-pghistory
|
||||
|
||||
### Pattern Implementation
|
||||
- **Tracking Method**: `pghistory.Snapshot()` applied to all concrete models
|
||||
- **Inheritance Strategy**: Base model class with history tracking
|
||||
- **Context Capture**:
|
||||
```python
|
||||
# core/models.py
|
||||
import pghistory
|
||||
|
||||
class HistoricalModel(models.Model):
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
@pghistory.track(pghistory.Snapshot())
|
||||
def save(self, *args, **kwargs):
|
||||
return super().save(*args, **kwargs)
|
||||
```
|
||||
|
||||
### Integration Scope
|
||||
1. **Model Layer**:
|
||||
- All concrete models inherit from `HistoricalModel`
|
||||
- Automatic event labeling:
|
||||
```python
|
||||
@pghistory.track(
|
||||
pghistory.Snapshot('model.create'),
|
||||
pghistory.AfterInsert('model.update'),
|
||||
pghistory.BeforeDelete('model.delete')
|
||||
)
|
||||
```
|
||||
|
||||
2. **Context Middleware**:
|
||||
```python
|
||||
# core/middleware.py
|
||||
pghistory.context(lambda request: {
|
||||
'user': str(request.user) if request.user.is_authenticated else None,
|
||||
'ip': request.META.get('REMOTE_ADDR'),
|
||||
'user_agent': request.META.get('HTTP_USER_AGENT'),
|
||||
'session_key': request.session.session_key
|
||||
})
|
||||
```
|
||||
|
||||
3. **Admin Integration**:
|
||||
- Custom history view for Django Admin
|
||||
- Version comparison interface
|
||||
Reference in New Issue
Block a user