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 ugettext_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'):
34 category = instance.category
37 content_type = ContentType.objects.get_for_model(sender)
38 Deleted.objects.create(
39 content_type=content_type, object_id=instance.id, created_at=instance.created_at, category=category,
41 pre_delete.connect(_pre_delete_handler)
44 class BookUserData(models.Model):
45 book = models.ForeignKey(Book, models.CASCADE)
46 user = models.ForeignKey(User, models.CASCADE)
47 complete = models.BooleanField(default=False)
48 last_changed = models.DateTimeField(auto_now=True)
52 return 'complete' if self.complete else 'reading'
55 def update(cls, book, user, state):
56 instance, created = cls.objects.get_or_create(book=book, user=user)
57 instance.complete = state == 'complete'
60 from django.conf import settings
67 ('pending', 'Pending approval'),
68 ('accepted', 'Accepted'),
69 ('canceled', 'Canceled'),
73 class Nonce(models.Model):
74 token_key = models.CharField(max_length=KEY_SIZE)
75 consumer_key = models.CharField(max_length=KEY_SIZE)
76 key = models.CharField(max_length=255)
79 return "Nonce %s for %s" % (self.key, self.consumer_key)
82 class Consumer(models.Model):
83 name = models.CharField(max_length=255)
84 description = models.TextField()
85 key = models.CharField(max_length=KEY_SIZE)
86 secret = models.CharField(max_length=SECRET_SIZE)
87 status = models.CharField(max_length=16, choices=CONSUMER_STATES, default='pending')
88 user = models.ForeignKey(settings.AUTH_USER_MODEL, models.CASCADE, null=True, blank=True, related_name='consumers')
91 return "Consumer %s with key %s" % (self.name, self.key)
94 class Token(models.Model):
97 TOKEN_TYPES = ((REQUEST, 'Request'), (ACCESS, 'Access'))
99 key = models.CharField(max_length=KEY_SIZE)
100 secret = models.CharField(max_length=SECRET_SIZE)
101 token_type = models.IntegerField(choices=TOKEN_TYPES)
102 timestamp = models.IntegerField()
103 is_approved = models.BooleanField(default=False)
104 user = models.ForeignKey(settings.AUTH_USER_MODEL, models.CASCADE, null=True, blank=True, related_name='tokens')
105 consumer = models.ForeignKey(Consumer, models.CASCADE)
108 return "%s Token %s for %s" % (self.get_token_type_display(), self.key, self.consumer)