Browse Source

dbmysql for webflask login

refactor
Ulrich 2 years ago
parent
commit
3f7c9f7d78
  1. 4
      basic/compexec.py
  2. 9
      basic/constants.py
  3. 19
      basic/entity.py
  4. 4
      basic/program.py
  5. 18
      basic/user.py
  6. 2
      test/test_21tdata.py
  7. 63
      test/test_31db.py
  8. 3
      test/testtools.py
  9. 1
      utils/data_const.py
  10. 13
      utils/db_abstract.py
  11. 110
      utils/dbmysql_tool.py
  12. 14
      utils/tdata_tool.py
  13. 61
      webflask/auth.py
  14. 4
      webflask/db.py

4
basic/compexec.py

@ -143,8 +143,6 @@ class Testexecuter():
self.m.logInfo("select db-content "+ self.name) self.m.logInfo("select db-content "+ self.name)
dbi = basic.toolHandling.getDbTool(job, self) dbi = basic.toolHandling.getDbTool(job, self)
data = dbi.selectTables(subdir, job) data = dbi.selectTables(subdir, job)
print("ppp")
#data = {}
for t in data[subdir]: for t in data[subdir]:
data[B.DATA_NODE_TABLES] = {} data[B.DATA_NODE_TABLES] = {}
data[B.DATA_NODE_TABLES][t] = data[subdir][t] data[B.DATA_NODE_TABLES][t] = data[subdir][t]
@ -255,7 +253,7 @@ class Testexecuter():
:return: :return:
""" """
if not step.fct in self.conf[B.DATA_NODE_STEPS]: if not step.fct in self.conf[B.DATA_NODE_STEPS]:
raise Exception(self.m.getMessageText(T.EXP_KEY_DOESNT_EXIST, [step.fct, self.name])) raise Exception(self.m.getMessageText(job, T.EXP_KEY_DOESNT_EXIST, [step.fct, self.name]))
if step.fct in self.conf[B.DATA_NODE_STEPS]: if step.fct in self.conf[B.DATA_NODE_STEPS]:
for stepconf in self.conf[B.DATA_NODE_STEPS][step.fct]: for stepconf in self.conf[B.DATA_NODE_STEPS][step.fct]:
if stepconf[B.SUBJECT_TOOL] == B.TOPIC_NODE_FILE: if stepconf[B.SUBJECT_TOOL] == B.TOPIC_NODE_FILE:

9
basic/constants.py

@ -126,7 +126,14 @@ ATTR_DB_SCHEMA = "schema"
""" optional attribute for technical name of the schema """ """ optional attribute for technical name of the schema """
ATTR_DB_TABNAME = "tabname" ATTR_DB_TABNAME = "tabname"
""" optional attribute in order to use a different technical name for the db-table """ """ optional attribute in order to use a different technical name for the db-table """
LIST_DB_ATTR = [ATTR_DB_PARTITION, ATTR_DB_DATABASE, ATTR_DB_SCHEMA, ATTR_DB_TABNAME, ATTR_ARTS_PRESTEP] + LIST_ARTS_ATTR ATTR_DB_USER = "user"
""" optional attribute in order to use a different technical name for the db-table """
ATTR_DB_PASSWD = "password"
""" optional attribute in order to use a different technical name for the db-table """
ATTR_DB_HOST = "hostname"
""" optional attribute in order to use a different technical name for the db-table """
LIST_DB_ATTR = [ATTR_DB_PARTITION, ATTR_DB_DATABASE, ATTR_DB_SCHEMA, ATTR_DB_TABNAME, ATTR_ARTS_PRESTEP,
ATTR_DB_USER, ATTR_DB_PASSWD, ATTR_DB_HOST] + LIST_ARTS_ATTR
TOPIC_NODE_CLI = "cli" TOPIC_NODE_CLI = "cli"
LIST_CLI_ATTR = [] + LIST_ARTS_ATTR LIST_CLI_ATTR = [] + LIST_ARTS_ATTR
TOPIC_NODE_API = "api" TOPIC_NODE_API = "api"

19
basic/entity.py

