X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/73b6a1639ba8f9f5fc7615c136364fdba73e5bb2..768497c92cb75290c032f4ffd586eccab6fd3eec:/src/api/models.py?ds=inline diff --git a/src/api/models.py b/src/api/models.py index cc71a06f3..71dedb56c 100644 --- a/src/api/models.py +++ b/src/api/models.py @@ -2,6 +2,7 @@ # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later. # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # +from django.conf import settings from django.contrib.auth.models import User from django.contrib.contenttypes.models import ContentType from django.db import models @@ -45,11 +46,64 @@ class BookUserData(models.Model): book = models.ForeignKey(Book) user = models.ForeignKey(User) complete = models.BooleanField(default=False) + last_changed = models.DateTimeField(auto_now=True) - def get_state(self): + @property + def state(self): return 'complete' if self.complete else 'reading' - def set_state(self, state): - self.complete = state == 'complete' + @classmethod + def update(cls, book, user, state): + instance, created = cls.objects.get_or_create(book=book, user=user) + instance.complete = state == 'complete' + instance.save() + return instance +from django.conf import settings - state = property(get_state, set_state) + +KEY_SIZE = 18 +SECRET_SIZE = 32 + +CONSUMER_STATES = ( + ('pending', 'Pending approval'), + ('accepted', 'Accepted'), + ('canceled', 'Canceled'), +) + + +class Nonce(models.Model): + token_key = models.CharField(max_length=KEY_SIZE) + consumer_key = models.CharField(max_length=KEY_SIZE) + key = models.CharField(max_length=255) + + def __unicode__(self): + return u"Nonce %s for %s" % (self.key, self.consumer_key) + + +class Consumer(models.Model): + name = models.CharField(max_length=255) + description = models.TextField() + key = models.CharField(max_length=KEY_SIZE) + secret = models.CharField(max_length=SECRET_SIZE) + status = models.CharField(max_length=16, choices=CONSUMER_STATES, default='pending') + user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True, related_name='consumers') + + def __unicode__(self): + return u"Consumer %s with key %s" % (self.name, self.key) + + +class Token(models.Model): + REQUEST = 1 + ACCESS = 2 + TOKEN_TYPES = ((REQUEST, u'Request'), (ACCESS, u'Access')) + + key = models.CharField(max_length=KEY_SIZE) + secret = models.CharField(max_length=SECRET_SIZE) + token_type = models.IntegerField(choices=TOKEN_TYPES) + timestamp = models.IntegerField() + is_approved = models.BooleanField(default=False) + user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True, related_name='tokens') + consumer = models.ForeignKey(Consumer) + + def __unicode__(self): + return u"%s Token %s for %s" % (self.get_token_type_display(), self.key, self.consumer)