Create basic API for models

This commit is contained in:
Tanner Collin 2018-09-12 17:18:11 -06:00
parent a432535997
commit 2a3823f739
10 changed files with 107 additions and 8 deletions

View File

@ -104,3 +104,6 @@ ENV/
# DB # DB
db.sqlite3 db.sqlite3
# media
media/*

View File

@ -1,3 +1,7 @@
from django.contrib import admin 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)

View File

@ -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/'),
),
]

View File

@ -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=''),
),
]

View File

@ -5,14 +5,20 @@ class Category(models.Model):
name = models.CharField(max_length=32) name = models.CharField(max_length=32)
slug = models.CharField(max_length=32) slug = models.CharField(max_length=32)
info = models.TextField(max_length=1024, blank=True) 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): class Tool(models.Model):
category = models.ForeignKey(Category, related_name='tools', on_delete=models.PROTECT) category = models.ForeignKey(Category, related_name='tools', on_delete=models.PROTECT)
name = models.CharField(max_length=32) name = models.CharField(max_length=32)
info = models.TextField(max_length=1024, blank=True) info = models.TextField(max_length=1024, blank=True)
wiki_id = models.IntegerField() wiki_id = models.IntegerField()
photo = models.FileField(upload_to='media/') photo = models.ImageField()
def __str__(self):
return self.name
class Profile(models.Model): class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE) user = models.OneToOneField(User, on_delete=models.CASCADE)

View File

@ -1,7 +1,27 @@
from django.contrib.auth.models import User from django.contrib.auth.models import User
from rest_framework import serializers 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): class UserSerializer(serializers.ModelSerializer):
profile = ProfileSerializer(read_only=True)
class Meta: class Meta:
model = User model = User
fields = ('username', 'email') fields = ('username', 'profile')
depth = 1

View File

@ -1,7 +1,20 @@
from django.contrib.auth.models import User from django.contrib.auth.models import User
from rest_framework import viewsets 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): class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all().order_by('-date_joined') queryset = models.User.objects.all().order_by('-date_joined')
serializer_class = UserSerializer serializer_class = serializers.UserSerializer

View File

@ -38,6 +38,7 @@ INSTALLED_APPS = [
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'rest_framework', 'rest_framework',
'authserver.api',
] ]
MIDDLEWARE = [ MIDDLEWARE = [
@ -119,3 +120,6 @@ USE_TZ = True
# https://docs.djangoproject.com/en/2.1/howto/static-files/ # https://docs.djangoproject.com/en/2.1/howto/static-files/
STATIC_URL = '/static/' STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

View File

@ -13,14 +13,19 @@ Including another URLconf
1. Import the include() function: from django.urls import include, path 1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 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.contrib import admin
from django.urls import path from django.urls import path
from django.conf.urls import url, include from django.conf.urls import url, include
from rest_framework import routers from rest_framework import routers
from authserver.api import views from .api import views
router = routers.DefaultRouter() 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') router.register(r'user', views.UserViewSet, 'user')
urlpatterns = [ urlpatterns = [
@ -28,3 +33,6 @@ urlpatterns = [
url(r'^admin/', admin.site.urls), url(r'^admin/', admin.site.urls),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')) 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)

View File