|
|
|
|
@ -12,6 +12,8 @@ from apps.content.models import ( |
|
|
|
|
Gallery, GalleryImage, ImageObject, |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
from .mixins import DispatchContentMixin, DispatchGalleryMixin, DispatchMaterialMixin |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MaterialCreateSerializer(serializers.ModelSerializer): |
|
|
|
|
|
|
|
|
|
@ -69,7 +71,11 @@ class CategorySerializer(serializers.ModelSerializer): |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CourseCreateSerializer(serializers.ModelSerializer): |
|
|
|
|
class CourseCreateSerializer(DispatchContentMixin, |
|
|
|
|
DispatchGalleryMixin, |
|
|
|
|
DispatchMaterialMixin, |
|
|
|
|
serializers.ModelSerializer |
|
|
|
|
): |
|
|
|
|
slug = serializers.SlugField(allow_unicode=True, required=False) |
|
|
|
|
content = serializers.ListSerializer( |
|
|
|
|
child=ContentCreateSerializer(), |
|
|
|
|
@ -100,6 +106,7 @@ class CourseCreateSerializer(serializers.ModelSerializer): |
|
|
|
|
'materials', |
|
|
|
|
'created_at', |
|
|
|
|
'update_at', |
|
|
|
|
'deactivated_at', |
|
|
|
|
'content', |
|
|
|
|
'gallery', |
|
|
|
|
) |
|
|
|
|
@ -109,146 +116,16 @@ class CourseCreateSerializer(serializers.ModelSerializer): |
|
|
|
|
'url', |
|
|
|
|
'created_at', |
|
|
|
|
'update_at', |
|
|
|
|
'deactivated_at', |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
def dispatch_content(self, course, content, materials): |
|
|
|
|
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']: |
|
|
|
|
image = Image.objects.get(id=c['data']['id']) |
|
|
|
|
image.position = c['data']['position'] |
|
|
|
|
image.title = c['data']['title'] |
|
|
|
|
image.course = course |
|
|
|
|
image.img = ImageObject.objects.get(id=c['data']['img']) |
|
|
|
|
image.save() |
|
|
|
|
else: |
|
|
|
|
image = 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'], |
|
|
|
|
) |
|
|
|
|
elif c['type'] == 'images': |
|
|
|
|
if 'id' in c['data'] and c['data']['id']: |
|
|
|
|
g = Gallery.objects.get(id=c['data']['id']) |
|
|
|
|
g.course = course |
|
|
|
|
g.position = c['data']['position'] |
|
|
|
|
g.title = c['data']['title'] |
|
|
|
|
g.save() |
|
|
|
|
if 'images' in c['data']: |
|
|
|
|
for image in c['data']['images']: |
|
|
|
|
gi = GalleryImage.objects.create( |
|
|
|
|
gallery=g, |
|
|
|
|
img=ImageObject.objects.get(id=image['img']) |
|
|
|
|
) |
|
|
|
|
else: |
|
|
|
|
g = Gallery.objects.create( |
|
|
|
|
course=course, |
|
|
|
|
position=c['data']['position'], |
|
|
|
|
title=c['data']['title'], |
|
|
|
|
) |
|
|
|
|
if 'images' in c['data']: |
|
|
|
|
for image in c['data']['images']: |
|
|
|
|
gi = GalleryImage.objects.create( |
|
|
|
|
gallery=g, |
|
|
|
|
img=ImageObject.objects.get(id=image['img']), |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
|
def dispatch_gallery(self, course, gallery): |
|
|
|
|
if gallery: |
|
|
|
|
if 'id' in gallery and gallery['id']: |
|
|
|
|
g = Gallery.objects.get(id=gallery['id']) |
|
|
|
|
g.title = gallery.get('title', g.title) |
|
|
|
|
g.position = 0 |
|
|
|
|
g.save() |
|
|
|
|
else: |
|
|
|
|
g = Gallery.objects.create( |
|
|
|
|
title=gallery.get('title', ''), |
|
|
|
|
position=0, |
|
|
|
|
) |
|
|
|
|
if 'images' in gallery: |
|
|
|
|
for image in gallery['images']: |
|
|
|
|
if 'id' in image and image['id']: |
|
|
|
|
gi = GalleryImage.objects.get(id=image['id']) |
|
|
|
|
gi.gallery = g |
|
|
|
|
gi.img = image['img'] |
|
|
|
|
gi.save() |
|
|
|
|
else: |
|
|
|
|
gi = GalleryImage.objects.create( |
|
|
|
|
gallery=g, |
|
|
|
|
img=image['img'], |
|
|
|
|
) |
|
|
|
|
course.gallery = g |
|
|
|
|
course.save() |
|
|
|
|
|
|
|
|
|
def create(self, validated_data): |
|
|
|
|
content = validated_data.pop('content', []) |
|
|
|
|
materials = validated_data.pop('materials', []) |
|
|
|
|
gallery = validated_data.pop('gallery', {}) |
|
|
|
|
course = super().create(validated_data) |
|
|
|
|
self.dispatch_content(course, content, materials) |
|
|
|
|
self.dispatch_content(course, content) |
|
|
|
|
self.dispatch_materials(course, materials) |
|
|
|
|
self.dispatch_gallery(course, gallery) |
|
|
|
|
return course |
|
|
|
|
|
|
|
|
|
@ -257,10 +134,14 @@ class CourseCreateSerializer(serializers.ModelSerializer): |
|
|
|
|
materials = validated_data.pop('materials', []) |
|
|
|
|
gallery = validated_data.pop('gallery', {}) |
|
|
|
|
course = super().update(instance, validated_data) |
|
|
|
|
self.dispatch_materials(course, materials) |
|
|
|
|
self.dispatch_content(course, content, materials) |
|
|
|
|
self.dispatch_gallery(course, gallery) |
|
|
|
|
return course |
|
|
|
|
|
|
|
|
|
def to_representation(self, instance): |
|
|
|
|
return CourseSerializer(instance, context=self.context).to_representation(instance) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CourseSerializer(CourseCreateSerializer): |
|
|
|
|
category = CategorySerializer() |
|
|
|
|
@ -287,87 +168,93 @@ class LessonCreateSerializer(serializers.ModelSerializer): |
|
|
|
|
'content', |
|
|
|
|
'created_at', |
|
|
|
|
'update_at', |
|
|
|
|
'deactivated_at', |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
read_only_fields = ( |
|
|
|
|
'id', |
|
|
|
|
'created_at', |
|
|
|
|
'update_at', |
|
|
|
|
'deactivated_at', |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
def dispatch_content(self, lesson, validated_data, content): |
|
|
|
|
def dispatch_content(self, lesson, content): |
|
|
|
|
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'] |
|
|
|
|
if 'type' not in c or not c['type'] or 'data' not in c or not c['data']: |
|
|
|
|
continue |
|
|
|
|
ctype = c['type'] |
|
|
|
|
cdata = c['data'] |
|
|
|
|
if ctype == 'text': |
|
|
|
|
if 'id' in cdata and cdata['id']: |
|
|
|
|
t = Text.objects.get(id=cdata['id']) |
|
|
|
|
t.position = cdata['position'] |
|
|
|
|
t.title = cdata['title'] |
|
|
|
|
t.lesson = lesson |
|
|
|
|
t.txt = c['data']['txt'] |
|
|
|
|
t.txt = cdata['txt'] |
|
|
|
|
t.save() |
|
|
|
|
else: |
|
|
|
|
t = Text.objects.create( |
|
|
|
|
position=c['data']['position'], |
|
|
|
|
title=c['data']['title'], |
|
|
|
|
position=cdata['position'], |
|
|
|
|
title=cdata['title'], |
|
|
|
|
lesson=lesson, |
|
|
|
|
txt=c['data']['txt'], |
|
|
|
|
txt=cdata['txt'], |
|
|
|
|
) |
|
|
|
|
elif c['type'] == 'image': |
|
|
|
|
if 'id' in c['data'] and c['data']['id']: |
|
|
|
|
image = Image.objects.get(id=c['data']['id']) |
|
|
|
|
image.position = c['data']['position'] |
|
|
|
|
image.title = c['data']['title'] |
|
|
|
|
elif ctype == 'image': |
|
|
|
|
if 'id' in cdata and cdata['id']: |
|
|
|
|
image = Image.objects.get(id=cdata['id']) |
|
|
|
|
image.position = cdata['position'] |
|
|
|
|
image.title = cdata['title'] |
|
|
|
|
image.lesson = lesson |
|
|
|
|
image.img = ImageObject.objects.get(id=c['data']['img']) |
|
|
|
|
image.img = ImageObject.objects.get(id=cdata['img']) |
|
|
|
|
image.save() |
|
|
|
|
else: |
|
|
|
|
image = Image.objects.create( |
|
|
|
|
position=c['data']['position'], |
|
|
|
|
title=c['data']['title'], |
|
|
|
|
position=cdata['position'], |
|
|
|
|
title=cdata['title'], |
|
|
|
|
lesson=lesson, |
|
|
|
|
img=ImageObject.objects.get(id=c['data']['img']), |
|
|
|
|
img=ImageObject.objects.get(id=cdata['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'] |
|
|
|
|
elif ctype == 'image-text': |
|
|
|
|
if 'id' in cdata and cdata['id']: |
|
|
|
|
it = ImageText.objects.get(id=cdata['id']) |
|
|
|
|
it.position = cdata['position'] |
|
|
|
|
it.title = cdata['title'] |
|
|
|
|
it.lesson = lesson |
|
|
|
|
it.img = ImageObject.objects.get(id=c['data']['img']) |
|
|
|
|
it.txt = c['data']['txt'] |
|
|
|
|
it.img = ImageObject.objects.get(id=cdata['img']) |
|
|
|
|
it.txt = cdata['txt'] |
|
|
|
|
it.save() |
|
|
|
|
else: |
|
|
|
|
it = ImageText.objects.create( |
|
|
|
|
position=c['data']['position'], |
|
|
|
|
title=c['data']['title'], |
|
|
|
|
position=cdata['position'], |
|
|
|
|
title=cdata['title'], |
|
|
|
|
lesson=lesson, |
|
|
|
|
img=ImageObject.objects.get(id=c['data']['img']), |
|
|
|
|
txt=c['data']['txt'], |
|
|
|
|
img=ImageObject.objects.get(id=cdata['img']), |
|
|
|
|
txt=cdata['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'] |
|
|
|
|
elif ctype == 'video': |
|
|
|
|
if 'id' in cdata and cdata['id']: |
|
|
|
|
v = Video.objects.get(id=cdata['id']) |
|
|
|
|
v.position = cdata['position'] |
|
|
|
|
v.title = cdata['title'] |
|
|
|
|
v.lesson = lesson |
|
|
|
|
v.url = c['data']['url'] |
|
|
|
|
v.url = cdata['url'] |
|
|
|
|
v.save() |
|
|
|
|
else: |
|
|
|
|
v = Video.objects.create( |
|
|
|
|
position=c['data']['position'], |
|
|
|
|
title=c['data']['title'], |
|
|
|
|
position=cdata['position'], |
|
|
|
|
title=cdata['title'], |
|
|
|
|
lesson=lesson, |
|
|
|
|
url=c['data']['url'], |
|
|
|
|
url=cdata['url'], |
|
|
|
|
) |
|
|
|
|
elif c['type'] == 'images': |
|
|
|
|
if 'id' in c['data'] and c['data']['id']: |
|
|
|
|
g = Gallery.objects.get(id=c['data']['id']) |
|
|
|
|
g.position = c['data']['position'] |
|
|
|
|
g.title = c['data']['title'] |
|
|
|
|
elif ctype == 'images': |
|
|
|
|
if 'id' in cdata and cdata['id']: |
|
|
|
|
g = Gallery.objects.get(id=cdata['id']) |
|
|
|
|
g.position = cdata['position'] |
|
|
|
|
g.title = cdata['title'] |
|
|
|
|
g.lesson = lesson |
|
|
|
|
g.save() |
|
|
|
|
if 'images' in c['data']: |
|
|
|
|
for image in c['data']['images']: |
|
|
|
|
if 'images' in cdata: |
|
|
|
|
for image in cdata['images']: |
|
|
|
|
gi = GalleryImage.objects.create( |
|
|
|
|
gallery=g, |
|
|
|
|
img=ImageObject.objects.get(id=image['img']), |
|
|
|
|
@ -375,11 +262,11 @@ class LessonCreateSerializer(serializers.ModelSerializer): |
|
|
|
|
else: |
|
|
|
|
g = Gallery.objects.create( |
|
|
|
|
lesson=lesson, |
|
|
|
|
position=c['data']['position'], |
|
|
|
|
title=c['data']['title'], |
|
|
|
|
position=cdata['position'], |
|
|
|
|
title=cdata['title'], |
|
|
|
|
) |
|
|
|
|
if 'images' in c['data']: |
|
|
|
|
for image in c['data']['images']: |
|
|
|
|
if 'images' in cdata: |
|
|
|
|
for image in cdata['images']: |
|
|
|
|
gi = GalleryImage.objects.create( |
|
|
|
|
gallery=g, |
|
|
|
|
img=ImageObject.objects.get(id=image['img']), |
|
|
|
|
@ -388,17 +275,17 @@ class LessonCreateSerializer(serializers.ModelSerializer): |
|
|
|
|
def create(self, validated_data): |
|
|
|
|
content = validated_data.pop('content', []) |
|
|
|
|
lesson = super().create(validated_data) |
|
|
|
|
self.dispatch_content(lesson, validated_data, content) |
|
|
|
|
self.dispatch_content(lesson, content) |
|
|
|
|
return lesson |
|
|
|
|
|
|
|
|
|
def update(self, instance, validated_data): |
|
|
|
|
content = validated_data.pop('content', []) |
|
|
|
|
lesson = super().update(instance, validated_data) |
|
|
|
|
self.dispatch_content(lesson, validated_data, content) |
|
|
|
|
self.dispatch_content(lesson, content) |
|
|
|
|
return lesson |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class LessonSerializer(LessonCreateSerializer): |
|
|
|
|
course = CourseSerializer() |
|
|
|
|
cover = ImageObjectSerializer() |
|
|
|
|
content = ContentSerializer() |
|
|
|
|
content = ContentSerializer(many=True) |
|
|
|
|
|