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

Develop #913

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

Develop #913

wants to merge 4 commits into from

Conversation

AversArthur
Copy link

No description provided.

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor 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 done a great job so far! 😊 However, there are a few areas that need some improvement. Please refer to the comments for more details. Keep up the good work! 💪

✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨

db/models.py Outdated
@@ -1,3 +1,5 @@
from django.contrib.auth.models import User, AbstractUser

Choose a reason for hiding this comment

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

The User model is imported but not used in this file. It is a good practice to remove unused imports, as they can lead to confusion and make the code harder to read. Additionally, use get_user_model() instead of AbstractUser or User when you need to refer to the user model. This is a best practice in Django and allows for easier customization of the user model in the future.

db/models.py Outdated
Comment on lines 58 to 61
created_at = models.DateTimeField(auto_now_add=True)
user = models.ForeignKey(to='User',
on_delete=models.CASCADE,
related_name="orders")

Choose a reason for hiding this comment

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

You're using a string to refer to the User model in the ForeignKey field. While this is not wrong and can be useful in avoiding circular import issues, it's better to use the settings.AUTH_USER_MODEL to refer to the user model. This allows for better flexibility if you decide to change the user model in the future.

db/models.py Outdated
Comment on lines 57 to 67
class Order(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
user = models.ForeignKey(to='User',
on_delete=models.CASCADE,
related_name="orders")

class Meta:
ordering = ("-created_at",)

def __str__(self) -> str:
return f"{self.created_at}"

Choose a reason for hiding this comment

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

The class Meta is implemented before some attributes are defined. Remember that it's a good practice to define all attributes before implementing class Meta.

Comment on lines +115 to +116
class User(AbstractUser):
pass

Choose a reason for hiding this comment

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

The User model is empty, it is defined but no fields are added. If you don't need to add any fields, you don't need to create this class. Django will use the default User model. If you want to add fields in the future, you can leave it, but it's better to add a comment explaining that.

from django.db.models import QuerySet
from django.utils.datetime_safe import datetime

from db.models import Ticket, Order, User, MovieSession

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 using the User model. This is a best practice in Django to avoid potential issues with custom user models.

Comment on lines +9 to +11
def create_order(tickets: list[Ticket],
username: str,
date: str = None) -> QuerySet:

Choose a reason for hiding this comment

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

The function create_order is missing a docstring. It's a good practice to include a docstring at the beginning of every function to describe its purpose, parameters, and return value.



def get_orders(username: str = None) -> QuerySet:
if username:

Choose a reason for hiding this comment

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

The function get_orders is missing a docstring. It's a good practice to include a docstring at the beginning of every function to describe its purpose, parameters, and return value.

from django.contrib.auth import get_user_model


User = get_user_model()

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 User directly. You've imported the function but didn't use it in the rest of your code.

Comment on lines +14 to +23
user = User.objects.create_user(username=username)
user.set_password(password)
if email:
user.email = email
if first_name:
user.first_name = first_name
if last_name:
user.last_name = last_name

user.save()

Choose a reason for hiding this comment

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

The create_user method actually sets the password for the user, so there's no need to call set_password separately. You can pass the password as an argument to create_user. Also, you can pass other optional parameters like email, first_name, last_name directly to create_user if they are not None. This way, you can avoid multiple if checks and call save() only once.

Comment on lines +39 to +51
user = User.objects.get(pk=user_id)
if username:
user.username = username
if password:
user.set_password(password)
if email:
user.email = email
if first_name:
user.first_name = first_name
if last_name:
user.last_name = last_name

user.save()

Choose a reason for hiding this comment

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

In the update_user function, you can also reduce the number of if checks by using the update method of the QuerySet API. This method allows you to update certain fields of the model directly in the database, without having to load it into Python. This can be more efficient if you are updating a lot of instances at once because it only requires one database query. However, keep in mind that the update method does not call the save method or trigger the pre_save or post_save signals, so if you need those to happen, you should stick with your current approach.

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

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

Great work! Your code meets the task requirements and follows the checklist. There are a few areas for improvement, but they don't significantly impact the functionality of your code. Keep going! 🚀

✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨

@@ -42,3 +42,8 @@ def update_movie_session(

def delete_movie_session_by_id(session_id: int) -> None:
MovieSession.objects.get(id=session_id).delete()

Choose a reason for hiding this comment

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

Same as the previous comment, it's recommended to handle exceptions where a record is not found in the database. You can use Django's get_object_or_404 function to raise a 404 error if the MovieSession object is not found.

from django.db.models import QuerySet
from django.utils.datetime_safe import datetime

from db.models import Ticket, Order, User, MovieSession

Choose a reason for hiding this comment

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

The User model is imported directly. According to the checklist, you should use the get_user_model function for this purpose.

def create_order(tickets: list[Ticket],
username: str,
date: str = None) -> QuerySet:
user = User.objects.get(username=username)

Choose a reason for hiding this comment

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

Here you are using User directly instead of get_user_model(). Replace User with get_user_model(). This is a best practice for Django.

Comment on lines +14 to +17
order = Order.objects.create(user=user)
if date:
order.created_at = datetime.strptime(date, "%Y-%m-%d %H:%M")
order.save(update_fields=["created_at"])

Choose a reason for hiding this comment

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

The save() method is called twice for the Order object. According to the checklist, the save() method should be called only once if possible. You can set the created_at attribute when creating the Order object.

Comment on lines +9 to +11
def create_order(tickets: list[Ticket],
username: str,
date: str = None) -> QuerySet:

Choose a reason for hiding this comment

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

The date argument is optional, but there is no default value provided. This can lead to a TypeError if the date argument is not provided when the function is called. You should provide a default value for date.

Comment on lines +20 to +21
movie_session = (MovieSession.objects
.get(id=ticket_data["movie_session"]))

Choose a reason for hiding this comment

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

The get method is used here, which can raise a DoesNotExist exception if no MovieSession object with the provided id is found. You should handle this potential exception.

first_name: str = None,
last_name: str = None,
) -> User:
user = User.objects.create_user(username=username)

Choose a reason for hiding this comment

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

This line of code does not meet the requirement 'Don't Repeat Yourself' from the checklist. The save() method is called in the create_user method of the User model and you call it again on line 23. You can avoid this by creating the user instance without saving it to the database using User(username=username) and then save it after all changes on line 23.

from django.contrib.auth import get_user_model


User = get_user_model()

Choose a reason for hiding this comment

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

According to the 'Code Style' section of the checklist, it is best to use get_user_model() instead of User. You've imported get_user_model, but then you've assigned it to User and used User throughout the code. It would be better to use get_user_model() directly when you need to access the User model.

first_name: str = None,
last_name: str = None,
) -> User:
user = User.objects.create_user(username=username)

Choose a reason for hiding this comment

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

This line of code does not meet the requirement 'Choose correct and suitable variable names' from the checklist. The variable user is suitable in this context, but remember to always use descriptive and meaningful variable names.

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.

2 participants