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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions db/migrations/0002_cinemahall_movie_moviesession.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Generated by Django 4.1.2 on 2025-01-10 20:01

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):
dependencies = [
("db", "0001_initial"),
]

operations = [
migrations.CreateModel(
name="CinemaHall",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("name", models.CharField(max_length=255)),
],
),
migrations.CreateModel(
name="Movie",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("title", models.CharField(max_length=255)),
(
"actors",
models.ManyToManyField(related_name="actors", to="db.actor"),
Comment on lines +42 to +43

Choose a reason for hiding this comment

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

The related_name for the actors field is set to 'actors'. This might conflict with the default related name Django generates for reverse lookups. Consider using a more specific name like 'movies_as_actor' to avoid potential conflicts.

Choose a reason for hiding this comment

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

The related_name for the actors field is set to "actors", which might conflict with Django's default related names. Consider using a more descriptive name like "movies_as_actor".

),
(
"genres",
models.ManyToManyField(related_name="genres", to="db.genre"),
Comment on lines +46 to +47

Choose a reason for hiding this comment

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

The related_name for the genres field is set to 'genres'. This might conflict with the default related name Django generates for reverse lookups. Consider using a more specific name like 'movies_as_genre' to avoid potential conflicts.

Choose a reason for hiding this comment

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

The related_name for the genres field is set to "genres", which might conflict with Django's default related names. Consider using a more descriptive name like "movies_as_genre".

),
],
),
migrations.CreateModel(
name="MovieSession",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("show_time", models.DateTimeField()),
(
"cinema_hall",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="movie_sessions",
to="db.cinemahall",
),
),
(
"movie",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="movie_sessions",
to="db.movie",
),
),
],
),
]
22 changes: 22 additions & 0 deletions db/migrations/0003_cinemahall_rows_cinemahall_seats_in_row.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 4.1.2 on 2025-01-10 20:07

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("db", "0002_cinemahall_movie_moviesession"),
]

operations = [
migrations.AddField(
model_name="cinemahall",
name="rows",
field=models.IntegerField(default=1),
),
migrations.AddField(
model_name="cinemahall",
name="seats_in_row",
field=models.IntegerField(default=1),
),
]
17 changes: 17 additions & 0 deletions db/migrations/0004_movie_description.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.1.2 on 2025-01-10 20:08

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("db", "0003_cinemahall_rows_cinemahall_seats_in_row"),
]

operations = [
migrations.AddField(
model_name="movie",
name="description",
field=models.TextField(null=True),
),
]
40 changes: 40 additions & 0 deletions db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,43 @@ class Actor(models.Model):

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


class Movie(models.Model):
title = models.CharField(max_length=255)
description = models.TextField(null=True)
actors = models.ManyToManyField(Actor, related_name="actors")

Choose a reason for hiding this comment

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

The related_name for the actors field is set to 'actors'. This could conflict with the default related name Django generates for reverse lookups. Consider using a more specific name like 'movies_as_actor' to avoid potential conflicts.

genres = models.ManyToManyField(Genre, related_name="genres")

Choose a reason for hiding this comment

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

The related_name for the genres field is set to 'genres'. This could conflict with the default related name Django generates for reverse lookups. Consider using a more specific name like 'movies_as_genre' to avoid potential conflicts.


def __str__(self) -> str:
return self.title


class CinemaHall(models.Model):
name = models.CharField(max_length=255)
rows = models.IntegerField(default=1)
seats_in_row = models.IntegerField(default=1)

def __str__(self) -> str:
return self.name

@property
def capacity(self) -> int:
return self.rows * self.seats_in_row


class MovieSession(models.Model):
show_time = models.DateTimeField()
cinema_hall = models.ForeignKey(
CinemaHall,
on_delete=models.CASCADE,
related_name="movie_sessions"
)
movie = models.ForeignKey(
Movie,
on_delete=models.CASCADE,
related_name="movie_sessions"
)

def __str__(self) -> str:
return f"{self.movie.title} {self.show_time}"
17 changes: 17 additions & 0 deletions services/cinema_hall.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from db.models import CinemaHall


def get_cinema_halls() -> list:
return CinemaHall.objects.all()


def create_cinema_hall(
hall_name: str,
hall_rows: int,
hall_seats_in_row: int
) -> str:

Choose a reason for hiding this comment

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

The return type of the create_cinema_hall function is specified as str, but it should be CinemaHall since the function returns an instance of the CinemaHall model.

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 create_cinema_hall is incorrect. It should return an instance of CinemaHall, not str. Update the return type annotation to CinemaHall.

return CinemaHall.objects.create(
name=hall_name,
rows=hall_rows,
seats_in_row=hall_seats_in_row
)
34 changes: 34 additions & 0 deletions services/movie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from db.models import Movie


def get_movies(genres_ids: list = None, actors_ids: list = None) -> str:

