[damned-lies] Fixed get_image_size utility
- From: Claude Paroz <claudep src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [damned-lies] Fixed get_image_size utility
- Date: Fri, 24 Feb 2017 18:24:19 +0000 (UTC)
commit 23a670b6309398154fcad60316ecdbe6038e2d8f
Author: Claude Paroz <claude 2xlibre net>
Date: Fri Feb 24 19:23:27 2017 +0100
Fixed get_image_size utility
people/forms.py | 15 +++++++++------
people/tests.py | 15 +++++++++++++++
2 files changed, 24 insertions(+), 6 deletions(-)
---
diff --git a/people/forms.py b/people/forms.py
index d21ca8b..a456932 100644
--- a/people/forms.py
+++ b/people/forms.py
@@ -1,4 +1,6 @@
import hashlib, random
+from http.client import InvalidURL
+from urllib.request import urlopen
from django import forms
from django.conf import settings
@@ -112,19 +114,19 @@ class TeamJoinForm(forms.Form):
def get_image_size(url):
""" Returns width and height (as tuple) of the image poited at by the url
Code partially copied from http://effbot.org/zone/pil-image-size.htm """
- import urllib
+ from urllib.request import urlopen
from PIL import ImageFile
- from httplib import InvalidURL
+ from http.client import InvalidURL
try:
- file = urllib.urlopen(url)
- except (IOError, UnicodeError, InvalidURL, EOFError):
+ im_file = urlopen(url)
+ except (IOError, InvalidURL, EOFError, ValueError):
raise forms.ValidationError(_("The URL you provided is not valid"))
size = None
p = ImageFile.Parser()
try:
while 1:
- data = file.read(1024)
+ data = im_file.read(1024)
if not data:
break
p.feed(data)
@@ -133,7 +135,8 @@ def get_image_size(url):
break
except Exception as e:
raise forms.ValidationError("Sorry, an error occurred while trying to get image size (%s)" % str(e))
- file.close()
+ finally:
+ im_file.close()
if not size:
raise forms.ValidationError(_("The URL you provided seems not to correspond to a valid image"))
return size
diff --git a/people/tests.py b/people/tests.py
index 03ae0d1..cee00cf 100644
--- a/people/tests.py
+++ b/people/tests.py
@@ -2,6 +2,7 @@ import datetime
from unittest import skipUnless
from django.test import TestCase
+from django.core.exceptions import ValidationError
from django.core.urlresolvers import reverse
from django.utils.safestring import SafeData
@@ -138,6 +139,20 @@ class PeopleTestCase(TestCase):
'<img alt="gravatar icon"
src="https://secure.gravatar.com/avatar/618b8b6c1c973c780ec218242c49cbe7.jpg?s=80&d=identicon&r=g" />'
)
+ def test_get_image_size(self):
+ from people.forms import get_image_size
+ self.assertEqual(
+ get_image_size("https://l10n.gnome.org/static/img/nobody.png"),
+ (48, 48)
+ )
+ invalid_urls = (
+ "absolutely invalid",
+ "http://hopefullythisurlshouldnotexist.com/grstzqwer.jpg",
+ )
+ for url in invalid_urls:
+ with self.assertRaises(ValidationError):
+ get_image_size(url)
+
@skipUnless(pyicu_present, "PyICU package is required for this test")
def test_all_languages_list_order(self):
"""
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]