Update dispatch_content method in DispatchContentMixin

remotes/origin/hasaccess
Ivlev Denis 8 years ago
parent e99d225892
commit 1cd30570ee
  1. 167
      api/v1/serializers/mixins.py

@ -1,114 +1,147 @@
from apps.course.models import Category, Course, Material, Lesson, Like from apps.course.models import Category, Course, Material, Lesson, Like
from apps.school.models import LiveLesson
from apps.content.models import ( from apps.content.models import (
Content, Image, Text, ImageText, Video, Content, Image, Text, ImageText, Video,
Gallery, GalleryImage, ImageObject, Gallery, GalleryImage, ImageObject,
) )
from .content import (
TextCreateSerializer, ImageCreateSerializer,
ImageTextCreateSerializer, VideoCreateSerializer,
)
class DispatchContentMixin(object): class DispatchContentMixin(object):
def dispatch_content(self, course, content): def dispatch_content(self, obj, content):
for c in content: for c in content:
if 'type' not in c or not c['type'] or 'data' not in c or not c['data']: if 'type' not in c or not c['type'] or 'data' not in c or not c['data']:
continue continue
ctype = c['type'] ctype = c['type']
cdata = c['data'] cdata = c['data']
if isinstance(obj, Course):
obj_type = 'course'
elif isinstance(obj, Lesson):
obj_type = 'lesson'
elif isinstance(obj, LiveLesson):
obj_type = 'live_lesson'
cdata[obj_type] = obj.id
if ctype == 'text': if ctype == 'text':
if 'id' in cdata and cdata['id']: if 'id' in cdata and cdata['id']:
t = Text.objects.get(id=cdata['id']) t = Text.objects.get(id=cdata.pop('id'))
t.position = cdata['position'] serializer = TextCreateSerializer(t, data=cdata)
t.title = cdata['title'] serializer.save()
t.course = course # t.position = cdata['position']
t.txt = cdata['txt'] # t.title = cdata['title']
t.uuid = cdata['uuid'] # t.course = course
t.save() # t.txt = cdata['txt']
# t.uuid = cdata['uuid']
# t.save()
else: else:
t = Text.objects.create( serializer = TextCreateSerializer(data=cdata)
position=cdata['position'], serializer.save()
title=cdata['title'], # t = Text.objects.create(
course=course, # position=cdata['position'],
txt=cdata['txt'], # title=cdata['title'],
uuid=cdata['uuid'], # course=course,
) # txt=cdata['txt'],
# uuid=cdata['uuid'],
# )
elif ctype == 'image': elif ctype == 'image':
if 'id' in cdata and cdata['id']: if 'id' in cdata and cdata['id']:
image = Image.objects.get(id=cdata['id']) image = Image.objects.get(id=cdata.pop('id'))
image.uuid = cdata['uuid'] serializer = ImageCreateSerializer(image, data=cdata)
image.position = cdata['position'] serializer.save()
image.title = cdata['title'] # image.uuid = cdata['uuid']
image.course = course # image.position = cdata['position']
# image.title = cdata['title']
# image.course = course
try: try:
image_object = ImageObject.objects.get(id=cdata['img']) image_object = ImageObject.objects.get(id=cdata['img'])
image.img = image_object
image.save()
except ImageObject.DoesNotExist: except ImageObject.DoesNotExist:
pass pass
else:
image.img = image_object
image.save()
image.save() # image.save()
else: else:
image = Image.objects.create( serializer = ImageCreateSerializer(data=cdata)
position=cdata['position'], image = serializer.save()
title=cdata['title'], # image = Image.objects.create(
course=course, # position=cdata['position'],
uuid=cdata['uuid'], # title=cdata['title'],
) # course=course,
# uuid=cdata['uuid'],
# )
try: try:
image_object = ImageObject.objects.get(id=cdata['img']) image_object = ImageObject.objects.get(id=cdata['img'])
image.img = image_object
image.save()
except ImageObject.DoesNotExist: except ImageObject.DoesNotExist:
pass pass
else:
image.img = image_object
image.save()
elif ctype == 'image-text': elif ctype == 'image-text':
if 'id' in cdata and cdata['id']: if 'id' in cdata and cdata['id']:
it = ImageText.objects.get(id=cdata['id']) it = ImageText.objects.get(id=cdata.pop('id'))
it.uuid = cdata['uuid'] serializer = ImageTextCreateSerializer(data=cdata)
it.position = cdata['position'] serializer.save()
it.title = cdata['title'] # it.uuid = cdata['uuid']
it.course = course # it.position = cdata['position']
# it.title = cdata['title']
# it.course = course
try: try:
image_object = ImageObject.objects.get(id=cdata['img']) image_object = ImageObject.objects.get(id=cdata['img'])
it.img = image_object
it.save()
except ImageObject.DoesNotExist: except ImageObject.DoesNotExist:
pass pass
else:
it.img = image_object
it.save()
it.txt = cdata['txt'] # it.txt = cdata['txt']
it.save() # it.save()
else: else:
it = ImageText.objects.create( serializer = ImageTextCreateSerializer(data=cdata)
position=cdata['position'], it = serializer.save()
title=cdata['title'], # it = ImageText.objects.create(
course=course, # position=cdata['position'],
txt=cdata['txt'], # title=cdata['title'],
uuid=cdata['uuid'], # course=course,
) # txt=cdata['txt'],
# uuid=cdata['uuid'],
# )
try: try:
image_object = ImageObject.objects.get(id=cdata['img']) image_object = ImageObject.objects.get(id=cdata['img'])
it.img = image_object
it.save()
except ImageObject.DoesNotExist: except ImageObject.DoesNotExist:
pass pass
else:
it.img = image_object
it.save()
elif ctype == 'video': elif ctype == 'video':
if 'id' in cdata and cdata['id']: if 'id' in cdata and cdata['id']:
v = Video.objects.get(id=cdata['id']) v = Video.objects.get(id=cdata.pop('id'))
v.position = cdata['position'] serializer = VideoCreateSerializer(v, data=cdata)
v.title = cdata['title'] serializer.save()
v.course = course # v.position = cdata['position']
v.url = cdata['url'] # v.title = cdata['title']
v.uuid = cdata['uuid'] # v.course = course
v.save() # v.url = cdata['url']
# v.uuid = cdata['uuid']
# v.save()
else: else:
v = Video.objects.create( serializer = VideoCreateSerializer(data=cdata)
position=cdata['position'], serializer.save()
title=cdata['title'], # v = Video.objects.create(
course=course, # position=cdata['position'],
url=cdata['url'], # title=cdata['title'],
uuid=cdata['uuid'], # course=course,
) # url=cdata['url'],
# uuid=cdata['uuid'],
# )
elif ctype == 'images': elif ctype == 'images':
if 'id' in cdata and cdata['id']: if 'id' in cdata and cdata['id']:
g = Gallery.objects.get(id=cdata['id']) g = Gallery.objects.get(id=cdata['id'])
@ -116,6 +149,8 @@ class DispatchContentMixin(object):
g.position = cdata['position'] g.position = cdata['position']
g.title = cdata['title'] g.title = cdata['title']
g.uuid = cdata['uuid'] g.uuid = cdata['uuid']
obj_attr = getattr(g, obj_type)
obj_attr = obj
g.save() g.save()
if 'images' in cdata: if 'images' in cdata:
for image in cdata['images']: for image in cdata['images']:
@ -129,12 +164,14 @@ class DispatchContentMixin(object):
img=ImageObject.objects.get(id=image['img']) img=ImageObject.objects.get(id=image['img'])
) )
else: else:
g = Gallery.objects.create( g = Gallery(
course=course,
position=cdata['position'], position=cdata['position'],
title=cdata['title'], title=cdata['title'],
uuid=cdata['uuid'], uuid=cdata['uuid'],
) )
obj_attr = getattr(g, obj_type)
obj_attr = obj
g.save()
if 'images' in cdata: if 'images' in cdata:
for image in cdata['images']: for image in cdata['images']:
if 'id' in image and image['id']: if 'id' in image and image['id']:

Loading…
Cancel
Save