[damned-lies] Add some checks for the size of the hackergotchi image



commit 3db8d5ec19b46ffcea830e7a7e71cded9ff750cb
Author: Claude Paroz <claude 2xlibre net>
Date:   Sat Jun 26 14:34:01 2010 +0200

    Add some checks for the size of the hackergotchi image

 people/forms.py  |   33 +++++++++++++++++++++++++++++++++
 people/models.py |    2 +-
 2 files changed, 34 insertions(+), 1 deletions(-)
---
diff --git a/people/forms.py b/people/forms.py
index e0e9cb9..012c369 100644
--- a/people/forms.py
+++ b/people/forms.py
@@ -77,8 +77,41 @@ class DetailForm(forms.ModelForm):
         model = Person
         fields = ('first_name', 'last_name', 'email', 'image', 'webpage_url', 'irc_nick', 'bugzilla_account')
 
+    def clean_image(self):
+        url = self.cleaned_data['image']
+        if not url:
+            return
+        size = get_image_size(url)
+        if size[0]>100 or size[1]>100:
+            raise forms.ValidationError(_(u"Image too high or too wide (%dx%d, maximum is 100x100 pixels)") % size) 
+        
 class TeamJoinForm(forms.Form):
     def __init__(self, *args, **kwargs):
         super(TeamJoinForm, self).__init__(*args, **kwargs)
         # FIXME: exclude team to which user is already member
         self.fields['teams'] = forms.ModelChoiceField(queryset=Team.objects.all())
+
+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
+    import ImageFile
+
+    try:
+        file = urllib.urlopen(url)
+    except IOError:
+        raise forms.ValidationError(_(u"The URL you provided is not valid"))
+    size = None
+    p = ImageFile.Parser()
+    while 1:
+        data = file.read(1024)
+        if not data:
+            break
+        p.feed(data)
+        if p.image:
+            size = p.image.size
+            break
+    file.close()
+    if not size:
+        raise forms.ValidationError(_(u"The URL you provided seems not to correspond to a valid image"))
+    return size
diff --git a/people/models.py b/people/models.py
index e2eb3f2..8345fa3 100644
--- a/people/models.py
+++ b/people/models.py
@@ -34,7 +34,7 @@ class Person(User):
 
     svn_account = models.SlugField(max_length=20, null=True, blank=True)
     image = models.URLField(_("Image"), null=True, blank=True,
-                            help_text=_("URL to an image file (.jpg, .png, ...) of an hackergotchi"))
+                            help_text=_("URL to an image file (.jpg, .png, ...) of an hackergotchi (max. 100x100 pixels)"))
     webpage_url = models.URLField(_("Web page"), null=True, blank=True)
     irc_nick = models.SlugField(_("IRC nickname"), max_length=20, null=True, blank=True)
     bugzilla_account = models.EmailField(_("Bugzilla account"), null=True, blank=True,



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]