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