|
|
|
|
@ -76,6 +76,7 @@ class CourseCreateSerializer(serializers.ModelSerializer): |
|
|
|
|
required=False, |
|
|
|
|
) |
|
|
|
|
materials = MaterialSerializer(many=True, required=False) |
|
|
|
|
gallery = GallerySerializer() |
|
|
|
|
|
|
|
|
|
class Meta: |
|
|
|
|
model = Course |
|
|
|
|
@ -110,7 +111,7 @@ class CourseCreateSerializer(serializers.ModelSerializer): |
|
|
|
|
'update_at', |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
def dispatch_content(self, course, validated_data, content, materials): |
|
|
|
|
def dispatch_content(self, course, content, materials): |
|
|
|
|
for c in content: |
|
|
|
|
if c['type'] == 'text': |
|
|
|
|
if 'id' in c['data'] and c['data']['id']: |
|
|
|
|
@ -189,18 +190,47 @@ class CourseCreateSerializer(serializers.ModelSerializer): |
|
|
|
|
) |
|
|
|
|
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.save() |
|
|
|
|
else: |
|
|
|
|
g = Gallery.objects.create( |
|
|
|
|
title=gallery.get('title', '') |
|
|
|
|
) |
|
|
|
|
if 'images' in gallery: |
|
|
|
|
for i in gallery['images']: |
|
|
|
|
if 'id' in i and i['id']: |
|
|
|
|
gi = GalleryImage.objects.get(id=i['id']) |
|
|
|
|
gi.gallery = g |
|
|
|
|
gi.img = i['img'] |
|
|
|
|
gi.save() |
|
|
|
|
else: |
|
|
|
|
gi = GalleryImage.objects.create( |
|
|
|
|
gallery=g, |
|
|
|
|
img=i['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, validated_data, content, materials) |
|
|
|
|
self.dispatch_content(course, content, materials) |
|
|
|
|
self.dispatch_gallery(course, gallery) |
|
|
|
|
return course |
|
|
|
|
|
|
|
|
|
def update(self, instance, validated_data): |
|
|
|
|
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, validated_data, content, materials) |
|
|
|
|
self.dispatch_content(course, content, materials) |
|
|
|
|
self.dispatch_gallery(course, gallery) |
|
|
|
|
return course |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|