1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 from django.contrib.contenttypes.models import ContentType
6 from django.db import models
7 from django.db.models.signals import pre_delete
9 from catalogue.models import Book, Tag
12 class Deleted(models.Model):
13 object_id = models.IntegerField()
14 content_type = models.ForeignKey(ContentType)
15 category = models.CharField(max_length=64, null=True, blank=True, db_index=True)
16 created_at = models.DateTimeField(editable=False, db_index=True)
17 deleted_at = models.DateTimeField(auto_now_add=True, db_index=True)
20 unique_together = (('content_type', 'object_id'),)
23 def _pre_delete_handler(sender, instance, **kwargs):
24 """ save deleted objects for change history purposes """
26 if sender in (Book, Tag):
28 if instance.category in ('book', 'set'):
31 category = instance.category
34 content_type = ContentType.objects.get_for_model(sender)
35 Deleted.objects.create(content_type=content_type, object_id=instance.id,
36 created_at=instance.created_at, category=category)
37 pre_delete.connect(_pre_delete_handler)