From e99d22589248ddfe2d149a75ee1b2215669c4232 Mon Sep 17 00:00:00 2001 From: Ivlev Denis Date: Sun, 22 Apr 2018 20:13:09 +0300 Subject: [PATCH] Fix LessonCreateSerializer dispatch_content methood --- api/v1/serializers/course.py | 106 +---------------------------------- 1 file changed, 1 insertion(+), 105 deletions(-) diff --git a/api/v1/serializers/course.py b/api/v1/serializers/course.py index 55272f85..d07e58b5 100644 --- a/api/v1/serializers/course.py +++ b/api/v1/serializers/course.py @@ -163,7 +163,7 @@ class CourseCreateSerializer(DispatchContentMixin, return CourseSerializer(instance=instance, context=self.context).to_representation(instance) -class LessonCreateSerializer(serializers.ModelSerializer): +class LessonCreateSerializer(DispatchContentMixin, serializers.ModelSerializer): content = serializers.ListSerializer( child=ContentCreateSerializer(), required=False, @@ -190,110 +190,6 @@ class LessonCreateSerializer(serializers.ModelSerializer): 'deactivated_at', ) - def dispatch_content(self, lesson, content): - for c in content: - 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 = cdata['txt'] - t.uuid = cdata['uuid'] - t.save() - else: - t = Text.objects.create( - position=cdata['position'], - title=cdata['title'], - lesson=lesson, - txt=cdata['txt'], - uuid=cdata['uuid'], - ) - 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=cdata['img']) - image.uuid = cdata['uuid'] - image.save() - else: - image = Image.objects.create( - position=cdata['position'], - title=cdata['title'], - lesson=lesson, - img=ImageObject.objects.get(id=cdata['img']), - uuid=cdata['uuid'], - ) - 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=cdata['img']) - it.txt = cdata['txt'] - it.uuid = cdata['uuid'] - it.save() - else: - it = ImageText.objects.create( - position=cdata['position'], - title=cdata['title'], - lesson=lesson, - img=ImageObject.objects.get(id=cdata['img']), - txt=cdata['txt'], - uuid=cdata['uuid'], - ) - 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 = cdata['url'] - v.uuid = cdata['uuid'] - v.save() - else: - v = Video.objects.create( - position=cdata['position'], - title=cdata['title'], - lesson=lesson, - url=cdata['url'], - uuid=cdata['uuid'], - ) - 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.uuid = cdata['uuid'] - g.save() - if 'images' in cdata: - for image in cdata['images']: - gi = GalleryImage.objects.create( - gallery=g, - img=ImageObject.objects.get(id=image['img']), - ) - else: - g = Gallery.objects.create( - lesson=lesson, - position=cdata['position'], - title=cdata['title'], - uuid=cdata['uuid'], - ) - if 'images' in cdata: - for image in cdata['images']: - gi = GalleryImage.objects.create( - gallery=g, - img=ImageObject.objects.get(id=image['img']), - ) - def create(self, validated_data): content = validated_data.pop('content', []) lesson = super().create(validated_data)