@ -7,6 +7,25 @@ class Entity:
def __int__(self, job): def __int__(self, job):
self.job = job self.job = job
def getDbAttr(self, job):
out = {}
for attr in [B.ATTR_DB_HOST, B.ATTR_DB_USER, B.ATTR_DB_DATABASE, B.ATTR_DB_PASSWD]:
out[attr] = job.conf.confs[B.TOPIC_NODE_DB][attr]
return out
def getDdl(self, job, ddl):
out = {}
for t in ddl:
out[t] = {}
for f in ddl[t]:
out[t][f] = {}
for a in ddl[t][f]:
print("entity-23 "+f+", "+a+" "+str(ddl))
out[t][f][a] = ddl[t][f][a]
out[t][f][D.DDL_FNAME] = f
out[t][B.DATA_NODE_HEADER] = list(ddl[t].keys())
return out
def createSchema(self): def createSchema(self):
if B.TOPIC_NODE_DB in self.job.conf.confs: if B.TOPIC_NODE_DB in self.job.conf.confs:
dbi = basic.toolHandling.getDbTool(self.job, None, self.job.conf.confs[B.TOPIC_NODE_DB][B.ATTR_TYPE]) dbi = basic.toolHandling.getDbTool(self.job, None, self.job.conf.confs[B.TOPIC_NODE_DB][B.ATTR_TYPE])

4
basic/program.py

