|
|
|
|
@ -179,19 +179,20 @@ 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() |
|
|
|
|
else: |
|
|
|
|
m = Material.objects.create( |
|
|
|
|
title=material['title'], |
|
|
|
|
cover=ImageObject.objects.get(id=material['cover']), |
|
|
|
|
short_description=material['short_description'], |
|
|
|
|
) |
|
|
|
|
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): |
|
|
|
|
@ -263,20 +264,20 @@ 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() |
|
|
|
|
else: |
|
|
|
|
m = Material.objects.create( |
|
|
|
|
title=material['title'], |
|
|
|
|
cover=ImageObject.objects.get(id=material['cover']), |
|
|
|
|
short_description=material['short_description'], |
|
|
|
|
) |
|
|
|
|
course.materials.add(m) |
|
|
|
|
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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -289,6 +290,10 @@ class CourseSerializer(CourseCreateSerializer): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class LessonCreateSerializer(serializers.ModelSerializer): |
|
|
|
|
content = serializers.ListSerializer( |
|
|
|
|
child=ContentCreateSerializer(), |
|
|
|
|
required=False, |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
class Meta: |
|
|
|
|
model = Lesson |
|
|
|
|
@ -305,12 +310,152 @@ class LessonCreateSerializer(serializers.ModelSerializer): |
|
|
|
|
|
|
|
|
|
read_only_fields = ( |
|
|
|
|
'id', |
|
|
|
|
'content', |
|
|
|
|
'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() |
|
|
|
|
|