NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

from django.db import models
from django.contrib.auth.models import User
from django.utils.timezone import now

class AuditLog(models.Model):
ACTION_CHOICES = [
('CREATE', 'Create'),
('UPDATE', 'Update'),
('DELETE', 'Delete'),
('LOGIN', 'Login'),
('LOGOUT', 'Logout'),
]

user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True, related_name='audit_logs')
action = models.CharField(max_length=10, choices=ACTION_CHOICES)
model_name = models.CharField(max_length=100) # E.g., Task, Category
object_id = models.IntegerField() # ID of the affected object
object_repr = models.CharField(max_length=255) # String representation of the object
timestamp = models.DateTimeField(default=now)
ip_address = models.GenericIPAddressField(null=True, blank=True)
details = models.TextField(blank=True) # Additional info about the action

def __str__(self):
return f"{self.user} performed {self.action} on {self.model_name} (ID: {self.object_id}) at {self.timestamp}"



class CaptureIPAddressMiddleware:
def __init__(self, get_response):
self.get_response = get_response

def __call__(self, request):
ip = self.get_client_ip(request)
request.META['REMOTE_ADDR'] = ip
return self.get_response(request)

def get_client_ip(self, request):
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
if x_forwarded_for:
ip = x_forwarded_for.split(',')[0]
else:
ip = request.META.get('REMOTE_ADDR')
return ip


MIDDLEWARE += ['your_app.middleware.CaptureIPAddressMiddleware']



from django.db.models.signals import post_save, post_delete
from django.dispatch import receiver
from .models import Task, AuditLog

@receiver(post_save, sender=Task)
def log_task_save(sender, instance, created, **kwargs):
action = 'CREATE' if created else 'UPDATE'
AuditLog.objects.create(
user=instance.user,
action=action,
model_name=sender.__name__,
object_id=instance.id,
object_repr=str(instance),
ip_address=get_ip_from_request(),
details=f"Task '{instance.title}' was {'created' if created else 'updated'}."
)

@receiver(post_delete, sender=Task)
def log_task_delete(sender, instance, **kwargs):
AuditLog.objects.create(
user=instance.user,
action='DELETE',
model_name=sender.__name__,
object_id=instance.id,
object_repr=str(instance),
ip_address=get_ip_from_request(),
details=f"Task '{instance.title}' was deleted."
)

def get_ip_from_request():
# Mocked IP for illustration. Use `request.META['REMOTE_ADDR']` in real cases.
return '127.0.0.1'


from django.apps import AppConfig

class YourAppConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'your_app'

def ready(self):
import your_app.signals


from django.shortcuts import render
from .models import AuditLog

def audit_logs_view(request):
logs = AuditLog.objects.order_by('-timestamp')
return render(request, 'admin_audit_logs.html', {'logs': logs})




     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.