Source code for odoo_xmlrpc_twisted.functions.create_resettoken

"""
During password reset procedure set the "reset" fields in the "res.users" table.
"""

import os
import configparser
import secrets
from datetime import datetime, timedelta


# call the function "get_settings_odoo" to get the parameters
# for the logging of the model on the "odoo Webservice API"
try:
    from .get_settings_odoo import get_settings_odoo
except ImportError:
    from get_settings_odoo import get_settings_odoo
result   = get_settings_odoo()
db       = result[0]
uid      = result[1]
password = result[3]
models   = result[4]


# ------- get the user parameter from the Settings File "user.ini" -------

# Name and full path of the "ini" file
filename  = "parameter_user.ini"
path_name = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))  # Go up to repo root
config_path = os.path.join(path_name, 'config')
fullpath  = os.path.join(config_path, filename)

config = configparser.ConfigParser()
config.read(fullpath)

# Value for max duration time in days of the reset_token
reset_token_duration = config.get('create_resettoken', 'reset_token_duration')

    
[docs] def create_resettoken(email): """ Function to set during password reset procedure the fields "x_reset_token", "x_reset_complete" and 'x_reset_date' in the database table 'res.users'. """ result = 'False' if email != '': # find user in the table 'res.users' with the email = email users_id = models.execute_kw(db, uid, password, 'res.users', 'search', [[['email', '=', email], ['x_activate_complete', '=', True], # user must be activated ]]) # generare a random URL-safe text string x_reset_token = secrets.token_urlsafe(None) x_reset_complete = False # set the max duration of the token duration_date = datetime.now() + timedelta(days=int(reset_token_duration)) x_reset_date = duration_date.strftime("%Y-%m-%d %H:%M:%S") # update the user record with the "reset" values for u_id in users_id: models.execute_kw(db,uid, password, 'res.users', 'write', [[u_id], {'x_reset_token': x_reset_token, 'x_reset_complete': x_reset_complete, 'x_reset_date': x_reset_date}]) result = x_reset_token else: pass return(result)