У вас есть два способа записи того, что делают пользователи. Это основано на том, что является целью отслеживания, это просто для регистрации или это будет на вашей базе данных, однако сначала можно проанализировать в db (no_sql) или (sql) тоже Вы можете взять этот абстрактный ответ и оптимизировать его в соответствии с вашими бизнес-требованиями и потребностями вашего проекта
Первый способ
views.py
from .utils import get_client_ip
DELETED_FILES_LOGGER = logging.getLogger("deleted_files")
def books_list(request):
books_list = Book.objects.filter(published=True).order_by('-timestamp')
if request.method == 'POST' and request.is_ajax():
id_book = request.POST.get('id_book')
try:
book = books_list.get(id=id_book)
book.delete()
DELETED_FILES_LOGGER.debug(
'DELETION: %s by %s at %s from IP Address %s' % (
book.filename(), request.user,
datetime.datetime.now(), get_client_ip(request)))
except Book.DoesNotExist:
return JsonResponse({'error':True,'msg':'Book not found'})
render(request,'book/books-list.html',context={'books_list':books_list})
утилит.py
def get_client_ip(request):
"""
to get client ip request
:param request:
:return: ip <str>
"""
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
Второй способ
models.py
from django.contrib.admin.models import LogEntry as AbstractLogEntry
class LogEntry(AbstractLogEntry):
class Meta:
proxy = True
app_label = 'data'
verbose_name_plural = 'Log Entries'
permissions = (("add_log", "add log entry"),
("delete_log", "delete log entry"),
("change_log", "change log entry"),
)
def __str__(self):
if self.action_flag == 5:
return ugettext('Download "%(object)s."') % {'object': self.object_repr}
if self.action_flag == 4:
return ugettext('Uploaded "%(object)s."') % {'object': self.object_repr}
elif self.is_addition():
return ugettext('Added "%(object)s".') % {'object': self.object_repr}
elif self.is_change():
return ugettext('Changed "%(object)s" - %(changes)s') % {
'object': self.object_repr,
'changes': self.change_message,
}
elif self.is_deletion():
return ugettext('Deleted "%(object)s."') % {'object': self.object_repr}
views.py
def books_list(request):
books_list = Book.objects.filter(published=True).order_by('-timestamp')
if request.method == 'POST' and request.is_ajax():
id_book = request.POST.get('id_book')
try:
book = books_list.get(id=id_book)
book.delete()
LogEntry.objects.log_action(user_id=request.user.id,
change_message='Upload %s for category %s' % (
book.filename(), book.category),
content_type_id=ContentType.objects.get(model__exact='book').id,
object_id=book.id,
object_repr=request.user.username,
action_flag=4
)
except Book.DoesNotExist:
return JsonResponse({'error':True,'msg':'Book not found'})
render(request,'book/books-list.html',context={'books_list':books_list})
Вы также можете добавить ведение журнала в менеджересам
class BookManager(models.Manager):
def delete(self):
# you logging as mentioned above
super(BookManager).delete()
class Book(models.Model):
.....
....
objects = BookManager()