@ -40,14 +40,14 @@ jobdef = {
"dirname": "workdir", "dirname": "workdir",
"loglevel": basic.message.LIMIT_INFO, "loglevel": basic.message.LIMIT_INFO,
"logpath": "{job.conf.data}/workspace/reorg_{job.start:H}.txt" }, "logpath": "{job.conf.data}/workspace/reorg_{job.start:H}.txt" },
"temp": { "service": {
"pardef": "", "pardef": "",
"pfilesource": "", "pfilesource": "",
"pfiletarget": "", "pfiletarget": "",
"basedir": "workbase", "basedir": "workbase",
"dirname": "workdir", "dirname": "workdir",
"loglevel": basic.message.LIMIT_INFO, "loglevel": basic.message.LIMIT_INFO,
"logpath": "{job.par.envdir}/{log}/log_{job.start:H}.txt" }, "logpath": "{job.conf.data}/workspace/service_{job.start:H}.txt" },
"unit": { "unit": {
"pardef": "", "pardef": "",
"pfilesource": "", "pfilesource": "",

18
basic/user.py

@ -9,6 +9,20 @@ import utils.data_const as D
import basic.constants as B import basic.constants as B
import basic.entity import basic.entity
ddl = {
"user": {
"id": {
D.DDL_TYPE: D.TYPE_PK,
},
"username": {
D.DDL_TYPE: D.TYPE_STR,
},
"password": {
D.DDL_TYPE: D.TYPE_STRING,
}
}
}
class User(basic.entity.Entity): class User(basic.entity.Entity):
username = "" username = ""
password = "" password = ""
@ -19,6 +33,10 @@ class User(basic.entity.Entity):
:param job: :param job:
""" """
self.job = job self.job = job
self.conf = {}
self.conf[B.SUBJECT_CONN] = self.getDbAttr(job)
self.conf[B.DATA_NODE_DDL] = self.getDdl(job, ddl)
self.m = job.m
def getSchema(self): def getSchema(self):
dbtype = self.job.conf.confs[B.TOPIC_NODE_DB][B.ATTR_TYPE] dbtype = self.job.conf.confs[B.TOPIC_NODE_DB][B.ATTR_TYPE]

2
test/test_21tdata.py

@ -20,7 +20,7 @@ OS_SYSTEM = test.constants.OS_SYSTEM
# if you minimize the list you can check the specific test-function # if you minimize the list you can check the specific test-function
TEST_FUNCTIONS = ["test_01tdata", "test_02getCsvSpec_data", "test_03getCsvSpec_tree", "test_04getCsvSpec_key", TEST_FUNCTIONS = ["test_01tdata", "test_02getCsvSpec_data", "test_03getCsvSpec_tree", "test_04getCsvSpec_key",
"test_05getCsvSpec_conf", "test_06parseCsv"] "test_05getCsvSpec_conf", "test_06parseCsv"]
# TEST_FUNCTIONS = ["test_getCsvSpec_data"] TEST_FUNCTIONS = ["test_05getCsvSpec_conf"]
# with this variable you can switch prints on and off # with this variable you can switch prints on and off
verbose = False verbose = False

63
test/test_31db.py

@ -16,14 +16,15 @@ import utils.db_abstract
import test.testtools import test.testtools
import utils.config_tool import utils.config_tool
import utils.data_const as D import utils.data_const as D
import basic.user
HOME_PATH = test.constants.HOME_PATH HOME_PATH = test.constants.HOME_PATH
conf = {} conf = {}
# here you can select single testfunction for developping the tests # here you can select single testfunction for developping the tests
# "test_toolhandling", "test_parseSql" -> test of components # "test_toolhandling", "test_parseSql" -> test of components
TEST_FUNCTIONS = ["test_formatDbRows"] TEST_FUNCTIONS = ["test_formatDbRows", "test_10insertion", "test_11selection", "test_12deletion"]
# TEST_FUNCTIONS = ["test_getTechnicalIDFields"] #TEST_FUNCTIONS = ["test_10insertion", "test_11selection"]
TEST_FUNCTIONS = ["test_10insertion","test_12deletion"]
class MyTestCase(unittest.TestCase): class MyTestCase(unittest.TestCase):
mymsg = "--------------------------------------------------------------" mymsg = "--------------------------------------------------------------"
@ -123,6 +124,62 @@ class MyTestCase(unittest.TestCase):
MyTestCase.mymsg += "\n----- "+actfunction+" : "+str(cnttest) MyTestCase.mymsg += "\n----- "+actfunction+" : "+str(cnttest)
def test_10insertion(self):
global mymsg
actfunction = str(inspect.currentframe().f_code.co_name)
cnttest = 0
if actfunction not in TEST_FUNCTIONS:
return
job = basic.program.Job("webflask", {})
if B.TOPIC_NODE_DB in job.conf.confs:
dbtype = job.conf.confs[B.TOPIC_NODE_DB][B.ATTR_TYPE]
userentity = basic.user.User(job)
dbi = basic.toolHandling.getDbTool(job, userentity, dbtype)
data = {}
data["user"] = {}
data["user"][B.DATA_NODE_HEADER] = userentity.conf[B.DATA_NODE_DDL]["user"][B.DATA_NODE_HEADER]
data["user"][B.DATA_NODE_DATA] = []
row = {"username": "alfons", "password": "alfons-secret"}
data["user"][B.DATA_NODE_DATA].append(row)
dbi.insertRows("user", data["user"][B.DATA_NODE_DATA], job)
def test_11selection(self):
global mymsg
actfunction = str(inspect.currentframe().f_code.co_name)
cnttest = 0
if actfunction not in TEST_FUNCTIONS:
return
job = basic.program.Job("webflask", {})
if B.TOPIC_NODE_DB in job.conf.confs:
dbtype = job.conf.confs[B.TOPIC_NODE_DB][B.ATTR_TYPE]
userentity = basic.user.User(job)
dbi = basic.toolHandling.getDbTool(job, userentity, dbtype)
rows = dbi.selectRows("user", job)
print (str(rows))
MyTestCase.mymsg += "\n----- "+actfunction+" : "+str(cnttest)
def test_12deletion(self):
global mymsg
actfunction = str(inspect.currentframe().f_code.co_name)
cnttest = 0
if actfunction not in TEST_FUNCTIONS:
return
job = basic.program.Job("webflask", {})
if B.TOPIC_NODE_DB in job.conf.confs:
dbtype = job.conf.confs[B.TOPIC_NODE_DB][B.ATTR_TYPE]
userentity = basic.user.User(job)
dbi = basic.toolHandling.getDbTool(job, userentity, dbtype)
rows = dbi.selectRows("user", job, "WHERE username = \'alfons\'")
cntpre = len(rows)
print (str(rows))
dbi.deleteRows("user", job, "WHERE username = \'alfons\'")
rows = dbi.selectRows("user", job, "WHERE username = \'alfons\'")
print (str(rows))
cntpost = len(rows)
self.assertGreaterEqual(cntpre, cntpost)
def test_zzz(self): def test_zzz(self):
print(MyTestCase.mymsg) print(MyTestCase.mymsg)

3
test/testtools.py

@ -74,13 +74,12 @@ def getJob(pgran="", papp="", penv="", ptstamp="", pmode=""):
path = DEFAULT_ARCHIV_DIR + "/TC0001/" + tstamp path = DEFAULT_ARCHIV_DIR + "/TC0001/" + tstamp
elif gran == "ts": elif gran == "ts":
path = DEFAULT_ARCHIV_DIR + "/testlauf/TST001_" + tstamp path = DEFAULT_ARCHIV_DIR + "/testlauf/TST001_" + tstamp
job = basic.program.Job("unit")
#job.conf.confs[B.SUBJECT_PATH]["components"] = T.COMP_PATH #job.conf.confs[B.SUBJECT_PATH]["components"] = T.COMP_PATH
args = {"application": app, "environment": env, "modus": mode, gran+"time": tstamp, args = {"application": app, "environment": env, "modus": mode, gran+"time": tstamp,
gran+"dir": path, gran+"dir": path,
"step": 2} "step": 2}
# "usecase": "TST001", "tstime": "2022-03-17_17-28"} # "usecase": "TST001", "tstime": "2022-03-17_17-28"}
job.par.setParameterArgs(job, args) job = basic.program.Job("unit", args)
return job return job

1
utils/data_const.py

@ -15,6 +15,7 @@ TYPE_FLOAT = "float"
TYPE_DOUBLE = "double" TYPE_DOUBLE = "double"
TYPE_DATE = "date" TYPE_DATE = "date"
TYPE_TIME = "time" TYPE_TIME = "time"
TYPE_PK = "pk"
# fields in DDL # fields in DDL
DDL_FNULLABLE = "nullable" DDL_FNULLABLE = "nullable"

13
utils/db_abstract.py

@ -61,15 +61,18 @@ def getDbAttributes(comp, table):
B.ATTR_DB_DATABASE: "", B.ATTR_DB_DATABASE: "",
B.ATTR_DB_SCHEMA: "", B.ATTR_DB_SCHEMA: "",
B.ATTR_DB_TABNAME: "", B.ATTR_DB_TABNAME: "",
B.ATTR_DB_USER: "",
B.ATTR_DB_PASSWD: "",
B.ATTR_DB_HOST: "",
B.ATTR_DB_PARTITION: D.DEFAULT_DB_PARTITION, B.ATTR_DB_PARTITION: D.DEFAULT_DB_PARTITION,
B.ATTR_DB_CONN_JAR: D.DEFAULT_DB_CONN_JAR B.ATTR_DB_CONN_JAR: D.DEFAULT_DB_CONN_JAR
} }
for attr in out.keys(): for attr in out.keys():
if (table in comp.conf[B.SUBJECT_ARTS][B.TOPIC_NODE_DB]) \ if (B.SUBJECT_ARTS in comp.conf and table in comp.conf[B.SUBJECT_ARTS][B.TOPIC_NODE_DB]) \
and (attr in comp.conf[B.SUBJECT_ARTS][B.TOPIC_NODE_DB][table]): and (attr in comp.conf[B.SUBJECT_ARTS][B.TOPIC_NODE_DB][table]):
out[attr] = comp.conf[B.SUBJECT_ARTS][B.TOPIC_NODE_DB][table][attr] out[attr] = comp.conf[B.SUBJECT_ARTS][B.TOPIC_NODE_DB][table][attr]
print("a " + attr + " " + out[attr]) print("a " + attr + " " + out[attr])
elif (attr in comp.conf[B.SUBJECT_ARTS][B.TOPIC_NODE_DB]): elif (B.SUBJECT_ARTS in comp.conf and attr in comp.conf[B.SUBJECT_ARTS][B.TOPIC_NODE_DB]):
out[attr] = comp.conf[B.SUBJECT_ARTS][B.TOPIC_NODE_DB][attr] out[attr] = comp.conf[B.SUBJECT_ARTS][B.TOPIC_NODE_DB][attr]
print("b " + attr + " " + out[attr]) print("b " + attr + " " + out[attr])
elif (B.TOPIC_NODE_DB in comp.conf[B.SUBJECT_CONN]) \ elif (B.TOPIC_NODE_DB in comp.conf[B.SUBJECT_CONN]) \
@ -241,7 +244,7 @@ def formatDbVal(msg, val, dtyp):
pass pass
def isCompTable(comp, data, table): def isCompTable(comp, job, data, table):
""" checks if the table in data relates to the component """ """ checks if the table in data relates to the component """
print(str(data)) print(str(data))
return isCompRow(comp, data[B.DATA_NODE_TABLES][table]) return isCompRow(comp, data[B.DATA_NODE_TABLES][table])
@ -334,7 +337,7 @@ class DbFcts():
self.loadDdl(job) self.loadDdl(job)
for t in tdata[B.DATA_NODE_TABLES]: for t in tdata[B.DATA_NODE_TABLES]:
print("einzufuegende Tabelle "+self.comp.name+" "+t) print("einzufuegende Tabelle "+self.comp.name+" "+t)
if isCompTable(self.comp, tdata, t): if isCompTable(self.comp, job, tdata, t):
self.insertRows(t, tdata[B.DATA_NODE_TABLES][t][B.DATA_NODE_DATA], job) self.insertRows(t, tdata[B.DATA_NODE_TABLES][t][B.DATA_NODE_DATA], job)
self.comp.m.logMsg("in Tabelle {} {} Zeilen eingefuegt".format( self.comp.m.logMsg("in Tabelle {} {} Zeilen eingefuegt".format(
t, len(tdata[B.DATA_NODE_TABLES][t][B.DATA_NODE_DATA]))) t, len(tdata[B.DATA_NODE_TABLES][t][B.DATA_NODE_DATA])))
@ -372,7 +375,7 @@ class DbFcts():
if len(value.strip()) == 0 and fo[D.DDL_FNULLABLE] == B.SVAL_YES: if len(value.strip()) == 0 and fo[D.DDL_FNULLABLE] == B.SVAL_YES:
return self.getDbNull() return self.getDbNull()
if fo[D.DATA_NODE_TYPE] == D.TYPE_STRING or fo[D.DATA_NODE_TYPE] == D.TYPE_STR: if fo[D.DATA_NODE_TYPE] == D.TYPE_STRING or fo[D.DATA_NODE_TYPE] == D.TYPE_STR:
return "'"+value.strip()+"'" return value.strip()
elif fo[D.DATA_NODE_TYPE] == D.TYPE_INT: elif fo[D.DATA_NODE_TYPE] == D.TYPE_INT:
return value.strip() return value.strip()
elif fo[D.DATA_NODE_TYPE] == D.TYPE_DOUBLE: elif fo[D.DATA_NODE_TYPE] == D.TYPE_DOUBLE:

