exception test
[wolnelektury.git] / src / 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.contrib.contenttypes.models import ContentType
6 from django.db import models
7 from django.db.models.signals import pre_delete
8 from django.utils.translation import ugettext_lazy as _
9
10 from catalogue.models import Book, Tag
11
12
13 class Deleted(models.Model):
14     object_id = models.IntegerField()
15     slug = models.SlugField(_('slug'), max_length=120, blank=True, db_index=True)
16     content_type = models.ForeignKey(ContentType)
17     category = models.CharField(max_length=64, null=True, blank=True, db_index=True)
18     created_at = models.DateTimeField(editable=False, db_index=True)
19     deleted_at = models.DateTimeField(auto_now_add=True, db_index=True)
20
21     class Meta:
22         unique_together = (('content_type', 'object_id'),)
23
24
25 def _pre_delete_handler(sender, instance, **kwargs):
26     """ save deleted objects for change history purposes """
27
28     if sender in (Book, Tag):
29         if sender == Tag:
30             if instance.category in ('book', 'set'):
31                 return
32             else:
33                 category = instance.category
34         else:
35             category = None
36         content_type = ContentType.objects.get_for_model(sender)
37         Deleted.objects.create(
38             content_type=content_type, object_id=instance.id, created_at=instance.created_at, category=category,
39             slug=instance.slug)
40 pre_delete.connect(_pre_delete_handler)