Commit ac6f84ff by Vik Paruchuri

Allow url submissions instead of images

parent 99ecc784
......@@ -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):
"""
......
......@@ -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
......
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