diff --git a/cinema/views.py b/cinema/views.py index a191bf5f..eb152642 100644 --- a/cinema/views.py +++ b/cinema/views.py @@ -1,10 +1,19 @@ from datetime import datetime from django.db.models import F, Count -from rest_framework import viewsets +from rest_framework import viewsets, mixins +from rest_framework.authentication import TokenAuthentication from rest_framework.pagination import PageNumberPagination - -from cinema.models import Genre, Actor, CinemaHall, Movie, MovieSession, Order +from rest_framework.permissions import IsAuthenticated + +from cinema.models import ( + Genre, + Actor, + CinemaHall, + Movie, + MovieSession, + Order +) from cinema.serializers import ( GenreSerializer, @@ -19,26 +28,43 @@ OrderSerializer, OrderListSerializer, ) +from user.permissions import IsAdminOrIfAuthenticatedReadOnly -class GenreViewSet(viewsets.ModelViewSet): +class GenreViewSet(viewsets.GenericViewSet, + mixins.ListModelMixin, + mixins.CreateModelMixin): queryset = Genre.objects.all() serializer_class = GenreSerializer + authentication_classes = (TokenAuthentication,) + permission_classes = (IsAdminOrIfAuthenticatedReadOnly,) -class ActorViewSet(viewsets.ModelViewSet): +class ActorViewSet(viewsets.GenericViewSet, + mixins.ListModelMixin, + mixins.CreateModelMixin): queryset = Actor.objects.all() serializer_class = ActorSerializer + authentication_classes = (TokenAuthentication,) + permission_classes = (IsAdminOrIfAuthenticatedReadOnly,) -class CinemaHallViewSet(viewsets.ModelViewSet): +class CinemaHallViewSet(viewsets.GenericViewSet, + mixins.ListModelMixin, + mixins.CreateModelMixin): queryset = CinemaHall.objects.all() serializer_class = CinemaHallSerializer + authentication_classes = (TokenAuthentication,) + permission_classes = (IsAdminOrIfAuthenticatedReadOnly,) -class MovieViewSet(viewsets.ModelViewSet): +class MovieViewSet(viewsets.GenericViewSet, + mixins.ListModelMixin, + mixins.CreateModelMixin): queryset = Movie.objects.prefetch_related("genres", "actors") serializer_class = MovieSerializer + authentication_classes = (TokenAuthentication,) + permission_classes = (IsAdminOrIfAuthenticatedReadOnly,) @staticmethod def _params_to_ints(qs): @@ -87,6 +113,8 @@ class MovieSessionViewSet(viewsets.ModelViewSet): ) ) serializer_class = MovieSessionSerializer + authentication_classes = (TokenAuthentication,) + permission_classes = (IsAdminOrIfAuthenticatedReadOnly,) def get_queryset(self): date = self.request.query_params.get("date") @@ -118,12 +146,16 @@ class OrderPagination(PageNumberPagination): max_page_size = 100 -class OrderViewSet(viewsets.ModelViewSet): +class OrderViewSet(viewsets.GenericViewSet, + mixins.ListModelMixin, + mixins.CreateModelMixin): queryset = Order.objects.prefetch_related( "tickets__movie_session__movie", "tickets__movie_session__cinema_hall" ) serializer_class = OrderSerializer pagination_class = OrderPagination + authentication_classes = (TokenAuthentication,) + permission_classes = (IsAuthenticated,) def get_queryset(self): return Order.objects.filter(user=self.request.user) diff --git a/cinema_service/settings.py b/cinema_service/settings.py index 29ea7dea..9030126c 100644 --- a/cinema_service/settings.py +++ b/cinema_service/settings.py @@ -12,6 +12,9 @@ from pathlib import Path +from django.urls import reverse_lazy + + # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent @@ -125,7 +128,7 @@ USE_I18N = True -USE_TZ = False +USE_TZ = True # Static files (CSS, JavaScript, Images) diff --git a/cinema_service/urls.py b/cinema_service/urls.py index bf903c00..a282f874 100644 --- a/cinema_service/urls.py +++ b/cinema_service/urls.py @@ -4,5 +4,6 @@ urlpatterns = [ path("admin/", admin.site.urls), path("api/cinema/", include("cinema.urls", namespace="cinema")), + path("api/user/", include("user.urls", namespace="user")), path("__debug__/", include("debug_toolbar.urls")), ] diff --git a/requirements.txt b/requirements.txt index 56e13554..ec476ab2 100644 Binary files a/requirements.txt and b/requirements.txt differ diff --git a/user/permissions.py b/user/permissions.py new file mode 100644 index 00000000..a289d94c --- /dev/null +++ b/user/permissions.py @@ -0,0 +1,13 @@ +from rest_framework.permissions import BasePermission, SAFE_METHODS + + +class IsAdminOrIfAuthenticatedReadOnly(BasePermission): + def has_permission(self, request, view): + return bool( + ( + request.method in SAFE_METHODS + and request.user + and request.user.is_authenticated + ) + or (request.user and request.user.is_staff) + ) diff --git a/user/serializers.py b/user/serializers.py index fa56336e..b44eba6d 100644 --- a/user/serializers.py +++ b/user/serializers.py @@ -1 +1,24 @@ -# write your code here +from django.contrib.auth import get_user_model +from rest_framework import serializers + + +class UserSerializer(serializers.ModelSerializer): + + class Meta: + model = get_user_model() + fields = ("id", "username", "email", "password", "is_staff") + read_only_fields = ("id", "is_staff") + extra_kwargs = {"password": {"write_only": True, "min_length": 5}} + + def create(self, validated_data): + return get_user_model().objects.create_user(**validated_data) + + def update(self, instance, validated_data): + password = validated_data.pop("password", None) + user = super().update(instance, validated_data) + + if password: + user.set_password(password) + user.save() + + return user diff --git a/user/urls.py b/user/urls.py index fa56336e..ca9bd3a2 100644 --- a/user/urls.py +++ b/user/urls.py @@ -1 +1,11 @@ -# write your code here +from django.urls import path + +from user.views import CreateUserView, CreateTokenView, ManageUserView + +urlpatterns = [ + path("register/", CreateUserView.as_view(), name="create"), + path("login/", CreateTokenView.as_view(), name="login"), + path("me/", ManageUserView.as_view(), name="manage") +] + +app_name = "user" diff --git a/user/views.py b/user/views.py index fa56336e..e58cffca 100644 --- a/user/views.py +++ b/user/views.py @@ -1 +1,24 @@ -# write your code here +from rest_framework import generics +from rest_framework.authentication import TokenAuthentication +from rest_framework.authtoken.views import ObtainAuthToken +from rest_framework.permissions import IsAuthenticated +from rest_framework.settings import api_settings + +from user.serializers import UserSerializer + + +class CreateUserView(generics.CreateAPIView): + serializer_class = UserSerializer + + +class CreateTokenView(ObtainAuthToken): + renderer_classes = api_settings.DEFAULT_RENDERER_CLASSES + + +class ManageUserView(generics.RetrieveUpdateAPIView): + serializer_class = UserSerializer + authentication_classes = (TokenAuthentication,) + permission_classes = (IsAuthenticated,) + + def get_object(self): + return self.request.user