From e56dd3f7086b88e745c15a1c650df2b6ab76f9e7 Mon Sep 17 00:00:00 2001 From: Antonio de la Rosa Date: Tue, 1 Jun 2021 22:56:13 +0200 Subject: [PATCH] Fixes in formsutils --- paramecio2/libraries/formsutils.py | 59 +++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 6 deletions(-) diff --git a/paramecio2/libraries/formsutils.py b/paramecio2/libraries/formsutils.py index 3311792..fbb53d0 100644 --- a/paramecio2/libraries/formsutils.py +++ b/paramecio2/libraries/formsutils.py @@ -96,6 +96,20 @@ class CheckForm(): return post, arr_form def check_form(post, arr_form, sufix_form='_error'): + """Function for make check form, passing errors to extra dict called error_form. Also returns an bool variable setting error. + + Args: + post (dict): Dict composed by a series of values. The keys need to be equal to keys of arr_form dict. + arr_form (dict): Dict composed by a series or forms objects. The keys need to be equal to keys of post dict. + sufix_form (str): Define the sufix of error_form keys + + Returns: + bool: Return False if not errors in checking, if errors return True + dict: A dict containing the errors in form fields. + post: Sanitized values + arr_form: arr_form with errors and values. + + """ error=0 error_form={} @@ -117,7 +131,23 @@ def check_form(post, arr_form, sufix_form='_error'): return error, error_form, post, arr_form def show_form(post, arr_form, t, yes_error=True, pass_values=True, modelform_tpl='forms/modelform.phtml'): - + """Function for generate a html form from a template + + Args: + post (dict): Dict composed by a series of values. The keys need to be equal to keys of arr_form dict. + arr_form (dict): Dict composed by a series or forms objects. The keys need to be equal to keys of post dict. + t (PTemplate): Object used for load template for form + yes_error (bool): Show errors in txt_error form variables. + pass_values (bool): Pass default values or values from post dict to arr_form dict items + modelform_tpl (str): Path for the template that generate the html form. By default is paramecio2/libraries/templates/forms/modelform.phtml + + Returns: + + An html string with the generated form. + + """ + + # Create csrf_token in session generate_csrf() @@ -131,6 +161,13 @@ def show_form(post, arr_form, t, yes_error=True, pass_values=True, modelform_tpl def set_extra_forms_user(user_admin): + """Helper function for add extraforms to UserModel form, not for general use + + Args: + user_admin (UserModel): Instance of UserModel object for modify forms and fields + + """ + user_admin.fields['password'].required=True user_admin.fields['email'].required=True @@ -142,13 +179,17 @@ def set_extra_forms_user(user_admin): user_admin.forms['repeat_password'].label=I18n.lang('common', 'repeat_password', 'Repeat Password') -#Function for initial values for necessary fields. - -def ini_fields(fields): - pass - def csrf_token(token_id='csrf_token'): + """Function for generate a csrf token html hide form using flask sessions + + Args: + token_id (str): Name of the html hide form + + Returns: + Return html input hidden with csrf token saved in session + """ + #s=get_session() if not 'csrf_token' in session: @@ -158,6 +199,12 @@ def csrf_token(token_id='csrf_token'): def generate_csrf(): + """Function for generate a csrf token in a variable + + Returns: + csrf token value + """ + if not 'csrf_token' in session: session['csrf_token']=create_key_encrypt()