Commit d6947f1d by Vik Paruchuri

Proper image submission, error handling

parent bae898ed
...@@ -183,16 +183,16 @@ class @CombinedOpenEnded ...@@ -183,16 +183,16 @@ class @CombinedOpenEnded
files = "" files = ""
if @can_upload_files == true if @can_upload_files == true
files = $('.file-upload-box')[0].files[0] files = $('.file-upload-box')[0].files[0]
if files.size > max_filesize if files != undefined
@can_upload_files = false if files.size > max_filesize
files = "" @can_upload_files = false
files = ""
fd = new FormData() fd = new FormData()
fd.append('student_answer', @answer_area.val()) fd.append('student_answer', @answer_area.val())
fd.append('student_file', files) fd.append('student_file', files)
fd.append('can_upload_files', files) fd.append('can_upload_files', @can_upload_files)
#data = {'student_answer' : @answer_area.val(), 'file_value' : file_value, 'file_id' : file_id}
settings = settings =
type: "POST" type: "POST"
data: fd data: fd
......
...@@ -12,7 +12,8 @@ log=logging.getLogger(__name__) ...@@ -12,7 +12,8 @@ log=logging.getLogger(__name__)
TRUSTED_IMAGE_DOMAINS = [ TRUSTED_IMAGE_DOMAINS = [
'wikipedia.com', 'wikipedia.com',
'wikipedia.net', 'wikipedia.net',
'wikipedia.org' 'wikipedia.org',
'edxuploads.s3.amazonaws.com'
] ]
ALLOWABLE_IMAGE_SUFFIXES = [ ALLOWABLE_IMAGE_SUFFIXES = [
......
...@@ -5,7 +5,7 @@ import json ...@@ -5,7 +5,7 @@ import json
import logging import logging
from lxml import etree from lxml import etree
from lxml.html import rewrite_links from lxml.html import rewrite_links
from lxml.html.clean import Cleaner from lxml.html.clean import Cleaner, autolink_html
from path import path from path import path
import os import os
import sys import sys
...@@ -139,8 +139,9 @@ class OpenEndedChild(object): ...@@ -139,8 +139,9 @@ class OpenEndedChild(object):
@staticmethod @staticmethod
def sanitize_html(answer): def sanitize_html(answer):
try: try:
cleaner = Cleaner(style=True, links=True, add_nofollow=False, page_structure=True, safe_attrs_only=False, allow_tags = ["img", "a"]) cleaner = Cleaner(style=True, links=True, add_nofollow=False, page_structure=True, safe_attrs_only=False, allow_tags = ["img", "a"], host_whitelist = open_ended_image_submission.TRUSTED_IMAGE_DOMAINS)
clean_html = cleaner.clean_html(answer) clean_html = cleaner.clean_html(answer)
autolink_html = autolink_html(clean_html)
clean_html = re.sub(r'</p>$', '', re.sub(r'^<p>', '', clean_html)) clean_html = re.sub(r'</p>$', '', re.sub(r'^<p>', '', clean_html))
except: except:
clean_html = answer clean_html = answer
...@@ -311,12 +312,13 @@ class OpenEndedChild(object): ...@@ -311,12 +312,13 @@ class OpenEndedChild(object):
error=False error=False
image_tag="" image_tag=""
if 'can_upload_files' in get_data: if 'can_upload_files' in get_data:
file = get_data['student_file'][0] if get_data['can_upload_files'] =='true':
success, s3_public_url = self.upload_image_to_s3(file) file = get_data['student_file'][0]
if success: success, s3_public_url = self.upload_image_to_s3(file)
image_tag = self.generate_image_tag_from_url(s3_public_url, file.name) if success:
error = not success image_tag = self.generate_image_tag_from_url(s3_public_url, file.name)
return success, error, image_tag error = not success
return success, error, image_tag
def generate_image_tag_from_url(self, s3_public_url, image_name): def generate_image_tag_from_url(self, s3_public_url, image_name):
image_template = """ image_template = """
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment