From bae898edb02b31634b42b1cc0e6b4e4fdb656146 Mon Sep 17 00:00:00 2001
From: Vik Paruchuri
Date: Wed, 30 Jan 2013 10:47:50 -0500
Subject: [PATCH] Auto convert image to link, display inside student answer
---
.../xmodule/open_ended_image_submission.py | 22 ++++++++++++++++---
common/lib/xmodule/xmodule/openendedchild.py | 14 +++++++-----
lms/templates/self_assessment_prompt.html | 2 +-
3 files changed, 28 insertions(+), 10 deletions(-)
diff --git a/common/lib/xmodule/xmodule/open_ended_image_submission.py b/common/lib/xmodule/xmodule/open_ended_image_submission.py
index 39e669a357..9bafa5209d 100644
--- a/common/lib/xmodule/xmodule/open_ended_image_submission.py
+++ b/common/lib/xmodule/xmodule/open_ended_image_submission.py
@@ -5,6 +5,9 @@ from boto.s3.connection import S3Connection
from boto.s3.key import Key
from django.conf import settings
import pickle
+import logging
+
+log=logging.getLogger(__name__)
TRUSTED_IMAGE_DOMAINS = [
'wikipedia.com',
@@ -18,10 +21,10 @@ ALLOWABLE_IMAGE_SUFFIXES = [
'gif'
]
-MAX_ALLOWED_IMAGE_DIM = 400
+MAX_ALLOWED_IMAGE_DIM = 1500
MAX_IMAGE_DIM = 150
MAX_COLORS_TO_COUNT = 16
-MAX_COLORS = 5
+MAX_COLORS = 20
class ImageProperties(object):
def __init__(self, image):
@@ -41,7 +44,9 @@ class ImageProperties(object):
else:
colors = len(colors)
- return colors <= MAX_COLORS
+ too_many_colors = (colors <= MAX_COLORS)
+ log.debug("Too many colors: {0}".format(too_many_colors))
+ return too_many_colors
def get_skin_ratio(self):
im = self.image
@@ -51,10 +56,13 @@ class ImageProperties(object):
is_okay = False
else:
is_okay = True
+ log.debug("Skin ratio okay: {0}".format(is_okay))
return is_okay
def run_tests(self):
image_is_okay = self.count_colors() and self.get_skin_ratio() and not self.image_too_large
+ log.debug("Image too large: {0}".format(self.image_too_large))
+ log.debug("Image Okay: {0}".format(image_is_okay))
return image_is_okay
class URLProperties(object):
@@ -98,6 +106,10 @@ def upload_to_s3(file_to_upload, keyname):
Returns:
public_url: URL to access uploaded file
'''
+ #im = Image.open(file_to_upload)
+ #out_im = cStringIO.StringIO()
+ #im.save(out_im, 'PNG')
+
try:
conn = S3Connection(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY)
bucketname = str(settings.AWS_STORAGE_BUCKET_NAME)
@@ -107,6 +119,10 @@ def upload_to_s3(file_to_upload, keyname):
k.key = keyname
k.set_metadata('filename', file_to_upload.name)
k.set_contents_from_file(file_to_upload)
+ #k.set_contents_from_string(out_im.getvalue())
+ #k.set_metadata("Content-Type", 'images/png')
+
+ k.set_acl("public-read")
public_url = k.generate_url(60*60*24*365) # URL timeout in seconds.
return True, public_url
diff --git a/common/lib/xmodule/xmodule/openendedchild.py b/common/lib/xmodule/xmodule/openendedchild.py
index a04a2efa87..df4a492517 100644
--- a/common/lib/xmodule/xmodule/openendedchild.py
+++ b/common/lib/xmodule/xmodule/openendedchild.py
@@ -139,7 +139,7 @@ class OpenEndedChild(object):
@staticmethod
def sanitize_html(answer):
try:
- cleaner = Cleaner(style=True, links=True, add_nofollow=True, page_structure=True, safe_attrs_only=True)
+ cleaner = Cleaner(style=True, links=True, add_nofollow=False, page_structure=True, safe_attrs_only=False, allow_tags = ["img", "a"])
clean_html = cleaner.clean_html(answer)
clean_html = re.sub(r'
$', '', re.sub(r'^', '', clean_html))
except:
@@ -284,19 +284,21 @@ class OpenEndedChild(object):
"""
success = False
s3_public_url = ""
-
+ image_data.seek(0)
try:
image = Image.open(image_data)
image_ok = open_ended_image_submission.run_image_tests(image)
+ log.debug("Image ok: {0}".format(image_ok))
success = True
except:
log.exception("Could not create image and check it.")
- if success:
+ if success and image_ok:
image_key = image_data.name + datetime.now().strftime("%Y%m%d%H%M%S")
try:
- success, public_url = open_ended_image_submission.upload_to_s3(image_data, image_key)
+ image_data.seek(0)
+ success, s3_public_url = open_ended_image_submission.upload_to_s3(image_data, image_key)
except:
success = False
log.exception("Could not upload image to S3.")
@@ -309,7 +311,7 @@ class OpenEndedChild(object):
error=False
image_tag=""
if 'can_upload_files' in get_data:
- file = get_data['student_file']
+ file = get_data['student_file'][0]
success, s3_public_url = self.upload_image_to_s3(file)
if success:
image_tag = self.generate_image_tag_from_url(s3_public_url, file.name)
@@ -318,7 +320,7 @@ class OpenEndedChild(object):
def generate_image_tag_from_url(self, s3_public_url, image_name):
image_template = """
-
{1}
+ {1}
""".format(s3_public_url, image_name)
return image_template
diff --git a/lms/templates/self_assessment_prompt.html b/lms/templates/self_assessment_prompt.html
index 50f5374750..89eeb472d1 100644
--- a/lms/templates/self_assessment_prompt.html
+++ b/lms/templates/self_assessment_prompt.html
@@ -6,7 +6,7 @@
-
+