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.
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 _
12 from catalogue.models import Book, Tag
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)
24 unique_together = (('content_type', 'object_id'),)
27 def _pre_delete_handler(sender, instance, **kwargs):
28 """ save deleted objects for change history purposes """
30 if sender in (Book, Tag):
32 if instance.category in ('book', 'set'):
35 category = instance.category
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,
42 pre_delete.connect(_pre_delete_handler)
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)
53 return 'complete' if self.complete else 'reading'
56 def update(cls, book, user, state):
57 instance, created = cls.objects.get_or_create(book=book, user=user)
58 instance.complete = state == 'complete'
61 from django.conf import settings
68 ('pending', 'Pending approval'),
69 ('accepted', 'Accepted'),
70 ('canceled', 'Canceled'),
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)
80 return u"Nonce %s for %s" % (self.key, self.consumer_key)
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')
92 return u"Consumer %s with key %s" % (self.name, self.key)
95 class Token(models.Model):
98 TOKEN_TYPES = ((REQUEST, u'Request'), (ACCESS, u'Access'))
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)
109 return u"%s Token %s for %s" % (self.get_token_type_display(), self.key, self.consumer)