Source code for odoo_xmlrpc_twisted.functions.create_activatetoken

"""
During the activation of a user
set the "activate" 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 parameter from the file "parameter_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 activate_token
activate_token_duration = config.get('create_activatetoken', 'activate_token_duration')

    
[docs] def create_activatetoken(login): """ Function to set during user activation the fields "x_activate_token" and 'x_activate_date' in the database table 'res.users'. """ result = 'False' if login != '': # find user in the table 'res.users' with the login = login users_id = models.execute_kw(db, uid, password, 'res.users', 'search', [[['login', '=', login], ['x_activate_complete', '=', False],]]) # generate a random URL-safe text string x_activate_token = secrets.token_urlsafe(None) # set the max duration of the token duration_date = datetime.now() + timedelta(days=int(activate_token_duration)) x_activate_date = duration_date.strftime("%Y-%m-%d %H:%M:%S") # update the user record with the "activate" values for u_id in users_id: models.execute_kw(db,uid, password, 'res.users', 'write', [[u_id], {'x_activate_token': x_activate_token, 'x_activate_date': x_activate_date}]) result = x_activate_token else: pass return(result)