-
Notifications
You must be signed in to change notification settings - Fork 1k
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 #1062
base: master
Are you sure you want to change the base?
Solution #1062
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Generated by Django 4.0.2 on 2025-01-09 14:16 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('db', '0001_initial'), | ||
] | ||
|
||
operations = [ | ||
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(related_name='movies', to='db.Actor')), | ||
('genres', models.ManyToManyField(related_name='movies', to='db.Genre')), | ||
], | ||
), | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Generated by Django 4.0.2 on 2025-01-09 14:27 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('db', '0002_movie'), | ||
] | ||
|
||
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='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, to='db.cinemahall')), | ||
('movie', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='db.movie')), | ||
], | ||
), | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Generated by Django 4.0.2 on 2025-01-09 14:48 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('db', '0003_cinemahall_moviesession'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='moviesession', | ||
name='cinema_hall', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='cinema_halls', to='db.cinemahall'), | ||
), | ||
migrations.AlterField( | ||
model_name='moviesession', | ||
name='movie', | ||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='movie_sessions', to='db.movie'), | ||
), | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,49 @@ 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( | ||
"db.Actor", | ||
related_name="movies" | ||
) | ||
genres = models.ManyToManyField( | ||
"db.Genre", | ||
related_name="movies" | ||
) | ||
|
||
def __str__(self) -> str: | ||
return self.title | ||
|
||
|
||
class CinemaHall(models.Model): | ||
name = models.CharField(max_length=255) | ||
rows = models.PositiveIntegerField() | ||
seats_in_row = models.PositiveIntegerField() | ||
|
||
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( | ||
"db.CinemaHall", | ||
on_delete=models.CASCADE, | ||
related_name="cinema_halls" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
) | ||
movie = models.ForeignKey( | ||
"db.Movie", | ||
on_delete=models.CASCADE, | ||
related_name="movie_sessions" | ||
) | ||
|
||
def __str__(self) -> str: | ||
return f"{str(self.movie)} {self.show_time}" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
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 | ||
) -> None: | ||
CinemaHall.objects.create( | ||
name=hall_name, | ||
rows=hall_rows, | ||
seats_in_row=hall_seats_in_row | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from typing import Optional | ||
|
||
from django.db.models import QuerySet | ||
|
||
from db.models import Movie | ||
|
||
|
||
def get_movies( | ||
genres_ids: Optional[list[int]] = None, | ||
actors_ids: Optional[list[int]] = None | ||
) -> QuerySet: | ||
if genres_ids and actors_ids: | ||
return Movie.objects.filter( | ||
genres__id__in=genres_ids | ||
).filter( | ||
actors__id__in=actors_ids | ||
).distinct() | ||
|
||
if genres_ids: | ||
return Movie.objects.filter(genres__id__in=genres_ids).distinct() | ||
|
||
if actors_ids: | ||
return Movie.objects.filter(actors__id__in=actors_ids).distinct() | ||
|
||
return Movie.objects.all() | ||
|
||
|
||
def get_movie_by_id(movie_id: int) -> Movie: | ||
return Movie.objects.get(id=movie_id) | ||
Comment on lines
+28
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Comment on lines
+28
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
||
|
||
def create_movie( | ||
movie_title: str, | ||
movie_description: str, | ||
genres_ids: Optional[list[int]] = None, | ||
actors_ids: Optional[list[int]] = None | ||
) -> None: | ||
movie = Movie.objects.create( | ||
title=movie_title, | ||
description=movie_description | ||
) | ||
if genres_ids: | ||
movie.genres.set(genres_ids) | ||
|
||
if actors_ids: | ||
movie.actors.set(actors_ids) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import datetime | ||
from typing import Optional | ||
|
||
from django.db.models import QuerySet | ||
|
||
from db.models import MovieSession | ||
|
||
|
||
def create_movie_session( | ||
movie_show_time: datetime.datetime, | ||
movie_id: int, | ||
cinema_hall_id: int | ||
) -> None: | ||
MovieSession.objects.create( | ||
show_time=movie_show_time, | ||
cinema_hall_id=cinema_hall_id, | ||
movie_id=movie_id | ||
) | ||
|
||
|
||
def get_movies_sessions(session_date: Optional[str] = None) -> QuerySet: | ||
if session_date: | ||
session_date = datetime.datetime.strptime(session_date, "%Y-%m-%d") | ||
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) | ||
Comment on lines
+28
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Comment on lines
+28
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
||
|
||
def update_movie_session( | ||
session_id: int, | ||
show_time: Optional[datetime.datetime] = None, | ||
movie_id: Optional[int] = None, | ||
cinema_hall_id: Optional[int] = None | ||
) -> None: | ||
ms = get_movie_session_by_id(session_id) | ||
|
||
if show_time: | ||
ms.show_time = show_time | ||
|
||
if movie_id: | ||
ms.movie_id = movie_id | ||
|
||
if cinema_hall_id: | ||
ms.cinema_hall_id = cinema_hall_id | ||
|
||
ms.save() | ||
|
||
|
||
def delete_movie_session_by_id(session_id: int) -> None: | ||
get_movie_session_by_id(session_id).delete() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
cinema_hall
field in theMovieSession
model is missing arelated_name
. Consider addingrelated_name='sessions'
to provide a descriptive reverse relation name.