From b1eac3b9e38586edbc3f3a232ae20365e667e90f Mon Sep 17 00:00:00 2001 From: Ivlev Denis Date: Fri, 9 Feb 2018 21:06:47 +0300 Subject: [PATCH] Minimize code --- api/v1/serializers/course.py | 172 ++++------------------------------- 1 file changed, 16 insertions(+), 156 deletions(-) diff --git a/api/v1/serializers/course.py b/api/v1/serializers/course.py index ad6b9276..19bace7d 100644 --- a/api/v1/serializers/course.py +++ b/api/v1/serializers/course.py @@ -110,12 +110,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, validated_data, content, materials): for c in content: if c['type'] == 'text': if 'id' in c['data'] and c['data']['id']: @@ -193,91 +188,19 @@ class CourseCreateSerializer(serializers.ModelSerializer): short_description=material['short_description'], ) course.materials.add(m) + + def create(self, validated_data): + content = validated_data.pop('content', []) + materials = validated_data.pop('materials', []) + course = super().create(validated_data) + self.dispatch_content(course, validated_data, content, materials) return course def update(self, instance, validated_data): - materials = validated_data.pop('materials', []) content = validated_data.pop('content', []) - + materials = validated_data.pop('materials', []) course = 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.course = course - t.txt = c['data']['txt'] - t.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'] 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() - else: - i = Image.objects.create( - position=c['data']['position'], - title=c['data']['title'], - course=course, - 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.course = course - 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'], - course=course, - 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.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 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) + self.dispatch_content(course, validated_data, content, materials) return course @@ -314,11 +237,7 @@ class LessonCreateSerializer(serializers.ModelSerializer): 'update_at', ) - def create(self, validated_data): - content = validated_data.pop('content', []) - - lesson = super().create(validated_data) - + 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']: @@ -382,76 +301,17 @@ class LessonCreateSerializer(serializers.ModelSerializer): lesson=lesson, url=c['data']['url'], ) + + def create(self, validated_data): + content = validated_data.pop('content', []) + lesson = super().create(validated_data) + self.dispatch_content(lesson, validated_data, content) 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'], - ) + self.dispatch_content(lesson, validated_data, content) return lesson