#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 2013-03-19

@author: Martin H. Bramwell
'''
from OErpModel import OErpModel

OPENERP_MODULE_NAME = 'res.users'

class ResUsers(OErpModel):

    def __init__(self):
        super(ResUsers, self).__init__()
        
        self.methods = {
              'chkTask': self.chkTask
            , 'update': self.update
            , 'load': self.load
        }

    def process(self, wrksht, rowTask):
        super(ResUsers, self).process(wrksht, rowTask)

        pos  = OErpModel.CONSTANTS['MAP_MASKS']

        # print '\n #   #   #   #   #   #   #   #   #   #  '
        
        for idx, aMethod in enumerate(self.methodNames):

            if super(ResUsers, self).todo(idx):
                print '    #{} Doing "{}" now.'.format(idx+1, aMethod)
                super(ResUsers, self).starting(idx)


            	self.methods[aMethod](self.parameters)


                super(ResUsers, self).finished(idx)
                print '__'
            else:
                print '    #{} Skipping "{}"!'.format(idx+1, aMethod)


            ### Temporary block            globals()[aMethod](self.parameters)
            pass

        # print ' #   #   #   #   #   #   #   #   #   #  \n'

    def chkTask(self, parms):
        print 'Task check . . . '
        for key  in parms:
            if key not in ("ResUsers", "login"):
                print "Key : " + key + "  Parm : "
                print OErpModel.parseSpecial(self, parms[key])
    
    
    def update(self, parms):
               
        user_model = OErpModel.openErpConnection.get_model(OPENERP_MODULE_NAME)

        thisUser = user_model.search([("login", "=", parms['login'])])[0]
        nameUser = user_model.read(thisUser, ["name"])["name"]
        print "Login " + parms['login'] + " -- " + nameUser

        user_info = user_model.read(thisUser, ["in_group_4", "in_group_6", "tz", "active"])

        for key  in parms:
            if key not in ("ResUsers", "login"):  
                val = OErpModel.parseSpecial(self, parms[key]) 
                print "What ? " + str(val)
                print 'Writing : (thisUser, "{}":"{}") from "{}"'.format(key, val, parms[key])
                user_model.write(thisUser, {key:val})
                print 'Wrote : (thisUser, {}:{}) from "{}"'.format(key, val, parms[key])

    def load(self, parms):
        print 'Calling parent to load to "{}".'.format(OPENERP_MODULE_NAME)

        data = super(ResUsers, self).load(parms, OPENERP_MODULE_NAME)
#        print 'Done in ResUsers!'

'''
XMLRPC: how to find inactive user?

I'm using OpenERP V7 on Ubuntu 12.04 with Python 2.7.3

For XMLRPC connections I use [openerplib](https://pypi.python.org/pypi/openerp-client-lib/1.0.0)

I deactivated a user with :

    thisUser = user_model.search([("login", "=", "billybob")])[0]
    user_model.write(thisUser, {'active':False})
'''

@@ -1289,270 +1289,8 @@
 %5Cn'%0D
-%0A%0D%0A    def chkTask(self, parms):%0D%0A        print 'Task check . . . '%0D%0A        for key  in parms:%0D%0A            if key not in (%22ResUsers%22, %22login%22):%0D%0A                print %22Key : %22 + key + %22  Parm : %22%0D%0A                print OErpModel.parseSpecial(self, parms%5Bkey%5D)%0D
 %0A   
@@ -2377,358 +2377,266 @@
 %0D%0A%0D%0A
-'''%0D%0AXMLRPC: how to find inactive user?%0D%0A%0D%0AI'm using OpenERP V7 on Ubuntu 12.04 with Python 2.7.3%0D%0A%0D%0AFor XMLRPC connections I use %5Bopenerplib%5D(https://pypi.python.org/pypi/openerp-client-lib/1.0.0)%0D%0A%0D%0AI deactivated a user with :%0D%0A%0D%0A    thisUser = user_model.search(%5B(%22login%22, %22=%22, %22billybob%22)%5D)%5B0%5D%0D%0A    user_model.write(thisUser, %7B'active':False%7D)%0D%0A'''
+%0D%0A    def chkTask(self, parms):%0D%0A        print 'Task check . . . '%0D%0A        for key  in parms:%0D%0A            if key not in (%22ResUsers%22, %22login%22):%0D%0A                print %22Key : %22 + key + %22  Parm : %22%0D%0A                print OErpModel.parseSpecial(self, parms%5Bkey%5D)
 %0D%0A

Update ResUsers.py