From 2a3823f7398c80926f5b88c9940af9c8675e2eb4 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Wed, 12 Sep 2018 17:18:11 -0600 Subject: [PATCH] Create basic API for models --- authserver/.gitignore | 3 +++ authserver/authserver/api/admin.py | 6 ++++- .../api/migrations/0002_auto_20180912_2156.py | 18 +++++++++++++++ .../api/migrations/0003_auto_20180912_2302.py | 23 +++++++++++++++++++ authserver/authserver/api/models.py | 10 ++++++-- authserver/authserver/api/serializers.py | 22 +++++++++++++++++- authserver/authserver/api/views.py | 19 ++++++++++++--- authserver/authserver/settings.py | 4 ++++ authserver/authserver/urls.py | 10 +++++++- authserver/media/.gitkeep | 0 10 files changed, 107 insertions(+), 8 deletions(-) create mode 100644 authserver/authserver/api/migrations/0002_auto_20180912_2156.py create mode 100644 authserver/authserver/api/migrations/0003_auto_20180912_2302.py create mode 100644 authserver/media/.gitkeep diff --git a/authserver/.gitignore b/authserver/.gitignore index 028b0f4..8fddfd2 100644 --- a/authserver/.gitignore +++ b/authserver/.gitignore @@ -104,3 +104,6 @@ ENV/ # DB db.sqlite3 + +# media +media/* diff --git a/authserver/authserver/api/admin.py b/authserver/authserver/api/admin.py index 8c38f3f..9d094a6 100644 --- a/authserver/authserver/api/admin.py +++ b/authserver/authserver/api/admin.py @@ -1,3 +1,7 @@ from django.contrib import admin -# Register your models here. +from . import models + +admin.site.register(models.Category) +admin.site.register(models.Tool) +admin.site.register(models.Profile) diff --git a/authserver/authserver/api/migrations/0002_auto_20180912_2156.py b/authserver/authserver/api/migrations/0002_auto_20180912_2156.py new file mode 100644 index 0000000..44f2695 --- /dev/null +++ b/authserver/authserver/api/migrations/0002_auto_20180912_2156.py @@ -0,0 +1,18 @@ +# Generated by Django 2.1.1 on 2018-09-12 21:56 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='category', + name='photo', + field=models.FileField(upload_to='static/media/'), + ), + ] diff --git a/authserver/authserver/api/migrations/0003_auto_20180912_2302.py b/authserver/authserver/api/migrations/0003_auto_20180912_2302.py new file mode 100644 index 0000000..5addb98 --- /dev/null +++ b/authserver/authserver/api/migrations/0003_auto_20180912_2302.py @@ -0,0 +1,23 @@ +# Generated by Django 2.1.1 on 2018-09-12 23:02 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0002_auto_20180912_2156'), + ] + + operations = [ + migrations.AlterField( + model_name='category', + name='photo', + field=models.ImageField(upload_to=''), + ), + migrations.AlterField( + model_name='tool', + name='photo', + field=models.ImageField(upload_to=''), + ), + ] diff --git a/authserver/authserver/api/models.py b/authserver/authserver/api/models.py index fa6bdb0..884e995 100644 --- a/authserver/authserver/api/models.py +++ b/authserver/authserver/api/models.py @@ -5,14 +5,20 @@ class Category(models.Model): name = models.CharField(max_length=32) slug = models.CharField(max_length=32) info = models.TextField(max_length=1024, blank=True) - photo = models.FileField(upload_to='media/') + photo = models.ImageField() + + def __str__(self): + return self.name class Tool(models.Model): category = models.ForeignKey(Category, related_name='tools', on_delete=models.PROTECT) name = models.CharField(max_length=32) info = models.TextField(max_length=1024, blank=True) wiki_id = models.IntegerField() - photo = models.FileField(upload_to='media/') + photo = models.ImageField() + + def __str__(self): + return self.name class Profile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) diff --git a/authserver/authserver/api/serializers.py b/authserver/authserver/api/serializers.py index 42c5093..351b007 100644 --- a/authserver/authserver/api/serializers.py +++ b/authserver/authserver/api/serializers.py @@ -1,7 +1,27 @@ from django.contrib.auth.models import User from rest_framework import serializers +from . import models + +class CategorySerializer(serializers.ModelSerializer): + class Meta: + model = models.Category + fields = '__all__' + +class ToolSerializer(serializers.ModelSerializer): + class Meta: + model = models.Tool + fields = '__all__' + +class ProfileSerializer(serializers.ModelSerializer): + class Meta: + model = models.Profile + fields = ('lockout_admin', 'authorized_tools') + class UserSerializer(serializers.ModelSerializer): + profile = ProfileSerializer(read_only=True) + class Meta: model = User - fields = ('username', 'email') + fields = ('username', 'profile') + depth = 1 diff --git a/authserver/authserver/api/views.py b/authserver/authserver/api/views.py index 560119a..a274cf9 100644 --- a/authserver/authserver/api/views.py +++ b/authserver/authserver/api/views.py @@ -1,7 +1,20 @@ from django.contrib.auth.models import User from rest_framework import viewsets -from authserver.api.serializers import UserSerializer + +from . import models, serializers + +class CategoryViewSet(viewsets.ModelViewSet): + queryset = models.Category.objects.all().order_by('id') + serializer_class = serializers.CategorySerializer + +class ToolViewSet(viewsets.ModelViewSet): + queryset = models.Tool.objects.all().order_by('id') + serializer_class = serializers.ToolSerializer + +class ProfileViewSet(viewsets.ModelViewSet): + queryset = models.Profile.objects.all().order_by('-user__date_joined') + serializer_class = serializers.ProfileSerializer class UserViewSet(viewsets.ModelViewSet): - queryset = User.objects.all().order_by('-date_joined') - serializer_class = UserSerializer + queryset = models.User.objects.all().order_by('-date_joined') + serializer_class = serializers.UserSerializer diff --git a/authserver/authserver/settings.py b/authserver/authserver/settings.py index 760520e..f93ff83 100644 --- a/authserver/authserver/settings.py +++ b/authserver/authserver/settings.py @@ -38,6 +38,7 @@ INSTALLED_APPS = [ 'django.contrib.messages', 'django.contrib.staticfiles', 'rest_framework', + 'authserver.api', ] MIDDLEWARE = [ @@ -119,3 +120,6 @@ USE_TZ = True # https://docs.djangoproject.com/en/2.1/howto/static-files/ STATIC_URL = '/static/' + +MEDIA_ROOT = os.path.join(BASE_DIR, 'media') +MEDIA_URL = '/media/' diff --git a/authserver/authserver/urls.py b/authserver/authserver/urls.py index 42c1d01..500f842 100644 --- a/authserver/authserver/urls.py +++ b/authserver/authserver/urls.py @@ -13,14 +13,19 @@ Including another URLconf 1. Import the include() function: from django.urls import include, path 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ +from django.conf import settings +from django.conf.urls.static import static from django.contrib import admin from django.urls import path from django.conf.urls import url, include from rest_framework import routers -from authserver.api import views +from .api import views router = routers.DefaultRouter() +router.register(r'tool', views.ToolViewSet, 'tool') +router.register(r'category', views.CategoryViewSet, 'category') +router.register(r'profile', views.ProfileViewSet, 'profile') router.register(r'user', views.UserViewSet, 'user') urlpatterns = [ @@ -28,3 +33,6 @@ urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')) ] + +if settings.DEBUG is True: + urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) diff --git a/authserver/media/.gitkeep b/authserver/media/.gitkeep new file mode 100644 index 0000000..e69de29