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 #683

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

Conversation

OleksiukStepan
Copy link

No description provided.

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.

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.

@OleksiukStepan
Copy link
Author

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:

  1. change
    genre = self.get_object(pk)
    genre.delete()
    to
    genre.self.get_objects(pk).delete()

  2. Use ModelSerializer for MovieSerializer (what as I understand unnecessary for this task)

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.

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()

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.

@@ -1,24 +1,63 @@
from rest_framework import serializers

from cinema.models import Movie
from cinema.models import Movie, Actor, Genre, CinemaHall

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,
)

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

@@ -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):

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.

Comment on lines 34 to 36
actors = validated_data.get("actors", None)
genres = validated_data.get("genres", None)

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
Comment on lines 60 to 72
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)

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.

Copy link
Author

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

Copy link
Author

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
Comment on lines 75 to 94
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)

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.

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.

4 participants