diff --git a/apiserver/apiserver/api/serializers.py b/apiserver/apiserver/api/serializers.py new file mode 100644 index 0000000..6930ee5 --- /dev/null +++ b/apiserver/apiserver/api/serializers.py @@ -0,0 +1,8 @@ +from django.contrib.auth.models import User +from rest_framework import serializers + +class UserSerializer(serializers.HyperlinkedModelSerializer): + class Meta: + model = User + fields = ['url', 'username', 'email', 'groups'] + diff --git a/apiserver/apiserver/api/views.py b/apiserver/apiserver/api/views.py index 91ea44a..a4e6924 100644 --- a/apiserver/apiserver/api/views.py +++ b/apiserver/apiserver/api/views.py @@ -1,3 +1,11 @@ -from django.shortcuts import render +from django.contrib.auth.models import User +from rest_framework import viewsets +from rest_framework import permissions +from apiserver.api import serializers + + +class UserViewSet(viewsets.ModelViewSet): + queryset = User.objects.all().order_by('-date_joined') + serializer_class = serializers.UserSerializer + permission_classes = [permissions.IsAuthenticated] -# Create your views here. diff --git a/apiserver/apiserver/settings.py b/apiserver/apiserver/settings.py index 817a2f1..83471e9 100644 --- a/apiserver/apiserver/settings.py +++ b/apiserver/apiserver/settings.py @@ -25,7 +25,9 @@ SECRET_KEY = 'django-insecure-au(y+z)$-iy#(obif&ilg*_pn0j_+0u=q*p7h(3c-ii-euncwx # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True -ALLOWED_HOSTS = [] +ALLOWED_HOSTS = [ + 'api.soak.stctech.ca', +] # Application definition @@ -37,6 +39,7 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + 'rest_framework', ] MIDDLEWARE = [ @@ -75,12 +78,15 @@ WSGI_APPLICATION = 'apiserver.wsgi.application' DATABASES = { 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': BASE_DIR / 'db.sqlite3', + 'ENGINE': 'django.db.backends.postgresql', + 'NAME': 'django', + 'USER': 'django', + 'PASSWORD': 'django', + 'HOST': '127.0.0.1', + 'PORT': '5432', } } - # Password validation # https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators diff --git a/apiserver/apiserver/urls.py b/apiserver/apiserver/urls.py index 0ef9dfd..44b6147 100644 --- a/apiserver/apiserver/urls.py +++ b/apiserver/apiserver/urls.py @@ -1,21 +1,13 @@ -"""apiserver URL Configuration +from django.urls import include, path +from rest_framework import routers +from apiserver.api import views -The `urlpatterns` list routes URLs to views. For more information please see: - https://docs.djangoproject.com/en/4.0/topics/http/urls/ -Examples: -Function views - 1. Add an import: from my_app import views - 2. Add a URL to urlpatterns: path('', views.home, name='home') -Class-based views - 1. Add an import: from other_app.views import Home - 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') -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.contrib import admin -from django.urls import path +router = routers.DefaultRouter() +router.register(r'users', views.UserViewSet) +# Wire up our API using automatic URL routing. +# Additionally, we include login URLs for the browsable API. urlpatterns = [ - path('admin/', admin.site.urls), + path('', include(router.urls)), + path('api-auth/', include('rest_framework.urls', namespace='rest_framework')) ]