Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions xblocks_contrib/video/ajax_handler_mixin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# NOTE: Code has been copied from the following source file
# https://github.com/openedx/edx-platform/blob/master/xmodule/x_module.py#L739

class XModuleToXBlockMixin:
"""
Common code needed by XModule and XBlocks converted from XModules.
"""
@property
def ajax_url(self):
"""
Returns the URL for the ajax handler.
"""
return self.runtime.handler_url(self, 'xmodule_handler', '', '').rstrip('/?')

@XBlock.handler
def xmodule_handler(self, request, suffix=None):
"""
XBlock handler that wraps `handle_ajax`
"""
class FileObjForWebobFiles:
"""
Turn Webob cgi.FieldStorage uploaded files into pure file objects.

Webob represents uploaded files as cgi.FieldStorage objects, which
have a .file attribute. We wrap the FieldStorage object, delegating
attribute access to the .file attribute. But the files have no
name, so we carry the FieldStorage .filename attribute as the .name.

"""
def __init__(self, webob_file):
self.file = webob_file.file
self.name = webob_file.filename

def __getattr__(self, name):
return getattr(self.file, name)

# WebOb requests have multiple entries for uploaded files. handle_ajax
# expects a single entry as a list.
request_post = MultiDict(request.POST)
for key in set(request.POST.keys()):
if hasattr(request.POST[key], "file"):
request_post[key] = list(map(FileObjForWebobFiles, request.POST.getall(key)))

response_data = self.handle_ajax(suffix, request_post)
return Response(response_data, content_type='application/json', charset='UTF-8')
Loading
Loading