[FIX] mail.alias: fix constraint creation warnings + properly hide alias on creation...
authorOlivier Dony <odo@openerp.com>
Tue, 14 Aug 2012 10:51:14 +0000 (12:51 +0200)
committerOlivier Dony <odo@openerp.com>
Tue, 14 Aug 2012 10:51:14 +0000 (12:51 +0200)
bzr revid: odo@openerp.com-20120814105114-ei0qbgy71fyc8jtb

addons/crm/crm.py
addons/hr_recruitment/hr_recruitment.py
addons/mail/mail_group.py
addons/mail/res_users.py
addons/project/project.py

index 891187f..b45b8ea 100644 (file)
@@ -132,9 +132,10 @@ class crm_case_section(osv.osv):
         return ids
 
     _defaults = {
-        'active': lambda *a: 1,
-        'allow_unlink': lambda *a: 1,
-        'stage_ids': _get_stage_common
+        'active': 1,
+        'allow_unlink': 1,
+        'stage_ids': _get_stage_common,
+        'alias_domain': False, # always hide alias during creation
     }
 
     _sql_constraints = [
index 9fe3cbf..89a81a3 100644 (file)
@@ -518,25 +518,41 @@ class hr_job(osv.osv):
                                          "create new applicants for this job position."),
     }
 
-    def init(self, cr):
-        # Installation hook to create aliases for all jobs, right after _auto_init
+    _defaults = {
+        'alias_domain': False, # always hide alias during creation
+    }
+
+    def _auto_init(self, cr, context=None):
+        """Installation hook to create aliases for all jobs and avoid constraint errors."""
+        
+        # disable the unique alias_id not null constraint, to avoid spurious warning during 
+        # super.auto_init. We'll reinstall it afterwards.
+        self._columns['alias_id'].required = False
+
+        super(hr_job,self)._auto_init(cr, context=context)
+        
         registry = RegistryManager.get(cr.dbname)
         mail_alias = registry.get('mail.alias')
-        hr_job = registry.get('hr.job')
-        jobs_no_alias = hr_job.search(cr, SUPERUSER_ID, [('alias_id', '=', False)])
+        hr_jobs = registry.get('hr.job')
+        jobs_no_alias = hr_jobs.search(cr, SUPERUSER_ID, [('alias_id', '=', False)])
         # Use read() not browse(), to avoid prefetching uninitialized inherited fields
-        for job_data in hr_job.read(cr, SUPERUSER_ID, jobs_no_alias, ['name']):
+        for job_data in hr_jobs.read(cr, SUPERUSER_ID, jobs_no_alias, ['name']):
             alias_id = mail_alias.create_unique_alias(cr, SUPERUSER_ID, {'alias_name': 'job+'+job_data['name'],
                                                                          'alias_defaults': {'job_id': job_data['id']}},
                                                       model_name='hr.applicant')
-            hr_job.write(cr, SUPERUSER_ID, job_data['id'], {'alias_id': alias_id})
+            hr_jobs.write(cr, SUPERUSER_ID, job_data['id'], {'alias_id': alias_id})
             _logger.info('Mail alias created for hr.job %s (uid %s)', job_data['name'], job_data['id'])
 
         # Finally attempt to reinstate the missing constraint
         try:
             cr.execute('ALTER TABLE hr_job ALTER COLUMN alias_id SET NOT NULL')
         except Exception:
-            pass
+            _logger.warning("Table '%s': unable to set a NOT NULL constraint on column '%s' !\n"\
+                            "If you want to have it, you should update the records and execute manually:\n"\
+                            "ALTER TABLE %s ALTER COLUMN %s SET NOT NULL",
+                            self._table, 'alias_id', self._table, 'alias_id')
+
+        self._columns['alias_id'].required = True
 
     def create(self, cr, uid, vals, context=None):
         mail_alias = self.pool.get('mail.alias')
index 77169e7..96ece8b 100644 (file)
@@ -135,6 +135,7 @@ class mail_group(osv.osv):
         'public': True,
         'responsible_id': (lambda s, cr, uid, ctx: uid),
         'image': _get_default_image,
+        'alias_domain': False, # always hide alias during creation 
     }
 
     def _subscribe_user_with_group_m2m_command(self, cr, uid, ids, group_ids_command, context=None):
index 30ef2b2..a2ca720 100644 (file)
@@ -54,6 +54,7 @@ class res_users(osv.osv):
     
     _defaults = {
         'notification_email_pref': 'to_me',
+        'alias_domain': False, # always hide alias during creation
     }
 
     def __init__(self, pool, cr):
@@ -67,25 +68,38 @@ class res_users(osv.osv):
         self.SELF_WRITEABLE_FIELDS.append('notification_email_pref')
         return init_res
 
-    def init(self, cr):
-        # Installation hook to create aliases for all users, right after _auto_init
+    def _auto_init(self, cr, context=None):
+        """Installation hook to create aliases for all users and avoid constraint errors."""
+        
+        # disable the unique alias_id not null constraint, to avoid spurious warning during 
+        # super.auto_init. We'll reinstall it afterwards.
+        self._columns['alias_id'].required = False
+
+        super(res_users,self)._auto_init(cr, context=context)
+        
         registry = RegistryManager.get(cr.dbname)
         mail_alias = registry.get('mail.alias')
-        res_users = registry.get('res.users')
-        users_no_alias = res_users.search(cr, SUPERUSER_ID, [('alias_id', '=', False)])
+        res_users_model = registry.get('res.users')
+        users_no_alias = res_users_model.search(cr, SUPERUSER_ID, [('alias_id', '=', False)])
         # Use read() not browse(), to avoid prefetching uninitialized inherited fields
-        for user_data in res_users.read(cr, SUPERUSER_ID, users_no_alias, ['login']):
+        for user_data in res_users_model.read(cr, SUPERUSER_ID, users_no_alias, ['login']):
             alias_id = mail_alias.create_unique_alias(cr, SUPERUSER_ID, {'alias_name': user_data['login'],
                                                                          'alias_force_id': user_data['id']},
                                                       model_name=self._name)
-            res_users.write(cr, SUPERUSER_ID, user_data['id'], {'alias_id': alias_id})
+            res_users_model.write(cr, SUPERUSER_ID, user_data['id'], {'alias_id': alias_id})
             _logger.info('Mail alias created for user %s (uid %s)', user_data['login'], user_data['id'])
 
         # Finally attempt to reinstate the missing constraint
         try:
             cr.execute('ALTER TABLE res_users ALTER COLUMN alias_id SET NOT NULL')
         except Exception:
-            pass
+            _logger.warning("Table '%s': unable to set a NOT NULL constraint on column '%s' !\n"\
+                            "If you want to have it, you should update the records and execute manually:\n"\
+                            "ALTER TABLE %s ALTER COLUMN %s SET NOT NULL",
+                            self._table, 'alias_id', self._table, 'alias_id')
+
+        self._columns['alias_id'].required = True
+
 
     def create(self, cr, uid, data, context=None):
         # create default alias same as the login
index c7038fc..7b848af 100644 (file)
@@ -267,6 +267,7 @@ class project(osv.osv):
         'type_ids': _get_type_common,
         'alias_model': 'project.task',
         'privacy_visibility': 'public',
+        'alias_domain': False, # always hide alias during creation
     }
 
     # TODO: Why not using a SQL contraints ?