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

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

Solution #870

wants to merge 4 commits into from

Conversation

dpiuro
Copy link

@dpiuro dpiuro commented Sep 1, 2024

No description provided.

db/models.py Outdated
class Order(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
user = models.ForeignKey(
"User",

Choose a reason for hiding this comment

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

you should use AUTH USER MODEL from settings

return f"{self.created_at}"

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

Choose a reason for hiding this comment

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

Meta class should be defined right after fields by django convention

db/models.py Outdated
)

def clean(self) -> None:
if self.row > self.movie_session.cinema_hall.rows:

Choose a reason for hiding this comment

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

you don't validate if numbers are below zero here

Choose a reason for hiding this comment

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

It is possible to combine these two conditions above one Validation Error such as : if not 0 < x <= y

db/models.py Outdated
constraints = [
models.UniqueConstraint(
fields=["movie_session", "row", "seat"],
name="unique_ticket_constraint"

Choose a reason for hiding this comment

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

constraint name should contain names of fields that are unique + Meta class should be moved above str method

{"row": ticket.row, "seat": ticket.seat}
for ticket in tickets
]
return taken_seats

Choose a reason for hiding this comment

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

can you do it using .values() on queryset?

id=ticket_data["movie_session"]
)
Ticket.objects.create(
movie_session=movie_session,

Choose a reason for hiding this comment

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

no need to create movie_session object - you can pass an id on Ticket creation instead

if username:
user = User.objects.get(username=username)
orders = Order.objects.filter(user=user)
else:

Choose a reason for hiding this comment

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

refactor so you use only one if statement and return intruction. Use lookup to filter data user__username

return order


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

Choose a reason for hiding this comment

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

Add annotation for optional parameters

services/user.py Outdated
def create_user(
username: str,
password: str,
email: str = None,

Choose a reason for hiding this comment

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

add optional annotation

services/user.py Outdated
first_name: str = None,
last_name: str = None
) -> User:
user = User.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.

reuse your get_user function here to make it DRY


from db.models import Movie


def get_movies(
genres_ids: list[int] = None,
actors_ids: list[int] = None,
title: str = None

Choose a reason for hiding this comment

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

Annotation that this parameter can be None is missing

db/models.py Outdated
)

def clean(self) -> None:
if self.row > self.movie_session.cinema_hall.rows:

Choose a reason for hiding this comment

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

It is possible to combine these two conditions above one Validation Error such as : if not 0 < x <= y



def get_taken_seats(movie_session_id: int) -> list[dict]:
tickets = (

Choose a reason for hiding this comment

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

no need to store tickets in a variable that you will not reuse. just return result

username: str,
date: str = None
) -> Order:
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.

yeah, my mistake there is no need for transaction in read operation here 😊

@@ -38,3 +40,7 @@ def create_movie(
movie.actors.set(actors_ids)

return movie

Choose a reason for hiding this comment

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

You don't need return anything in create/update/delete functions, check everywhere

Comment on lines +78 to +85
movie_session = models.ForeignKey(
to=MovieSession,
on_delete=models.CASCADE,
related_name="tickets")
order = models.ForeignKey(
to=Order,
on_delete=models.CASCADE,
related_name="tickets")
Copy link

Choose a reason for hiding this comment

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

the closing bracket should be on a separate line

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