~jah256/openerp/openerp-redmine-connector

« back to all changes in this revision

Viewing changes to res_users.py

  • Committer: Richard Lewis
  • Date: 2013-06-13 15:29:56 UTC
  • Revision ID: richard.lewis@credativ.co.uk-20130613152956-94ennedyfxpkz2wv
[WIP] Added user mapping
A few bits of code tidying

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    OpenERP, Open Source Management Solution
 
5
#    Copyright (C) 2013 credativ (http://www.credativ.co.uk). All Rights Reserved
 
6
#
 
7
#    This program is free software: you can redistribute it and/or modify
 
8
#    it under the terms of the GNU Affero General Public License as
 
9
#    published by the Free Software Foundation, either version 3 of the
 
10
#    License, or (at your option) any later version.
 
11
#
 
12
#    This program is distributed in the hope that it will be useful,
 
13
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
#    GNU Affero General Public License for more details.
 
16
#
 
17
#    You should have received a copy of the GNU Affero General Public License
 
18
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
#
 
20
##############################################################################
 
21
 
 
22
from openerp.osv import osv, fields
 
23
from redmine_osv import Connection
 
24
 
 
25
import logging
 
26
_logger = logging.getLogger(__name__)
 
27
_logger.setLevel(logging.DEBUG)
 
28
 
 
29
class users(osv.osv):
 
30
    '''
 
31
    Add a redmine login name field to res.users.
 
32
    '''
 
33
    _inherit = 'res.users'
 
34
 
 
35
    def _get_rm_user_id(self, cr, uid, ids, field, args, context=None):
 
36
        referential_type_pool = self.pool.get('external.referential.type')
 
37
        type_ids = referential_type_pool.search(cr, uid, [('name','ilike','redmine')], context=context)
 
38
        
 
39
        referential_pool = self.pool.get('external.referential')
 
40
        referential_ids = referential_pool.search(cr, uid, [('type_id','in',type_ids)], context=context)
 
41
 
 
42
        if len(referential_ids) == 1:
 
43
            referential = referential_pool.browse(cr, uid, referential_ids[0], context=context)
 
44
            try:
 
45
                conn = Connection(referential.location, referential.apiusername, referential.apipass)
 
46
                return dict([(user.id, conn.call('Users.find', user.rm_login))
 
47
                             for user in self.browse(cr, uid, ids, context=context)])
 
48
            except Exception, e:
 
49
                _logger.error('Redmine: could not retrieve redmine user ID(s): %s' % (str(e),))
 
50
        elif len(referential_ids) > 0:
 
51
            raise osv.except_osv('Integrity error',
 
52
                                 'Found multiple external referentials of type Redmine')
 
53
        else:
 
54
            raise osv.except_osv('Integrity error',
 
55
                                 'Could not find an external referential of type Redmine')
 
56
 
 
57
    def _update_rm_user_id(self, cr, uid, ids, context=None):
 
58
        return self._get_rm_user_id(cr, uid, ids, field='rm_user_id', args=[], context=context)
 
59
 
 
60
    _columns = {
 
61
        'rm_login': fields.char(
 
62
            'Redmine login',
 
63
            size=64,
 
64
            help='This user\'s login name on Redmine'),
 
65
        # FIXME Seems like access to users.xml is further restricted somehow
 
66
        # 'rm_user_id': fields.function(
 
67
        #     _get_rm_user_id,
 
68
        #     store={'res.users': (_update_rm_user_id, ['rm_login'], 10)},
 
69
        #     type='integer',
 
70
        #     string='Redmine user ID',
 
71
        #     readonly=True),
 
72
        # FIXME For now we'll just required that the user ID is manually entered
 
73
        'rm_user_id': fields.integer(
 
74
            string='Redmine user ID'),
 
75
    }
 
76
 
 
77
    _defaults = {
 
78
        'rm_user_id': -1,
 
79
    }
 
80
 
 
81
users()