Skip to content

Commit

Permalink
solution
Browse files Browse the repository at this point in the history
  • Loading branch information
dxrrkwm committed Dec 11, 2024
1 parent def55c2 commit c368dab
Show file tree
Hide file tree
Showing 18 changed files with 130 additions and 88 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
venv/
.pytest_cache/
**__pycache__/
db.sqlite3
11 changes: 2 additions & 9 deletions cinema/admin.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
from django.contrib import admin

from .models import (
CinemaHall,
Genre,
Actor,
Movie,
MovieSession,
Order,
Ticket,
)
from .models import (Actor, CinemaHall, Genre, Movie, MovieSession, Order,
Ticket)

admin.site.register(CinemaHall)
admin.site.register(Genre)
Expand Down
2 changes: 1 addition & 1 deletion cinema/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Generated by Django 4.0.4 on 2022-05-09 18:11

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


class Migration(migrations.Migration):
Expand Down
8 changes: 5 additions & 3 deletions cinema/migrations/0002_initial.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Generated by Django 4.0.4 on 2022-05-09 18:11

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


class Migration(migrations.Migration):
Expand All @@ -19,14 +19,16 @@ class Migration(migrations.Migration):
model_name="order",
name="user",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL
on_delete=django.db.models.deletion.CASCADE,
to=settings.AUTH_USER_MODEL,
),
),
migrations.AddField(
model_name="moviesession",
name="cinema_hall",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="cinema.cinemahall"
on_delete=django.db.models.deletion.CASCADE,
to="cinema.cinemahall",
),
),
migrations.AddField(
Expand Down
6 changes: 3 additions & 3 deletions cinema/migrations/0003_movie_duration.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
class Migration(migrations.Migration):

dependencies = [
('cinema', '0002_initial'),
("cinema", "0002_initial"),
]

operations = [
migrations.AddField(
model_name='movie',
name='duration',
model_name="movie",
name="duration",
field=models.IntegerField(default=123),
preserve_default=False,
),
Expand Down
6 changes: 3 additions & 3 deletions cinema/migrations/0004_alter_genre_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
class Migration(migrations.Migration):

dependencies = [
('cinema', '0003_movie_duration'),
("cinema", "0003_movie_duration"),
]

operations = [
migrations.AlterField(
model_name='genre',
name='name',
model_name="genre",
name="name",
field=models.CharField(max_length=255, unique=True),
),
]
7 changes: 4 additions & 3 deletions cinema/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.conf import settings
from django.core.exceptions import ValidationError
from django.db import models
from django.conf import settings


class CinemaHall(models.Model):
Expand All @@ -27,8 +27,9 @@ class Actor(models.Model):
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)

def get_full_name(self):
return self.first_name + " " + self.last_name
@property
def full_name(self):
return str(self)

def __str__(self):
return self.first_name + " " + self.last_name
Expand Down
107 changes: 66 additions & 41 deletions cinema/serializers.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,66 @@
from rest_framework import serializers

from cinema.models import Genre, Actor, CinemaHall, Movie, MovieSession


class GenreSerializer(serializers.ModelSerializer):
class Meta:
model = Genre
fields = ["id", "name"]


class ActorSerializer(serializers.ModelSerializer):
full_name = serializers.CharField(source="get_full_name", read_only=True)

class Meta:
model = Actor
fields = ["id", "first_name", "last_name", "full_name"]


class CinemaHallSerializer(serializers.ModelSerializer):
capacity = serializers.IntegerField(read_only=True)

class Meta:
model = CinemaHall
fields = ["id", "name", "rows", "seats_in_row", "capacity"]


class MovieSerializer(serializers.ModelSerializer):
class Meta:
model = Movie
fields = ["id", "title", "description", "duration", "genres", "actors"]


class MovieSessionSerializer(serializers.ModelSerializer):
movie_title = serializers.CharField(source="movie.title", read_only=True)
cinema_hall_name = serializers.CharField(source="cinema_hall.name", read_only=True)
cinema_hall_capacity = serializers.IntegerField(source="cinema_hall.capacity", read_only=True)

class Meta:
model = MovieSession
fields = ["id", "show_time", "movie", "cinema_hall", "movie_title", "cinema_hall_name", "cinema_hall_capacity"]
from rest_framework import serializers

from cinema.models import Actor, CinemaHall, Genre, Movie, MovieSession


class GenreSerializer(serializers.ModelSerializer):
class Meta:
model = Genre
fields = ["id", "name"]


class ActorSerializer(serializers.ModelSerializer):
class Meta:
model = Actor
fields = ["id", "first_name", "last_name", "full_name"]


class CinemaHallSerializer(serializers.ModelSerializer):
class Meta:
model = CinemaHall
fields = ["id", "name", "rows", "seats_in_row", "capacity"]


class MovieSerializer(serializers.ModelSerializer):
class Meta:
model = Movie
fields = ["id", "title", "description", "duration", "genres", "actors"]


class MovieListSerializer(MovieSerializer):
genres = serializers.SlugRelatedField(
slug_field="name", queryset=Genre.objects.all(), many=True
)
actors = serializers.SlugRelatedField(
slug_field="full_name", queryset=Actor.objects.all(), many=True
)


class MovieDetailSerializer(MovieSerializer):
genres = GenreSerializer(many=True)
actors = ActorSerializer(many=True)


class MovieSessionSerializer(serializers.ModelSerializer):
class Meta:
model = MovieSession
fields = ["id", "show_time", "movie", "cinema_hall"]


class MovieSessionListSerializer(serializers.ModelSerializer):
movie_title = serializers.CharField(source="movie.title", read_only=True)
cinema_hall_name = serializers.CharField(
source="cinema_hall.name", read_only=True
)
cinema_hall_capacity = serializers.IntegerField(
source="cinema_hall.capacity", read_only=True
)

class Meta:
model = MovieSession
exclude = ["cinema_hall", "movie"]


class MovieSessionDetailSerializer(MovieSessionSerializer):
movie = MovieListSerializer()
cinema_hall = CinemaHallSerializer(read_only=True)
1 change: 0 additions & 1 deletion cinema/tests/test_actor_api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from django.test import TestCase

from rest_framework import status
from rest_framework.test import APIClient

Expand Down
3 changes: 1 addition & 2 deletions cinema/tests/test_cinema_hall_api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from django.test import TestCase

from rest_framework.test import APIClient
from rest_framework import status
from rest_framework.test import APIClient

from cinema.models import CinemaHall

Expand Down
1 change: 0 additions & 1 deletion cinema/tests/test_genre_api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from django.test import TestCase

from rest_framework import status
from rest_framework.test import APIClient

Expand Down
5 changes: 2 additions & 3 deletions cinema/tests/test_movie_api.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from django.test import TestCase

from rest_framework.test import APIClient
from rest_framework import status
from rest_framework.test import APIClient

from cinema.models import Movie, Genre, Actor
from cinema.models import Actor, Genre, Movie


class MovieApiTests(TestCase):
Expand Down
5 changes: 2 additions & 3 deletions cinema/tests/test_movie_session_api.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import datetime

from django.test import TestCase

from rest_framework.test import APIClient
from rest_framework import status
from rest_framework.test import APIClient

from cinema.models import Movie, Genre, Actor, MovieSession, CinemaHall
from cinema.models import Actor, CinemaHall, Genre, Movie, MovieSession


class MovieSessionApiTests(TestCase):
Expand Down
8 changes: 5 additions & 3 deletions cinema/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from django.urls import path, include
from django.urls import include, path
from rest_framework.routers import DefaultRouter
from cinema.views import GenreViewSet, ActorViewSet, CinemaHallViewSet, MovieViewSet, MovieSessionViewSet

from cinema.views import (ActorViewSet, CinemaHallViewSet, GenreViewSet,
MovieSessionViewSet, MovieViewSet)

router = DefaultRouter()
router.register("genres", GenreViewSet)
Expand All @@ -11,4 +13,4 @@

urlpatterns = [
path("", include(router.urls)),
]
]
30 changes: 23 additions & 7 deletions cinema/views.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from rest_framework import viewsets

from cinema.models import Genre, Actor, Movie, CinemaHall, MovieSession
from cinema.serializers import GenreSerializer, ActorSerializer, CinemaHallSerializer, MovieSerializer, \
MovieSessionSerializer
from cinema.models import Actor, CinemaHall, Genre, Movie, MovieSession
from cinema.serializers import (ActorSerializer, CinemaHallSerializer,
GenreSerializer, MovieDetailSerializer,
MovieListSerializer, MovieSerializer,
MovieSessionDetailSerializer,
MovieSessionListSerializer,
MovieSessionSerializer)


class GenreViewSet(viewsets.ModelViewSet):
Expand All @@ -21,10 +25,22 @@ class CinemaHallViewSet(viewsets.ModelViewSet):


class MovieViewSet(viewsets.ModelViewSet):
queryset = Movie.objects.all()
serializer_class = MovieSerializer
queryset = Movie.objects.all().prefetch_related("actors", "genres")

def get_serializer_class(self):
if self.action == "list":
return MovieListSerializer
elif self.action == "retrieve":
return MovieDetailSerializer
return MovieSerializer


class MovieSessionViewSet(viewsets.ModelViewSet):
queryset = MovieSession.objects.all()
serializer_class = MovieSessionSerializer
queryset = MovieSession.objects.select_related("movie", "cinema_hall")

def get_serializer_class(self):
if self.action == "list":
return MovieSessionListSerializer
elif self.action == "retrieve":
return MovieSessionDetailSerializer
return MovieSessionSerializer
2 changes: 1 addition & 1 deletion cinema_service/urls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.contrib import admin
from django.urls import path, include
from django.urls import include, path

urlpatterns = [
path("admin/", admin.site.urls),
Expand Down
1 change: 1 addition & 0 deletions user/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin

from .models import User

admin.site.register(User, UserAdmin)
14 changes: 10 additions & 4 deletions user/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import django.contrib.auth.models
import django.contrib.auth.validators
from django.db import migrations, models
import django.utils.timezone
from django.db import migrations, models


class Migration(migrations.Migration):
Expand All @@ -27,7 +27,10 @@ class Migration(migrations.Migration):
verbose_name="ID",
),
),
("password", models.CharField(max_length=128, verbose_name="password")),
(
"password",
models.CharField(max_length=128, verbose_name="password"),
),
(
"last_login",
models.DateTimeField(
Expand Down Expand Up @@ -72,7 +75,9 @@ class Migration(migrations.Migration):
(
"email",
models.EmailField(
blank=True, max_length=254, verbose_name="email address"
blank=True,
max_length=254,
verbose_name="email address",
),
),
(
Expand All @@ -94,7 +99,8 @@ class Migration(migrations.Migration):
(
"date_joined",
models.DateTimeField(
default=django.utils.timezone.now, verbose_name="date joined"
default=django.utils.timezone.now,
verbose_name="date joined",
),
),
(
Expand Down

0 comments on commit c368dab

Please sign in to comment.