From ba0dacfa24b101529690115e3995a579bd5f101b Mon Sep 17 00:00:00 2001 From: Ivlev Denis Date: Tue, 13 Feb 2018 09:44:40 +0300 Subject: [PATCH] Move spaghetti code to mixins --- api/v1/serializers/course.py | 151 +++------------------------------- api/v1/serializers/mixins.py | 152 +++++++++++++++++++++++++++++++++++ 2 files changed, 162 insertions(+), 141 deletions(-) create mode 100644 api/v1/serializers/mixins.py diff --git a/api/v1/serializers/course.py b/api/v1/serializers/course.py index 30c39c58..8d14e42b 100644 --- a/api/v1/serializers/course.py +++ b/api/v1/serializers/course.py @@ -12,6 +12,8 @@ from apps.content.models import ( Gallery, GalleryImage, ImageObject, ) +from .mixins import DispatchContentMixin, DispatchGalleryMixin, DispatchMaterialMixin + class MaterialCreateSerializer(serializers.ModelSerializer): @@ -69,7 +71,11 @@ class CategorySerializer(serializers.ModelSerializer): ) -class CourseCreateSerializer(serializers.ModelSerializer): +class CourseCreateSerializer(DispatchContentMixin, + DispatchGalleryMixin, + DispatchMaterialMixin, + serializers.ModelSerializer + ): slug = serializers.SlugField(allow_unicode=True, required=False) content = serializers.ListSerializer( child=ContentCreateSerializer(), @@ -111,143 +117,6 @@ class CourseCreateSerializer(serializers.ModelSerializer): 'update_at', ) - def dispatch_content(self, course, 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.course = course - t.txt = cdata['txt'] - t.save() - else: - t = Text.objects.create( - position=cdata['position'], - title=cdata['title'], - course=course, - txt=cdata['txt'], - ) - 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.course = course - image.img = ImageObject.objects.get(id=cdata['img']) - image.save() - else: - image = Image.objects.create( - position=cdata['position'], - title=cdata['title'], - course=course, - img=ImageObject.objects.get(id=cdata['img']), - ) - 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.course = course - it.img = ImageObject.objects.get(id=cdata['img']) - it.txt = cdata['txt'] - it.save() - else: - it = ImageText.objects.create( - position=cdata['position'], - title=cdata['title'], - course=course, - img=ImageObject.objects.get(id=cdata['img']), - txt=cdata['txt'], - ) - 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.course = course - v.url = cdata['url'] - v.save() - else: - v = Video.objects.create( - position=cdata['position'], - title=cdata['title'], - course=course, - url=cdata['url'], - ) - elif ctype == 'images': - if 'id' in cdata and cdata['id']: - g = Gallery.objects.get(id=cdata['id']) - g.course = course - g.position = cdata['position'] - g.title = cdata['title'] - 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( - course=course, - position=cdata['position'], - title=cdata['title'], - ) - if 'images' in cdata: - for image in cdata['images']: - gi = GalleryImage.objects.create( - gallery=g, - img=ImageObject.objects.get(id=image['img']), - ) - - def dispatch_materials(self, course, materials): - 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) - - 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.position = 0 - g.save() - else: - g = Gallery.objects.create( - title=gallery.get('title', ''), - position=0, - ) - if 'images' in gallery: - for image in gallery['images']: - if 'id' in image and image['id']: - gi = GalleryImage.objects.get(id=image['id']) - gi.gallery = g - gi.img = image['img'] - gi.save() - else: - gi = GalleryImage.objects.create( - gallery=g, - img=image['img'], - ) - course.gallery = g - course.save() - def create(self, validated_data): content = validated_data.pop('content', []) materials = validated_data.pop('materials', []) @@ -302,7 +171,7 @@ class LessonCreateSerializer(serializers.ModelSerializer): 'update_at', ) - def dispatch_content(self, lesson, validated_data, content): + 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 @@ -399,13 +268,13 @@ class LessonCreateSerializer(serializers.ModelSerializer): def create(self, validated_data): content = validated_data.pop('content', []) lesson = super().create(validated_data) - self.dispatch_content(lesson, validated_data, content) + self.dispatch_content(lesson, content) return lesson def update(self, instance, validated_data): content = validated_data.pop('content', []) lesson = super().update(instance, validated_data) - self.dispatch_content(lesson, validated_data, content) + self.dispatch_content(lesson, content) return lesson diff --git a/api/v1/serializers/mixins.py b/api/v1/serializers/mixins.py new file mode 100644 index 00000000..e709002e --- /dev/null +++ b/api/v1/serializers/mixins.py @@ -0,0 +1,152 @@ +from apps.course.models import Category, Course, Material, Lesson, Like + +from apps.content.models import ( + Content, Image, Text, ImageText, Video, + Gallery, GalleryImage, ImageObject, +) + + +class DispatchContentMixin(object): + + def dispatch_content(self, course, 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.course = course + t.txt = cdata['txt'] + t.save() + else: + t = Text.objects.create( + position=cdata['position'], + title=cdata['title'], + course=course, + txt=cdata['txt'], + ) + 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.course = course + image.img = ImageObject.objects.get(id=cdata['img']) + image.save() + else: + image = Image.objects.create( + position=cdata['position'], + title=cdata['title'], + course=course, + img=ImageObject.objects.get(id=cdata['img']), + ) + 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.course = course + it.img = ImageObject.objects.get(id=cdata['img']) + it.txt = cdata['txt'] + it.save() + else: + it = ImageText.objects.create( + position=cdata['position'], + title=cdata['title'], + course=course, + img=ImageObject.objects.get(id=cdata['img']), + txt=cdata['txt'], + ) + 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.course = course + v.url = cdata['url'] + v.save() + else: + v = Video.objects.create( + position=cdata['position'], + title=cdata['title'], + course=course, + url=cdata['url'], + ) + elif ctype == 'images': + if 'id' in cdata and cdata['id']: + g = Gallery.objects.get(id=cdata['id']) + g.course = course + g.position = cdata['position'] + g.title = cdata['title'] + 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( + course=course, + position=cdata['position'], + title=cdata['title'], + ) + if 'images' in cdata: + for image in cdata['images']: + gi = GalleryImage.objects.create( + gallery=g, + img=ImageObject.objects.get(id=image['img']), + ) + + +class DispatchMaterialMixin(object): + + def dispatch_materials(self, course, materials): + 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) + + +class DispatchGalleryMixin(object): + + 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.position = 0 + g.save() + else: + g = Gallery.objects.create( + title=gallery.get('title', ''), + position=0, + ) + if 'images' in gallery: + for image in gallery['images']: + if 'id' in image and image['id']: + gi = GalleryImage.objects.get(id=image['id']) + gi.gallery = g + gi.img = image['img'] + gi.save() + else: + gi = GalleryImage.objects.create( + gallery=g, + img=image['img'], + ) + course.gallery = g + course.save()