-
Notifications
You must be signed in to change notification settings - Fork 714
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
solution #734
Open
OlhaStadnik
wants to merge
5
commits into
mate-academy:master
Choose a base branch
from
OlhaStadnik:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
solution #734
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,67 @@ | ||
# write serializers here | ||
from rest_framework import serializers | ||
from .models import Actor, CinemaHall, Genre, Movie, MovieSession, Order, \ | ||
Ticket | ||
|
||
|
||
class ActorSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Actor | ||
fields = ["id", "first_name", "last_name", "full_name"] | ||
|
||
|
||
class GenreSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Genre | ||
fields = ["id", "name"] | ||
|
||
|
||
class MovieSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Movie | ||
fields = ["id", "title", "description", "duration", "genres", "actors"] | ||
|
||
|
||
class MovieListSerializer(MovieSerializer): | ||
genres = serializers.SlugRelatedField( | ||
many=True, read_only=True, slug_field="name" | ||
) | ||
actors = serializers.SlugRelatedField( | ||
many=True, read_only=True, slug_field="full_name" | ||
) | ||
Comment on lines
+28
to
+30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
||
|
||
class MovieDetailSerializer(MovieSerializer): | ||
genres = GenreSerializer(many=True, read_only=True) | ||
actors = ActorSerializer(many=True, read_only=True) | ||
|
||
|
||
class CinemaHallSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = CinemaHall | ||
fields = ["id", "name", "rows", "seats_in_row", "capacity"] | ||
|
||
|
||
class MovieSessionSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = MovieSession | ||
fields = ["id", "show_time", "movie", "cinema_hall"] | ||
|
||
|
||
class MovieSessionListSerializer(MovieSessionSerializer): | ||
movie_title = serializers.CharField(source="movie.title", read_only=True) | ||
cinema_hall_name = serializers.CharField( | ||
source="cinema_hall.name", read_only=True | ||
) | ||
cinema_hall_capacity = serializers.IntegerField( | ||
source="cinema_hall.capacity", read_only=True | ||
) | ||
|
||
class Meta: | ||
model = MovieSession | ||
fields = ["id", "show_time", "movie_title", "cinema_hall_name", | ||
"cinema_hall_capacity"] | ||
|
||
|
||
class MovieSessionDetailSerializer(MovieSessionSerializer): | ||
movie = MovieListSerializer(read_only=True) | ||
cinema_hall = CinemaHallSerializer(read_only=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,16 @@ | ||
# write urls here | ||
from django.urls import path, include | ||
from rest_framework import routers | ||
|
||
from cinema.views import GenreViewSet, ActorViewSet, CinemaHallViewSet, \ | ||
MovieViewSet, MovieSessionViewSet | ||
|
||
router = routers.DefaultRouter() | ||
router.register("genres", GenreViewSet) | ||
router.register("actors", ActorViewSet) | ||
router.register("cinema_halls", CinemaHallViewSet) | ||
router.register("movies", MovieViewSet) | ||
router.register("movie_sessions", MovieSessionViewSet) | ||
|
||
urlpatterns = [ | ||
path("", include(router.urls)), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,43 @@ | ||
# write views here | ||
from rest_framework import viewsets | ||
from cinema.models import Genre, Actor, CinemaHall, Movie, MovieSession | ||
from cinema.serializers import GenreSerializer, ActorSerializer, \ | ||
CinemaHallSerializer, MovieSerializer, MovieSessionSerializer, \ | ||
MovieListSerializer, MovieDetailSerializer, MovieSessionListSerializer, \ | ||
MovieSessionDetailSerializer | ||
|
||
|
||
class GenreViewSet(viewsets.ModelViewSet): | ||
queryset = Genre.objects.all() | ||
serializer_class = GenreSerializer | ||
|
||
|
||
class ActorViewSet(viewsets.ModelViewSet): | ||
queryset = Actor.objects.all() | ||
serializer_class = ActorSerializer | ||
|
||
|
||
class CinemaHallViewSet(viewsets.ModelViewSet): | ||
queryset = CinemaHall.objects.all() | ||
serializer_class = CinemaHallSerializer | ||
|
||
|
||
class MovieViewSet(viewsets.ModelViewSet): | ||
queryset = Movie.objects.prefetch_related("actors", "genres") | ||
|
||
def get_serializer_class(self): | ||
if self.action == "list": | ||
return MovieListSerializer | ||
elif self.action == "retrieve": | ||
return MovieDetailSerializer | ||
return MovieSerializer | ||
|
||
|
||
class MovieSessionViewSet(viewsets.ModelViewSet): | ||
queryset = MovieSession.objects.select_related("movie", "cinema_hall") | ||
|
||
def get_serializer_class(self): | ||
if self.action == "list": | ||
return MovieSessionListSerializer | ||
elif self.action == "retrieve": | ||
return MovieSessionDetailSerializer | ||
return MovieSessionSerializer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
from django.contrib import admin | ||
from django.urls import path | ||
from django.urls import path, include | ||
|
||
urlpatterns = [ | ||
path("admin/", admin.site.urls), | ||
path("api/cinema/", include("cinema.urls")), | ||
] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
actors
field inMovieListSerializer
usesSlugRelatedField
withslug_field='full_name'
. However,full_name
is a property, not a model field, which will cause an error. Consider using aSerializerMethodField
to serialize the full name instead.