X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/5207c4d8ee23d6d5df532faa8103a34b1572e45f..9f5432c9c13b7b44d9f28d98807ff41311c6cce8:/apps/api/models.py diff --git a/apps/api/models.py b/apps/api/models.py index 8858162e6..2cdc74a4b 100644 --- a/apps/api/models.py +++ b/apps/api/models.py @@ -2,6 +2,7 @@ # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later. # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # +from django.contrib.contenttypes.models import ContentType from django.db import models from django.db.models.signals import pre_delete @@ -10,18 +11,27 @@ from catalogue.models import Book, Tag class Deleted(models.Model): object_id = models.IntegerField() - type = models.CharField(max_length="50", db_index=True) + content_type = models.ForeignKey(ContentType) + category = models.CharField(max_length=64, null=True, blank=True, db_index=True) created_at = models.DateTimeField(editable=False, db_index=True) deleted_at = models.DateTimeField(auto_now_add=True, db_index=True) class Meta: - unique_together = (('type', 'object_id'),) - + unique_together = (('content_type', 'object_id'),) def _pre_delete_handler(sender, instance, **kwargs): """ save deleted objects for change history purposes """ if sender in (Book, Tag): - Deleted.objects.create(type=sender.__name__, object_id=instance.id, created_at=instance.created_at) + if sender == Tag: + if instance.category in ('book', 'set'): + return + else: + category = instance.category + else: + category = None + content_type = ContentType.objects.get_for_model(sender) + Deleted.objects.create(content_type=content_type, object_id=instance.id, + created_at=instance.created_at, category=category) pre_delete.connect(_pre_delete_handler)