From 3ba66f282a0ee31876ccc31f1626648ead9ffc26 Mon Sep 17 00:00:00 2001 From: Chneemann Date: Thu, 1 Aug 2024 13:54:49 +0200 Subject: [PATCH] added users & groups view --- backend/videoflix/users/serializer.py | 12 ++++++++++++ backend/videoflix/users/views.py | 19 ++++++++++++++++++- backend/videoflix/videoflix/settings.py | 9 ++++++++- backend/videoflix/videoflix/urls.py | 10 +++++++++- 4 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 backend/videoflix/users/serializer.py diff --git a/backend/videoflix/users/serializer.py b/backend/videoflix/users/serializer.py new file mode 100644 index 0000000..d265c6d --- /dev/null +++ b/backend/videoflix/users/serializer.py @@ -0,0 +1,12 @@ +from django.contrib.auth.models import User, Group +from rest_framework import serializers + +class UserSerializer(serializers.HyperlinkedModelSerializer): + class Meta: + model = User + fields = ['url', 'id', 'username', 'email'] + +class GroupSerializer(serializers.HyperlinkedModelSerializer): + class Meta: + model = Group + fields = ['url', 'name'] \ No newline at end of file diff --git a/backend/videoflix/users/views.py b/backend/videoflix/users/views.py index 91ea44a..786d925 100644 --- a/backend/videoflix/users/views.py +++ b/backend/videoflix/users/views.py @@ -1,3 +1,20 @@ from django.shortcuts import render +from django.contrib.auth.models import Group, User +from rest_framework import permissions, viewsets +from users.serializer import GroupSerializer, UserSerializer -# Create your views here. +class UserViewSet(viewsets.ModelViewSet): + """ + API endpoint that allows users to be viewed or edited. + """ + queryset = User.objects.all().order_by('id') + serializer_class = UserSerializer + # permission_classes = [permissions.IsAuthenticated] + +class GroupViewSet(viewsets.ModelViewSet): + """ + API endpoint that allows groups to be viewed or edited. + """ + queryset = Group.objects.all().order_by('name') + serializer_class = GroupSerializer + # permission_classes = [permissions.IsAuthenticated] \ No newline at end of file diff --git a/backend/videoflix/videoflix/settings.py b/backend/videoflix/videoflix/settings.py index b88b5a1..7a0d131 100644 --- a/backend/videoflix/videoflix/settings.py +++ b/backend/videoflix/videoflix/settings.py @@ -20,7 +20,7 @@ BASE_DIR = Path(__file__).resolve().parent.parent # See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = 'django-insecure-hyqc43^!fqs^t_qgs0eycn-ukig$=@ac$wz-ix2n4*53ybmgri' +SECRET_KEY = 'django-insecure-l88ot$w3&q2v439@j_5ps()@1*fa&ho1o=eiig9##0!#3mz9nr' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True @@ -30,6 +30,11 @@ ALLOWED_HOSTS = [] # Application definition +REST_FRAMEWORK = { + 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination', + 'PAGE_SIZE': 10 +} + INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', @@ -37,6 +42,8 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + 'rest_framework', + 'users', ] MIDDLEWARE = [ diff --git a/backend/videoflix/videoflix/urls.py b/backend/videoflix/videoflix/urls.py index 6b7d18f..7ed4a4d 100644 --- a/backend/videoflix/videoflix/urls.py +++ b/backend/videoflix/videoflix/urls.py @@ -15,8 +15,16 @@ Including another URLconf 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin -from django.urls import path +from django.urls import include, path +from rest_framework import routers +from users import views + +router = routers.DefaultRouter() +router.register(r'users', views.UserViewSet) +router.register(r'groups', views.GroupViewSet) urlpatterns = [ path('admin/', admin.site.urls), + path('', include(router.urls)), + path('api-auth/', include('rest_framework.urls', namespace='rest_framework')) ]