1
0
mirror of https://gitlab.crans.org/bde/nk20 synced 2024-11-26 18:37:12 +00:00

Add IP address to migrations

This commit is contained in:
Yohann D'ANELLO 2020-02-27 14:47:34 +01:00
parent 079e423343
commit 2f51057f4c
2 changed files with 37 additions and 37 deletions

View File

@ -21,6 +21,12 @@ class Changelog(models.Model):
verbose_name=_('user'), verbose_name=_('user'),
) )
ip = models.GenericIPAddressField(
null=True,
blank=True,
verbose_name=_("IP Address")
)
model = models.ForeignKey( model = models.ForeignKey(
ContentType, ContentType,
on_delete=models.PROTECT, on_delete=models.PROTECT,

View File

@ -10,7 +10,7 @@ from django.dispatch import receiver
from .models import Changelog from .models import Changelog
def get_request_in_signal(sender, **kwargs): def get_request_in_signal(sender):
req = None req = None
for entry in reversed(inspect.stack()): for entry in reversed(inspect.stack()):
try: try:
@ -28,6 +28,20 @@ def get_request_in_signal(sender, **kwargs):
return req return req
def get_user_and_ip(sender):
req = get_request_in_signal(sender)
try:
user = req.user
if 'HTTP_X_FORWARDED_FOR' in req.META:
ip = req.META.get('HTTP_X_FORWARDED_FOR')
else:
ip = req.META.get('REMOTE_ADDR')
except:
user = None
ip = None
return user, ip
EXCLUDED = [ EXCLUDED = [
'changelog', 'changelog',
'migration', 'migration',
@ -44,29 +58,19 @@ def save_object(sender, instance, **kwargs):
previous = sender.objects.filter(pk=instance.pk).all() previous = sender.objects.filter(pk=instance.pk).all()
req = get_request_in_signal(sender, **kwargs) user, ip = get_user_and_ip(sender)
try:
user = req.user
if 'X-Real-Ip' in req.headers:
ip = req.headers.get('X-Real-Ip')
else:
ip = req.headers.get('REMOTE_ADDR')
print(ip)
print(req.headers)
except:
user = None
ip = None
from rest_framework.renderers import JSONRenderer from rest_framework.renderers import JSONRenderer
previous_json = JSONRenderer().render(previous) previous_json = JSONRenderer().render(previous)
instance_json = JSONRenderer().render(instance) instance_json = JSONRenderer().render(instance)
Changelog.objects.create(user=user, Changelog.objects.create(user=user,
model=ContentType.objects.get_for_model(instance), ip=ip,
instance_pk=instance.pk, model=ContentType.objects.get_for_model(instance),
previous=previous_json, instance_pk=instance.pk,
data=instance_json, previous=previous_json,
action=("edit" if previous.exists() else "create") data=instance_json,
)#.save() action=("edit" if previous.exists() else "create")
).save()
@receiver(pre_delete) @receiver(pre_delete)
@ -75,24 +79,14 @@ def delete_object(sender, instance, **kwargs):
if model_name.lower() in EXCLUDED: if model_name.lower() in EXCLUDED:
return return
req = get_request_in_signal(sender, **kwargs) user, ip = get_user_and_ip(sender)
try:
user = req.user
if 'X-Real-Ip' in req.headers:
ip = req.headers.get('X-Real-Ip')
else:
ip = req.headers.get('REMOTE_ADDR')
print(ip)
print(req.headers)
except:
user = None
ip = None
instance_json = serializers.serialize('json', [instance, ])[1:-1] instance_json = serializers.serialize('json', [instance, ])[1:-1]
Changelog.objects.create(user=user, Changelog.objects.create(user=user,
model=ContentType.objects.get_for_model(instance), ip=ip,
instance_pk=instance.pk, model=ContentType.objects.get_for_model(instance),
previous=instance_json, instance_pk=instance.pk,
data=None, previous=instance_json,
action="delete" data=None,
).save() action="delete"
).save()