def addorupdate(id_profil): """ Route affichant un formulaire vierge ou non (selon l'url) pour ajouter ou mettre à jour un profil L'envoie du formulaire permet l'ajout ou la maj du profil dans la base Retourne un template accompagné d'un formulaire pré-rempli ou non selon le paramètre id_profil Une fois le formulaire validé on retourne une redirection vers la liste de profil """ form = t_profilsforms.Profil() if id_profil == None: if request.method == "POST": if form.validate() and form.validate_on_submit(): form_profil = pops(form.data) form_profil.pop("id_profil") TProfils.post(form_profil) return redirect(url_for("profils.profils")) return render_template("profil.html", form=form, title="Formulaire Profil") else: profil = TProfils.get_one(id_profil) if request.method == "GET": form = process(form, profil) if request.method == "POST": if form.validate() and form.validate_on_submit(): form_profil = pops(form.data) form_profil["id_profil"] = profil["id_profil"] TProfils.update(form_profil) return redirect(url_for("profils.profils")) return render_template("profil.html", form=form, title="Formulaire Profil")
def profils_for_app(id_application): """ Route affichant la liste des profils utilisables par l'application et ceux disponibles. Avec pour paramètre un id d'application Retourne un template avec pour paramètres: - une entête des tableaux --> fLine - le nom des colonnes de la base --> data - liste des profils utilisables --> table - liste des profils non utilisables mais disponibles --> table2 """ profils_in_app = TProfils.get_profils_in_app(id_application) profils_out_app = TProfils.get_profils_out_app(id_application) header = ["ID", "Profil"] data = ["id_profil", "nom_profil"] app = TApplications.get_one(id_application) if request.method == "POST": data = request.get_json() new_profils = data["tab_add"] delete_profils = data["tab_del"] try: CorProfilForApp.add_cor(id_application, new_profils) CorProfilForApp.del_cor(id_application, delete_profils) except Exception as e: return jsonify(str(e)), 500 return jsonify({"redirect": url_for("application.applications")}), 200 return render_template( "app_profils.html", fLine=header, data=data, table=profils_out_app, table2=profils_in_app, info="Profils utilisables dans l'application " + app["nom_application"], )
def delete(id_profil): """ Route qui supprime un profil dont l'id est donné en paramètres dans l'url Retourne une redirection vers la liste de profil """ TProfils.delete(id_profil) return redirect(url_for("profils.profils"))
def profils(): """ Route qui affiche la liste des profils Retourne un template avec pour paramètres : - une entête de tableau --> fLine - le nom des colonnes de la base --> line - le contenu du tableau --> table - le chemin de mise à jour --> pathU - le chemin de suppression --> pathD - le chemin d'ajout --> pathA - le chemin des roles du profil --> pathP - une clé (clé primaire dans la plupart des cas) --> key - un nom (nom de la table) pour le bouton ajout --> name - un nom de listes --> name_list - ajoute une colonne de bouton ('True' doit être de type string)--> otherCol - nom affiché sur le bouton --> Members """ fLine = ['ID', 'CODE', 'Nom', 'Description'] columns = ['id_profil', 'code_profil', 'nom_profil', 'desc_profil'] tab = [data for data in TProfils.get_all()] return render_template('table_database.html', fLine=fLine, line=columns, table=tab, key='id_profil', pathU=config.URL_APPLICATION + '/profil/update/', pathD=config.URL_APPLICATION + '/profil/delete/', pathA=config.URL_APPLICATION + '/profil/add/new', name="un profil", name_list="Profils", otherCol='False', profil_app='True', App="Application")
def change_application_right(): """ Change les droits d'un utilisateur pour une application """ req_data = request.get_json() id_application = req_data.get("id_application", None) # Test assurant une rétrocompatibilité (à l'époque des niveaux de droits) if req_data.get("id_droit", None): code_profil = req_data.get("id_droit", None) else: code_profil = req_data.get("id_profil", None) # Récupération de l'identifiant du profil à partir de son code profil = TProfils.get_profil_in_app_with_code(id_application, str(code_profil)) if not profil: return ( { "msg": "pas de profil " + str(code_profil) + "corespondant pour l'application" # noqa }, 500, ) id_profil = profil.id_profil id_role = req_data.get("id_role", None) role = db.session.query(TRoles).filter(TRoles.id_role == id_role).first() if not id_application or not id_role or not code_profil: return {"msg": "Problème de paramètres POST"}, 400 cor = (db.session.query(CorRoleAppProfil).filter( id_role == CorRoleAppProfil.id_role).filter( id_application == CorRoleAppProfil.id_application).first()) if not cor: cor = CorRoleAppProfil( **{ "id_role": id_role, "id_application": id_application, "id_profil": id_profil, }) else: cor.id_profil = id_profil db.session.commit() return { "id_role": id_role, "id_profil": code_profil, "id_droit": code_profil, # Retrocompatiblité pour l'OEASC "id_application": id_application, "role": role.as_dict(), }
def users(id_profil): """ Route affichant la liste des users du profil et ceux disponibles. Avec pour paramètre un id de profil Retourne un template avec pour paramètres: - une entête des tableaux --> fLine - le nom des colonnes de la base --> data - liste des profils utilisables --> table - liste des profils non utilisables mais disponibles --> table2 """ users_in_profil = TUsers.get_users_in_profil(id_profil) users_out_profil = TUsers.get_users_out_profil(id_profil) header = ['ID', 'User'] data = ['id_user', 'full_name'] profil = TProfils.get_one(id_profil) if request.method == 'POST': data = request.get_json() new_users = data["tab_add"] delete_users = data["tab_del"] try: CorUserProfil.add_cor(new_users,id_profil) CorUserProfil.del_cor(delete_users,id_profil) return jsonify({"msg":"Enregistrement réussi"}) except (exc.SQLAlchemyError, exc.DBAPIError) as e: return jsonify({"msg":"Quelque chose s'est mal passé :" + e}) return render_template( 'tobelong.html', fLine=header, data=data, table=users_out_profil, table2=users_in_profil, info='Utilisateurs ayant le profil "' + profil['profil_name'] + '"' )
def delete(id_profil): """ Route qui supprime un profil dont l'id est donné en paramètres dans l'url Retourne une redirection vers la liste de profil """ try: TProfils.delete(id_profil) return redirect(url_for('profil.profils')) except (exc.SQLAlchemyError, exc.DBAPIError) as e: flash("Peut-être que tu essaies de faire quelque chose qui n'est pas cohérent.") flash(e) return render_template( 'error.html', title="Houps ! Une erreur s'est produite" )
def addorupdate(id_application): """ Route affichant un formulaire vierge ou non (selon l'url) pour ajouter ou mettre à jour une application L'envoie du formulaire permet l'ajout ou la maj d'une application dans la base Retourne un template accompagné d'un formulaire pré-rempli ou non selon le paramètre id_application Une fois le formulaire validé on retourne une redirection vers la liste d'application """ form = t_applicationsforms.Application() form.id_parent.choices = TApplications.choixSelect( "id_application", "nom_application", 1, order_by="nom_application") if id_application == None: form.id_parent.process_data(-1) if request.method == "POST": if form.validate() and form.validate_on_submit(): form_app = pops(form.data) if form.id_parent.data == -1: form_app["id_parent"] = None form_app.pop("id_application") TApplications.post(form_app) return redirect(url_for("application.applications")) return render_template("application.html", form=form, title="Formulaire Application") else: application = TApplications.get_one(id_application) form.id_parent.choices.remove( (application["id_application"], application["nom_application"])) if request.method == "GET": if application["id_parent"] == None: form.id_parent.process_data(-1) else: form.id_parent.process_data(application["id_parent"]) form_app = process(form, application) profils_in_app = TProfils.get_profils_in_app(id_application) if request.method == "POST": if form.validate_on_submit() and form.validate(): form_app = pops(form.data) if form.id_parent.data == -1: form_app["id_parent"] = None form_app["id_application"] = application["id_application"] TApplications.update(form_app) return redirect(url_for("application.applications")) return render_template( "application.html", form=form, title="Formulaire Application", profils=profils_in_app, id_application=id_application, )
def __init__(self, id_application, *args, **kwargs): super(AppProfil, self).__init__(*args, **kwargs) # on ne met que les users qui n'ont pas déja un profil dans l'app # sous requete utilisateurs ayant deja un profil pour une app user_with_profil_in_app = db.session.query( CorRoleAppProfil.id_role).filter( CorRoleAppProfil.id_application == id_application) users_no_profils_in_app = db.session.query(TRoles).filter( TRoles.id_role.notin_(user_with_profil_in_app)).all() users_select_choices = [] for user in users_no_profils_in_app: user_as_dict = user.as_dict_full_name() users_select_choices.append( (user_as_dict['id_role'], user_as_dict['full_name'])) self.role.choices = users_select_choices # choix des profils dispo pour une appli self.profil.choices = TProfils.choixSelect( id_application=id_application)
def profils(): """ Route qui affiche la liste des profils Retourne un template avec pour paramètres : - les droits de l'utilisateur selon son porfil --> user_right - une entête de tableau --> fLine - le nom des colonnes de la base --> line - le contenu du tableau --> table - le chemin de mise à jour --> pathU - le chemin de suppression --> pathD - le chemin d'ajout --> pathA - le chemin des roles du profil --> pathP - une clé (clé primaire dans la plupart des cas) --> key - un nom (nom de la table) pour le bouton ajout --> name - un nom de listes --> name_list - ajoute une colonne de bouton ('True' doit être de type string)--> otherCol - nom affiché sur le bouton --> Members """ user_profil = user_from_token(request.cookies['token']).id_profil user_right = list() if user_profil == 6: user_right = ['C','R','U','D'] else: user_right = ['R'] fLine = ['Code', 'Nom', 'Description'] columns = ['id_profil', 'profil_code', 'profil_name', 'profil_comment'] tab = [data for data in TProfils.get_all()] return render_template( 'table_database.html', user_right=user_right, fLine=fLine, line=columns, table=tab, key='id_profil', pathU=config.URL_APPLICATION + '/profil/update/', pathD=config.URL_APPLICATION + '/profil/delete/', pathA=config.URL_APPLICATION + '/profil/add/new', pathP=config.URL_APPLICATION + "/profil/users/", name="un profil", name_list="Profils", otherCol='True', Members="Utilisateurs" )
def profils(): """ Route qui affiche la liste des profils Retourne un template avec pour paramètres : - une entête de tableau --> fLine - le nom des colonnes de la base --> line - le contenu du tableau --> table - le chemin de mise à jour --> pathU - le chemin de suppression --> pathD - le chemin d'ajout --> pathA - le chemin des roles du profil --> pathP - une clé (clé primaire dans la plupart des cas) --> key - un nom (nom de la table) pour le bouton ajout --> name - un nom de listes --> name_list - ajoute une colonne de bouton ('True' doit être de type string)--> otherCol - nom affiché sur le bouton --> Members """ fLine = ["ID", "CODE", "Nom", "Description"] columns = ["id_profil", "code_profil", "nom_profil", "desc_profil"] tab = [data for data in TProfils.get_all(order_by="nom_profil")] return render_template( "table_database.html", fLine=fLine, line=columns, table=tab, key="id_profil", pathU=URL_APPLICATION + "/profil/update/", pathD=URL_APPLICATION + "/profil/delete/", pathA=URL_APPLICATION + "/profil/add/new", name="un profil", name_list="Profils", otherCol="False", profil_app="True", App="Application", )