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

Added Solution #517

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
64 changes: 39 additions & 25 deletions db/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,58 @@
# Generated by Django 4.0.2 on 2022-08-19 07:39
# Generated by Django 4.0.2 on 2023-10-29 16:58

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


class Migration(migrations.Migration):

initial = True

dependencies = []
dependencies = [
]

operations = [
migrations.CreateModel(
name="Actor",
name='Actor',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('first_name', models.CharField(max_length=255)),
('last_name', models.CharField(max_length=255)),
],
),
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.IntegerField()),
('seats_in_row', models.IntegerField()),
],
),
migrations.CreateModel(
name='Genre',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=255, unique=True)),
],
),
migrations.CreateModel(
name='Movie',
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("first_name", models.CharField(max_length=255)),
("last_name", models.CharField(max_length=255)),
('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="Genre",
name='MovieSession',
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("name", models.CharField(max_length=255, unique=True)),
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('show_time', models.DateTimeField()),
('cinema', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='db.cinemahall')),
('movie', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='db.movie')),
],
),
]
18 changes: 18 additions & 0 deletions db/migrations/0002_rename_cinema_moviesession_cinema_hall.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.0.2 on 2023-10-29 17:00

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('db', '0001_initial'),
]

operations = [
migrations.RenameField(
model_name='moviesession',
old_name='cinema',
new_name='cinema_hall',
),
]
32 changes: 32 additions & 0 deletions db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,35 @@ 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 (f"{self.title}")


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

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

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


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

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

from db.models import CinemaHall


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


def create_cinema_hall(hall_name: str,
hall_rows: int,
hall_seats_in_row: int
) -> CinemaHall:
CinemaHall.objects.create(
name=hall_name,
rows=hall_rows,
seats_in_row=hall_seats_in_row
)
40 changes: 40 additions & 0 deletions services/movie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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[Movie]:
queryset = Movie.objects.all()
if genres_ids is not None and actors_ids is not None:
return queryset.filter(
genres__id__in=genres_ids
).filter(actors__id__in=actors_ids)
if genres_ids is not None:
return queryset.filter(genres__id__in=genres_ids)
if actors_ids is not None:
return queryset.filter(actors__id__in=actors_ids)

return queryset


def get_movie_by_id(movie_id: int) -> QuerySet[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
) -> QuerySet[Movie]:
create_movie = Movie.objects.create(
title=movie_title,
description=movie_description
)
if genres_ids is not None:
create_movie.genres.set(genres_ids)
if actors_ids is not None:
create_movie.actors.set(actors_ids)
return create_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 datetime import datetime

from db.models import MovieSession


def create_movie_session(
movie_show_time: datetime,
movie_id: int,
cinema_hall_id: int
) -> None:
MovieSession.objects.create(
show_time=movie_show_time,
movie_id=movie_id,
cinema_hall_id=cinema_hall_id
)


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


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: datetime = None,
movie_id: int = None,
cinema_hall_id: int = None
) -> None:
movie_session = get_movie_session_by_id(session_id)
if show_time:
movie_session.show_time = show_time
if movie_id:
movie_session.movie_id = movie_id
if cinema_hall_id:
movie_session.cinema_hall_id = cinema_hall_id

movie_session.save()


def delete_movie_session_by_id(session_id: int) -> None:
get_movie_session_by_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",)