110
utils/dbmysql_tool.py

@ -20,30 +20,46 @@ class DbFcts(utils.dbrel_tool.DbFcts):
def __init__(self): def __init__(self):
pass pass
def selectRows(self, table, job): def selectRows(self, table, job, where=""):
""" method to select rows from a database """ method to select rows from a database
statement written in sql """ statement written in sql """
tdata = {} tdata = {}
verify = -1+job.getDebugLevel("db_tool") verify = -1+job.getDebugLevel("db_tool")
cmd = "SELECT * FROM "+table+";" attr = self.getDbAttributes(B.SVAL_NULL)
#mycursor = self.getConnector() sql = "SELECT * FROM "+attr[B.ATTR_DB_DATABASE]+"."+table
#mycursor.execute(cmd) if len(where) > 3:
#myresult = mycursor.fetchall() sql += " "+where
sql += ";"
self.comp.m.logInfo(sql)
connector = self.getConnector()
mycursor = connector.cursor()
mycursor.execute(sql)
myresult = mycursor.fetchall()
tdata[B.DATA_NODE_HEADER] = [] tdata[B.DATA_NODE_HEADER] = []
for f in self.comp.conf[B.DATA_NODE_DDL][table][B.DATA_NODE_HEADER]: for f in self.comp.conf[B.DATA_NODE_DDL][table][B.DATA_NODE_HEADER]:
tdata[B.DATA_NODE_HEADER].append(f) tdata[B.DATA_NODE_HEADER].append(f)
myresult = [] tdata[B.DATA_NODE_DATA] = []
for x in myresult: for x in myresult:
print(x) r = {}
self.comp.m.logInfo(cmd) i = 0
for f in self.comp.conf[B.DATA_NODE_DDL][table][B.DATA_NODE_HEADER]:
r[f] = x[i]
i += 1
tdata[B.DATA_NODE_DATA].append(r)
self.comp.m.logInfo(str(tdata))
return tdata return tdata
def deleteRows(self, table, job): def deleteRows(self, table, job, where=""):
""" method to delete rows from a database """ method to delete rows from a database
statement written in sql """ statement written in sql """
verify = -1+job.getDebugLevel("db_tool") verify = -1+job.getDebugLevel("db_tool")
cmd = "DELETE FROM "+table+";" attr = self.getDbAttributes(B.SVAL_NULL)
self.comp.m.logInfo(cmd) sql = "DELETE FROM "+attr[B.ATTR_DB_DATABASE]+"."+table
if len(where) > 3:
sql += " "+where
sql += ";"
self.comp.m.logInfo(sql)
self.execStatement(sql)
def updateRows(self, statement, job): def updateRows(self, statement, job):
""" method to delete rows from a database """ method to delete rows from a database
@ -55,43 +71,61 @@ class DbFcts(utils.dbrel_tool.DbFcts):
the rows will be interpreted by the ddl of the component the rows will be interpreted by the ddl of the component
""" """
verify = -1+job.getDebugLevel("db_tool") verify = -1+job.getDebugLevel("db_tool")
cmd = "INSERT INTO "+table+";" attr = self.getDbAttributes(B.SVAL_NULL)
header = "" sql = "INSERT INTO "+attr[B.ATTR_DB_DATABASE]+"."+table
for h in self.comp.conf[B.DATA_NODE_DDL][table][B.DATA_NODE_HEADER]: sql += " ( "+",".join(self.comp.conf[B.DATA_NODE_DDL][table][B.DATA_NODE_HEADER]) + " ) "
print(h) sql += " VALUES ( "
header += ", "+h for x in self.comp.conf[B.DATA_NODE_DDL][table][B.DATA_NODE_HEADER]:
cmd += " (" + header[1:]+" ) " sql += "%s, "
rowvalues = "" sql = sql[0:-2] + " )"
self.comp.m.logInfo(sql)
values = []
for r in rows: for r in rows:
print("r-----------------") rowvalues = []
print(r)
rowvalues = ""
cmd += "\n ( "
for h in self.comp.conf[B.DATA_NODE_DDL][table][B.DATA_NODE_HEADER]: for h in self.comp.conf[B.DATA_NODE_DDL][table][B.DATA_NODE_HEADER]:
print("h "+h) if (self.comp.conf[B.DATA_NODE_DDL][table][h] == D.TYPE_PK):
continue
if (h in r): if (h in r):
rowvalues += ", "+self.getDbValue(self.comp.conf[B.DATA_NODE_DDL][table][B.DATA_NODE_DATA][h], r[h]) rowvalues.append(self.getDbValue(self.comp.conf[B.DATA_NODE_DDL][table][h], r[h]))
else: else:
rowvalues += ", "+self.getDbValue(self.comp.conf[B.DATA_NODE_DDL][table][B.DATA_NODE_DATA][h], "") rowvalues.append(self.getDbValue(self.comp.conf[B.DATA_NODE_DDL][table][h], ""))
print("rv " + rowvalues) values.append( tuple(rowvalues))
cmd += rowvalues[1:]+" )," self.comp.m.logInfo(str(values))
cmd = cmd[0:-1]+";" try:
self.comp.m.logInfo(cmd) connector = self.getConnector()
mycursor = connector.cursor()
mycursor.executemany(sql, values)
connector.commit()
except Exception as e:
self.comp.m.setError("")
return
self.comp.m.setMsg(str(len(values))+" rows inserted into "+table)
def execStatement(self, dbconn, statement): def execStatement(self, statement):
""" add-on-method to execute the statement """ add-on-method to execute the statement
this method should only called by the class itself """ this method should only called by the class itself """
print("execStatement "+statement) connector = self.getConnector()
connector = mysql.connector.connect(
host=dbconn["host"],
user=dbconn["user"],
password=dbconn["passwd"]
)
cursor = connector.cursor() cursor = connector.cursor()
try: try:
cursor.execute(statement) cursor.execute(statement)
except: connector.commit()
except Exception as e:
if "CREATE INDEX " in statement: if "CREATE INDEX " in statement:
return return
raise Exception("DB-Exception "+statement) raise Exception("DB-Exception "+statement+"\n"+e.__str__())
print("Statement executed "+statement)
self.comp.m.setMsg("Statement executed")
def getConnector(self):
""" add-on-method to get the connector
this method should only called by the class itself """
job = self.job # basic.program.Job.getInstance()
attr = self.getDbAttributes(B.SVAL_NULL)
cnx = mysql.connector.connect(
host=attr[B.ATTR_DB_HOST],
user=attr[B.ATTR_DB_USER],
password=attr[B.ATTR_DB_PASSWD],
database=attr[B.ATTR_DB_DATABASE]
)
return cnx

