How to notify the user if his password expired in Odoo 10?

65 Views Asked by At

When a user tries to log in he needs to be notified that his password is expired. For the password expiration*,* I used the password_security module, but don't show anything if the password is expired.

models/password.py

class ResUsers(models.Model):
_inherit = 'res.users'
days_left = fields.Integer(compute='_compute_days_left', string='Days Left')

@api.depends('company_id.password_expiration')
def _compute_days_left(self):
    for user in self:
        if user.company_id.password_expiration:

            password_updated_on = datetime.datetime.strptime(user.password_write_date, "%Y-%m-%d %H:%M:%S")
            expiration_date = password_updated_on +datetime.timedelta(days=user.company_id.password_expiration)

            today = datetime.datetime.now()
            if today > expiration_date:
                user.days_left = -1
            else:
                user.days_left = (expiration_date - today).days
        else:
            user.days_left = -1

def __init__(self, pool, cr):
    super(ResUsers, self).__init__(pool, cr)
    type(self).SELF_WRITEABLE_FIELDS = list(self.SELF_WRITEABLE_FIELDS) + ['days_left']

controllers/main.py

class MyController(http.Controller):

@http.route('/web/login', type='http', auth="public", website=True)
def web_login(self, **kw):

     # Fetch the user
    user = request.env['res.users'].sudo().search([('login', '=', kw.get('login'))], limit=1)        
    kw['days_left'] = user.days_left
    # Pass the days_left value to the login template
    return request.render('module_name.templates', kw)

views/templates.xml

<odoo>
    <template id="login_template" inherit_id="web.login" name="Expired Password Message">
        <t t-call="web.login_layout">
            <xpath expr="//div[@class='field-password']" position="after">
                <t t-if="days_left == -1">
                    <div class="alert alert-danger">Your password has expired. Please change it.
                    </div>
                </t>
            </xpath>
        </t>
    </template>
</odoo>

It seems like the template is not getting the data from controller. Any idea?

0

There are 0 best solutions below