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

"Solution" #656

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

"Solution" #656

wants to merge 4 commits into from

Conversation

mnezbrytska
Copy link

No description provided.

order = Order.objects.create(**validated_data)
for ticket_data in tickets_data:
Ticket.objects.create(order=order, **ticket_data)
return order

Choose a reason for hiding this comment

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

In create/update/delete function you don't need to return anything

cinema/views.py Outdated
@@ -11,7 +14,7 @@
MovieSessionListSerializer,
MovieDetailSerializer,
MovieSessionDetailSerializer,
MovieListSerializer,
MovieListSerializer, OrderSerializer,

Choose a reason for hiding this comment

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

Each from new line)

cinema/views.py Outdated
Comment on lines 44 to 55
if genres:
genres_ids = [int(str_id) for str_id in genres.split(",")]
queryset = queryset.filter(genres__id__in=genres_ids)

actors = self.request.query_params.get("actors")
if actors:
actors_ids = [int(str_id) for str_id in actors.split(",")]
queryset = queryset.filter(actors__id__in=actors_ids)

title = self.request.query_params.get("title")
if title:
queryset = queryset.filter(title__icontains=title)

Choose a reason for hiding this comment

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

Better to check all of these things only when self.action == "list"

cinema/views.py Outdated

actors = self.request.query_params.get("actors")
if actors:
actors_ids = [int(str_id) for str_id in actors.split(",")]

Choose a reason for hiding this comment

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

You have a duplicates, create a separate function for this

cinema/views.py Outdated
@@ -48,6 +73,32 @@ class MovieSessionViewSet(viewsets.ModelViewSet):
queryset = MovieSession.objects.all()
serializer_class = MovieSessionSerializer

def get_queryset(self):
queryset = MovieSession.objects.all()

Choose a reason for hiding this comment

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

First will be good to solve N+1 problem
So you can wrap your code in if self.actions == "list" also
And don't forget about retrieve

Choose a reason for hiding this comment

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

You should fix n+1 with movie and cinema_hall


class Meta:
model = Ticket
fields = ("id", "row", "seat", "movie_session", "order")

Choose a reason for hiding this comment

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

you don't need to display order on a ticket - in this task we actually display a ticket inside of the Order

class MovieSessionDetailSerializer(MovieSessionSerializer):
movie = MovieListSerializer(many=False, read_only=True)
cinema_hall = CinemaHallSerializer(many=False, read_only=True)
taken_places = TakenPlacesSerializer(
source="tickets",
many=True, read_only=True

Choose a reason for hiding this comment

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

each argument from new line please

cinema/urls.py Outdated
@@ -6,7 +6,7 @@
ActorViewSet,
CinemaHallViewSet,
MovieViewSet,
MovieSessionViewSet,
MovieSessionViewSet, OrderViewSet,

Choose a reason for hiding this comment

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

please move to the new line

model = Ticket
fields = ("row", "seat")


class MovieSessionDetailSerializer(MovieSessionSerializer):

Choose a reason for hiding this comment

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

What bout MovieSessionSerializer that will be used to create movie session - it must have a bit different signature

fields = ("id", "show_time", "movie", "cinema_hall", "taken_places")


class OrderSerializer(serializers.ModelSerializer):

Choose a reason for hiding this comment

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

What about OrderSerializer that will be used with list action? It will have a different signature

if self.action == "retrieve":
return MovieDetailSerializer

return MovieSerializer


class MovieSessionViewSet(viewsets.ModelViewSet):
queryset = MovieSession.objects.all()

Choose a reason for hiding this comment

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

you don't need this if you define get_queryset method

cinema/views.py Outdated
@@ -48,6 +73,32 @@ class MovieSessionViewSet(viewsets.ModelViewSet):
queryset = MovieSession.objects.all()
serializer_class = MovieSessionSerializer

def get_queryset(self):
queryset = MovieSession.objects.all()

Choose a reason for hiding this comment

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

You should fix n+1 with movie and cinema_hall

total_capacity=F(
"cinema_hall__rows") * F("cinema_hall__seats_in_row"),
tickets_sold=Count("tickets"),
tickets_available=F("total_capacity") - F("tickets_sold")

Choose a reason for hiding this comment

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

tickets available and taken seats must be displayed on different actions and different serializers



class OrderViewSet(viewsets.ModelViewSet):
queryset = Order.objects.all()

Choose a reason for hiding this comment

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

N+1 problem is present here since you access a lot of nested fields


class OrderViewSet(viewsets.ModelViewSet):
queryset = Order.objects.all()
serializer_class = OrderSerializer

Choose a reason for hiding this comment

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

you will need to add a different serializer for list action and override get_serializer_class method here

Copy link

@Arsen-hrynevych Arsen-hrynevych left a comment

Choose a reason for hiding this comment

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

ok

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants