feat [api]: GET all and POST film

This commit is contained in:
Alice 2022-04-03 01:20:53 +02:00
parent e12a067959
commit 11c336e887
8 changed files with 114 additions and 5 deletions

View file

@ -1,3 +1,4 @@
from django.contrib import admin
from .models import Film
# Register your models here.
admin.site.register(Film)

View file

@ -0,0 +1,35 @@
# Generated by Django 3.2.12 on 2022-03-03 18:09
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Film',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('projection_date', models.DateTimeField()),
('title', models.CharField(max_length=60)),
('actors', models.JSONField(default='[]')),
('director', models.CharField(max_length=60)),
('duration', models.DurationField()),
('synopsis', models.TextField()),
('origin_country', models.CharField(max_length=60)),
('release_year', models.SmallIntegerField()),
('trailer_link', models.URLField()),
('is_in_color', models.BooleanField()),
('movie_format', models.CharField(choices=[('35mm', 'Analog 35'), ('DVD', 'Dvd'), ('Blu ray', 'Blu Ray')], max_length=20)),
('language_subtitles', models.CharField(choices=[('VOF', 'French'), ('VOSTFR', 'Foreign')], max_length=20)),
('poster_link', models.URLField()),
('banner_link', models.URLField()),
('is_confirmed', models.BooleanField(default=False)),
],
),
]

View file

@ -1,3 +1,37 @@
from django.db import models
# Create your models here.
"""
TODO implement validators
- nullable or sure
"""
class Film(models.Model):
class MovieFormat(models.TextChoices):
ANALOG_35 = '35mm'
DVD = 'DVD'
BLU_RAY = 'Blu ray'
class LanguageSubtitles(models.TextChoices):
FRENCH = 'VOF'
FOREIGN = 'VOSTFR'
projection_date = models.DateTimeField()
title = models.CharField(max_length=60)
actors = models.JSONField(default="[]")
director = models.CharField(max_length=60)
duration = models.DurationField()
synopsis = models.TextField()
origin_country = models.CharField(max_length=60)
release_year = models.SmallIntegerField()
trailer_link = models.URLField()
is_in_color = models.BooleanField()
movie_format = models.CharField(max_length=20, choices=MovieFormat.choices)
language_subtitles = models.CharField(max_length=20, choices=LanguageSubtitles.choices)
poster_link = models.URLField()
banner_link = models.URLField()
is_confirmed = models.BooleanField(default=False)
def __str__(self):
return f'{self.title} de {self.director} ({projection_date.strftime("%d/%m/%Y")})'

View file

@ -0,0 +1,7 @@
from rest_framework import serializers
from .models import Film
class FilmSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Film
fields = '__all__' # TODO also send id

13
server/myapi/urls.py Normal file
View file

@ -0,0 +1,13 @@
from django.urls import include, path
from rest_framework import routers
from . import views
router = routers.DefaultRouter()
router.register(r'films', views.FilmViewSet)
# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
path('', include(router.urls)),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]

View file

@ -1,3 +1,11 @@
from django.shortcuts import render
# from django.shortcuts import render
from rest_framework import viewsets
from .serializers import FilmSerializer
from .models import Film
class FilmViewSet(viewsets.ModelViewSet):
queryset = Film.objects.all().order_by('projection_date')
serializer_class = FilmSerializer
# Create your views here.

View file

@ -37,7 +37,8 @@ INSTALLED_APPS = [
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapi.apps.MyapiConfig'
'myapi.apps.MyapiConfig',
'rest_framework'
]
MIDDLEWARE = [
@ -124,3 +125,12 @@ STATIC_URL = '/static/'
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
REST_FRAMEWORK = {
'DEFAULT_RENDERER_CLASSES': [
'rest_framework.renderers.JSONRenderer',
],
'DEFAULT_PARSER_CLASSES': [
'rest_framework.parsers.JSONParser',
]
}

View file

@ -14,8 +14,9 @@ Including another URLconf
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', include('myapi.urls'))
]