Choose a reason for hiding this comment

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

The return type of the get_movies function is specified as str, but it should be QuerySet or list depending on the desired return type, as the function returns a queryset of Movie instances.

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_movies is incorrect. It should return a QuerySet of Movie instances, not str. Update the return type annotation accordingly.

query = Movie.objects.all()
if genres_ids:
query = query.filter(genres__id__in=genres_ids)
if actors_ids:
query = query.filter(actors__id__in=actors_ids)
return query.distinct()


def get_movie_by_id(movie_id: int) -> str:

Choose a reason for hiding this comment

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

The return type of the get_movie_by_id function is specified as str, but it should be Movie since the function returns an instance of the Movie model.

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_movie_by_id is incorrect. It should return an instance of Movie, not str. Update the return type annotation accordingly.

return Movie.objects.get(id=movie_id)


def create_movie(
movie_title: str,
movie_description: str,
genres_ids: list = None,
actors_ids: list = None
) -> str:

Choose a reason for hiding this comment

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

The return type of the create_movie function is specified as str, but it should be Movie since the function returns an instance of the Movie model.

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 create_movie is incorrect. It should return an instance of Movie, not str. Update the return type annotation accordingly.


movie = Movie.objects.create(
title=movie_title,
description=movie_description,
actors=actors_ids,
genres=genres_ids

Choose a reason for hiding this comment

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

The actors and genres fields should not be assigned actors_ids and genres_ids directly during creation. Use the set method to assign these fields after the movie object is created.

)
if genres_ids:
movie.genres.set(genres_ids)
if actors_ids:
movie.actors.set(actors_ids)
return movie
47 changes: 47 additions & 0 deletions services/movie_session.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from db.models import MovieSession


def create_movie_session(
movie_show_time: str,

Choose a reason for hiding this comment

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

The movie_show_time parameter should be of type datetime instead of str to match the DateTimeField in the MovieSession model.

movie_id: int,
cinema_hall_id: int
) -> str:

Choose a reason for hiding this comment

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

The return type of the create_movie_session function is specified as str, but it should be MovieSession since the function returns an instance of the MovieSession model.

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 create_movie_session is incorrect. It should return an instance of MovieSession, not str. Update the return type annotation accordingly.

return MovieSession.objects.create(
show_time=movie_show_time,
cinema_hall=cinema_hall_id,
movie=movie_id

Choose a reason for hiding this comment

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

The cinema_hall and movie fields should be assigned CinemaHall and Movie instances, respectively, not their IDs. Consider using CinemaHall.objects.get(id=cinema_hall_id) and Movie.objects.get(id=movie_id) to retrieve the instances.

)


def get_movies_sessions(session_date: str = None) -> str:

Choose a reason for hiding this comment

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

The return type of the get_movies_sessions function is specified as str, but it should be QuerySet or list depending on the desired return type, as the function returns a queryset of MovieSession instances.

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_movies_sessions is incorrect. It should return a QuerySet of MovieSession instances, not str. Update the return type annotation accordingly.

query = MovieSession.objects.all()
if session_date:
query = query.filter(show_time__date=session_date)
return query


def get_movie_session_by_id(movie_session_id: int) -> str:

Choose a reason for hiding this comment

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

The return type of the get_movie_session_by_id function is specified as str, but it should be MovieSession since the function returns an instance of the MovieSession model.

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_movie_session_by_id is incorrect. It should return an instance of MovieSession, not str. Update the return type annotation accordingly.

return MovieSession.objects.get(id=movie_session_id)


def update_movie_session(
session_id: int,
show_time: str = None,

Choose a reason for hiding this comment

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

The show_time parameter should be of type datetime instead of str to match the DateTimeField in the MovieSession model.

movie_id: int = None,
cinema_hall_id: int = None
) -> str:

Choose a reason for hiding this comment

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

The return type of the update_movie_session function is specified as str, but it should be MovieSession since the function returns an instance of the MovieSession model.

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 update_movie_session is incorrect. It should return an instance of MovieSession, not str. Update the return type annotation accordingly.

session = MovieSession.objects.get(id=session_id)

if show_time:
session.show_time = show_time
if movie_id:
session.movie = movie_id
if cinema_hall_id:
session.cinema_hall = cinema_hall_id

Choose a reason for hiding this comment

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

The movie and cinema_hall fields should be assigned Movie and CinemaHall instances, respectively, not their IDs. Consider using Movie.objects.get(id=movie_id) and CinemaHall.objects.get(id=cinema_hall_id) to retrieve the instances.


session.save()
return session


def delete_movie_session_by_id(session_id: int) -> None:
MovieSession.objects.filter(id=session_id).delete()
2 changes: 1 addition & 1 deletion settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@

USE_I18N = True

USE_TZ = False
USE_TZ = True

INSTALLED_APPS = ("db",)
Loading