Get image upload, check for issues, upload to S3
This commit is contained in:
@@ -18,6 +18,7 @@ ALLOWABLE_IMAGE_SUFFIXES = [
|
||||
'gif'
|
||||
]
|
||||
|
||||
MAX_ALLOWED_IMAGE_DIM = 400
|
||||
MAX_IMAGE_DIM = 150
|
||||
MAX_COLORS_TO_COUNT = 16
|
||||
MAX_COLORS = 5
|
||||
@@ -26,6 +27,9 @@ class ImageProperties(object):
|
||||
def __init__(self, image):
|
||||
self.image = image
|
||||
image_size = self.image.size
|
||||
self.image_too_large = False
|
||||
if image_size[0]> MAX_ALLOWED_IMAGE_DIM or image_size[1] > MAX_ALLOWED_IMAGE_DIM:
|
||||
self.image_too_large = True
|
||||
if image_size[0]> MAX_IMAGE_DIM or image_size[1] > MAX_IMAGE_DIM:
|
||||
self.image = self.image.resize((MAX_IMAGE_DIM, MAX_IMAGE_DIM))
|
||||
self.image_size = self.image.size
|
||||
@@ -50,7 +54,7 @@ class ImageProperties(object):
|
||||
return is_okay
|
||||
|
||||
def run_tests(self):
|
||||
image_is_okay = self.count_colors() and self.get_skin_ratio()
|
||||
image_is_okay = self.count_colors() and self.get_skin_ratio() and not self.image_too_large
|
||||
return image_is_okay
|
||||
|
||||
class URLProperties(object):
|
||||
|
||||
@@ -553,6 +553,7 @@ class OpenEndedModule(openendedchild.OpenEndedChild):
|
||||
return self.out_of_sync_error(get)
|
||||
|
||||
# add new history element with answer and empty score and hint.
|
||||
get = self.append_image_to_student_answer(get)
|
||||
self.new_history_entry(get['student_answer'])
|
||||
get['student_answer'] = OpenEndedModule.sanitize_html(get['student_answer'])
|
||||
self.send_to_grader(get['student_answer'], system)
|
||||
|
||||
@@ -283,25 +283,51 @@ class OpenEndedChild(object):
|
||||
@return:
|
||||
"""
|
||||
success = False
|
||||
image = Image.open(image_data)
|
||||
s3_public_url = ""
|
||||
|
||||
try:
|
||||
image = Image.open(image_data)
|
||||
image_ok = open_ended_image_submission.run_image_tests(image)
|
||||
success = True
|
||||
except:
|
||||
pass
|
||||
log.exception("Could not create image and check it.")
|
||||
|
||||
if success:
|
||||
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)
|
||||
success = True
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
success = False
|
||||
log.exception("Could not upload image to S3.")
|
||||
|
||||
log.debug(s3_public_url)
|
||||
return success, s3_public_url
|
||||
|
||||
def check_for_image_and_upload(self, get_data):
|
||||
success = False
|
||||
error=False
|
||||
image_tag=""
|
||||
if 'can_upload_files' in get_data:
|
||||
file = get_data['student_file']
|
||||
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)
|
||||
error = not success
|
||||
return success, error, image_tag
|
||||
|
||||
def generate_image_tag_from_url(self, s3_public_url, image_name):
|
||||
image_template = """
|
||||
<img src="{0}">{1}</img>
|
||||
""".format(s3_public_url, image_name)
|
||||
return image_template
|
||||
|
||||
def append_image_to_student_answer(self, get_data):
|
||||
success, error, image_tag = self.check_for_image_and_upload(get_data)
|
||||
if success and not error:
|
||||
get_data['student_answer'] += image_tag
|
||||
|
||||
return get_data
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -203,6 +203,7 @@ class SelfAssessmentModule(openendedchild.OpenEndedChild):
|
||||
return self.out_of_sync_error(get)
|
||||
|
||||
# add new history element with answer and empty score and hint.
|
||||
get = self.append_image_to_student_answer(get)
|
||||
self.new_history_entry(get['student_answer'])
|
||||
self.change_state(self.ASSESSING)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user