Move API models to api app.
authorRadek Czajka <rczajka@rczajka.pl>
Thu, 28 Feb 2019 22:46:46 +0000 (23:46 +0100)
committerRadek Czajka <rczajka@rczajka.pl>
Thu, 28 Feb 2019 22:52:40 +0000 (23:52 +0100)
src/api/admin.py [new file with mode: 0644]
src/api/migrations/0005_consumer_nonce_resource_token.py [new file with mode: 0644]
src/api/models.py
src/api/piston/admin.py [deleted file]
src/api/piston/migrations/0002_auto_20190228_2338.py [new file with mode: 0644]
src/api/piston/models.py
src/api/request_validator.py
src/api/tests/tests.py
src/api/views.py

diff --git a/src/api/admin.py b/src/api/admin.py
new file mode 100644 (file)
index 0000000..c931db0
--- /dev/null
@@ -0,0 +1,11 @@
+from django.contrib import admin
+from . import models
+
+
+class TokenAdmin(admin.ModelAdmin):
+    raw_id_fields = ['user']
+
+
+admin.site.register(models.Nonce)
+admin.site.register(models.Consumer)
+admin.site.register(models.Token, TokenAdmin)
diff --git a/src/api/migrations/0005_consumer_nonce_resource_token.py b/src/api/migrations/0005_consumer_nonce_resource_token.py
new file mode 100644 (file)
index 0000000..95873a7
--- /dev/null
@@ -0,0 +1,57 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.20 on 2019-02-28 22:38
+from __future__ import unicode_literals
+
+from django.conf import settings
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
+        ('api', '0004_bookuserdata_last_changed'),
+        ('piston', '0002_auto_20190228_2338'),
+    ]
+
+    state_operations = [
+        migrations.CreateModel(
+            name='Consumer',
+            fields=[
+                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+                ('name', models.CharField(max_length=255)),
+                ('description', models.TextField()),
+                ('key', models.CharField(max_length=18)),
+                ('secret', models.CharField(max_length=32)),
+                ('status', models.CharField(choices=[(b'pending', b'Pending approval'), (b'accepted', b'Accepted'), (b'canceled', b'Canceled')], default=b'pending', max_length=16)),
+                ('user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='consumers', to=settings.AUTH_USER_MODEL)),
+            ],
+        ),
+        migrations.CreateModel(
+            name='Nonce',
+            fields=[
+                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+                ('token_key', models.CharField(max_length=18)),
+                ('consumer_key', models.CharField(max_length=18)),
+                ('key', models.CharField(max_length=255)),
+            ],
+        ),
+        migrations.CreateModel(
+            name='Token',
+            fields=[
+                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+                ('key', models.CharField(max_length=18)),
+                ('secret', models.CharField(max_length=32)),
+                ('token_type', models.IntegerField(choices=[(1, 'Request'), (2, 'Access')])),
+                ('timestamp', models.IntegerField()),
+                ('is_approved', models.BooleanField(default=False)),
+                ('consumer', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='api.Consumer')),
+                ('user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='tokens', to=settings.AUTH_USER_MODEL)),
+            ],
+        ),
+    ]
+
+    operations = [
+        migrations.SeparateDatabaseAndState(state_operations=state_operations),
+    ]
index d2716b9..71dedb5 100644 (file)
@@ -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
@@ -57,3 +58,52 @@ class BookUserData(models.Model):
         instance.complete = state == 'complete'
         instance.save()
         return instance
+from django.conf import settings
+
+
+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)
diff --git a/src/api/piston/admin.py b/src/api/piston/admin.py
deleted file mode 100644 (file)
index c931db0..0000000
+++ /dev/null
@@ -1,11 +0,0 @@
-from django.contrib import admin
-from . import models
-
-
-class TokenAdmin(admin.ModelAdmin):
-    raw_id_fields = ['user']
-
-
-admin.site.register(models.Nonce)
-admin.site.register(models.Consumer)
-admin.site.register(models.Token, TokenAdmin)
diff --git a/src/api/piston/migrations/0002_auto_20190228_2338.py b/src/api/piston/migrations/0002_auto_20190228_2338.py
new file mode 100644 (file)
index 0000000..4fa2b90
--- /dev/null
@@ -0,0 +1,52 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.20 on 2019-02-28 22:38
+from __future__ import unicode_literals
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('piston', '0001_initial'),
+    ]
+
+    database_operations = [
+        migrations.AlterModelTable('Nonce', 'api_nonce'),
+        migrations.AlterModelTable('Consumer', 'api_consumer'),
+        migrations.AlterModelTable('Token', 'api_token'),
+    ]
+
+    state_operations = [
+        migrations.RemoveField(
+            model_name='consumer',
+            name='user',
+        ),
+        migrations.DeleteModel(
+            name='Nonce',
+        ),
+        migrations.RemoveField(
+            model_name='token',
+            name='consumer',
+        ),
+        migrations.RemoveField(
+            model_name='token',
+            name='user',
+        ),
+        migrations.DeleteModel(
+            name='Consumer',
+        ),
+        migrations.DeleteModel(
+            name='Token',
+        ),
+    ]
+
+    operations = [
+        migrations.SeparateDatabaseAndState(
+            database_operations=database_operations,
+            state_operations=state_operations
+        ),
+        migrations.DeleteModel(
+            name='Resource',
+        ),
+    ]
index 7bb106c..e69de29 100644 (file)
@@ -1,56 +0,0 @@
-from django.conf import settings
-from django.db import models
-
-
-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 Resource(models.Model):
-    name = models.CharField(max_length=255)
-    url = models.TextField(max_length=2047)
-    is_readonly = models.BooleanField(default=True)
-
-
-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)
index 55bd6fb..2c16691 100644 (file)
@@ -4,7 +4,7 @@
 #
 import time
 from oauthlib.oauth1 import RequestValidator
-from api.piston.models import Consumer, Nonce, Token
+from api.models import Consumer, Nonce, Token
 
 
 class PistonRequestValidator(RequestValidator):
@@ -20,7 +20,7 @@ class PistonRequestValidator(RequestValidator):
     # iOS app generates 8-char nonces.
     nonce_length = 8, 250
 
-    # Because piston.models.Token.key is char(18).
+    # Because Token.key is char(18).
     request_token_length = 18, 32
     access_token_length = 18, 32
     # TODO: oauthlib request-access switch.
index 8dd29a5..b3cc54f 100644 (file)
@@ -17,7 +17,7 @@ from django.core.files.uploadedfile import SimpleUploadedFile
 from django.test import TestCase
 from django.test.utils import override_settings
 from mock import patch
-from api.piston.models import Consumer, Token
+from api.models import Consumer, Token
 
 from catalogue.models import Book, Tag
 from picture.forms import PictureImportForm
index f28a4e4..741e454 100644 (file)
@@ -6,7 +6,7 @@ from django.http import Http404
 from django.views.generic.base import View
 from oauthlib.common import urlencode
 from oauthlib.oauth1 import RequestTokenEndpoint, AccessTokenEndpoint
-from api.piston.models import KEY_SIZE, SECRET_SIZE
+from api.models import KEY_SIZE, SECRET_SIZE
 from rest_framework.permissions import IsAuthenticated
 from rest_framework.response import Response
 from rest_framework.views import APIView