14
utils/tdata_tool.py

@ -372,9 +372,13 @@ def splitFields(line, delimiter, job):
def writeCsvData(filename, tdata, comp, job): def writeCsvData(filename, tdata, comp, job):
text = "" text = ""
if B.DATA_NODE_TABLES in tdata: data = tdata
for k in tdata[B.DATA_NODE_TABLES]: for p in [B.DATA_NODE_TABLES, P.KEY_PRECOND, P.KEY_POSTCOND]:
text += buildCsvData(tdata, k, comp, job) if p in data:
print("data "+p)
data = data[p]
for k in data:
text += buildCsvData(data, k, comp, job)
text += "\n" text += "\n"
utils.file_tool.writeFileText(comp.m, job, filename, text) utils.file_tool.writeFileText(comp.m, job, filename, text)
@ -391,10 +395,6 @@ def buildCsvData(tdata, tableName, comp, job=None):
for k in [D.DATA_ATTR_DATE, D.DATA_ATTR_COUNT]: for k in [D.DATA_ATTR_DATE, D.DATA_ATTR_COUNT]:
if k in tdata: if k in tdata:
text += k+";"+str(tdata[k])+"\n" text += k+";"+str(tdata[k])+"\n"
x0 = "-------"+str(f"{B.DATA_NODE_TABLES=}")
x1 = "-------"+str(tableName)
x2 = str(utils.i18n_tool.I18n.getInstance(job).getText(f"{B.DATA_NODE_TABLES=}", job))
print(x0+" "+x1+" "+x2)
if tableName in tdata: if tableName in tdata:
actdata = tdata[tableName] actdata = tdata[tableName]
else: else:

