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
10 from catalogue.models import Book, Tag
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, models.CASCADE)
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)
22 unique_together = (('content_type', 'object_id'),)
25 def _pre_delete_handler(sender, instance, **kwargs):
26 """ save deleted objects for change history purposes """
28 if sender in (Book, Tag):
30 if instance.category in ('book', 'set'):
32 category = instance.category
35 content_type = ContentType.objects.get_for_model(sender)
36 Deleted.objects.create(
37 content_type=content_type,
38 object_id=instance.id,
39 created_at=instance.created_at,
43 pre_delete.connect(_pre_delete_handler)
46 class BookUserData(models.Model):
47 book = models.ForeignKey(Book, models.CASCADE)
48 user = models.ForeignKey(User, models.CASCADE)
49 complete = models.BooleanField(default=False)
50 last_changed = models.DateTimeField(auto_now=True)
53 unique_together = [('user', 'book')]
57 return 'complete' if self.complete else 'reading'
60 def update(cls, book, user, state):
61 instance, created = cls.objects.get_or_create(book=book, user=user)
62 instance.complete = state == 'complete'
71 ('pending', 'Pending approval'),
72 ('accepted', 'Accepted'),
73 ('canceled', 'Canceled'),
77 class Nonce(models.Model):
78 token_key = models.CharField(max_length=KEY_SIZE)
79 consumer_key = models.CharField(max_length=KEY_SIZE)
80 key = models.CharField(max_length=255)
83 return "Nonce %s for %s" % (self.key, self.consumer_key)
86 class Consumer(models.Model):
87 name = models.CharField(max_length=255)
88 description = models.TextField()
89 key = models.CharField(max_length=KEY_SIZE)
90 secret = models.CharField(max_length=SECRET_SIZE)
91 status = models.CharField(max_length=16, choices=CONSUMER_STATES, default='pending')
92 user = models.ForeignKey(
93 settings.AUTH_USER_MODEL, models.CASCADE,
94 null=True, blank=True, related_name='consumers'
98 return "Consumer %s with key %s" % (self.name, self.key)
101 class Token(models.Model):
104 TOKEN_TYPES = ((REQUEST, 'Request'), (ACCESS, 'Access'))
106 key = models.CharField(max_length=KEY_SIZE)
107 secret = models.CharField(max_length=SECRET_SIZE)
108 token_type = models.IntegerField(choices=TOKEN_TYPES)
109 timestamp = models.IntegerField()
110 is_approved = models.BooleanField(default=False)
111 user = models.ForeignKey(
112 settings.AUTH_USER_MODEL, models.CASCADE,
113 null=True, blank=True, related_name='tokens'
115 consumer = models.ForeignKey(Consumer, models.CASCADE)
118 return "%s Token %s for %s" % (self.get_token_type_display(), self.key, self.consumer)