initial data for android app, api a little simplified
[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.contrib.contenttypes.models import ContentType
6 from django.db import models
7 from django.db.models.signals import pre_delete
8
9 from catalogue.models import Book, Tag
10
11
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)
18
19     class Meta:
20         unique_together = (('content_type', 'object_id'),)
21
22
23 def _pre_delete_handler(sender, instance, **kwargs):
24     """ save deleted objects for change history purposes """
25
26     if sender in (Book, Tag):
27         if sender == Tag:
28             if instance.category in ('book', 'set'):
29                 return
30             else:
31                 category = instance.category
32         else:
33             category = None
34         Deleted.objects.create(type=sender, object_id=instance.id, 
35             created_at=instance.created_at, category=category)
36 pre_delete.connect(_pre_delete_handler)