61
webflask/auth.py

@ -1,12 +1,18 @@
# https://flask.palletsprojects.com/en/2.0.x/tutorial/views/ # https://flask.palletsprojects.com/en/2.0.x/tutorial/views/
# -------------------------------------------------------------- # --------------------------------------------------------------
import functools import functools
import traceback
import basic.user
import basic.program
import basic.constants as B
import basic.toolHandling
from flask import ( from flask import (
Blueprint, flash, g, redirect, render_template, request, session, url_for Blueprint, flash, g, redirect, render_template, request, session, url_for
) )
from werkzeug.security import check_password_hash, generate_password_hash from werkzeug.security import check_password_hash, generate_password_hash
import basic.program
from webflask.db import get_db from webflask.db import get_db
bp = Blueprint('auth', __name__, url_prefix='/auth') bp = Blueprint('auth', __name__, url_prefix='/auth')
@ -16,21 +22,24 @@ def login():
if request.method == 'POST': if request.method == 'POST':
username = request.form['username'] username = request.form['username']
password = request.form['password'] password = request.form['password']
db = get_db() job = basic.program.Job("webflask", {})
session['datest_job'] = job
dbtype = job.conf.confs[B.TOPIC_NODE_DB][B.ATTR_TYPE]
userentity = basic.user.User(job)
dbi = basic.toolHandling.getDbTool(job, userentity, dbtype)
error = None error = None
user = db.execute( row = dbi.selectRows("user", job, "WHERE username = \'" + username + "\'")
'SELECT * FROM user WHERE username = ?', (username,)
).fetchone()
if user is None: if row is None or len(row[B.DATA_NODE_DATA]) == 0:
error = 'Incorrect username.' error = 'Incorrect username.'
elif not check_password_hash(user['password'], password): elif not check_password_hash(row[B.DATA_NODE_DATA][0]['password'], password):
error = 'Incorrect password.' error = 'Incorrect password.'
if error is None: if error is None:
session.clear() session.clear()
session['user_id'] = user['id'] session['user_id'] = row[B.DATA_NODE_DATA][0]['id']
return redirect(url_for('index')) return redirect(url_for('testcase.overview'))
flash(error) flash(error)
@ -41,7 +50,10 @@ def register():
if request.method == 'POST': if request.method == 'POST':
username = request.form['username'] username = request.form['username']
password = request.form['password'] password = request.form['password']
db = get_db() job = basic.program.Job("webflask", {})
dbtype = job.conf.confs[B.TOPIC_NODE_DB][B.ATTR_TYPE]
dbi = basic.toolHandling.getDbTool(job, None, "mysql")
# db = get_db()
error = None error = None
if not username: if not username:
@ -51,13 +63,13 @@ def register():
if error is None: if error is None:
try: try:
db.execute( sql = "INSERT INTO datest.user (username, password) "
"INSERT INTO user (username, password) VALUES (?, ?)", sql += "VALUES ( '"+username+"', '"+generate_password_hash(password)+"');"
(username, generate_password_hash(password)), dbi.execStatement(job.conf.confs[B.TOPIC_NODE_DB], sql )
) #db.commit()
db.commit() except Exception as e:
except db.IntegrityError: error = str(e)
error = f"User {username} is already registered." # error = f"User {username} is already registered."
else: else:
return redirect(url_for("auth.login")) return redirect(url_for("auth.login"))
@ -72,9 +84,20 @@ def load_logged_in_user():
if user_id is None: if user_id is None:
g.user = None g.user = None
else: else:
g.user = get_db().execute( job = basic.program.Job("webflask", {})
'SELECT * FROM user WHERE id = ?', (user_id,) dbtype = job.conf.confs[B.TOPIC_NODE_DB][B.ATTR_TYPE]
).fetchone() userentity = basic.user.User(job)
dbi = basic.toolHandling.getDbTool(job, userentity, dbtype)
error = None
sql = "WHERE id = " + str(user_id)
row = dbi.selectRows("user", job, sql)
if len(row[B.DATA_NODE_DATA]) > 0:
g.user = row[B.DATA_NODE_DATA][0]
else:
g.user = None
# get_db().execute(
# 'SELECT * FROM user WHERE id = ?', (user_id,)
#).fetchone()
@bp.route('/logout') @bp.route('/logout')
def logout(): def logout():

4
webflask/db.py

@ -13,6 +13,10 @@ def init_db():
db.executescript(f.read().decode('utf8')) db.executescript(f.read().decode('utf8'))
def get_db(): def get_db():
"""
read config and check the db-connection
:return:
"""
if 'db' not in g: if 'db' not in g:
g.db = sqlite3.connect( g.db = sqlite3.connect(
current_app.config['DATABASE'], current_app.config['DATABASE'],

Loading…
Cancel
Save