diff --git a/users_ldap_populate/model/populate_wizard.py b/users_ldap_populate/model/populate_wizard.py index 039244462..513516902 100644 --- a/users_ldap_populate/model/populate_wizard.py +++ b/users_ldap_populate/model/populate_wizard.py @@ -19,24 +19,27 @@ # ############################################################################## -from openerp.osv import orm, fields +from openerp import models, fields, api -class CompanyLDAPPopulateWizard(orm.TransientModel): +class CompanyLDAPPopulateWizard(models.TransientModel): _name = 'res.company.ldap.populate_wizard' _description = 'Populate users from LDAP' - _columns = { - 'name': fields.char('Name', size=16), - 'ldap_id': fields.many2one( - 'res.company.ldap', 'LDAP Configuration'), - 'users_created': fields.integer( - 'Number of users created', readonly=True), - } - def create(self, cr, uid, vals, context=None): - ldap_pool = self.pool.get('res.company.ldap') + name = fields.Char('Name', size=16) + ldap_id = fields.Many2one( + 'res.company.ldap', + 'LDAP Configuration' + ) + users_created = fields.Integer( + 'Number of users created', + readonly=True + ) + + @api.model + @api.returns('self', lambda value: value.id) + def create(self, vals): if 'ldap_id' in vals: - vals['users_created'] = ldap_pool.action_populate( - cr, uid, vals['ldap_id'], context=context) - return super(CompanyLDAPPopulateWizard, self).create( - cr, uid, vals, context=None) + ldap = self.env['res.company.ldap'].browse(vals['ldap_id']) + vals['users_created'] = ldap.action_populate() + return super(CompanyLDAPPopulateWizard, self).create(vals) diff --git a/users_ldap_populate/model/users_ldap.py b/users_ldap_populate/model/users_ldap.py index 8edb5ef37..b2ef3638c 100644 --- a/users_ldap_populate/model/users_ldap.py +++ b/users_ldap_populate/model/users_ldap.py @@ -20,7 +20,9 @@ ############################################################################## import re -from openerp.osv import orm + +from openerp import models, api +from openerp.exceptions import UserError import logging _logger = logging.getLogger(__name__) @@ -31,10 +33,11 @@ except ImportError: _logger.debug('Can not `from ldap.filter import filter_format`.') -class CompanyLDAP(orm.Model): +class CompanyLDAP(models.Model): _inherit = 'res.company.ldap' - def action_populate(self, cr, uid, ids, context=None): + @api.multi + def action_populate(self): """ Prepopulate the user table from one or more LDAP resources. @@ -43,16 +46,13 @@ class CompanyLDAP(orm.Model): Return the number of users created (as far as we can tell). """ - if isinstance(ids, (int, float)): - ids = [ids] - - users_pool = self.pool.get('res.users') - users_no_before = users_pool.search( - cr, uid, [], context=context, count=True) + users_pool = self.env['res.users'] + users_no_before = users_pool.search_count([]) logger = logging.getLogger('orm.ldap') - logger.debug("action_populate called on res.company.ldap ids %s", ids) + logger.debug("action_populate called on res.company.ldap ids %s", + self.ids) - for conf in self.get_ldap_dicts(cr, ids): + for conf in self.get_ldap_dicts(self.cr, self.ids): if not conf['create_user']: continue attribute_match = re.search( @@ -60,33 +60,29 @@ class CompanyLDAP(orm.Model): if attribute_match: login_attr = attribute_match.group(1) else: - raise orm.except_orm( - "No login attribute found", + raise UserError( + "No login attribute found" "Could not extract login attribute from filter %s" % conf['ldap_filter']) ldap_filter = filter_format(conf['ldap_filter'] % '*', ()) for result in self.query(conf, ldap_filter): - self.get_or_create_user( - cr, uid, conf, result[1][login_attr][0], result) + self.get_or_create_user(conf, result[1][login_attr][0], result) - users_no_after = users_pool.search( - cr, uid, [], context=context, count=True) + users_no_after = users_pool.search_count([]) users_created = users_no_after - users_no_before logger.debug("%d users created", users_created) return users_created - def populate_wizard(self, cr, uid, ids, context=None): + @api.multi + def populate_wizard(self): """ GUI wrapper for the populate method that reports back the number of users created. """ - if not ids: + if not self: return - if isinstance(ids, (int, float)): - ids = [ids] - wizard_obj = self.pool.get('res.company.ldap.populate_wizard') - res_id = wizard_obj.create( - cr, uid, {'ldap_id': ids[0]}, context=context) + wizard_obj = self.env['res.company.ldap.populate_wizard'] + res_id = wizard_obj.create({'ldap_id': self.id}).id return { 'name': wizard_obj._description, @@ -94,7 +90,7 @@ class CompanyLDAP(orm.Model): 'view_mode': 'form', 'res_model': wizard_obj._name, 'domain': [], - 'context': context, + 'context': self.env.context, 'type': 'ir.actions.act_window', 'target': 'new', 'res_id': res_id,