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

Open
wants to merge 3 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
42 changes: 42 additions & 0 deletions db/migrations/0002_cinemahall_movie_moviesession.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Generated by Django 4.0.2 on 2025-01-11 15:20

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)),
('rows', models.PositiveIntegerField()),
('seats_in_row', models.PositiveIntegerField()),
],
),
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)),
('description', models.TextField()),
('actors', models.ManyToManyField(to='db.Actor')),
('genres', models.ManyToManyField(to='db.Genre')),
],
),
migrations.CreateModel(
name='MovieSession',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),

Choose a reason for hiding this comment

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

The show_time field uses auto_now_add=True, which automatically sets the field to the current date and time when the object is created. This might not be suitable if you need to set the show time manually. Consider using auto_now=False and auto_now_add=False to allow manual setting.

('show_time', models.DateTimeField(auto_now_add=True)),

Choose a reason for hiding this comment

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

The show_time field is set with auto_now_add=True, which automatically sets the field to the current date and time upon creation. This is not suitable for a field that should be set manually. Consider removing auto_now_add=True to allow manual setting.

('cinema_hall', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='db.cinemahall')),
('movie', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='db.movie')),
Comment on lines +38 to +39

Choose a reason for hiding this comment

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

The on_delete=django.db.models.deletion.DO_NOTHING setting for foreign keys means that no action is taken when the referenced object is deleted. This might lead to orphaned records in your database. Consider using CASCADE, SET_NULL, or another appropriate option to handle deletions more gracefully.

Comment on lines +38 to +39

Choose a reason for hiding this comment

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

The on_delete=DO_NOTHING setting for foreign keys can lead to orphaned records. Consider using CASCADE or SET_NULL to handle deletions more gracefully.

],
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 4.0.2 on 2025-01-11 15:16

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('db', '0002_cinemahall_movie_moviesession'),
]

operations = [
migrations.AlterField(
model_name='cinemahall',
name='rows',
field=models.IntegerField(),

Choose a reason for hiding this comment

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

Changing rows from PositiveIntegerField to IntegerField allows negative values, which might not be appropriate for this field. Consider using PositiveIntegerField to ensure only positive values are accepted.

),
migrations.AlterField(
model_name='cinemahall',
name='seats_in_row',
field=models.IntegerField(),

Choose a reason for hiding this comment

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

Changing seats_in_row from PositiveIntegerField to IntegerField allows negative values, which might not be appropriate for this field. Consider using PositiveIntegerField to ensure only positive values are accepted.

Comment on lines +16 to +21

Choose a reason for hiding this comment

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

The fields rows and seats_in_row have been altered to IntegerField, which allows negative values. It would be more appropriate to use PositiveIntegerField to ensure only positive values are accepted.

),
migrations.AlterField(
model_name='moviesession',
name='show_time',
field=models.DateTimeField(),
),
]
24 changes: 24 additions & 0 deletions db/migrations/0004_alter_moviesession_cinema_hall_and_more.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 4.0.2 on 2025-01-11 15:30

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


class Migration(migrations.Migration):

dependencies = [
('db', '0003_alter_cinemahall_rows_alter_cinemahall_seats_in_row_and_more'),
]

operations = [
migrations.AlterField(
model_name='moviesession',
name='cinema_hall',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='cinema_halls', to='db.cinemahall'),

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 cinema_hall ForeignKey is set to 'cinema_halls', which might be misleading. Consider using a more descriptive name like 'movie_sessions' to better reflect the relationship.

),
migrations.AlterField(
model_name='moviesession',
name='movie',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='movie_sessions', to='db.movie'),
),
]
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()
actors = models.ManyToManyField(Actor)
genres = models.ManyToManyField(Genre)

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


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

Choose a reason for hiding this comment

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

Consider using PositiveIntegerField for rows to ensure only positive values are accepted, as negative numbers do not make sense for this field.

seats_in_row = models.IntegerField()

Choose a reason for hiding this comment

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

Consider using PositiveIntegerField for seats_in_row to ensure only positive values are accepted, as negative numbers do not make sense for this field.


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="cinema_halls"

Choose a reason for hiding this comment

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

The related_name 'cinema_halls' might be misleading as it suggests multiple halls. Consider using a more descriptive name like 'sessions' or 'movie_sessions' to better reflect the relationship.

)
movie = models.ForeignKey(
Movie,
on_delete=models.CASCADE,
related_name="movie_sessions"
)

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

from db.models import CinemaHall


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


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

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

from db.models import Movie


def get_movies(
genres_ids: list[int] = None,
actors_ids: list[int] = None
) -> QuerySet:

if genres_ids and actors_ids:
return Movie.objects.filter(genres__id__in=genres_ids,
actors__id__in=actors_ids)
if genres_ids:
return Movie.objects.filter(genres__id__in=genres_ids)
if actors_ids:
return Movie.objects.filter(actors__id__in=actors_ids)
return Movie.objects.all()


def get_movie_by_id(movie_id: int) -> Movie:
return Movie.objects.get(id=movie_id)


def create_movie(
movie_title: str,
movie_description: str,
genres_ids: list[int] = None,
actors_ids: list[int] = None
) -> Movie:
new_movie = Movie.objects.create(
title=movie_title,
description=movie_description
)
if genres_ids:
new_movie.genres.set(genres_ids)
if actors_ids:
new_movie.actors.set(actors_ids)
return new_movie
48 changes: 48 additions & 0 deletions services/movie_session.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from django.db.models import QuerySet
from datetime import datetime
from db.models import Movie, CinemaHall, MovieSession


def create_movie_session(
movie_show_time: datetime,
movie_id: int, cinema_hall_id: int
) -> MovieSession:

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


def get_movies_sessions(session_date: str = None) -> QuerySet:
if session_date:
return MovieSession.objects.filter(show_time__date=session_date)
return MovieSession.objects.all()


def get_movie_session_by_id(movie_session_id: int) -> MovieSession:
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 is expected to be a string, but MovieSession.show_time is a DateTimeField. Consider converting the string to a datetime object before assigning it to session_to_update.show_time.

movie_id: int = None,
cinema_hall_id: int = None
) -> MovieSession:
session_to_update = MovieSession.objects.get(id=session_id)
if show_time:
session_to_update.show_time = show_time
if movie_id:
session_to_update.movie = Movie.objects.get(id=movie_id)
if cinema_hall_id:
session_to_update.cinema_hall = CinemaHall.objects.get(
id=cinema_hall_id
)
session_to_update.save()
return session_to_update


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