Commits

Christian Scholz committed 1d6656b

added support for profile pictures in edit
Missing: create pic on create user

Comments (0)

Files changed (1)

adhocracy/controllers/user.py

 import urllib
+import hashlib
 from datetime import datetime
 
 from pylons.i18n import _
+from pylons import config
 from babel import Locale
 
 import quantumcore.contenttypes.registry
 import adhocracy.i18n as i18n
 import adhocracy.lib.util as libutil
 import adhocracy.lib.mail as libmail
+import adhocracy.lib.images as images
 
 
 log = logging.getLogger(__name__)
     chained_validators = [validators.FieldsMatch(
          'password', 'password_confirm')]
 
-class ImageValidator(formencode.FancyValidator):                                                                                                                                                
+class ImageValidator(formencode.FancyValidator):
+    """a validator for checking the correct file size and
+    file type of image uploads. Use it like this::
+
+    f = ImageValidator(max_size=1024, allowed_types=['image/png', 'image/jpeg', 'image/jpg', 'image/x-png'])
+
+    The ``max_size`` is given in KB.
+    """
 
     messages = {
         'too_big': 'The file exceeds the maximum file size of %(max_size)i KB ',
         }
     
     def validate_python(self, value, state):
-        # check file size
         if len(value.value)/1024>self.max_size:
             raise formencode.Invalid(self.message("too_big", state,
                                        max_size=self.max_size),
                           value, state)
-        # check file type
         mr = quantumcore.contenttypes.registry.MIMETypesRegistry()
         content_type = mr.classify(value.filename, value.value, default="application/octet-stream")
         if content_type not in self.allowed_types:
     @ActionProtector(has_permission("user.edit"))
     @validate(schema=UserUpdateForm(), form="edit", post_only=True)
     def update(self, id):
+        
         c.page_user = get_entity_or_abort(model.User, id, instance_filter=False)
         require.user.edit(c.page_user)
-        if request.POST.get("profilepic",u'')!=u'':
-            f=request.POST["profilepic"]
-            fs = quantumcore.storages.FileSystemStorage("/tmp")
         if self.form_result.get("password_change"):
             c.page_user.password = self.form_result.get("password_change")
         c.page_user.display_name = self.form_result.get("display_name")
         locale = Locale(self.form_result.get("locale"))
         if locale and locale in i18n.LOCALES:
             c.page_user.locale = locale 
+
+        if request.POST.get("profilepic",u'')!=u'':
+            f=request.POST["profilepic"]
+            fs_location = config.get('adhocracy.profile_pic.dir', '/tmp/')
+            fs = quantumcore.storages.FilesystemStore(fs_location, "")
+
+            # create the image sizes
+            filename=unicode(hashlib.new(u"md5",unicode(c.page_user.id)).hexdigest())
+            image_sizer = images.ImageSizer(fs)
+            image_sizer.process(f.value, filename)
+
         model.meta.Session.add(c.page_user)
         model.meta.Session.commit()
         if email_changed: