-
Notifications
You must be signed in to change notification settings - Fork 722
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 #683
base: master
Are you sure you want to change the base?
Solution #683
Conversation
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.
You've duplicated a lot of code that's already implemented in Django. To adhere to the DRY (Don't Repeat Yourself) principle, you could simplify your code.
Could you please provide more details on what I need to refactor? I have seen another approved solution for this task and identified two areas for improvement:
|
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.
Thank you! Instead of just resending your task for review, please take a moment to look it over or ask any questions. After making the fixes, I'll wait for the solution.
cinema/models.py
Outdated
def __str__(self) -> str: | ||
return f"{self.name}: rows - {self.rows}, seats - {self.seats_in_row}" | ||
|
||
|
||
class Movie(models.Model): | ||
title = models.CharField(max_length=255) | ||
description = models.TextField() | ||
duration = models.IntegerField() |
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.
Using IntegerField for movie duration might work, but it's unclear what unit of time you're storing (minutes, seconds, etc.). It might be beneficial to add a help_text attribute or use DurationField for clarity and flexibility.
cinema/serializers.py
Outdated
@@ -1,24 +1,63 @@ | |||
from rest_framework import serializers | |||
|
|||
from cinema.models import Movie | |||
from cinema.models import Movie, Actor, Genre, CinemaHall |
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.
If importing more than two items, it is better to write them on separate lines. This improves readability and maintenance of the code.
Example:
From taxi.models import (
Driver,
Manufacturer,
Car
)
Read more about topic here
ActorDetail, | ||
CinemaHallViewSet, | ||
) | ||
|
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.
2 blank lines after imports
cinema/serializers.py
Outdated
@@ -1,24 +1,63 @@ | |||
from rest_framework import serializers | |||
|
|||
from cinema.models import Movie | |||
from cinema.models import Movie, Actor, Genre, CinemaHall | |||
|
|||
|
|||
class MovieSerializer(serializers.Serializer): |
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 MovieSerializer could be simplified by using ModelSerializer instead of Serializer. Since you're working with a Django model, ModelSerializer automatically handles common functionality (like field definitions and create/update methods) for you.
cinema/serializers.py
Outdated
actors = validated_data.get("actors", None) | ||
genres = validated_data.get("genres", None) | ||
|
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.
In the update method of MovieSerializer, the checks for actors and genres could be simplified. Instead of checking whether they exist and updating only if provided, you might directly set these values, as .set() works with empty lists as well.
cinema/views.py
Outdated
class ActorList( | ||
mixins.ListModelMixin, | ||
mixins.CreateModelMixin, | ||
generics.GenericAPIView | ||
): | ||
queryset = Actor.objects.all() | ||
serializer_class = ActorSerializer | ||
|
||
def get(self, request: HttpRequest, *args, **kwargs) -> Response: | ||
return self.list(request, *args, **kwargs) | ||
|
||
def post(self, request: HttpRequest, *args, **kwargs) -> Response: | ||
return self.create(request, *args, **kwargs) |
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.
In your ActorList and ActorDetail classes, the get and post methods could be simplified further by calling the methods directly.
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.
I cannot delete these methods because it will cause test failures in both classes
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.
OK. I've found the solution for this already. I have changed inherit, but in READMY.md write that for the Actor model I need to use a GenericAPIView
cinema/views.py
Outdated
class ActorDetail( | ||
mixins.RetrieveModelMixin, | ||
mixins.UpdateModelMixin, | ||
mixins.DestroyModelMixin, | ||
generics.GenericAPIView, | ||
): | ||
queryset = Actor.objects.all() | ||
serializer_class = ActorSerializer | ||
|
||
def get(self, request: HttpRequest, *args, **kwargs) -> Response: | ||
return self.retrieve(request, *args, **kwargs) | ||
|
||
def put(self, request: HttpRequest, *args, **kwargs) -> Response: | ||
return self.update(request, *args, **kwargs) | ||
|
||
def patch(self, request: HttpRequest, *args, **kwargs) -> Response: | ||
return self.partial_update(request, *args, **kwargs) | ||
|
||
def delete(self, request: HttpRequest, *args, **kwargs) -> Response: | ||
return self.destroy(request, *args, **kwargs) |
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.
No need for overrides:
Since ListModelMixin provides the logic for listing and creating objects, and RetrieveModelMixin, UpdateModelMixin, and DestroyModelMixin handle retrieval, updating, and deletion, you don't need to redefine those methods.
No description provided.