|
|
|
|
@ -76,6 +76,7 @@ class CourseCreateSerializer(serializers.ModelSerializer): |
|
|
|
|
required=False, |
|
|
|
|
) |
|
|
|
|
materials = MaterialSerializer(many=True, required=False) |
|
|
|
|
gallery = GallerySerializer() |
|
|
|
|
|
|
|
|
|
class Meta: |
|
|
|
|
model = Course |
|
|
|
|
@ -110,12 +111,7 @@ class CourseCreateSerializer(serializers.ModelSerializer): |
|
|
|
|
'update_at', |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
def create(self, validated_data): |
|
|
|
|
materials = validated_data.pop('materials', []) |
|
|
|
|
content = validated_data.pop('content', []) |
|
|
|
|
|
|
|
|
|
course = super().create(validated_data) |
|
|
|
|
|
|
|
|
|
def dispatch_content(self, course, content, materials): |
|
|
|
|
for c in content: |
|
|
|
|
if c['type'] == 'text': |
|
|
|
|
if 'id' in c['data'] and c['data']['id']: |
|
|
|
|
@ -134,14 +130,14 @@ class CourseCreateSerializer(serializers.ModelSerializer): |
|
|
|
|
) |
|
|
|
|
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() |
|
|
|
|
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: |
|
|
|
|
i = Image.objects.create( |
|
|
|
|
image = Image.objects.create( |
|
|
|
|
position=c['data']['position'], |
|
|
|
|
title=c['data']['title'], |
|
|
|
|
course=course, |
|
|
|
|
@ -179,56 +175,156 @@ class CourseCreateSerializer(serializers.ModelSerializer): |
|
|
|
|
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() |
|
|
|
|
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'] |
|
|
|
|
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['id']) |
|
|
|
|
) |
|
|
|
|
else: |
|
|
|
|
m = Material.objects.create( |
|
|
|
|
title=material['title'], |
|
|
|
|
cover=ImageObject.objects.get(id=material['cover']), |
|
|
|
|
short_description=material['short_description'], |
|
|
|
|
g = Gallery.objects.create( |
|
|
|
|
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['id']), |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
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.course = course |
|
|
|
|
g.position = 0 |
|
|
|
|
g.save() |
|
|
|
|
else: |
|
|
|
|
g = Gallery.objects.create( |
|
|
|
|
title=gallery.get('title', ''), |
|
|
|
|
course=course, |
|
|
|
|
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_gallery(course, gallery) |
|
|
|
|
return course |
|
|
|
|
|
|
|
|
|
def update(self, instance, validated_data): |
|
|
|
|
materials = validated_data.pop('materials', []) |
|
|
|
|
content = validated_data.pop('content', []) |
|
|
|
|
|
|
|
|
|
materials = validated_data.pop('materials', []) |
|
|
|
|
gallery = validated_data.pop('gallery', {}) |
|
|
|
|
course = super().update(instance, validated_data) |
|
|
|
|
self.dispatch_content(course, content, materials) |
|
|
|
|
self.dispatch_gallery(course, gallery) |
|
|
|
|
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 dispatch_content(self, lesson, validated_data, 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'] |
|
|
|
|
t.course = course |
|
|
|
|
t.lesson = lesson |
|
|
|
|
t.txt = c['data']['txt'] |
|
|
|
|
t.save() |
|
|
|
|
else: |
|
|
|
|
t = Text.objects.create( |
|
|
|
|
position=c['data']['position'], |
|
|
|
|
title=c['data']['title'], |
|
|
|
|
course=course, |
|
|
|
|
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.course = course |
|
|
|
|
i.img = ImageObject.objects.get(id=c['data']['img']) |
|
|
|
|
i.save() |
|
|
|
|
image = Image.objects.get(id=c['data']['id']) |
|
|
|
|
image.position = c['data']['position'] |
|
|
|
|
image.title = c['data']['title'] |
|
|
|
|
image.lesson = lesson |
|
|
|
|
image.img = ImageObject.objects.get(id=c['data']['img']) |
|
|
|
|
image.save() |
|
|
|
|
else: |
|
|
|
|
i = Image.objects.create( |
|
|
|
|
image = Image.objects.create( |
|
|
|
|
position=c['data']['position'], |
|
|
|
|
title=c['data']['title'], |
|
|
|
|
course=course, |
|
|
|
|
lesson=lesson, |
|
|
|
|
img=ImageObject.objects.get(id=c['data']['img']), |
|
|
|
|
) |
|
|
|
|
elif c['type'] == 'image-text': |
|
|
|
|
@ -236,7 +332,7 @@ class CourseCreateSerializer(serializers.ModelSerializer): |
|
|
|
|
it = ImageText.objects.get(id=c['data']['id']) |
|
|
|
|
it.position = c['data']['position'] |
|
|
|
|
it.title = c['data']['title'] |
|
|
|
|
it.course = course |
|
|
|
|
it.lesson = lesson |
|
|
|
|
it.img = ImageObject.objects.get(id=c['data']['img']) |
|
|
|
|
it.txt = c['data']['txt'] |
|
|
|
|
it.save() |
|
|
|
|
@ -244,7 +340,7 @@ class CourseCreateSerializer(serializers.ModelSerializer): |
|
|
|
|
it = ImageText.objects.create( |
|
|
|
|
position=c['data']['position'], |
|
|
|
|
title=c['data']['title'], |
|
|
|
|
course=course, |
|
|
|
|
lesson=lesson, |
|
|
|
|
img=ImageObject.objects.get(id=c['data']['img']), |
|
|
|
|
txt=c['data']['txt'], |
|
|
|
|
) |
|
|
|
|
@ -253,64 +349,54 @@ class CourseCreateSerializer(serializers.ModelSerializer): |
|
|
|
|
v = Video.objects.get(id=c['data']['id']) |
|
|
|
|
v.position = c['data']['position'] |
|
|
|
|
v.title = c['data']['title'] |
|
|
|
|
v.course = course |
|
|
|
|
v.lesson = lesson |
|
|
|
|
v.url = c['data']['url'] |
|
|
|
|
v.save() |
|
|
|
|
else: |
|
|
|
|
v = Video.objects.create( |
|
|
|
|
position=c['data']['position'], |
|
|
|
|
title=c['data']['title'], |
|
|
|
|
course=course, |
|
|
|
|
lesson=lesson, |
|
|
|
|
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() |
|
|
|
|
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'] |
|
|
|
|
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['id']), |
|
|
|
|
) |
|
|
|
|
else: |
|
|
|
|
m = Material.objects.create( |
|
|
|
|
title=material['title'], |
|
|
|
|
cover=ImageObject.objects.get(id=material['cover']), |
|
|
|
|
short_description=material['short_description'], |
|
|
|
|
g = Gallery.objects.create( |
|
|
|
|
position=c['data']['position'], |
|
|
|
|
title=c['data']['title'], |
|
|
|
|
) |
|
|
|
|
course.materials.add(m) |
|
|
|
|
return course |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CourseSerializer(CourseCreateSerializer): |
|
|
|
|
category = CategorySerializer() |
|
|
|
|
materials = MaterialSerializer(many=True) |
|
|
|
|
cover = ImageObjectSerializer() |
|
|
|
|
gallery = GallerySerializer() |
|
|
|
|
content = ContentSerializer(many=True) |
|
|
|
|
|
|
|
|
|
if 'images' in c['data']: |
|
|
|
|
for image in c['data']['images']: |
|
|
|
|
gi = GalleryImage.objects.create( |
|
|
|
|
gallery=g, |
|
|
|
|
img=ImageObject.objects.get(id=image['id']), |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
class LessonCreateSerializer(serializers.ModelSerializer): |
|
|
|
|
|
|
|
|
|
class Meta: |
|
|
|
|
model = Lesson |
|
|
|
|
fields = ( |
|
|
|
|
'id', |
|
|
|
|
'title', |
|
|
|
|
'short_description', |
|
|
|
|
'course', |
|
|
|
|
'cover', |
|
|
|
|
'content', |
|
|
|
|
'created_at', |
|
|
|
|
'update_at', |
|
|
|
|
) |
|
|
|
|
def create(self, validated_data): |
|
|
|
|
content = validated_data.pop('content', []) |
|
|
|
|
lesson = super().create(validated_data) |
|
|
|
|
self.dispatch_content(lesson, validated_data, content) |
|
|
|
|
return lesson |
|
|
|
|
|
|
|
|
|
read_only_fields = ( |
|
|
|
|
'id', |
|
|
|
|
'content', |
|
|
|
|
'created_at', |
|
|
|
|
'update_at', |
|
|
|
|
) |
|
|
|
|
def update(self, instance, validated_data): |
|
|
|
|
content = validated_data.pop('content', []) |
|
|
|
|
lesson = super().update(instance, validated_data) |
|
|
|
|
self.dispatch_content(lesson, validated_data, content) |
|
|
|
|
return lesson |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class LessonSerializer(LessonCreateSerializer): |
|
|
|
|
course = CourseSerializer() |
|
|
|
|
cover = ImageObjectSerializer() |
|
|
|
|
content = ContentSerializer() |
|
|
|
|
|