Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions flask-app/!!build__dist_pyinstaller.bat
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ choice /C AN /M "Clean build ? A/N"
if errorlevel 1 set buildClean=--clean
if errorlevel 2 set buildClean=

echo.
choice /C AN /M "Test run ? A/N"
if errorlevel 1 set testRun=1
if errorlevel 2 set testRun=0

echo.
echo Build using pyinstaller : %python%

set srcDir=application
Expand Down
4 changes: 2 additions & 2 deletions flask-app/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
},
"args": [
//"--port", "80",
//"--config", "conf/mtw.ini",
"--config", "conf/mtw.ini",
//"--config", "conf/mtw-fr.ini",
"--debug",
"--fqdn=test.medvik.cz",
Expand All @@ -72,7 +72,7 @@
},
"args": [
//"--port", "5903",
//"--config", "conf/mtw.ini",
"--config", "conf/mtw.ini",
//"--config", "conf/mtw-fr.ini",
"--debug"
//"--relax"
Expand Down
27 changes: 27 additions & 0 deletions flask-app/application/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import logging
from application.modules import utils as mtu


def configure_log(app, logname: str = "app"):

if any(isinstance(h, logging.FileHandler) for h in app.logger.handlers):
return

if app.debug:
level = logging.DEBUG
log_file = mtu.get_instance_dir(app, f"logs/{logname}_debug.log")
else:
level = logging.INFO
log_file = mtu.get_instance_dir(app, f"logs/{logname}.log")

app.logger.setLevel(level)
app.logger.propagate = False

formatter = mtu.SafeFormatter("%(asctime)s - %(levelname)s: [%(ip)s] %(message)s")

handler = logging.FileHandler(log_file, encoding="utf-8")
handler.setLevel(level)
handler.setFormatter(formatter)
handler.addFilter(mtu.RequestIPFilter())

app.logger.addHandler(handler)
27 changes: 2 additions & 25 deletions flask-app/application/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"""
MeSH Traslation Workflow (MTW) - Flask app factory
"""

import datetime
import logging
import os
from flask import Flask
from cachelib import FileSystemCache
Expand All @@ -21,7 +21,6 @@

def create_app(
debug=False,
logger=None,
port=5900,
config_path="conf/mtw.ini",
server_name=None,
Expand Down Expand Up @@ -52,33 +51,11 @@ def create_app(
if app.debug:
print("MTW Config: ", config_path, " - port: ", port)

if logger:
app.logger = logger

if not app.debug:
file_handler = logging.FileHandler(
mtu.get_instance_dir(app, "logs/mtw_server.log")
)
file_handler.setLevel(logging.INFO)
file_handler.setFormatter(
logging.Formatter("%(asctime)s %(levelname)s: %(message)s ")
)
app.logger.addHandler(file_handler)
else:
file_handler = logging.FileHandler(
mtu.get_instance_dir(app, "logs/mtw_server_debug.log")
)
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(
logging.Formatter("%(asctime)s %(levelname)s: %(message)s ")
)
app.logger.addHandler(file_handler)

app.config.update(
dict(
APPLICATION_ROOT=url_prefix,
APP_NAME="MTW",
APP_VER="1.7.4",
APP_VER="1.7.5",
API_VER="1.0.0",
DBVERSION=1.0,
CACHE_DIR=mtu.get_instance_dir(app, "cache"),
Expand Down
4 changes: 2 additions & 2 deletions flask-app/application/modules/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def check_public(*args, **kwargs):
print(code, msg)

if code != 200:
app.logger.error(msg)
app.logger.error(f"[check_public] {msg}")
abort(403)

return f(*args, **kwargs)
Expand All @@ -81,7 +81,7 @@ def validateBasicAuth():
return True

if has_app_context():
app.logger.error("Basic Auth failed")
app.logger.error("[validateBasicAuth] failed")


def getReqHost():
Expand Down
8 changes: 4 additions & 4 deletions flask-app/application/modules/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""
MeSH Traslation Workflow (MTW) - database ops
"""

import uuid
from sqlite3 import dbapi2 as sqlite3

Expand All @@ -10,7 +11,6 @@

from application.modules.auth import hash_pwd


# Database


Expand All @@ -25,10 +25,10 @@ def get_db():
try:
g.sqlite_db = connect_db()
# g.sqlite_db.execute('pragma foreign_keys=on')
except sqlite3.Error as e:
error = "Flask get_db error : " + str(e.args[0])
except sqlite3.Error as err:
error = "Flask get_db error : " + str(err.args[0])
flash(error, "danger")
app.logger.error(error)
app.logger.error(f"[get_db] {err}")
return None
else:
return g.sqlite_db
Expand Down
1 change: 1 addition & 0 deletions flask-app/application/modules/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""
Flask Extensions
"""

from flask_caching import Cache

# from flask_limiter import Limiter
Expand Down
1 change: 0 additions & 1 deletion flask-app/application/modules/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from flask import current_app as app
from application.modules import utils as mtu


# Filters & Context processors


Expand Down
43 changes: 13 additions & 30 deletions flask-app/application/modules/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""
Routes & pages
"""

import bcrypt
import json
import pprint
Expand Down Expand Up @@ -162,9 +163,6 @@ def settings():
return redirect(ref_redirect())


# Audit


@app.route("/todo/", defaults={"tlist": "Preferred"}, methods=["GET"])
@app.route("/todo/list:<tlist>", methods=["GET"])
@login_required
Expand All @@ -190,6 +188,7 @@ def todo(tlist):
# pp.pprint(data)
hits = sparql.parseSparqlData(data)
# pp.pprint(hits)

else:
session.pop("tlist", None)
tlist = "Select a list"
Expand Down Expand Up @@ -479,7 +478,7 @@ def update_concept(dui, pref):
else:
msg = "Operation FAILED for Concept : " + concept
flash(msg, "danger")
app.logger.error(msg)
app.logger.error(f"[update_concept] {msg}")

return redirect(url_for("search", dui=dui))

Expand Down Expand Up @@ -525,7 +524,7 @@ def add_cpid(dui, cui):
check = False
msg = "CUI already exist ! "
flash(msg, "danger")
app.logger.error(msg + cpid + " for " + curi)
app.logger.error(f"[add_cpid] {msg} : {cpid} for {curi}")
return redirect(ref_redirect())
if check:
updated = sparql.updateTriple(
Expand Down Expand Up @@ -554,11 +553,11 @@ def add_cpid(dui, cui):
)
msg = "CUI generated: " + cpid
flash(msg, "info")
app.logger.info(msg + " for " + curi)
app.logger.info(f"[add_cpid] {msg} : {cpid} for {curi}")
else:
msg = "CUI update FAILED ! "
flash(msg, "danger")
app.logger.error(msg + cpid + " for " + curi)
app.logger.error(f"[add_cpid] {msg} : {cpid} for {curi}")

else:
msg = "CUI already issued - please try again"
Expand All @@ -574,6 +573,7 @@ def update_note(dui):
if session["ugroup"] in ["viewer", "disabled", "locked"]:
msg = "Insufficient priviledges"
flash(msg, "warning")
app.logger.warning(f"[update_note] {dui} : {msg}")
return render_template("errors/error_page.html", errcode=403, error=msg), 403

if request.form.get("predicate") and request.form.get("label"):
Expand All @@ -582,9 +582,9 @@ def update_note(dui):
label = request.form["label"].replace("?", "").strip()

if predicate not in app.config["DESC_NOTES"]:
msg = "Update note: Unknown mesht predicate"
msg = "Unknown mesht: predicate"
flash(msg, "danger")
app.logger.error(msg)
app.logger.error(f"[update_note] {msg}")
return (
render_template("errors/error_page.html", errcode=403, error=msg),
403,
Expand Down Expand Up @@ -1011,9 +1011,6 @@ def update_audit():
return redirect(url_for("search", dui=dui, tab="history"))


# Compare


@app.route("/compare/", defaults={"dui": ""}, methods=["GET"])
@app.route("/compare/dui:<dui>", methods=["GET"])
@login_required
Expand Down Expand Up @@ -1063,9 +1060,6 @@ def compare(dui):
)


# Audit


@app.route("/audit/", defaults={"dui": "", "cui": ""}, methods=["GET"])
@app.route("/audit/dui:<dui>", defaults={"cui": ""}, methods=["GET"])
@app.route("/audit/dui:<dui>/cui:<cui>", methods=["GET"])
Expand Down Expand Up @@ -1105,9 +1099,6 @@ def audit(dui, cui):
)


# Approval


@app.route(
"/approve/",
defaults={"userid": "", "username": "", "status": "", "event": ""},
Expand Down Expand Up @@ -1489,16 +1480,17 @@ def search(dui, action):
if text_query:
data = sparql.getSparqlData(
"search",
# "search_old",
query=text_query,
show=session.get("sshow"),
status=session.get("sstatus"),
slang=session.get("slang"),
scr=session.get("scr"),
qtp=session.get("qtp"),
)
# pp.pprint(data)

if data:
# if app.debug:
# app.logger.error(json.dumps(data))
started = timer()
hits = sparql.parseSparqlData(data)
# pp.pprint(hits)
Expand Down Expand Up @@ -1565,9 +1557,6 @@ def store_visited(dui, label):
del session["visited"][rem]


# Reporting


@app.route("/report/", defaults={"userid": "", "year": ""}, methods=["GET"])
@app.route("/report/userid:<userid>", defaults={"year": ""}, methods=["GET"])
@app.route("/report/year:<year>", defaults={"userid": ""}, methods=["GET"])
Expand Down Expand Up @@ -1672,9 +1661,6 @@ def report(userid, year):
)


# Management


@app.route("/manage/", defaults={"action": ""}, methods=["GET"])
@app.route("/manage/action:<action>", methods=["POST"])
@login_required
Expand Down Expand Up @@ -1773,7 +1759,7 @@ def manage(action):
exp_files[f]["fname"] = fp.name
exp_files[f]["fdate"] = mtu.getFpathDate(fp)

# 2024_umls.tsv
# YYYY_umls.tsv
show_umls_exports = False
umls_tsv = mtu.getTempFpath("umls", ext="tsv")
if umls_tsv.is_file():
Expand Down Expand Up @@ -2056,9 +2042,6 @@ def update_user():
return redirect(url_for("manage"))


# Login | Logout


@app.route("/login/", methods=["GET", "POST"])
# @limiter.limit("10/minute")
def login():
Expand Down
5 changes: 5 additions & 0 deletions flask-app/application/modules/sparql.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""
MeSH Traslation Workflow (MTW) - SPARQL ops
"""

import datetime
import requests
import pprint
Expand Down Expand Up @@ -108,6 +109,7 @@ def getSparqlData(
endpoint, headers=headers, data=sparql.encode("utf-8"), timeout=600
)
) as r:
r.raise_for_status()
show_elapsed(t0, tag="result ready: " + template)
if r.status_code == 200:
if output == "json":
Expand All @@ -126,6 +128,9 @@ def getSparqlData(
"%s \n\n %s \n\n %s \n\n %s", endpoint, template, query, str(err)
)

except Exception as err:
app.logger.error(f"[getSparqlData] {err}")


def getSparqlDataExt(dui, output, year="", key=None, cache=None, otype="Descriptor"):

Expand Down
Loading
Loading