diff --git a/server/server/api/serializers.py b/server/server/api/serializers.py new file mode 100644 index 0000000..e6d7f61 --- /dev/null +++ b/server/server/api/serializers.py @@ -0,0 +1,7 @@ +from django.contrib.auth.models import User, Group +from rest_framework import serializers + +class UserSerializer(serializers.ModelSerializer): + class Meta: + model = User + fields = ['username', 'email', 'groups'] diff --git a/server/server/api/views.py b/server/server/api/views.py index 91ea44a..524cfa2 100644 --- a/server/server/api/views.py +++ b/server/server/api/views.py @@ -1,3 +1,9 @@ -from django.shortcuts import render +from django.contrib.auth.models import User, Group +from rest_framework import viewsets +from rest_framework import permissions +from server.api import serializers -# Create your views here. +class UserViewSet(viewsets.ModelViewSet): + queryset = User.objects.all().order_by('-date_joined') + serializer_class = serializers.UserSerializer + permission_classes = [permissions.IsAuthenticated] diff --git a/server/server/settings.py b/server/server/settings.py index 9a3c92c..60964c5 100644 --- a/server/server/settings.py +++ b/server/server/settings.py @@ -37,6 +37,7 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + 'rest_framework', ] MIDDLEWARE = [ diff --git a/server/server/urls.py b/server/server/urls.py index b4b6c46..5dbb73d 100644 --- a/server/server/urls.py +++ b/server/server/urls.py @@ -1,21 +1,11 @@ -"""server URL Configuration +from django.urls import include, path +from rest_framework import routers +from .api import views -The `urlpatterns` list routes URLs to views. For more information please see: - https://docs.djangoproject.com/en/3.1/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) urlpatterns = [ - path('admin/', admin.site.urls), + path('', include(router.urls)), + path('api-auth/', include('rest_framework.urls', namespace='rest_framework')) ]