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

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

Solution #951

wants to merge 2 commits into from

Conversation

sumseer
Copy link

@sumseer sumseer commented Nov 12, 2024

No description provided.

Copy link

@FursykIvan FursykIvan left a comment

Choose a reason for hiding this comment

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

Looks great!)

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.


Thank you for your hard work on this pull request! 😊 While your code is mostly well-structured and meets many of the task requirements, there are a few critical issues that need to be addressed before we can approve it:

  1. Repetition of save() Method: In the create_order function, the save() method is called twice. This can lead to inefficiencies and potential errors. Please refactor the code to ensure save() is called only once.

  2. Use of Previously Created Functions: In the update_user function, directly retrieving the user instead of using the existing get_user function affects readability and maintainability. Please update the code to use the get_user function.

  3. Exception Handling: Ensure that exceptions are handled when retrieving users by ID or username. This will prevent the application from crashing if a user is not found.

  4. Return Type for create_movie: The return type for create_movie is incorrectly specified. It should return a Movie instance, not a QuerySet.

  5. Missing Migrations: Please confirm that the migration file is up-to-date with the current model changes to ensure tests pass.

Once these issues are addressed, your code will be in great shape! Keep up the good work, and feel free to reach out if you have any questions or need further assistance. 🚀

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

🔄 Fix the issues, commit the changes, and then re-request my review using the Re-request review button in the Reviewers tab.

fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created_at', models.DateTimeField(auto_now_add=True)),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='orders', to=settings.AUTH_USER_MODEL)),

Choose a reason for hiding this comment

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

Ensure that the AUTH_USER_MODEL setting in your Django settings is correctly configured to use this custom User model. This is crucial for the ForeignKey relationship defined here.

),
migrations.AddConstraint(
model_name='ticket',
constraint=models.UniqueConstraint(fields=('row', 'seat', 'movie_session'), name='ticket_constraint'),

Choose a reason for hiding this comment

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

The unique constraint on the Ticket model ensures that each combination of row, seat, and movie_session is unique. This is a good practice to prevent duplicate tickets for the same seat in a session.

class Order(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
user = models.ForeignKey(
settings.AUTH_USER_MODEL,

Choose a reason for hiding this comment

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

Ensure that the AUTH_USER_MODEL setting in your Django settings is correctly configured to use this custom User model. This is crucial for the ForeignKey relationship defined here.

Comment on lines +97 to +116
def clean(self) -> None:
cinema_hall = self.movie_session.cinema_hall
if self.row > cinema_hall.rows:
raise ValidationError(
{
"row": [
"row number must be in available range: "
f"(1, rows): (1, {cinema_hall.rows})"
]
}
)
if self.seat > cinema_hall.seats_in_row:
raise ValidationError(
{
"seat": [
"seat number must be in available range: "
f"(1, seats_in_row): (1, {cinema_hall.seats_in_row})"
]
}
)

Choose a reason for hiding this comment

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

The clean method in the Ticket model is correctly implemented to validate that the row and seat numbers are within the valid range for the associated cinema_hall. This ensures data integrity and prevents invalid ticket entries.

)

def save(self, *args, **kwargs) -> None:
self.full_clean()

Choose a reason for hiding this comment

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

Calling self.full_clean() in the save method ensures that the clean method is executed before saving, which is a good practice to maintain data integrity.

username: str,
date: datetime = None) -> Order:

user = get_user_model().objects.get(username=username)

Choose a reason for hiding this comment

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

Consider handling exceptions when retrieving the user by username. If the user does not exist, get() will raise a DoesNotExist exception, which should be caught and handled appropriately.


def get_orders(username: str = None) -> Order:
if username:
user = get_user_model().objects.get(username=username)

Choose a reason for hiding this comment

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

Similar to the comment above, ensure that exceptions are handled when retrieving the user by username. This will prevent the application from crashing if the user is not found.

services/user.py Outdated


def get_user(user_id: int) -> User:
return get_user_model().objects.get(id=user_id)

Choose a reason for hiding this comment

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

Consider handling exceptions when retrieving the user by ID. If the user does not exist, get() will raise a DoesNotExist exception, which should be caught and handled appropriately.

services/user.py Outdated
last_name: str = None
) -> User:

user = get_user_model().objects.get(id=user_id)

Choose a reason for hiding this comment

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

Similar to the comment above, ensure that exceptions are handled when retrieving the user by ID. This will prevent the application from crashing if the user is not found.

"db",
]

AUTH_USER_MODEL = "db.User"

Choose a reason for hiding this comment

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

The AUTH_USER_MODEL setting is correctly configured to use the custom User model from the db app. This ensures that the custom user model is used throughout the project.

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 job on addressing most of the previous issues! 🎉 Your code is well-structured and adheres to Django's best practices in many areas. However, there are a few points that need your attention to enhance the code's functionality and maintainability:

  1. Repetition of save() Method: In the create_order function, the save() method is called twice. Refactor this to ensure save() is called only once per object to avoid redundancy.

  2. Use of Previously Created Functions: Improve maintainability by using the get_user function within the update_user function for consistency.

  3. Return Type for get_orders: Update the return type annotation to QuerySet[Order] to accurately reflect the function's output.

  4. Exception Handling: Implement exception handling for scenarios where a MovieSession with the given ID is not found to prevent runtime errors.

  5. Timezone Settings: Consider setting USE_TZ = True to enable timezone-aware datetimes, which is a recommended practice for handling time data in Django applications.

These improvements will not only enhance the robustness of your code but also make it more maintainable and consistent. Keep up the great work, and continue learning from these experiences to refine your skills! 🚀

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

Comment on lines 43 to 44
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.

Implement exception handling to manage scenarios where the MovieSession with the given ID is not found. This will prevent potential runtime errors during deletion.


if date:
order.created_at = date
order.save()

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 in the create_order function: once after setting the created_at date and again implicitly when tickets are associated with the order. Consider refactoring to call save() only once after all modifications are complete to avoid redundancy.

return order


def get_orders(username: str = None) -> Order:

Choose a reason for hiding this comment

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

The return type annotation for get_orders is incorrect. This function returns a QuerySet of Order objects, not a single Order. Consider updating the return type to QuerySet[Order].

Comment on lines +46 to +47
try:
user = get_user_model().objects.get(id=user_id)

Choose a reason for hiding this comment

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

Consider using the get_user function to retrieve the user instance in the update_user function. This will improve code maintainability and consistency by reusing existing functionality.

@@ -24,5 +24,9 @@
USE_TZ = False

Choose a reason for hiding this comment

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

Consider setting USE_TZ = True to enable timezone-aware datetimes, which is a recommended practice for handling time data in Django applications.

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.

3 participants