Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created a solution #668

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
ab962a3
Changed to True in USE_TZ static variable.
MaksymProtsak Nov 12, 2024
e6d53db
Created permissions.py.
MaksymProtsak Nov 13, 2024
6ad869d
Added authentications_classes, permission_classes vars in ActorViewSet.
MaksymProtsak Nov 13, 2024
f82da1c
Created CreateUserView, CreateTokenView, ManageUserView classes.
MaksymProtsak Nov 13, 2024
ac89995
Created pathes 'create', 'login', 'manage'.
MaksymProtsak Nov 13, 2024
a9fcdfa
Added path 'api/user/'.
MaksymProtsak Nov 13, 2024
c059fb2
Created UserSerializer class with created and update methods.
MaksymProtsak Nov 13, 2024
1c6dd63
In ManageUserView class change permission_classes to IsAdminOrIfAuthe…
MaksymProtsak Nov 13, 2024
4989fa3
Added REST_FRAMEWORK variable in settings.py.
MaksymProtsak Nov 13, 2024
0453d63
Replaced permission_classes in ManageUserVies.
MaksymProtsak Nov 14, 2024
11c6f51
Created get_permissions method in ActorVieSet.
MaksymProtsak Nov 14, 2024
1d331a2
Updated name for 'login' path.
MaksymProtsak Nov 14, 2024
321444d
Removed REST_FRAMEWORK variable.
MaksymProtsak Nov 14, 2024
f662b9a
Updated row spaces.
MaksymProtsak Nov 14, 2024
3886edd
Removed get_permissions method in ActorViewSet. Added authentication_…
MaksymProtsak Nov 14, 2024
12fcaf6
flake8
MaksymProtsak Nov 14, 2024
c439554
Apdated actions for GenreViewSet, ActorViewSet, CinemaHallViewSet, Mo…
MaksymProtsak Nov 14, 2024
45dd28c
Removed row permission_classes.
MaksymProtsak Nov 14, 2024
ab1ace8
Changed permission_classes in OrderViewSet to IsAuthenticated.
MaksymProtsak Nov 14, 2024
07568a4
Removed unused imports.
MaksymProtsak Nov 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 51 additions & 6 deletions cinema/views.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
from datetime import datetime

from django.db.models import F, Count

from rest_framework import viewsets
from rest_framework import mixins
from rest_framework.authentication import TokenAuthentication
from rest_framework.pagination import PageNumberPagination

from cinema.models import Genre, Actor, CinemaHall, Movie, MovieSession, Order
from cinema.models import (
Genre,
Actor,
CinemaHall,
Movie,
MovieSession,
Order
)

from cinema.serializers import (
GenreSerializer,
Expand All @@ -20,25 +30,52 @@
OrderListSerializer,
)

from user.permissions import IsAdminOrIfAuthenticatedReadOnly


class GenreViewSet(viewsets.ModelViewSet):
class GenreViewSet(
mixins.ListModelMixin,
mixins.CreateModelMixin,
viewsets.GenericViewSet,
):
queryset = Genre.objects.all()
serializer_class = GenreSerializer
authentication_classes = (TokenAuthentication,)
permission_classes = (IsAdminOrIfAuthenticatedReadOnly,)


class ActorViewSet(viewsets.ModelViewSet):
class ActorViewSet(
mixins.ListModelMixin,
mixins.CreateModelMixin,
viewsets.GenericViewSet,
):
queryset = Actor.objects.all()
serializer_class = ActorSerializer
authentication_classes = (TokenAuthentication,)
permission_classes = (IsAdminOrIfAuthenticatedReadOnly,)


class CinemaHallViewSet(viewsets.ModelViewSet):
class CinemaHallViewSet(
mixins.ListModelMixin,
mixins.CreateModelMixin,
viewsets.GenericViewSet,
):
queryset = CinemaHall.objects.all()
serializer_class = CinemaHallSerializer
authentication_classes = (TokenAuthentication,)
permission_classes = (IsAdminOrIfAuthenticatedReadOnly,)


class MovieViewSet(viewsets.ModelViewSet):
class MovieViewSet(
mixins.CreateModelMixin,
mixins.RetrieveModelMixin,
mixins.ListModelMixin,
viewsets.GenericViewSet,
):
queryset = Movie.objects.prefetch_related("genres", "actors")
serializer_class = MovieSerializer
authentication_classes = (TokenAuthentication,)
permission_classes = (IsAdminOrIfAuthenticatedReadOnly,)

@staticmethod
def _params_to_ints(qs):
Expand Down Expand Up @@ -87,6 +124,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")
Expand Down Expand Up @@ -118,12 +157,18 @@ class OrderPagination(PageNumberPagination):
max_page_size = 100


class OrderViewSet(viewsets.ModelViewSet):
class OrderViewSet(
mixins.ListModelMixin,
mixins.CreateModelMixin,
viewsets.GenericViewSet,
):
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 = (IsAdminOrIfAuthenticatedReadOnly,)

def get_queryset(self):
return Order.objects.filter(user=self.request.user)
Expand Down
2 changes: 1 addition & 1 deletion cinema_service/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@

USE_I18N = True

USE_TZ = False
USE_TZ = True


# Static files (CSS, JavaScript, Images)
Expand Down
1 change: 1 addition & 0 deletions cinema_service/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that the namespace attribute in the include function matches the namespaces defined in the respective urls.py files of the cinema and user apps. This is important for URL reversing and namespacing.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that the namespace attribute in the include function matches the namespaces defined in the respective urls.py files of the cinema and user apps. This is important for URL reversing and namespacing.

path("__debug__/", include("debug_toolbar.urls")),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Debug Toolbar should be used only in development. Ensure that it is disabled in production by checking the DEBUG setting in your settings file.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The debug_toolbar URL pattern is included. Ensure it is only enabled in development environments, not in production, to maintain security.

]
15 changes: 15 additions & 0 deletions user/permissions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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
)
)
23 changes: 22 additions & 1 deletion user/serializers.py
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
# 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
12 changes: 11 additions & 1 deletion user/urls.py
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
# write your code here
from django.urls import path

from user.views import CreateUserView, LoginUserView, ManageUserView

app_name = "user"

urlpatterns = [
path("register/", CreateUserView.as_view(), name="create"),
path("login/", LoginUserView.as_view(), name="login"),
path("me/", ManageUserView.as_view(), name="manage")
]
26 changes: 25 additions & 1 deletion user/views.py
Original file line number Diff line number Diff line change
@@ -1 +1,25 @@
# write your code here
from rest_framework import generics
from rest_framework.authentication import TokenAuthentication
from rest_framework.permissions import IsAuthenticated
from rest_framework.settings import api_settings
from rest_framework.authtoken.views import ObtainAuthToken

from user.permissions import IsAdminOrIfAuthenticatedReadOnly
from user.serializers import UserSerializer


class CreateUserView(generics.CreateAPIView):
serializer_class = UserSerializer


class LoginUserView(ObtainAuthToken):
renderer_classes = api_settings.DEFAULT_RENDERER_CLASSES


class ManageUserView(generics.RetrieveUpdateAPIView):
serializer_class = UserSerializer
authentication_classes = (TokenAuthentication,)
permission_classes = (IsAuthenticated,)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ManageUserView should use the IsAdminOrIfAuthenticatedReadOnly permission class instead of IsAuthenticated to meet the task requirements for access control.

def get_object(self):
return self.request.user
Loading