Python 3
[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.conf import settings
6 from django.contrib.auth.models import User
7 from django.contrib.contenttypes.models import ContentType
8 from django.db import models
9 from django.db.models.signals import pre_delete
10 from django.utils.translation import ugettext_lazy as _
11
12 from catalogue.models import Book, Tag
13
14
15 class Deleted(models.Model):
16     object_id = models.IntegerField()
17     slug = models.SlugField(_('slug'), max_length=120, blank=True, db_index=True)
18     content_type = models.ForeignKey(ContentType)
19     category = models.CharField(max_length=64, null=True, blank=True, db_index=True)
20     created_at = models.DateTimeField(editable=False, db_index=True)
21     deleted_at = models.DateTimeField(auto_now_add=True, db_index=True)
22
23     class Meta:
24         unique_together = (('content_type', 'object_id'),)
25
26
27 def _pre_delete_handler(sender, instance, **kwargs):
28     """ save deleted objects for change history purposes """
29
30     if sender in (Book, Tag):
31         if sender == Tag:
32             if instance.category in ('book', 'set'):
33                 return
34             else:
35                 category = instance.category
36         else:
37             category = None
38         content_type = ContentType.objects.get_for_model(sender)
39         Deleted.objects.create(
40             content_type=content_type, object_id=instance.id, created_at=instance.created_at, category=category,
41             slug=instance.slug)
42 pre_delete.connect(_pre_delete_handler)
43
44
45 class BookUserData(models.Model):
46     book = models.ForeignKey(Book)
47     user = models.ForeignKey(User)
48     complete = models.BooleanField(default=False)
49     last_changed = models.DateTimeField(auto_now=True)
50
51     @property
52     def state(self):
53         return 'complete' if self.complete else 'reading'
54
55     @classmethod
56     def update(cls, book, user, state):
57         instance, created = cls.objects.get_or_create(book=book, user=user)
58         instance.complete = state == 'complete'
59         instance.save()
60         return instance
61 from django.conf import settings
62
63
64 KEY_SIZE = 18
65 SECRET_SIZE = 32
66
67 CONSUMER_STATES = (
68     ('pending', 'Pending approval'),
69     ('accepted', 'Accepted'),
70     ('canceled', 'Canceled'),
71 )
72
73
74 class Nonce(models.Model):
75     token_key = models.CharField(max_length=KEY_SIZE)
76     consumer_key = models.CharField(max_length=KEY_SIZE)
77     key = models.CharField(max_length=255)
78
79     def __str__(self):
80         return u"Nonce %s for %s" % (self.key, self.consumer_key)
81
82
83 class Consumer(models.Model):
84     name = models.CharField(max_length=255)
85     description = models.TextField()
86     key = models.CharField(max_length=KEY_SIZE)
87     secret = models.CharField(max_length=SECRET_SIZE)
88     status = models.CharField(max_length=16, choices=CONSUMER_STATES, default='pending')
89     user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True, related_name='consumers')
90
91     def __str__(self):
92         return u"Consumer %s with key %s" % (self.name, self.key)
93
94
95 class Token(models.Model):
96     REQUEST = 1
97     ACCESS = 2
98     TOKEN_TYPES = ((REQUEST, u'Request'), (ACCESS, u'Access'))
99
100     key = models.CharField(max_length=KEY_SIZE)
101     secret = models.CharField(max_length=SECRET_SIZE)
102     token_type = models.IntegerField(choices=TOKEN_TYPES)
103     timestamp = models.IntegerField()
104     is_approved = models.BooleanField(default=False)
105     user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True, related_name='tokens')
106     consumer = models.ForeignKey(Consumer)
107
108     def __str__(self):
109         return u"%s Token %s for %s" % (self.get_token_type_display(), self.key, self.consumer)