Optimize vars

remotes/origin/hasaccess
Ivlev Denis 8 years ago
parent af58cf44af
commit 98a51574db
  1. 211
      api/v1/serializers/course.py

@ -111,79 +111,83 @@ class CourseCreateSerializer(serializers.ModelSerializer):
'update_at', 'update_at',
) )
def dispatch_content(self, course, content, materials): def dispatch_content(self, course, content):
for c in content: for c in content:
if c['type'] == 'text': if 'type' not in c or not c['type'] or 'data' not in c or not c['data']:
if 'id' in c['data'] and c['data']['id']: continue
t = Text.objects.get(id=c['data']['id']) ctype = c['type']
t.position = c['data']['position'] cdata = c['data']
t.title = c['data']['title'] 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.course = course
t.txt = c['data']['txt'] t.txt = cdata['txt']
t.save() t.save()
else: else:
t = Text.objects.create( t = Text.objects.create(
position=c['data']['position'], position=cdata['position'],
title=c['data']['title'], title=cdata['title'],
course=course, course=course,
txt=c['data']['txt'], txt=cdata['txt'],
) )
elif c['type'] == 'image': elif ctype == 'image':
if 'id' in c['data'] and c['data']['id']: if 'id' in cdata and cdata['id']:
image = Image.objects.get(id=c['data']['id']) image = Image.objects.get(id=cdata['id'])
image.position = c['data']['position'] image.position = cdata['position']
image.title = c['data']['title'] image.title = cdata['title']
image.course = course image.course = course
image.img = ImageObject.objects.get(id=c['data']['img']) image.img = ImageObject.objects.get(id=cdata['img'])
image.save() image.save()
else: else:
image = Image.objects.create( image = Image.objects.create(
position=c['data']['position'], position=cdata['position'],
title=c['data']['title'], title=cdata['title'],
course=course, course=course,
img=ImageObject.objects.get(id=c['data']['img']), img=ImageObject.objects.get(id=cdata['img']),
) )
elif c['type'] == 'image-text': elif ctype == 'image-text':
if 'id' in c['data'] and c['data']['id']: if 'id' in cdata and cdata['id']:
it = ImageText.objects.get(id=c['data']['id']) it = ImageText.objects.get(id=cdata['id'])
it.position = c['data']['position'] it.position = cdata['position']
it.title = c['data']['title'] it.title = cdata['title']
it.course = course it.course = course
it.img = ImageObject.objects.get(id=c['data']['img']) it.img = ImageObject.objects.get(id=cdata['img'])
it.txt = c['data']['txt'] it.txt = cdata['txt']
it.save() it.save()
else: else:
it = ImageText.objects.create( it = ImageText.objects.create(
position=c['data']['position'], position=cdata['position'],
title=c['data']['title'], title=cdata['title'],
course=course, course=course,
img=ImageObject.objects.get(id=c['data']['img']), img=ImageObject.objects.get(id=cdata['img']),
txt=c['data']['txt'], txt=cdata['txt'],
) )
elif c['type'] == 'video': elif ctype == 'video':
if 'id' in c['data'] and c['data']['id']: if 'id' in cdata and cdata['id']:
v = Video.objects.get(id=c['data']['id']) v = Video.objects.get(id=cdata['id'])
v.position = c['data']['position'] v.position = cdata['position']
v.title = c['data']['title'] v.title = cdata['title']
v.course = course v.course = course
v.url = c['data']['url'] v.url = cdata['url']
v.save() v.save()
else: else:
v = Video.objects.create( v = Video.objects.create(
position=c['data']['position'], position=cdata['position'],
title=c['data']['title'], title=cdata['title'],
course=course, course=course,
url=c['data']['url'], url=cdata['url'],
) )
elif c['type'] == 'images': elif ctype == 'images':
if 'id' in c['data'] and c['data']['id']: if 'id' in cdata and cdata['id']:
g = Gallery.objects.get(id=c['data']['id']) g = Gallery.objects.get(id=cdata['id'])
g.course = course g.course = course
g.position = c['data']['position'] g.position = cdata['position']
g.title = c['data']['title'] g.title = cdata['title']
g.save() g.save()
if 'images' in c['data']: if 'images' in cdata:
for image in c['data']['images']: for image in cdata['images']:
gi = GalleryImage.objects.create( gi = GalleryImage.objects.create(
gallery=g, gallery=g,
img=ImageObject.objects.get(id=image['img']) img=ImageObject.objects.get(id=image['img'])
@ -191,16 +195,17 @@ class CourseCreateSerializer(serializers.ModelSerializer):
else: else:
g = Gallery.objects.create( g = Gallery.objects.create(
course=course, course=course,
position=c['data']['position'], position=cdata['position'],
title=c['data']['title'], title=cdata['title'],
) )
if 'images' in c['data']: if 'images' in cdata:
for image in c['data']['images']: for image in cdata['images']:
gi = GalleryImage.objects.create( gi = GalleryImage.objects.create(
gallery=g, gallery=g,
img=ImageObject.objects.get(id=image['img']), img=ImageObject.objects.get(id=image['img']),
) )
def dispatch_materials(self, course, materials):
for material in materials: for material in materials:
if 'id' in material and material['id']: if 'id' in material and material['id']:
m = Material.objects.get(id=material['id']) m = Material.objects.get(id=material['id'])
@ -248,7 +253,8 @@ class CourseCreateSerializer(serializers.ModelSerializer):
materials = validated_data.pop('materials', []) materials = validated_data.pop('materials', [])
gallery = validated_data.pop('gallery', {}) gallery = validated_data.pop('gallery', {})
course = super().create(validated_data) course = super().create(validated_data)
self.dispatch_content(course, content, materials) self.dispatch_content(course, content)
self.dispatch_materials(course, materials)
self.dispatch_gallery(course, gallery) self.dispatch_gallery(course, gallery)
return course return course
@ -257,6 +263,7 @@ class CourseCreateSerializer(serializers.ModelSerializer):
materials = validated_data.pop('materials', []) materials = validated_data.pop('materials', [])
gallery = validated_data.pop('gallery', {}) gallery = validated_data.pop('gallery', {})
course = super().update(instance, validated_data) course = super().update(instance, validated_data)
self.dispatch_materials(course, materials)
self.dispatch_content(course, content, materials) self.dispatch_content(course, content, materials)
self.dispatch_gallery(course, gallery) self.dispatch_gallery(course, gallery)
return course return course
@ -297,77 +304,81 @@ class LessonCreateSerializer(serializers.ModelSerializer):
def dispatch_content(self, lesson, validated_data, content): def dispatch_content(self, lesson, validated_data, content):
for c in content: for c in content:
if c['type'] == 'text': if 'type' not in c or not c['type'] or 'data' not in c or not c['data']:
if 'id' in c['data'] and c['data']['id']: continue
t = Text.objects.get(id=c['data']['id']) ctype = c['type']
t.position = c['data']['position'] cdata = c['data']
t.title = c['data']['title'] 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.lesson = lesson
t.txt = c['data']['txt'] t.txt = cdata['txt']
t.save() t.save()
else: else:
t = Text.objects.create( t = Text.objects.create(
position=c['data']['position'], position=cdata['position'],
title=c['data']['title'], title=cdata['title'],
lesson=lesson, lesson=lesson,
txt=c['data']['txt'], txt=cdata['txt'],
) )
elif c['type'] == 'image': elif ctype == 'image':
if 'id' in c['data'] and c['data']['id']: if 'id' in cdata and cdata['id']:
image = Image.objects.get(id=c['data']['id']) image = Image.objects.get(id=cdata['id'])
image.position = c['data']['position'] image.position = cdata['position']
image.title = c['data']['title'] image.title = cdata['title']
image.lesson = lesson image.lesson = lesson
image.img = ImageObject.objects.get(id=c['data']['img']) image.img = ImageObject.objects.get(id=cdata['img'])
image.save() image.save()
else: else:
image = Image.objects.create( image = Image.objects.create(
position=c['data']['position'], position=cdata['position'],
title=c['data']['title'], title=cdata['title'],
lesson=lesson, lesson=lesson,
img=ImageObject.objects.get(id=c['data']['img']), img=ImageObject.objects.get(id=cdata['img']),
) )
elif c['type'] == 'image-text': elif ctype == 'image-text':
if 'id' in c['data'] and c['data']['id']: if 'id' in cdata and cdata['id']:
it = ImageText.objects.get(id=c['data']['id']) it = ImageText.objects.get(id=cdata['id'])
it.position = c['data']['position'] it.position = cdata['position']
it.title = c['data']['title'] it.title = cdata['title']
it.lesson = lesson it.lesson = lesson
it.img = ImageObject.objects.get(id=c['data']['img']) it.img = ImageObject.objects.get(id=cdata['img'])
it.txt = c['data']['txt'] it.txt = cdata['txt']
it.save() it.save()
else: else:
it = ImageText.objects.create( it = ImageText.objects.create(
position=c['data']['position'], position=cdata['position'],
title=c['data']['title'], title=cdata['title'],
lesson=lesson, lesson=lesson,
img=ImageObject.objects.get(id=c['data']['img']), img=ImageObject.objects.get(id=cdata['img']),
txt=c['data']['txt'], txt=cdata['txt'],
) )
elif c['type'] == 'video': elif ctype == 'video':
if 'id' in c['data'] and c['data']['id']: if 'id' in cdata and cdata['id']:
v = Video.objects.get(id=c['data']['id']) v = Video.objects.get(id=cdata['id'])
v.position = c['data']['position'] v.position = cdata['position']
v.title = c['data']['title'] v.title = cdata['title']
v.lesson = lesson v.lesson = lesson
v.url = c['data']['url'] v.url = cdata['url']
v.save() v.save()
else: else:
v = Video.objects.create( v = Video.objects.create(
position=c['data']['position'], position=cdata['position'],
title=c['data']['title'], title=cdata['title'],
lesson=lesson, lesson=lesson,
url=c['data']['url'], url=cdata['url'],
) )
elif c['type'] == 'images': elif ctype == 'images':
if 'id' in c['data'] and c['data']['id']: if 'id' in cdata and cdata['id']:
g = Gallery.objects.get(id=c['data']['id']) g = Gallery.objects.get(id=cdata['id'])
g.position = c['data']['position'] g.position = cdata['position']
g.title = c['data']['title'] g.title = cdata['title']
g.lesson = lesson g.lesson = lesson
g.save() g.save()
if 'images' in c['data']: if 'images' in cdata:
for image in c['data']['images']: for image in cdata['images']:
gi = GalleryImage.objects.create( gi = GalleryImage.objects.create(
gallery=g, gallery=g,
img=ImageObject.objects.get(id=image['img']), img=ImageObject.objects.get(id=image['img']),
@ -375,11 +386,11 @@ class LessonCreateSerializer(serializers.ModelSerializer):
else: else:
g = Gallery.objects.create( g = Gallery.objects.create(
lesson=lesson, lesson=lesson,
position=c['data']['position'], position=cdata['position'],
title=c['data']['title'], title=cdata['title'],
) )
if 'images' in c['data']: if 'images' in cdata:
for image in c['data']['images']: for image in cdata['images']:
gi = GalleryImage.objects.create( gi = GalleryImage.objects.create(
gallery=g, gallery=g,
img=ImageObject.objects.get(id=image['img']), img=ImageObject.objects.get(id=image['img']),

Loading…
Cancel
Save