From ac6f84ff2d9145988631d7d3f4d1f054e617fbd7 Mon Sep 17 00:00:00 2001 From: Vik Paruchuri Date: Wed, 30 Jan 2013 15:17:47 -0500 Subject: [PATCH] Allow url submissions instead of images --- .../xmodule/open_ended_image_submission.py | 20 +++++++++++++----- common/lib/xmodule/xmodule/openendedchild.py | 21 ++++++++++++++++++- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/common/lib/xmodule/xmodule/open_ended_image_submission.py b/common/lib/xmodule/xmodule/open_ended_image_submission.py index 7ccd85d281..2f6906faf9 100644 --- a/common/lib/xmodule/xmodule/open_ended_image_submission.py +++ b/common/lib/xmodule/xmodule/open_ended_image_submission.py @@ -11,15 +11,15 @@ from boto.s3.key import Key from django.conf import settings import pickle import logging +import re log = logging.getLogger(__name__) #Domains where any image linked to can be trusted to have acceptable content. TRUSTED_IMAGE_DOMAINS = [ - 'wikipedia.com', - 'wikipedia.net', - 'wikipedia.org', - 'edxuploads.s3.amazonaws.com' + 'wikipedia', + 'edxuploads.s3.amazonaws.com', + 'wikimedia', ] #Suffixes that are allowed in image urls @@ -156,9 +156,19 @@ class URLProperties(object): Runs all available url tests @return: True if URL passes tests, false if not. """ - url_is_okay = self.check_suffix() and self.check_if_parses() + url_is_okay = self.check_suffix() and self.check_if_parses() and self.check_domain() return url_is_okay + def check_domain(self): + """ + Checks to see if url is from a trusted domain + """ + success = False + for domain in TRUSTED_IMAGE_DOMAINS: + if domain in self.url_string: + success = True + return success + return success def run_url_tests(url_string): """ diff --git a/common/lib/xmodule/xmodule/openendedchild.py b/common/lib/xmodule/xmodule/openendedchild.py index 2b9d1e17b1..68c38f494b 100644 --- a/common/lib/xmodule/xmodule/openendedchild.py +++ b/common/lib/xmodule/xmodule/openendedchild.py @@ -329,14 +329,33 @@ class OpenEndedChild(object): return image_template def append_image_to_student_answer(self, get_data): + overall_success = False if not self.accept_file_upload: return True, get_data success, has_file_to_upload, image_tag = self.check_for_image_and_upload(get_data) if success and has_file_to_upload: get_data['student_answer'] += image_tag + overall_success = (success and has_file_to_upload) + else: + success, get_data['student_answer'] = self.check_for_url_in_text(get_data['student_answer']) + overall_success = success + + return success, get_data + + def check_for_url_in_text(self, string): + success = False + links = re.findall(r'(https?://\S+)', string) + if len(links)>0: + for link in links: + success = open_ended_image_submission.run_url_tests(link) + if not success: + string = re.sub(link, '', string) + else: + success = True + + return success, string - return (success and has_file_to_upload), get_data