def panel(tipo_repor):
if not auth.get_user():
return redirect(url_for("login"))
user=auth.get_user()
usuario=user['oid']
agrupacion="admin"
tiporepor = obtener_report_por_id_user(db,usuario,agrupacion,tipo_repor)
return render_template('panel_workspace.html', tiporepor=tiporepor, version=__version__)
#recupera el id del reporte para mostrar el embedded
@app.route("/view_repor/<int:id_repor>")
def view_repor(id_repor):
if not auth.get_user():
return redirect(url_for("login"))
user=auth.get_user()
usuario=user['oid']
global repor
repor = obtener_report_por_id_user2(db,id_repor)
repors = obtener_report_por_id_user(db,usuario,agrupacion)
return render_template("view_repor.html", repor=repor, repors=repors)
in the first url I pass the type_report and in the second id_report is passed
the two defs use the def “obtener_report_por_id_user” to attack the database and get the information.
def obtener_report_por_id_user(db,usuario,agrupacion,tipo_repor):
with db.connection.cursor() as cursor:
tiporepor = []
result = cursor.execute("""SELECT id,name,report,role,descripcion,name_img,agrupacion,tipo_repor FROM reports_workspace WHERE id_user = %s AND agrupacion = %s AND tipo_repor = %s""", (usuario,agrupacion,tipo_repor))
tiporepor = cursor.fetchall()
return tiporepor
What is more recommended, that each function (panel and view_repor) attack a different def or that I pass the variables I need from one def to another?
You should reuse your functions anywhere they make sense.
If you want to try to reuse the same database function instead of having a separate obtener_report_por_id_user and obtener_report_por_id_user2, then we need to see both of them in order to understand the problem (make sure it makes sense, show how to use a common function for the work that is in common, and show how to use it in both cases).
Also, do not set global variables from your views. They will not work like you want them to, especially once you have more than one user logged in and using the site at the same time.
The topic global variables from its views that you comment on, I imagine that you refer to global report
So what do you advise me?
In relation to having obtain_report_by_id_user and obtain_report_by_id_user2, in the end the query would be almost the same although I would not need two of the parameters that I need in the previous one
It would be, obtener_report_por_id_user
def obtener_report_por_id_user(db,usuario,agrupacion,tipo_repor):
with db.connection.cursor() as cursor:
tiporepor = []
result = cursor.execute("""SELECT id,name,report,role,descripcion,name_img,agrupacion,tipo_repor FROM reports_workspace WHERE id_user = %s AND agrupacion = %s AND tipo_repor = %s""", (usuario,agrupacion,tipo_repor))
tiporepor = cursor.fetchall()
return tiporepor
and obtener_report_por_id_user2
def obtener_report_por_id_user(db,usuario):
with db.connection.cursor() as cursor:
tiporepor = []
result = cursor.execute("""SELECT id,name,report,role,descripcion,name_img,agrupacion,tipo_repor FROM reports_workspace WHERE id_user = %s """, (usuario))
tiporepor = cursor.fetchall()
return tiporepor