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:
parent
079e423343
commit
2f51057f4c
@ -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,
|
||||||
|
@ -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,
|
||||||
|
ip=ip,
|
||||||
model=ContentType.objects.get_for_model(instance),
|
model=ContentType.objects.get_for_model(instance),
|
||||||
instance_pk=instance.pk,
|
instance_pk=instance.pk,
|
||||||
previous=previous_json,
|
previous=previous_json,
|
||||||
data=instance_json,
|
data=instance_json,
|
||||||
action=("edit" if previous.exists() else "create")
|
action=("edit" if previous.exists() else "create")
|
||||||
)#.save()
|
).save()
|
||||||
|
|
||||||
|
|
||||||
@receiver(pre_delete)
|
@receiver(pre_delete)
|
||||||
@ -75,21 +79,11 @@ 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,
|
||||||
|
ip=ip,
|
||||||
model=ContentType.objects.get_for_model(instance),
|
model=ContentType.objects.get_for_model(instance),
|
||||||
instance_pk=instance.pk,
|
instance_pk=instance.pk,
|
||||||
previous=instance_json,
|
previous=instance_json,
|
||||||
|
Loading…
Reference in New Issue
Block a user