|
|
|
|
@ -130,7 +130,20 @@ class CategorySerializer(serializers.ModelSerializer): |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ContentSerializer(serializers.Serializer): |
|
|
|
|
TYPE_CHOICES = ( |
|
|
|
|
'text', |
|
|
|
|
'image', |
|
|
|
|
'imagetext', |
|
|
|
|
'video', |
|
|
|
|
) |
|
|
|
|
type = serializers.ChoiceField(choices=TYPE_CHOICES) |
|
|
|
|
data = serializers.JSONField() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CourseCreateSerializer(serializers.ModelSerializer): |
|
|
|
|
content = serializers.ListSerializer(child=ContentSerializer()) |
|
|
|
|
materials = MaterialSerializer(many=True) |
|
|
|
|
|
|
|
|
|
class Meta: |
|
|
|
|
model = Course |
|
|
|
|
@ -159,11 +172,180 @@ class CourseCreateSerializer(serializers.ModelSerializer): |
|
|
|
|
|
|
|
|
|
read_only_fields = ( |
|
|
|
|
'id', |
|
|
|
|
'content', |
|
|
|
|
# 'content', |
|
|
|
|
'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']: |
|
|
|
|
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'] |
|
|
|
|
i.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']: |
|
|
|
|
i = Image.objects.get(id=c['data']['id']) |
|
|
|
|
i.position = c['data']['position'] |
|
|
|
|
i.title = c['data']['title'] |
|
|
|
|
i.course = course |
|
|
|
|
i.img = Image.objects.get(c['data']['img']) |
|
|
|
|
i.save() |
|
|
|
|
else: |
|
|
|
|
i = Image.objects.create( |
|
|
|
|
position=c['data']['position'], |
|
|
|
|
title=c['data']['title'], |
|
|
|
|
course=course, |
|
|
|
|
img=Image.objects.get(c['data']['img']), |
|
|
|
|
) |
|
|
|
|
elif c['type'] == 'imagetext': |
|
|
|
|
if 'id' in c['data']: |
|
|
|
|
it = ImageText.objects.get(id=c['data']['id']) |
|
|
|
|
it.position = c['data']['position'] |
|
|
|
|
it.title = c['data']['title'] |
|
|
|
|
it.course = course |
|
|
|
|
it.img = Image.objects.get(c['data']['img']) |
|
|
|
|
it.txt = c['data']['txt'] |
|
|
|
|
it.save() |
|
|
|
|
else: |
|
|
|
|
i = ImageText.objects.create( |
|
|
|
|
position=c['data']['position'], |
|
|
|
|
title=c['data']['title'], |
|
|
|
|
course=course, |
|
|
|
|
img=Image.objects.get(c['data']['img']), |
|
|
|
|
txt=c['data']['txt'], |
|
|
|
|
) |
|
|
|
|
elif c['type'] == 'video': |
|
|
|
|
if 'id' in c['data']: |
|
|
|
|
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: |
|
|
|
|
m = Material.objects.get(id=material['id']) |
|
|
|
|
m.title = material['title'] |
|
|
|
|
m.cover = ImageObject.objects.get(material['cover']) |
|
|
|
|
m.short_description = material['short_description'] |
|
|
|
|
m.save() |
|
|
|
|
else: |
|
|
|
|
m = Material.objects.create( |
|
|
|
|
title=material['title'], |
|
|
|
|
cover=ImageObject.objects.get(material['cover']), |
|
|
|
|
short_description=material['short_description'], |
|
|
|
|
) |
|
|
|
|
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']: |
|
|
|
|
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'] |
|
|
|
|
i.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']: |
|
|
|
|
i = Image.objects.get(id=c['data']['id']) |
|
|
|
|
i.position = c['data']['position'] |
|
|
|
|
i.title = c['data']['title'] |
|
|
|
|
i.course = course |
|
|
|
|
i.img = Image.objects.get(c['data']['img']) |
|
|
|
|
i.save() |
|
|
|
|
else: |
|
|
|
|
i = Image.objects.create( |
|
|
|
|
position=c['data']['position'], |
|
|
|
|
title=c['data']['title'], |
|
|
|
|
course=course, |
|
|
|
|
img=Image.objects.get(c['data']['img']), |
|
|
|
|
) |
|
|
|
|
elif c['type'] == 'imagetext': |
|
|
|
|
if 'id' in c['data']: |
|
|
|
|
it = ImageText.objects.get(id=c['data']['id']) |
|
|
|
|
it.position = c['data']['position'] |
|
|
|
|
it.title = c['data']['title'] |
|
|
|
|
it.course = course |
|
|
|
|
it.img = Image.objects.get(c['data']['img']) |
|
|
|
|
it.txt = c['data']['txt'] |
|
|
|
|
it.save() |
|
|
|
|
else: |
|
|
|
|
i = ImageText.objects.create( |
|
|
|
|
position=c['data']['position'], |
|
|
|
|
title=c['data']['title'], |
|
|
|
|
course=course, |
|
|
|
|
img=Image.objects.get(c['data']['img']), |
|
|
|
|
txt=c['data']['txt'], |
|
|
|
|
) |
|
|
|
|
elif c['type'] == 'video': |
|
|
|
|
if 'id' in c['data']: |
|
|
|
|
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: |
|
|
|
|
m = Material.objects.get(id=material['id']) |
|
|
|
|
m.title = material['title'] |
|
|
|
|
m.cover = ImageObject.objects.get(material['cover']) |
|
|
|
|
m.short_description = material['short_description'] |
|
|
|
|
m.save() |
|
|
|
|
else: |
|
|
|
|
m = Material.objects.create( |
|
|
|
|
title=material['title'], |
|
|
|
|
cover=ImageObject.objects.get(material['cover']), |
|
|
|
|
short_description=material['short_description'], |
|
|
|
|
) |
|
|
|
|
course.materials.add(m) |
|
|
|
|
return course |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CourseSerializer(CourseCreateSerializer): |
|
|
|
|
category = CategorySerializer() |
|
|
|
|
|