This question has been flagged

Summary:

Inheriting Auth_signup and overriding _signup_create_user function in res_users.py to raise a clean signup error if email

I am trying to inherit the _signup_create_user() function defined in the res_users class within the res_users.py file in the auth_signup module.


The inheritence code and function is as below to raise an error that removes the ugly traceback (""duplicate key value violates unique constraint "res_users_login_key" DETAIL: Key (login)=(test@test.com) already exists. "") and adds a more user friendly message, the code added to the function is in bold underneath the assert values lines. This raises an internal server error, and just seems to halt where the the traceback is called and doesn't seem to return from the function. But basically the problem isn't the lines of code because those two lines work if added into that function itself within the base code of auth_signup. So the problem is with the inheritence and I can't figure out why.

So essentially, after that long winded explanation, my question is this, how do you override a function for a model that was defined in a module's class that itself inherited the original model? Is there another import I'm missing perhaps? Should the inherit field contain a longer path to associate my class with auth_signup's module res.user inheritence and not the base res.user model. I can't find any documentation on how to override a function within a class that's already inheriting a model itself.  (If that is indeed the problem here, I can only speculate at the moment for I'm not sure what is going wrong). In the meantime I'll have to just edit auth_signup itself, but I know this is not good practice editing the module itself.

If anyone can help I would be greatly appreciative, or just point me in the right direction if inheriting and overriding functions in python that have already been inherited and overriden in dependent modules.

And perhaps Odoo can just fix the function itself in auth_signup on their next commit?

Code from my file below


from datetime import datetime, timedelta
import random
from urlparse import urljoin
import werkzeug

from openerp.addons.base.ir.ir_mail_server import MailDeliveryException
from openerp.osv import osv, fields
from openerp.tools.misc import DEFAULT_SERVER_DATETIME_FORMAT, ustr
from ast import literal_eval
from openerp.tools.translate import _

class SignupError(Exception):
pass


# Inherit Res User - Auth Signup
#==============================================================
class auth_res_users_inherit(osv.Model):
_inherit = 'res.users'

# Inherit Res User - Auth Signup
#==============================================================
class auth_res_users_inherit(osv.Model):
_inherit = 'res.users'

def _signup_create_user(self, cr, uid, values, context=None):
""" create a new user from the template user """
ir_config_parameter = self.pool.get('ir.config_parameter')
template_user_id = literal_eval(ir_config_parameter.get_param(cr, uid, 'auth_signup.template_user_id', 'False'))
assert template_user_id and self.exists(cr, uid, template_user_id, context=context), 'Signup: invalid template user'

# check that uninvited users may sign up
if 'partner_id' not in values:
if not literal_eval(ir_config_parameter.get_param(cr, uid, 'auth_signup.allow_uninvited', 'False')):
raise SignupError('Signup is not allowed for uninvited users')

assert values.get('login'), "Signup: no login given for new user"
assert values.get('partner_id') or values.get('name'), "Signup: no name or partner given for new user"

        if self.search(cr, uid, [('login','=',values.get('login'))],context=context):
raise SignupError("User with email login '%s' already exists, please use a unique email address." %values.get('login'))

        # create a copy of the template user (attached to a specific partner_id if given)
values['active'] = True
context = dict(context or {}, no_reset_password=True)
try:
with cr.savepoint():
return self.copy(cr, uid, template_user_id, values, context=context)
except Exception, e:
# copy may failed if asked login is not available.
raise SignupError(ustr(e))

auth_res_users_inherit()
Avatar
Discard
Best Answer

You just need to add a dependency with the other module that inherit the original function in your __openerp__.py. That will cause that your function get called first than the other module function. This dependency is done by put the name of the other module in the depends list of your module __openerp__.py 

Another suggestion is that if you just wanna add a validation you could add only that code in your inherit function and then make a call to the super function to call the function that you are overriding

Hope this helps

Avatar
Discard
Author

Hi Alex, thanks for the reply. I already did have the dependency for the auth_signup module in my __openerp__.py, but even if I just inherit that class and copy the function as is without changing it, leaving it as is, then assuming my function will get called first, it still throws an internal server error. I will try play around with calling the super function and see if that works rather, thanks for the suggestion. Will post the code here if I get that right.