-
Notifications
You must be signed in to change notification settings - Fork 857
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 #911
base: master
Are you sure you want to change the base?
Solution #911
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.
Great job on your code! However, there are a few improvements that need to be made. Please handle potential exceptions to provide user-friendly messages and avoid multiple saves on the same object for efficiency. Keep going! 🚀
✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨
services/movie_session.py
Outdated
@@ -42,3 +42,12 @@ def update_movie_session( | |||
|
|||
def delete_movie_session_by_id(session_id: int) -> None: | |||
MovieSession.objects.get(id=session_id).delete() |
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.
This line of code can raise a MovieSession.DoesNotExist
exception if the session with the provided id doesn't exist. It's a good practice to handle this exception and provide a user-friendly message. You can do this by using Django's get_object_or_404()
function or by adding a try/except
block around this line.
order = Order.objects.create(username=username) | ||
if date: | ||
order.created_at = date | ||
order.save() |
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're calling the save()
method twice for the order
object which is against the DRY principle. You should only call save()
once after you've finished modifying the object. Here's how you can refactor your code:
python
order = Order.objects.create(username=username)
if date:
order.created_at = date
order.save()
services/order.py
Outdated
row=ticket.get("row"), | ||
seats=ticket.get("seat") | ||
) | ||
ticket.save() |
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.
Similarly, you're calling save()
method twice for the ticket
object. You don't need to call save()
after create()
because create()
already saves the object in the database. You can safely remove this line.
services/user.py
Outdated
@@ -0,0 +1,43 @@ | |||
from django.contrib.auth import get_user_model | |||
|
|||
from db.models import User |
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.
According to the checklist, you should use get_user_model()
instead of directly importing User
from db.models
. This is a best practice in Django to avoid potential issues with custom user models.
email: str = None, | ||
first_name: str = None, | ||
last_name: str = None) -> None: | ||
user = get_user_model().objects.create_user(username=username) |
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.
According to the checklist, you should use double quotes in your code. Please, replace single quotes with double quotes.
services/user.py
Outdated
user.last_name = last_name | ||
|
||
|
||
def get_user(user_id: int) -> User: |
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.
It's better to use get_user_model()
function instead of directly using User
. This is a good practice in Django.
services/user.py
Outdated
email: str = None, | ||
first_name: str = None, | ||
last_name: str = None) -> None: | ||
user = User.objects.get(pk=user_id) |
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.
It's better to use get_user_model()
function instead of directly using User
. This is a good practice in Django.
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.
Great job on the pull request! Most of the code looks good, and there are no critical issues that would impact functionality or security. There are, however, a few minor points that you could improve upon. Keep up the good work, and remember to consider the suggestions provided. Happy coding! 😊
✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨
|
||
|
||
class User(AbstractUser): | ||
pass |
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.
Code Style: It's recommended to use get_user_model()
instead of directly referring to the User
model. This is considered a best practice in Django because it allows for flexibility if the user model is swapped out in the future.
|
||
|
||
class Order(models.Model): | ||
created_at = models.DateTimeField(auto_now_add=True) |
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.
Code Style: The class Meta
is defined before all attributes of the Order
model. According to the checklist, the class Meta
should be defined after all attributes. Please move class Meta
after the user
attribute.
row = models.IntegerField() | ||
seat = models.IntegerField() | ||
|
||
class Meta: |
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.
Code Style: Similar to the Order
model, the class Meta
is defined before all attributes of the Ticket
model. Please move class Meta
after the seat
attribute.
@@ -1,13 +1,18 @@ | |||
from django.db.models import QuerySet | |||
|
|||
from django.db import transaction | |||
|
|||
from db.models import Movie | |||
|
|||
|
|||
def get_movies( | |||
genres_ids: list[int] = 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.
The variable name genres_ids
may be a bit misleading. It seems to suggest that you are expecting multiple ids for a single genre. Consider renaming it to genre_ids
to make it clear that it refers to ids of multiple genres.
@@ -1,13 +1,18 @@ | |||
from django.db.models import QuerySet | |||
|
|||
from django.db import transaction | |||
|
|||
from db.models import Movie | |||
|
|||
|
|||
def get_movies( | |||
genres_ids: list[int] = None, | |||
actors_ids: list[int] = 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.
The same issue with the variable name actors_ids
. Consider renaming it to actor_ids
.
for ticket in tickets: | ||
dic = {"row": ticket.row, "seat": ticket.seat} | ||
result_list.append(dic) | ||
return result_list |
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.
Instead of manually creating a list of dictionaries, you could use Django's values()
method. This method returns a QuerySet that returns dictionaries, rather than model instances, when used as an iterable. Each dictionary represents an object, with the keys corresponding to the attribute names of model objects.
username: str, | ||
date: datetime | ||
) -> None: | ||
order = Order.objects.create(username=username) |
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.
Code style issue: It's better to use get_user_model().objects.create_user()
instead of Order.objects.create(username=username)
. This is a best practice.
if date: | ||
order.created_at = date | ||
order.save() |
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.
Don't Repeat Yourself: You are calling the save()
method twice. It's better to call it once after all changes are made.
ticket = Ticket.objects.create( | ||
movie_session=ticket.get("movie_session"), | ||
order=order, | ||
row=ticket.get("row"), | ||
seats=ticket.get("seat") | ||
) |
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.
Code style issue: Use one style of quotes in your code. You have used both single and double quotes. Double quotes are preferable.
first_name: str = None, | ||
last_name: str = None) -> None: | ||
user = get_user_model().objects.create_user(username=username) | ||
user.set_password(password) |
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.
According to the checklist, you should call the save()
method only once if possible. In the create_user
function, you are calling set_password
method which internally calls save()
. It's better to set the password directly while creating the user. Here is an example: user = get_user_model().objects.create_user(username=username, password=password)
.
No description provided.