def info(id_role):
    user = TRoles.get_one(id_role)
    organisme = Bib_Organismes.get_one(user['id_organisme'])
    groups = TRoles.get_user_groups(id_role)
    lists = TRoles.get_user_lists(id_role)
    rights = TRoles.get_user_app_profils(id_role)
    return render_template("info_user.html",
                           user=user,
                           organisme=organisme,
                           groups=groups,
                           lists=lists,
                           rights=rights,
                           pathU=config.URL_APPLICATION + '/user/update/')
def addorupdate(id_role=None):
    """
    Route affichant un formulaire vierge ou non (selon l'url) pour ajouter ou mettre à jour un utilisateurs
    L'envoie du formulaire permet l'ajout ou la mise à jour de l'utilisateur dans la base
    Retourne un template accompagné du formulaire pré-rempli ou non selon le paramètre id_role
    Une fois le formulaire validé on retourne une redirection vers la liste des utilisateurs
    """
    form = t_rolesforms.Utilisateur()
    form.id_organisme.choices = Bib_Organismes.choixSelect(
        'id_organisme', 'nom_organisme')
    form.a_groupe.choices = TRoles.choix_group('id_role',
                                               'nom_role',
                                               aucun=None)

    if id_role is not None:
        user = TRoles.get_one(id_role, as_model=True)
        user_as_dict = user.as_dict_full_name()
        # format group to prepfil the form
        formated_groups = [
            group.id_role for group in TRoles.get_user_groups(id_role)
        ]
        if request.method == 'GET':
            form = process(form, user_as_dict, formated_groups)

    if request.method == 'POST':
        if form.validate_on_submit() and form.validate():
            groups = form.data['a_groupe']
            form_user = pops(form.data)
            form_user['groupe'] = False
            form_user.pop('id_role')

            # if a password is set
            # check they are the same
            if form.pass_plus.data:
                try:
                    (form_user['pass_plus'],
                     form_user['pass_md5']) = TRoles.set_password(
                         form.pass_plus.data, form.mdpconf.data)
                except Exception as exp:
                    flash(str(exp), 'error')
                    return render_template('user.html',
                                           form=form,
                                           title="Formulaire Utilisateur")

            if id_role is not None:
                #HACK a l'update on remet a la main les mdp
                # car on les masque dans le form
                form_user['pass_plus'] = user.pass_plus
                form_user['pass_md5'] = user.pass_md5
                form_user['id_role'] = user.id_role
                new_role = TRoles.update(form_user)
            else:
                new_role = TRoles.post(form_user)
            # set groups
            if len(groups) > 0:
                if id_role:
                    #first delete all groups of the user
                    cor_role_to_delete = CorRoles.get_all(params=[{
                        'col':
                        'id_role_utilisateur',
                        'filter':
                        id_role
                    }],
                                                          as_model=True)
                    print(cor_role_to_delete)
                    for cor_role in cor_role_to_delete:
                        db.session.delete(cor_role)
                    db.session.commit()
                for group in groups:
                    # add new groups
                    new_group = CorRoles(id_role_groupe=group,
                                         id_role_utilisateur=new_role.id_role)
                    db.session.add(new_group)
                db.session.commit()
            return redirect(url_for('user.users'))

        else:
            flash(strigify_dict(form.errors), 'error')
    return render_template('user.html',
                           form=form,
                           title="Formulaire Utilisateur",
                           id_role=id_role)
Example #3
0
def addorupdate(id_role=None):
    """
    Route affichant un formulaire vierge ou non (selon l'url) pour ajouter ou mettre à jour un utilisateurs
    L'envoie du formulaire permet l'ajout ou la mise à jour de l'utilisateur dans la base
    Retourne un template accompagné du formulaire pré-rempli ou non selon le paramètre id_role
    Une fois le formulaire validé on retourne une redirection vers la liste des utilisateurs
    """
    form = t_rolesforms.Utilisateur()
    form.id_organisme.choices = Bib_Organismes.choixSelect(
        "id_organisme", "nom_organisme", order_by="nom_organisme")
    form.a_groupe.choices = TRoles.choix_group("id_role",
                                               "nom_role",
                                               aucun=None)

    if id_role is not None:
        user = TRoles.get_one(id_role, as_model=True)
        user_as_dict = user.as_dict_full_name()
        # format group to prepfil the form
        formated_groups = [
            group.id_role for group in TRoles.get_user_groups(id_role)
        ]
        if request.method == "GET":
            form = process(form, user_as_dict, formated_groups)

    if request.method == "POST":
        if form.validate_on_submit() and form.validate():
            groups = form.data["a_groupe"]
            form_user = pops(form.data)
            form_user["groupe"] = False
            form_user.pop("id_role")

            # if a password is set
            # check they are the same
            if form.pass_plus.data:
                try:
                    (
                        form_user["pass_plus"],
                        form_user["pass_md5"],
                    ) = check_and_encrypt_password(
                        form.pass_plus.data,
                        form.mdpconf.data,
                        current_app.config["PASS_METHOD"] == "md5"
                        or current_app.config["FILL_MD5_PASS"],
                    )
                except Exception as exp:
                    flash(str(exp), "error")
                    return render_template("user.html",
                                           form=form,
                                           title="Formulaire Utilisateur")

            if id_role is not None:
                # HACK a l'update on remet a la main les mdp
                # car on les masque dans le form
                form_user["pass_plus"] = user.pass_plus
                form_user["pass_md5"] = user.pass_md5
                form_user["id_role"] = user.id_role
                new_role = TRoles.update(form_user)
            else:
                new_role = TRoles.post(form_user)
            # set groups
            if len(groups) > 0:
                if id_role:
                    # first delete all groups of the user
                    cor_role_to_delete = CorRoles.get_all(
                        params=[{
                            "col": "id_role_utilisateur",
                            "filter": id_role
                        }],
                        as_model=True,
                    )
                    for cor_role in cor_role_to_delete:
                        db.session.delete(cor_role)
                    db.session.commit()
                for group in groups:
                    # add new groups
                    new_group = CorRoles(id_role_groupe=group,
                                         id_role_utilisateur=new_role.id_role)
                    db.session.add(new_group)
                db.session.commit()
            return redirect(url_for("user.users"))

        else:
            flash(strigify_dict(form.errors), "error")
    return render_template("user.html",
                           form=form,
                           title="Formulaire Utilisateur",
                           id_role=id_role)