8858162e6c53fddb465abe3e37adbd2e08799d76
[wolnelektury.git] / apps / api / models.py
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.
4 #
5 from django.db import models
6 from django.db.models.signals import pre_delete
7
8 from catalogue.models import Book, Tag
9
10
11 class Deleted(models.Model):
12     object_id = models.IntegerField()
13     type = models.CharField(max_length="50", db_index=True)
14     created_at = models.DateTimeField(editable=False, db_index=True)
15     deleted_at = models.DateTimeField(auto_now_add=True, db_index=True)
16
17     class Meta:
18         unique_together = (('type', 'object_id'),)
19
20
21
22 def _pre_delete_handler(sender, instance, **kwargs):
23     """ save deleted objects for change history purposes """
24
25     if sender in (Book, Tag):
26         Deleted.objects.create(type=sender.__name__, object_id=instance.id, created_at=instance.created_at)
27 pre_delete.connect(_pre_delete_handler)