[FIX] mail.alias: fix constraint creation warnings + properly hide alias on creation...
[odoo/odoo.git] / addons / crm / crm.py
index ca4ef46..b45b8ea 100644 (file)
@@ -21,6 +21,7 @@
 
 import base64
 import time
+from lxml import etree
 from osv import fields
 from osv import osv
 import tools
@@ -111,7 +112,7 @@ class crm_case_section(osv.osv):
         'active': fields.boolean('Active', help="If the active field is set to "\
                         "true, it will allow you to hide the sales team without removing it."),
         'allow_unlink': fields.boolean('Allow Delete', help="Allows to delete non draft cases"),
-        'change_responsible': fields.boolean('Reassign Escalated', help="When escalating to this team override the saleman with the team leader."),
+        'change_responsible': fields.boolean('Reassign Escalated', help="When escalating to this team override the salesman with the team leader."),
         'user_id': fields.many2one('res.users', 'Team Leader'),
         'member_ids':fields.many2many('res.users', 'sale_member_rel', 'section_id', 'member_id', 'Team Members'),
         'reply_to': fields.char('Reply-To', size=64, help="The email address put in the 'Reply-To' of all emails sent by OpenERP about cases in this sales team"),
@@ -121,7 +122,9 @@ class crm_case_section(osv.osv):
         'note': fields.text('Description'),
         'working_hours': fields.float('Working Hours', digits=(16,2 )),
         'stage_ids': fields.many2many('crm.case.stage', 'section_stage_rel', 'section_id', 'stage_id', 'Stages'),
-        'alias_id': fields.many2one('mail.alias', 'Mail Alias', ondelete="cascade", required=True),
+        'alias_id': fields.many2one('mail.alias', 'Alias', ondelete="cascade", required=True, 
+                                    help="The email address associated with this team. New emails received will automatically "
+                                         "create new leads assigned to the team."),
     }
     
     def _get_stage_common(self, cr, uid, context):
@@ -129,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 = [
@@ -157,21 +161,27 @@ class crm_case_section(osv.osv):
                 name = record['parent_id'][1] + ' / ' + name
             res.append((record['id'], name))
         return res
-        
+    
     def create(self, cr, uid, vals, context=None):
-        model_pool = self.pool.get('ir.model.data')
-        alias_pool = self.pool.get('mail.alias')
+        mail_alias = self.pool.get('mail.alias')
         if not vals.get('alias_id'):
-            model, res_id = model_pool.get_object_reference( cr, uid, "crm", "model_crm_lead")
-            vals.update({'alias_name': "sales",
-                         'alias_model_id': res_id})
-            alias_pool.create_unique_alias(cr, uid, vals, context=context)
-            res = super(crm_case_section, self).create(cr, uid, vals, context)
-            record = self.read(cr, uid, res, context)
-            alias_pool.write(cr, uid, [record['alias_id']],{'alias_defaults':{'section_id':res,'type':'lead'}},context)
-            return res
-        return super(crm_case_section, self).create(cr, uid, vals, context)
-
+            vals.pop('alias_name', None) # prevent errors during copy()
+            alias_id = mail_alias.create_unique_alias(cr, uid, 
+                    {'alias_name': vals['name']},
+                    model_name="crm.lead",
+                    context=context)
+            vals['alias_id'] = alias_id
+        res = super(crm_case_section, self).create(cr, uid, vals, context)
+        mail_alias.write(cr, uid, [vals['alias_id']], {'alias_defaults': {'section_id': res, 'type':'lead'}}, context)
+        return res
+        
+    def unlink(self, cr, uid, ids, context=None):
+        # Cascade-delete mail aliases as well, as they should not exist without the sales team.
+        mail_alias = self.pool.get('mail.alias')
+        alias_ids = [team.alias_id.id for team in self.browse(cr, uid, ids, context=context) if team.alias_id ]
+        res = super(crm_case_section, self).unlink(cr, uid, ids, context=context)
+        mail_alias.unlink(cr, uid, alias_ids, context=context)
+        return res
 
 class crm_case_categ(osv.osv):
     """ Category of Case """
@@ -211,4 +221,13 @@ def _links_get(self, cr, uid, context=None):
     res = obj.read(cr, uid, ids, ['object', 'name'], context)
     return [(r['object'], r['name']) for r in res]
 
+class crm_payment_mode(osv.osv):
+    """ Payment Mode for Fund """
+    _name = "crm.payment.mode"
+    _description = "CRM Payment Mode"
+    _columns = {
+        'name': fields.char('Name', size=64, required=True),
+        'section_id': fields.many2one('crm.case.section', 'Sales Team'),
+    }
+
 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: