[IMP] hr: image is not modified at display when choosing a new one; btu resized when...
[odoo/odoo.git] / addons / hr / hr.py
index a03acc8..a153d63 100644 (file)
 ##############################################################################
 
 import addons
-import io
 import logging
 from osv import fields, osv
-from PIL import Image
-import StringIO
+import tools
+_logger = logging.getLogger(__name__)
 
 class hr_employee_category(osv.osv):
 
@@ -107,7 +106,7 @@ class hr_job(osv.osv):
             },
             multi='no_of_employee'),
         'no_of_recruitment': fields.float('Expected in Recruitment', help='Number of new employees you expect to recruit.'),
-        'employee_ids': fields.one2many('hr.employee', 'job_id', 'Employees'),
+        'employee_ids': fields.one2many('hr.employee', 'job_id', 'Employees', groups='base.group_user'),
         'description': fields.text('Job Description'),
         'requirements': fields.text('Requirements'),
         'department_id': fields.many2one('hr.department', 'Department'),
@@ -148,33 +147,22 @@ class hr_employee(osv.osv):
     _description = "Employee"
     _inherits = {'resource.resource': "resource_id"}
 
-    def onchange_photo(self, cr, uid, ids, value, context=None):
-        if not value:
-            return {'value': {'photo_big': value, 'photo': value} }
-        return {'value': {'photo_big': self._photo_resize(cr, uid, value, 540, 450, context=context), 'photo': self._photo_resize(cr, uid, value, context=context)} }
-    
-    def _set_photo(self, cr, uid, id, name, value, args, context=None):
-        if not value:
-            vals = {'photo_big': value}
-        else:
-            vals = {'photo_big': self._photo_resize(cr, uid, value, 540, 450, context=context)}
-        return self.write(cr, uid, [id], vals, context=context)
-    
-    def _photo_resize(self, cr, uid, photo, heigth=180, width=150, context=None):
-        image_stream = io.BytesIO(photo.decode('base64'))
-        img = Image.open(image_stream)
-        img.thumbnail((heigth, width), Image.ANTIALIAS)
-        img_stream = StringIO.StringIO()
-        img.save(img_stream, "JPEG")
-        return img_stream.getvalue().encode('base64')
-    
-    def _get_photo(self, cr, uid, ids, name, args, context=None):
+    def _get_image(self, cr, uid, ids, name, args, context=None):
         result = dict.fromkeys(ids, False)
-        for hr_empl in self.browse(cr, uid, ids, context=context):
-            if hr_empl.photo_big:
-                result[hr_empl.id] = self._photo_resize(cr, uid, hr_empl.photo_big, context=context)
+        for obj in self.browse(cr, uid, ids, context=context):
+            resized_image_dict = tools.get_resized_images(obj.image)
+            result[obj.id] = {
+                'image_medium': resized_image_dict['image_medium'],
+                'image_small': resized_image_dict['image_small'],
+                }
         return result
     
