-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from KHIT93/v2-compliance-app
New feature: Compliance
- Loading branch information
Showing
32 changed files
with
831 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class ComplianceConfig(AppConfig): | ||
name = 'compliance' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import threading | ||
import time | ||
from functools import partial | ||
from django.apps import apps | ||
from django.conf import settings | ||
from django.db.models.signals import pre_save | ||
from django.utils.deprecation import MiddlewareMixin | ||
from .models import DataLogEntry | ||
|
||
threadlocal = threading.local() | ||
|
||
|
||
class DataLogMiddleware(MiddlewareMixin): | ||
""" | ||
Middleware to couple the request's user to log items. This is accomplished by currying the signal receiver with the | ||
user from the request (or None if the user is not authenticated). | ||
""" | ||
|
||
def process_request(self, request): | ||
""" | ||
Gets the current user from the request and prepares and connects a signal receiver with the user already | ||
attached to it. | ||
""" | ||
# Initialize thread local storage | ||
threadlocal.datalog = { | ||
'signal_duid': (self.__class__, time.time()), | ||
'remote_addr': request.META.get('REMOTE_ADDR'), | ||
} | ||
|
||
# In case of proxy, set 'original' address | ||
if request.META.get('HTTP_X_FORWARDED_FOR'): | ||
threadlocal.datalog['remote_addr'] = request.META.get('HTTP_X_FORWARDED_FOR').split(',')[0] | ||
|
||
# Connect signal for automatic logging | ||
if hasattr(request, 'user') and getattr(request.user, 'is_authenticated', False): | ||
set_actor = partial(self.set_actor, user=request.user, signal_duid=threadlocal.datalog['signal_duid']) | ||
pre_save.connect(set_actor, sender=DataLogEntry, dispatch_uid=threadlocal.datalog['signal_duid'], weak=False) | ||
|
||
def process_response(self, request, response): | ||
""" | ||
Disconnects the signal receiver to prevent it from staying active. | ||
""" | ||
if hasattr(threadlocal, 'datalog'): | ||
pre_save.disconnect(sender=DataLogEntry, dispatch_uid=threadlocal.datalog['signal_duid']) | ||
|
||
return response | ||
|
||
def process_exception(self, request, exception): | ||
""" | ||
Disconnects the signal receiver to prevent it from staying active in case of an exception. | ||
""" | ||
if hasattr(threadlocal, 'datalog'): | ||
pre_save.disconnect(sender=DataLogEntry, dispatch_uid=threadlocal.datalog['signal_duid']) | ||
|
||
return None | ||
|
||
@staticmethod | ||
def set_actor(user, sender, instance, signal_duid, **kwargs): | ||
""" | ||
Signal receiver with an extra, required 'user' kwarg. This method becomes a real (valid) signal receiver when | ||
it is curried with the actor. | ||
""" | ||
if hasattr(threadlocal, 'datalog'): | ||
if signal_duid != threadlocal.datalog['signal_duid']: | ||
return | ||
try: | ||
app_label, model_name = settings.AUTH_USER_MODEL.split('.') | ||
auth_user_model = apps.get_model(app_label, model_name) | ||
except ValueError: | ||
auth_user_model = apps.get_model('auth', 'user') | ||
if sender == DataLogEntry and isinstance(user, auth_user_model) and instance.actor is None: | ||
instance.actor = user | ||
|
||
instance.remote_addr = threadlocal.datalog['remote_addr'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Generated by Django 3.1.1 on 2020-09-07 19:16 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
('contenttypes', '0002_remove_content_type_name'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='DataLogEntry', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('object_pk', models.CharField(db_index=True, max_length=255, verbose_name='object pk')), | ||
('object_id', models.BigIntegerField(blank=True, db_index=True, null=True, verbose_name='object id')), | ||
('object_repr', models.TextField(verbose_name='object representation')), | ||
('action', models.CharField(blank=True, choices=[('created', 'create'), ('updated', 'update'), ('deleted', 'delete')], max_length=32, null=True, verbose_name='action')), | ||
('changes', models.TextField(blank=True, verbose_name='change message')), | ||
('remote_addr', models.GenericIPAddressField(blank=True, null=True, verbose_name='remote address')), | ||
('timestamp', models.DateTimeField(auto_now_add=True, verbose_name='timestamp')), | ||
('additional_data', models.JSONField(blank=True, null=True, verbose_name='additional data')), | ||
('actor', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to=settings.AUTH_USER_MODEL, verbose_name='actor')), | ||
('content_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='+', to='contenttypes.contenttype', verbose_name='content type')), | ||
], | ||
options={ | ||
'verbose_name': 'datalog entry', | ||
'verbose_name_plural': 'datalog entries', | ||
'ordering': ['-timestamp'], | ||
'get_latest_by': 'timestamp', | ||
}, | ||
), | ||
] |
Empty file.
Oops, something went wrong.