Skip to content

Commit

Permalink
move hooks back to hooks.py
Browse files Browse the repository at this point in the history
  • Loading branch information
El-khamisi committed Jan 23, 2025
1 parent 13f74eb commit 15afd7b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 36 deletions.
34 changes: 2 additions & 32 deletions auth_user_case_insensitive/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,6 @@
# Copyright 2021 Open Source Integrators
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).

from odoo import _
from odoo.exceptions import ValidationError
from . import models


def pre_init_hook_login_check(env):
"""This hook will look to see if any conflicting logins exist before
the module is installed
:param openerp.sql_db.Cursor cr:
Database cursor.
"""
with env.cr.savepoint():
users = []
env.cr.execute("SELECT login FROM res_users")
for user in env.cr.fetchall():
login = user[0].lower()
if login not in users:
users.append(login)
else:
raise ValidationError(
_("Conflicting user logins exist for `%s`", login)
)


def post_init_hook_login_convert(env):
"""After the module is installed, set all logins to lowercase
:param openerp.sql_db.Cursor cr:
Database cursor.
:param openerp.modules.registry.RegistryManager registry:
Database registry, using v7 api.
"""
with env.cr.savepoint():
env.cr.execute("UPDATE res_users SET login=lower(login)")
from .hooks import pre_init_hook_login_check
from .hooks import post_init_hook_login_convert
34 changes: 34 additions & 0 deletions auth_user_case_insensitive/hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright 2017 LasLabs Inc.
# Copyright 2021 Open Source Integrators
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

from odoo import _
from odoo.exceptions import ValidationError


def pre_init_hook_login_check(env):
"""This hook will look to see if any conflicting logins exist before
the module is installed
:param env:
Environment.
"""
with env.cr.savepoint():
users = []
env.cr.execute("SELECT login FROM res_users")
for user in env.cr.fetchall():
login = user[0].lower()
if login not in users:
users.append(login)
else:
raise ValidationError(

Check warning on line 23 in auth_user_case_insensitive/hooks.py

View check run for this annotation

Codecov / codecov/patch

auth_user_case_insensitive/hooks.py#L23

Added line #L23 was not covered by tests
_("Conflicting user logins exist for `%s`", login)
)


def post_init_hook_login_convert(env):
"""After the module is installed, set all logins to lowercase
:param env:
Environment.
"""
with env.cr.savepoint():
env.cr.execute("UPDATE res_users SET login=lower(login)")
6 changes: 2 additions & 4 deletions auth_user_case_insensitive/models/res_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ class ResUsers(models.Model):
def _login(cls, db, credential, user_agent_env):
"""Overload _login to lowercase the `login` before passing to the
super."""
if credential.get("type") and credential["type"] == "password":
credential["login"] = credential["login"].lower()

return super()._login(db, credential, user_agent_env)
credential["login"] = credential["login"].lower()
return super()._login(db, credential, user_agent_env=user_agent_env)

@api.model_create_multi
def create(self, vals_list):
Expand Down

0 comments on commit 15afd7b

Please sign in to comment.