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)
55 return 'complete' if self.complete else 'reading'
58 def update(cls, book, user, state):
59 instance, created = cls.objects.get_or_create(book=book, user=user)
60 instance.complete = state == 'complete'
69 ('pending', 'Pending approval'),
70 ('accepted', 'Accepted'),
71 ('canceled', 'Canceled'),
75 class Nonce(models.Model):
76 token_key = models.CharField(max_length=KEY_SIZE)
77 consumer_key = models.CharField(max_length=KEY_SIZE)
78 key = models.CharField(max_length=255)
81 return "Nonce %s for %s" % (self.key, self.consumer_key)
84 class Consumer(models.Model):
85 name = models.CharField(max_length=255)
86 description = models.TextField()
87 key = models.CharField(max_length=KEY_SIZE)
88 secret = models.CharField(max_length=SECRET_SIZE)
89 status = models.CharField(max_length=16, choices=CONSUMER_STATES, default='pending')
90 user = models.ForeignKey(
91 settings.AUTH_USER_MODEL, models.CASCADE,
92 null=True, blank=True, related_name='consumers'
96 return "Consumer %s with key %s" % (self.name, self.key)
99 class Token(models.Model):
102 TOKEN_TYPES = ((REQUEST, 'Request'), (ACCESS, 'Access'))
104 key = models.CharField(max_length=KEY_SIZE)
105 secret = models.CharField(max_length=SECRET_SIZE)
106 token_type = models.IntegerField(choices=TOKEN_TYPES)
107 timestamp = models.IntegerField()
108 is_approved = models.BooleanField(default=False)
109 user = models.ForeignKey(
110 settings.AUTH_USER_MODEL, models.CASCADE,
111 null=True, blank=True, related_name='tokens'
113 consumer = models.ForeignKey(Consumer, models.CASCADE)
116 return "%s Token %s for %s" % (self.get_token_type_display(), self.key, self.consumer)