Generic and secure logging for changes to Django model instances
Project description
# django-model-logging
Logging for changes made to Django model instances.
## Installation
`pip install django-model-logging`
Add `model_logging` to your `INSTALLED_APPS`.
This library uses [`django-pgcrypto-fields`](https://github.com/incuna/django-pgcrypto-fields),
which means you will need PGP public and private keys. See the `settings.configure()`
block in `model_logging/tests/run.py` for an (extremely insecure) example.
## Usage
#### Low-level use
```python
from model_logging.models import LogEntry
LogEntry.objects.log(
log_entry_creator,
operation,
model,
user,
json_data,
)
```
The parameters are as follows:
* `log_entry_creator`: The user who made this change.
* `operation`: One of `LogEntry.OPERATION_ADDED`, `LogEntry.OPERATION_REMOVED`,
or `LogEntry.OPERATION_MODIFIED`.
* `model`: The path to the model being logged (e.g. 'users.models.User').
* `user`: The user to which the model instance belongs.
* `json_data`: A full or partial JSON representation of data on the model instance.
#### Medium-level use
To add methods to a view(set) that can be used to straightforwardly log changes:
```python
from model_logging.views import LoggingMethodMixin
class AViewOrViewset(LoggingMethodMixin, ModelViewSet):
def _get_logging_user(self):
# Override this method to return a suitable
# value for the `user` parameter above.
return self.instance.user # or similar
def extra_data(self):
# Overriding this isn't mandatory, it's just a hook
return {'any additional data': 'you wish to log'}
```
The class now has access to the following:
```python
def log(self, operation, data):
# A simplified version of LogEntry.objects.log,
# with some parameters pre-filled. The return
# value of extra_data() will be added to the
# supplied data.
def _log_on_create(self, serializer):
# Log a LogEntry.OPERATION_ADDED change, using
# the log() method above.
def _log_on_update(self, serializer):
# Log a LogEntry.OPERATION_MODIFIED change, using
# the log() method above.
def _log_on_destroy(self, instance):
# Log a LogEntry.OPERATION_DELETED change, using
# the log() method above.
```
More abstract still:
#### High-level use
A viewset can log its own changes!
```python
from model_logging.views import LoggingViewSetMixin
class AVeryShinyViewSet(LoggingViewSetMixin, ModelViewSet):
def _get_logging_user(self):
# Override this method to return a suitable
# value for the `user` parameter above.
return self.instance.user # or similar
def extra_data(self):
# Overriding this isn't mandatory, it's just a hook
return {'any additional data': 'you wish to log'}
```
This mixin is a wrapper around `LoggingMethodMixin` that calls the appropriate logging
methods during `perform_create`, `perform_update` and `perform_destroy`.
Logging for changes made to Django model instances.
## Installation
`pip install django-model-logging`
Add `model_logging` to your `INSTALLED_APPS`.
This library uses [`django-pgcrypto-fields`](https://github.com/incuna/django-pgcrypto-fields),
which means you will need PGP public and private keys. See the `settings.configure()`
block in `model_logging/tests/run.py` for an (extremely insecure) example.
## Usage
#### Low-level use
```python
from model_logging.models import LogEntry
LogEntry.objects.log(
log_entry_creator,
operation,
model,
user,
json_data,
)
```
The parameters are as follows:
* `log_entry_creator`: The user who made this change.
* `operation`: One of `LogEntry.OPERATION_ADDED`, `LogEntry.OPERATION_REMOVED`,
or `LogEntry.OPERATION_MODIFIED`.
* `model`: The path to the model being logged (e.g. 'users.models.User').
* `user`: The user to which the model instance belongs.
* `json_data`: A full or partial JSON representation of data on the model instance.
#### Medium-level use
To add methods to a view(set) that can be used to straightforwardly log changes:
```python
from model_logging.views import LoggingMethodMixin
class AViewOrViewset(LoggingMethodMixin, ModelViewSet):
def _get_logging_user(self):
# Override this method to return a suitable
# value for the `user` parameter above.
return self.instance.user # or similar
def extra_data(self):
# Overriding this isn't mandatory, it's just a hook
return {'any additional data': 'you wish to log'}
```
The class now has access to the following:
```python
def log(self, operation, data):
# A simplified version of LogEntry.objects.log,
# with some parameters pre-filled. The return
# value of extra_data() will be added to the
# supplied data.
def _log_on_create(self, serializer):
# Log a LogEntry.OPERATION_ADDED change, using
# the log() method above.
def _log_on_update(self, serializer):
# Log a LogEntry.OPERATION_MODIFIED change, using
# the log() method above.
def _log_on_destroy(self, instance):
# Log a LogEntry.OPERATION_DELETED change, using
# the log() method above.
```
More abstract still:
#### High-level use
A viewset can log its own changes!
```python
from model_logging.views import LoggingViewSetMixin
class AVeryShinyViewSet(LoggingViewSetMixin, ModelViewSet):
def _get_logging_user(self):
# Override this method to return a suitable
# value for the `user` parameter above.
return self.instance.user # or similar
def extra_data(self):
# Overriding this isn't mandatory, it's just a hook
return {'any additional data': 'you wish to log'}
```
This mixin is a wrapper around `LoggingMethodMixin` that calls the appropriate logging
methods during `perform_create`, `perform_update` and `perform_destroy`.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django-model-logging-0.1.0.tar.gz
.
File metadata
- Download URL: django-model-logging-0.1.0.tar.gz
- Upload date:
- Size: 7.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 492eef3a5b2e1b6d26c10d0986dc91aa913609ad412a75dd037bbe659d337336 |
|
MD5 | da1ea67401f5f03980bb77d3b666d7a4 |
|
BLAKE2b-256 | 457a99e4b3627e9fe2b803fd633facdcef00d52ddbfd50f824ffbd98b77f713f |
File details
Details for the file django_model_logging-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: django_model_logging-0.1.0-py3-none-any.whl
- Upload date:
- Size: 12.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 04b4a2a6e8edf2d94f57e298137b42df2c71ba49a187a5ff4edda87eba35403f |
|
MD5 | c34c21f6c0fbf7b129ae938ec860dc5d |
|
BLAKE2b-256 | 8156d1d02b5c2e65fee2560c178bf188863b8e52431cf889ac079523cd8f122b |