+    def _set_image(self, cr, uid, id, name, value, args, context=None):
+        return self.write(cr, uid, [id], {'image': tools.resize_image_big(value)}, context=context)
+    
+    def onchange_image(self, cr, uid, ids, value, context=None):
+        return {'value': {'image_medium': tools.resize_image_big(value), 'image_small': tools.resize_image_big(value)}}
+    
     _columns = {
         'country_id': fields.many2one('res.country', 'Nationality'),
         'birthday': fields.date("Date of Birth"),
@@ -199,11 +187,27 @@ class hr_employee(osv.osv):
         'resource_id': fields.many2one('resource.resource', 'Resource', ondelete='cascade', required=True),
         'coach_id': fields.many2one('hr.employee', 'Coach'),
         'job_id': fields.many2one('hr.job', 'Job'),
-        'photo_big': fields.binary('Big-sized employee photo', help="This field holds the photo of the employee. The photo field is used as an interface to access this field. The image is base64 encoded, and PIL-supported. Full-sized photo are however resized to 540x450 px."),
-        'photo': fields.function(_get_photo, fnct_inv=_set_photo, string='Employee photo', type="binary",
+        'image': fields.binary("Photo",
+            help="This field holds the image used as a photo for the "\
+                 "employee. The image is base64 encoded, and PIL-supported. "\
+                 "It is limited to a 1024x1024 px image."),
+        'image_medium': fields.function(_get_image, fnct_inv=_set_image,
+            string="Medium-sized photo", type="binary", multi="_get_image",
+            store = {
+                'hr.employee': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10),
+            },
+            help="Medium-sized photo of the employee. It is automatically "\
+                 "resized as a 180x180 px image, with aspect ratio preserved. "\
+                 "Use this field in form views or some kanban views."),
+        'image_small': fields.function(_get_image, fnct_inv=_set_image,
+            string="Smal-sized photo", type="binary", multi="_get_image",
             store = {
-                'hr.employee': (lambda self, cr, uid, ids, c={}: ids, ['photo_big'], 10),
-            }, help="Image used as photo for the employee. It is automatically resized as a 180x150 px image. A larger photo is stored inside the photo_big field."),
+                'hr.employee': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10),
+            },
+            help="Small-sized photo of the employee. It is automatically "\
+                 "resized as a 50x50 px image, with aspect ratio preserved. "\
+                 "Use this field anywhere a small image is required."),
+        'active': fields.boolean('Active'),
         'passport_id':fields.char('Passport No', size=64),
         'color': fields.integer('Color Index'),
         'city': fields.related('address_id', 'city', type='char', string='City'),
@@ -211,6 +215,16 @@ class hr_employee(osv.osv):
         'last_login': fields.related('user_id', 'date', type='datetime', string='Latest Connection', readonly=1),
     }
 
+    def create(self, cr, uid, data, context=None):
+        employee_id = super(hr_employee, self).create(cr, uid, data, context=context)
+        try:
+            (model, mail_group_id) = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'mail', 'group_all_employees')
+            employee = self.browse(cr, uid, employee_id, context=context)
+            self.pool.get('mail.group').message_append_note(cr, uid, [mail_group_id], body='Welcome to %s! Please help him make its first steps in OpenERP!' % (employee.name), context=context)
+        except:
+            pass # group deleted: do not push a message
+        return employee_id
+
     def unlink(self, cr, uid, ids, context=None):
         resource_obj = self.pool.get('resource.resource')
         resource_ids = []
@@ -249,13 +263,13 @@ class hr_employee(osv.osv):
             work_email = self.pool.get('res.users').browse(cr, uid, user_id, context=context).user_email
         return {'value': {'work_email' : work_email}}
 
-    def _get_photo(self, cr, uid, context=None):
-        photo_path = addons.get_module_resource('hr','images','photo.png')
-        return self._photo_resize(cr, uid, open(photo_path, 'rb').read().encode('base64'), context=context)
+    def _get_default_image(self, cr, uid, context=None):
+        image_path = addons.get_module_resource('hr', 'static/src/img', 'default_image.png')
+        return tools.resize_image_big(open(image_path, 'rb').read().encode('base64'))
 
     _defaults = {
         'active': 1,
-        'photo': _get_photo,
+        'image': _get_default_image,
         'marital': 'single',
         'color': 0,
     }
@@ -304,10 +318,14 @@ class res_users(osv.osv):
                                             'user_id': user_id}, context=context)
             except:
                 # Tolerate a missing shortcut. See product/product.py for similar code.
-                logging.getLogger('orm').debug('Skipped meetings shortcut for user "%s"', data.get('name','<new'))
+                _logger.debug('Skipped meetings shortcut for user "%s"', data.get('name','<new'))
 
         return user_id
 
+    _columns = {
+        'employee_ids': fields.one2many('hr.employee', 'user_id', 'Related employees'),
+        }
+
 res_users()