1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 from django.conf import settings
5 from django.contrib.auth.models import User
6 from django.contrib.contenttypes.models import ContentType
7 from django.db import models
8 from django.db.models.signals import pre_delete
9 from django.utils.translation import gettext_lazy as _
11 from catalogue.models import Book, Tag
14 class Deleted(models.Model):
15 object_id = models.IntegerField()
16 slug = models.SlugField(_('slug'), max_length=120, blank=True, db_index=True)
17 content_type = models.ForeignKey(ContentType, models.CASCADE)
18 category = models.CharField(max_length=64, null=True, blank=True, db_index=True)
19 created_at = models.DateTimeField(editable=False, db_index=True)
20 deleted_at = models.DateTimeField(auto_now_add=True, db_index=True)
23 unique_together = (('content_type', 'object_id'),)
26 def _pre_delete_handler(sender, instance, **kwargs):
27 """ save deleted objects for change history purposes """
29 if sender in (Book, Tag):
31 if instance.category in ('book', 'set'):
33 category = instance.category
36 content_type = ContentType.objects.get_for_model(sender)
37 Deleted.objects.create(
38 content_type=content_type,
39 object_id=instance.id,
40 created_at=instance.created_at,
44 pre_delete.connect(_pre_delete_handler)
47 class BookUserData(models.Model):
48 book = models.ForeignKey(Book, models.CASCADE)
49 user = models.ForeignKey(User, models.CASCADE)
50 complete = models.BooleanField(default=False)
51 last_changed = models.DateTimeField(auto_now=True)
54 unique_together = [('user', 'book')]
58 return 'complete' if self.complete else 'reading'
61 def update(cls, book, user, state):
62 instance, created = cls.objects.get_or_create(book=book, user=user)
63 instance.complete = state == 'complete'
72 ('pending', 'Pending approval'),
73 ('accepted', 'Accepted'),
74 ('canceled', 'Canceled'),
78 class Nonce(models.Model):
79 token_key = models.CharField(max_length=KEY_SIZE)
80 consumer_key = models.CharField(max_length=KEY_SIZE)
81 key = models.CharField(max_length=255)
84 return "Nonce %s for %s" % (self.key, self.consumer_key)
87 class Consumer(models.Model):
88 name = models.CharField(max_length=255)
89 description = models.TextField()
90 key = models.CharField(max_length=KEY_SIZE)
91 secret = models.CharField(max_length=SECRET_SIZE)
92 status = models.CharField(max_length=16, choices=CONSUMER_STATES, default='pending')
93 user = models.ForeignKey(
94 settings.AUTH_USER_MODEL, models.CASCADE,
95 null=True, blank=True, related_name='consumers'
99 return "Consumer %s with key %s" % (self.name, self.key)
102 class Token(models.Model):
105 TOKEN_TYPES = ((REQUEST, 'Request'), (ACCESS, 'Access'))
107 key = models.CharField(max_length=KEY_SIZE)
108 secret = models.CharField(max_length=SECRET_SIZE)
109 token_type = models.IntegerField(choices=TOKEN_TYPES)
110 timestamp = models.IntegerField()
111 is_approved = models.BooleanField(default=False)
112 user = models.ForeignKey(
113 settings.AUTH_USER_MODEL, models.CASCADE,
114 null=True, blank=True, related_name='tokens'
116 consumer = models.ForeignKey(Consumer, models.CASCADE)
119 return "%s Token %s for %s" % (self.get_token_type_display(), self.key, self.consumer)