You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
461 lines
17 KiB
461 lines
17 KiB
from rest_framework import serializers
|
|
|
|
from apps.course.models import Category, Course, Material, Lesson, Like
|
|
|
|
from .content import (
|
|
ImageObjectSerializer, ContentSerializer, ContentCreateSerializer,
|
|
GallerySerializer, GalleryImageSerializer,
|
|
)
|
|
|
|
from apps.content.models import (
|
|
Content, Image, Text, ImageText, Video,
|
|
Gallery, GalleryImage, ImageObject,
|
|
)
|
|
|
|
|
|
class MaterialCreateSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
model = Material
|
|
fields = (
|
|
'id',
|
|
'title',
|
|
'cover',
|
|
'short_description',
|
|
'created_at',
|
|
'update_at',
|
|
)
|
|
|
|
read_only_fields = (
|
|
'id',
|
|
'created_at',
|
|
'update_at',
|
|
)
|
|
|
|
|
|
class MaterialSerializer(MaterialCreateSerializer):
|
|
cover = ImageObjectSerializer(allow_null=True)
|
|
|
|
|
|
class LikeSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
model = Like
|
|
fields = (
|
|
'id',
|
|
'user',
|
|
'created_at',
|
|
'update_at',
|
|
)
|
|
|
|
read_only_fields = (
|
|
'id',
|
|
'created_at',
|
|
'update_at',
|
|
)
|
|
|
|
|
|
class CategorySerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
model = Category
|
|
fields = (
|
|
'id',
|
|
'title',
|
|
)
|
|
|
|
read_only_fields = (
|
|
'id',
|
|
)
|
|
|
|
|
|
class CourseCreateSerializer(serializers.ModelSerializer):
|
|
slug = serializers.SlugField(allow_unicode=True, required=False)
|
|
content = serializers.ListSerializer(
|
|
child=ContentCreateSerializer(),
|
|
required=False,
|
|
)
|
|
materials = MaterialSerializer(many=True, required=False)
|
|
|
|
class Meta:
|
|
model = Course
|
|
fields = (
|
|
'id',
|
|
'slug',
|
|
'author',
|
|
'title',
|
|
'short_description',
|
|
'from_author',
|
|
'cover',
|
|
'price',
|
|
'is_infinite',
|
|
'deferred_start_at',
|
|
'category',
|
|
'duration',
|
|
'is_featured',
|
|
'url',
|
|
'status',
|
|
'likes',
|
|
'materials',
|
|
'created_at',
|
|
'update_at',
|
|
'content',
|
|
'gallery',
|
|
)
|
|
|
|
read_only_fields = (
|
|
'id',
|
|
'url',
|
|
'created_at',
|
|
'update_at',
|
|
)
|
|
|
|
def create(self, validated_data):
|
|
materials = validated_data.pop('materials', [])
|
|
content = validated_data.pop('content', [])
|
|
|
|
course = super().create(validated_data)
|
|
|
|
for c in content:
|
|
if c['type'] == 'text':
|
|
if 'id' in c['data'] and c['data']['id']:
|
|
t = Text.objects.get(id=c['data']['id'])
|
|
t.position = c['data']['position']
|
|
t.title = c['data']['title']
|
|
t.course = course
|
|
t.txt = c['data']['txt']
|
|
t.save()
|
|
else:
|
|
t = Text.objects.create(
|
|
position=c['data']['position'],
|
|
title=c['data']['title'],
|
|
course=course,
|
|
txt=c['data']['txt'],
|
|
)
|
|
elif c['type'] == 'image':
|
|
if 'id' in c['data'] and c['data']['id']:
|
|
i = Image.objects.get(id=c['data']['id'])
|
|
i.position = c['data']['position']
|
|
i.title = c['data']['title']
|
|
i.course = course
|
|
i.img = ImageObject.objects.get(id=c['data']['img'])
|
|
i.save()
|
|
else:
|
|
i = Image.objects.create(
|
|
position=c['data']['position'],
|
|
title=c['data']['title'],
|
|
course=course,
|
|
img=ImageObject.objects.get(id=c['data']['img']),
|
|
)
|
|
elif c['type'] == 'image-text':
|
|
if 'id' in c['data'] and c['data']['id']:
|
|
it = ImageText.objects.get(id=c['data']['id'])
|
|
it.position = c['data']['position']
|
|
it.title = c['data']['title']
|
|
it.course = course
|
|
it.img = ImageObject.objects.get(id=c['data']['img'])
|
|
it.txt = c['data']['txt']
|
|
it.save()
|
|
else:
|
|
it = ImageText.objects.create(
|
|
position=c['data']['position'],
|
|
title=c['data']['title'],
|
|
course=course,
|
|
img=ImageObject.objects.get(id=c['data']['img']),
|
|
txt=c['data']['txt'],
|
|
)
|
|
elif c['type'] == 'video':
|
|
if 'id' in c['data'] and c['data']['id']:
|
|
v = Video.objects.get(id=c['data']['id'])
|
|
v.position = c['data']['position']
|
|
v.title = c['data']['title']
|
|
v.course = course
|
|
v.url = c['data']['url']
|
|
v.save()
|
|
else:
|
|
v = Video.objects.create(
|
|
position=c['data']['position'],
|
|
title=c['data']['title'],
|
|
course=course,
|
|
url=c['data']['url'],
|
|
)
|
|
for material in materials:
|
|
if 'id' in material and material['id']:
|
|
m = Material.objects.get(id=material['id'])
|
|
m.title = material['title']
|
|
m.cover = ImageObject.objects.get(id=material['cover'])
|
|
m.short_description = material['short_description']
|
|
m.save()
|
|
else:
|
|
m = Material.objects.create(
|
|
title=material['title'],
|
|
cover=ImageObject.objects.get(id=material['cover']),
|
|
short_description=material['short_description'],
|
|
)
|
|
course.materials.add(m)
|
|
return course
|
|
|
|
def update(self, instance, validated_data):
|
|
materials = validated_data.pop('materials', [])
|
|
content = validated_data.pop('content', [])
|
|
|
|
course = super().update(instance, validated_data)
|
|
|
|
for c in content:
|
|
if c['type'] == 'text':
|
|
if 'id' in c['data'] and c['data']['id']:
|
|
t = Text.objects.get(id=c['data']['id'])
|
|
t.position = c['data']['position']
|
|
t.title = c['data']['title']
|
|
t.course = course
|
|
t.txt = c['data']['txt']
|
|
t.save()
|
|
else:
|
|
t = Text.objects.create(
|
|
position=c['data']['position'],
|
|
title=c['data']['title'],
|
|
course=course,
|
|
txt=c['data']['txt'],
|
|
)
|
|
elif c['type'] == 'image':
|
|
if 'id' in c['data'] and c['data']['id']:
|
|
i = Image.objects.get(id=c['data']['id'])
|
|
i.position = c['data']['position']
|
|
i.title = c['data']['title']
|
|
i.course = course
|
|
i.img = ImageObject.objects.get(id=c['data']['img'])
|
|
i.save()
|
|
else:
|
|
i = Image.objects.create(
|
|
position=c['data']['position'],
|
|
title=c['data']['title'],
|
|
course=course,
|
|
img=ImageObject.objects.get(id=c['data']['img']),
|
|
)
|
|
elif c['type'] == 'image-text':
|
|
if 'id' in c['data'] and c['data']['id']:
|
|
it = ImageText.objects.get(id=c['data']['id'])
|
|
it.position = c['data']['position']
|
|
it.title = c['data']['title']
|
|
it.course = course
|
|
it.img = ImageObject.objects.get(id=c['data']['img'])
|
|
it.txt = c['data']['txt']
|
|
it.save()
|
|
else:
|
|
it = ImageText.objects.create(
|
|
position=c['data']['position'],
|
|
title=c['data']['title'],
|
|
course=course,
|
|
img=ImageObject.objects.get(id=c['data']['img']),
|
|
txt=c['data']['txt'],
|
|
)
|
|
elif c['type'] == 'video':
|
|
if 'id' in c['data'] and c['data']['id']:
|
|
v = Video.objects.get(id=c['data']['id'])
|
|
v.position = c['data']['position']
|
|
v.title = c['data']['title']
|
|
v.course = course
|
|
v.url = c['data']['url']
|
|
v.save()
|
|
else:
|
|
v = Video.objects.create(
|
|
position=c['data']['position'],
|
|
title=c['data']['title'],
|
|
course=course,
|
|
url=c['data']['url'],
|
|
)
|
|
for material in materials:
|
|
if 'id' in material and material['id']:
|
|
m = Material.objects.get(id=material['id'])
|
|
m.title = material['title']
|
|
m.cover = ImageObject.objects.get(id=material['cover'])
|
|
m.short_description = material['short_description']
|
|
m.save()
|
|
else:
|
|
m = Material.objects.create(
|
|
title=material['title'],
|
|
cover=ImageObject.objects.get(id=material['cover']),
|
|
short_description=material['short_description'],
|
|
)
|
|
course.materials.add(m)
|
|
return course
|
|
|
|
|
|
class CourseSerializer(CourseCreateSerializer):
|
|
category = CategorySerializer()
|
|
materials = MaterialSerializer(many=True)
|
|
cover = ImageObjectSerializer()
|
|
gallery = GallerySerializer()
|
|
content = ContentSerializer(many=True)
|
|
|
|
|
|
class LessonCreateSerializer(serializers.ModelSerializer):
|
|
content = serializers.ListSerializer(
|
|
child=ContentCreateSerializer(),
|
|
required=False,
|
|
)
|
|
|
|
class Meta:
|
|
model = Lesson
|
|
fields = (
|
|
'id',
|
|
'title',
|
|
'short_description',
|
|
'course',
|
|
'cover',
|
|
'content',
|
|
'created_at',
|
|
'update_at',
|
|
)
|
|
|
|
read_only_fields = (
|
|
'id',
|
|
'created_at',
|
|
'update_at',
|
|
)
|
|
|
|
def create(self, validated_data):
|
|
content = validated_data.pop('content', [])
|
|
|
|
lesson = super().create(validated_data)
|
|
|
|
for c in content:
|
|
if c['type'] == 'text':
|
|
if 'id' in c['data'] and c['data']['id']:
|
|
t = Text.objects.get(id=c['data']['id'])
|
|
t.position = c['data']['position']
|
|
t.title = c['data']['title']
|
|
t.lesson = lesson
|
|
t.txt = c['data']['txt']
|
|
t.save()
|
|
else:
|
|
t = Text.objects.create(
|
|
position=c['data']['position'],
|
|
title=c['data']['title'],
|
|
lesson=lesson,
|
|
txt=c['data']['txt'],
|
|
)
|
|
elif c['type'] == 'image':
|
|
if 'id' in c['data'] and c['data']['id']:
|
|
i = Image.objects.get(id=c['data']['id'])
|
|
i.position = c['data']['position']
|
|
i.title = c['data']['title']
|
|
i.lesson = lesson
|
|
i.img = ImageObject.objects.get(id=c['data']['img'])
|
|
i.save()
|
|
else:
|
|
i = Image.objects.create(
|
|
position=c['data']['position'],
|
|
title=c['data']['title'],
|
|
lesson=lesson,
|
|
img=ImageObject.objects.get(id=c['data']['img']),
|
|
)
|
|
elif c['type'] == 'image-text':
|
|
if 'id' in c['data'] and c['data']['id']:
|
|
it = ImageText.objects.get(id=c['data']['id'])
|
|
it.position = c['data']['position']
|
|
it.title = c['data']['title']
|
|
it.lesson = lesson
|
|
it.img = ImageObject.objects.get(id=c['data']['img'])
|
|
it.txt = c['data']['txt']
|
|
it.save()
|
|
else:
|
|
it = ImageText.objects.create(
|
|
position=c['data']['position'],
|
|
title=c['data']['title'],
|
|
lesson=lesson,
|
|
img=ImageObject.objects.get(id=c['data']['img']),
|
|
txt=c['data']['txt'],
|
|
)
|
|
elif c['type'] == 'video':
|
|
if 'id' in c['data'] and c['data']['id']:
|
|
v = Video.objects.get(id=c['data']['id'])
|
|
v.position = c['data']['position']
|
|
v.title = c['data']['title']
|
|
v.lesson = lesson
|
|
v.url = c['data']['url']
|
|
v.save()
|
|
else:
|
|
v = Video.objects.create(
|
|
position=c['data']['position'],
|
|
title=c['data']['title'],
|
|
lesson=lesson,
|
|
url=c['data']['url'],
|
|
)
|
|
return lesson
|
|
|
|
def update(self, instance, validated_data):
|
|
content = validated_data.pop('content', [])
|
|
|
|
lesson = super().update(instance, validated_data)
|
|
|
|
for c in content:
|
|
if c['type'] == 'text':
|
|
if 'id' in c['data'] and c['data']['id']:
|
|
t = Text.objects.get(id=c['data']['id'])
|
|
t.position = c['data']['position']
|
|
t.title = c['data']['title']
|
|
t.lesson = lesson
|
|
t.txt = c['data']['txt']
|
|
t.save()
|
|
else:
|
|
t = Text.objects.create(
|
|
position=c['data']['position'],
|
|
title=c['data']['title'],
|
|
lesson=lesson,
|
|
txt=c['data']['txt'],
|
|
)
|
|
elif c['type'] == 'image':
|
|
if 'id' in c['data'] and c['data']['id']:
|
|
i = Image.objects.get(id=c['data']['id'])
|
|
i.position = c['data']['position']
|
|
i.title = c['data']['title']
|
|
i.lesson = lesson
|
|
i.img = ImageObject.objects.get(id=c['data']['img'])
|
|
i.save()
|
|
else:
|
|
i = Image.objects.create(
|
|
position=c['data']['position'],
|
|
title=c['data']['title'],
|
|
lesson=lesson,
|
|
img=ImageObject.objects.get(id=c['data']['img']),
|
|
)
|
|
elif c['type'] == 'image-text':
|
|
if 'id' in c['data'] and c['data']['id']:
|
|
it = ImageText.objects.get(id=c['data']['id'])
|
|
it.position = c['data']['position']
|
|
it.title = c['data']['title']
|
|
it.lesson = lesson
|
|
it.img = ImageObject.objects.get(id=c['data']['img'])
|
|
it.txt = c['data']['txt']
|
|
it.save()
|
|
else:
|
|
it = ImageText.objects.create(
|
|
position=c['data']['position'],
|
|
title=c['data']['title'],
|
|
lesson=lesson,
|
|
img=ImageObject.objects.get(id=c['data']['img']),
|
|
txt=c['data']['txt'],
|
|
)
|
|
elif c['type'] == 'video':
|
|
if 'id' in c['data'] and c['data']['id']:
|
|
v = Video.objects.get(id=c['data']['id'])
|
|
v.position = c['data']['position']
|
|
v.title = c['data']['title']
|
|
v.lesson = lesson
|
|
v.url = c['data']['url']
|
|
v.save()
|
|
else:
|
|
v = Video.objects.create(
|
|
position=c['data']['position'],
|
|
title=c['data']['title'],
|
|
lesson=lesson,
|
|
url=c['data']['url'],
|
|
)
|
|
return lesson
|
|
|
|
|
|
class LessonSerializer(LessonCreateSerializer):
|
|
course = CourseSerializer()
|
|
cover = ImageObjectSerializer()
|
|
content = ContentSerializer()
|
|
|