Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-platform
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
edx-platform
Commits
929160e1
Commit
929160e1
authored
Jan 29, 2013
by
Vik Paruchuri
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add in ability to validate images and image paths (urls)
parent
5d4ad4b8
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
59 additions
and
1 deletions
+59
-1
common/lib/xmodule/xmodule/open_ended_image_submission.py
+59
-1
No files found.
common/lib/xmodule/xmodule/open_ended_image_submission.py
View file @
929160e1
from
PIL
import
Image
import
urlparse
TRUSTED_IMAGE_DOMAINS
=
[
'wikipedia.com'
,
...
...
@@ -6,6 +7,12 @@ TRUSTED_IMAGE_DOMAINS = [
'wikipedia.org'
]
ALLOWABLE_IMAGE_SUFFIXES
=
[
'jpg'
,
'png'
,
'gif'
]
MAX_IMAGE_DIM
=
150
MAX_COLORS_TO_COUNT
=
16
MAX_COLORS
=
5
...
...
@@ -25,5 +32,56 @@ class ImageProperties(object):
else
:
colors
=
len
(
colors
)
return
colors
<=
MAX_COLORS
def
get_skin_ratio
(
self
):
im
=
self
.
image
skin
=
sum
([
count
for
count
,
rgb
in
im
.
getcolors
(
im
.
size
[
0
]
*
im
.
size
[
1
])
if
rgb
[
0
]
>
60
and
rgb
[
1
]
<
(
rgb
[
0
]
*
0.85
)
and
rgb
[
2
]
<
(
rgb
[
0
]
*
0.7
)
and
rgb
[
1
]
>
(
rgb
[
0
]
*
0.4
)
and
rgb
[
2
]
>
(
rgb
[
0
]
*
0.2
)])
bad_color_val
=
float
(
skin
)
/
float
(
im
.
size
[
0
]
*
im
.
size
[
1
])
if
bad_color_val
>
.
4
:
is_okay
=
False
else
:
is_okay
=
True
return
is_okay
def
run_tests
(
self
):
image_is_okay
=
self
.
count_colors
()
and
self
.
get_skin_ratio
()
return
image_is_okay
class
URLProperties
(
object
):
def
__init__
(
self
,
url_string
):
self
.
url_string
=
url_string
def
check_if_parses
(
self
):
success
=
False
try
:
self
.
parsed_url
=
urlparse
.
urlparse
(
url_string
)
success
=
True
except
:
pass
return
success
def
check_suffix
(
self
):
good_suffix
=
False
for
suffix
in
ALLOWABLE_IMAGE_SUFFIXES
:
if
self
.
url_string
.
endswith
(
suffix
)
good_suffix
=
True
break
return
good_suffix
def
run_tests
(
self
):
url_is_okay
=
self
.
check_suffix
()
and
self
.
check_if_parses
()
return
url_is_okay
def
run_url_tests
(
url_string
):
url_properties
=
URLProperties
(
url_string
)
return
url_properties
.
run_tests
()
def
run_image_tests
(
image
):
image_properties
=
ImageProperties
(
image
)
return
image_properties
.
run_tests
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment