Commit f178c59f authored by Matheus Miranda's avatar Matheus Miranda
Browse files

Simplify video models scheme

parent 8ca3e461
# Generated by Django 2.2.24 on 2021-07-28 12:35
import courses.videos.models
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('courses', '0029_auto_20210719_2107'),
]
operations = [
migrations.AddField(
model_name='video',
name='file',
field=models.FileField(default=None, null=True, upload_to=courses.videos.models.get_upload_path),
),
]
# Generated by Django 2.2.24 on 2021-07-26 19:22
import courses.videos.models
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('courses', '0029_auto_20210719_2107'),
]
operations = [
migrations.CreateModel(
name='VideoFile',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=255, null=True)),
('file', models.FileField(default=None, null=True, upload_to=courses.videos.models.get_upload_path)),
('video', models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='file', to='courses.Video', verbose_name='file')),
],
),
]
......@@ -15,7 +15,7 @@ from .views import (
CoursePictureUploadViewSet,
)
from .videos.views import VideoFileViewSet
from .videos.views import VideoViewSet
from .course_material.views import CourseMaterialViewSet, CourseMaterialFileViewSet
from .import_export.views import ExportCourseView, ImportCourseView
from courses.workspaces.views import (
......@@ -55,7 +55,7 @@ router.register(r'my-courses', MyCoursesViewSet, base_name='my-courses')
router.register(r'course_material', CourseMaterialViewSet, base_name='course_material')
router.register(r'course_material_file', CourseMaterialFileViewSet, base_name='course_material_file')
router.register(r'course-by-slug', CourseBySlugViewSet, base_name='course_by_slug'),
router.register(r'videos', VideoFileViewSet, base_name='course_by_slug'),
router.register(r'videos', VideoViewSet, base_name='course_by_slug'),
# Lessons
router.register(r'course-lessons/(?P<course_id>[1-9][0-9]*)', LessonViewSet, base_name='lessons')
......
......@@ -8,33 +8,18 @@ def get_upload_path(instance, filename):
filename = slugify(filename.split("/", 2)[-1])
return 'course_videos/{0}.{1}'.format(filename, fileextension)
class VideoFile(models.Model):
class Video(models.Model):
name = models.CharField(
max_length=255,
null=True,
)
youtube_id = models.CharField(
max_length=100,
)
file = models.FileField(
upload_to=get_upload_path,
default=None,
null=True,
)
video = models.OneToOneField(
'Video',
models.CASCADE,
related_name='file',
verbose_name=_('file'),
null=True,
blank=True,
)
class Video(models.Model):
name = models.CharField(
max_length=255,
)
youtube_id = models.CharField(
max_length=100,
)
class Meta:
verbose_name = _('Video')
......
from rest_framework import serializers
from modeltranslation.utils import fallbacks
from .models import Video, VideoFile
class VideoFileSerializer(serializers.ModelSerializer):
class Meta:
model = VideoFile
fields = ('id', 'name', 'video', 'file')
def get_file(self, obj):
with fallbacks(False):
files = VideoFileSerializer(required=False, allow_null=True, read_only=True, **{'context': self.context}).data
files = [f for f in files if f['file'] != None]
return files
from .models import Video
class VideoSerializer(serializers.ModelSerializer):
......@@ -22,8 +9,8 @@ class VideoSerializer(serializers.ModelSerializer):
model = Video
fields = ('id', 'name', 'youtube_id', 'file')
def get_file(self, obj):
with fallbacks(False):
files = VideoFileSerializer(required=False, allow_null=True, read_only=True, **{'context': self.context}).data
files = [f for f in files if f['file'] != None]
return files
#def get_file(self, obj):
# with fallbacks(False):
# files = VideoFileSerializer(required=False, allow_null=True, read_only=True, **{'context': self.context}).data
# files = [f for f in files if f['file'] != None]
# return files
......@@ -2,17 +2,17 @@ from django.shortcuts import render
from rest_framework import viewsets
from courses.videos.models import VideoFile
from courses.videos.serializers import VideoFileSerializer
from courses.videos.models import Video
from courses.videos.serializers import VideoSerializer
from courses.permissions import (
IsProfessorCoordinatorOrAdminPermissionOrReadOnly,
)
class VideoFileViewSet(viewsets.ModelViewSet):
class VideoViewSet(viewsets.ModelViewSet):
model = VideoFile
queryset = VideoFile.objects.all()
serializer_class = VideoFileSerializer
model = Video
queryset = Video.objects.all()
serializer_class = VideoSerializer
permission_classes = (IsProfessorCoordinatorOrAdminPermissionOrReadOnly,)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment