Abort on errors in import file before even trying to activate

Moves every check regarding the imported file outside of import.py into
the validator. Also removes every follow-up checks regarding it out of
import.py.

Looks a whole lot cleaner now!
pull/4/head
Darksider3 5 years ago
parent 2ab27aa019
commit 77a31a44d1

@ -2,7 +2,6 @@ import lib.CFG as CFG
import csv
import os
import lib.UserExceptions
import lib.validator
def ImportFromFile(fname: str = CFG.args.file, db: str = CFG.REG_FILE, userids: tuple = tuple([])):
@ -17,33 +16,17 @@ def ImportFromFile(fname: str = CFG.args.file, db: str = CFG.REG_FILE, userids:
# noinspection PyBroadException
try:
with open(fname, 'r', newline='') as f:
import lib.validator
err = lib.validator.checkImportFile(f)
if err is not True:
print(err)
exit(0)
import lib.sqlitedb
import lib.System
sysctl = lib.System.System()
sql = lib.sqlitedb.SQLitedb(CFG.REG_FILE)
reader = csv.DictReader(f) # @TODO csv.Sniffer to compare? When yes, give force-accept option
for row in reader:
db_insert = False
# if any of this fails move on to the next user, just print a relatively helpful message lel
if not lib.validator.checkUsernameCharacters(row["username"]):
print(f"The username contains unsupported characters or starts with a number: "
f"{row['username']}. Skipping.")
continue
if not lib.validator.checkUsernameLength(row["username"]):
print(f"The username {row['username']} is either too long(>16) or short(<3). Skipping.")
continue
if not lib.validator.checkSSHKey(row["pubkey"]):
print(f"Following SSH-Key isn't valid: {row['pubkey']}. Skipping.")
continue
if not lib.validator.checkEmail(row["email"]):
print(f"The E-Mail address {row['email']} is not valid. Skipping")
continue
if not lib.validator.checkUserExists(row["username"]):
print(f"The user '{row['username']}' already exists. Skipping.")
continue
if not lib.validator.checkDatetimeFormat(row["timestamp"]):
print(f"The timestamp '{row['timestamp']}' from user '{row['username']}' is invalid. Skipping.")
continue
if row["status"] == "1":
try:
sysctl.register(row["username"])
@ -63,13 +46,9 @@ def ImportFromFile(fname: str = CFG.args.file, db: str = CFG.REG_FILE, userids:
except Exception as E: # @TODO well less broad is hard to achieve Kappa
print(E)
continue
db_insert = True
elif row["status"] == "0":
print(row['username'] + " not approved, therefore not registered.")
db_insert = True
try:
if not db_insert:
continue
sql.safequery(
"INSERT INTO `applications` (username, name, timestamp, email, pubkey, status) "
"VALUES (?,?,?,?,?,?)", tuple([row["username"], row["name"], row["timestamp"],

@ -76,3 +76,39 @@ def checkDatetimeFormat(form: str):
except ValueError:
return False
return True
def checkImportFile(f):
import csv
reador = csv.DictReader(f)
error_list = str()
valid = True
ln = 1 # line number
for row in reador:
# if any of this fails move on to the next user, just print a relatively helpful message lel
if not lib.validator.checkUsernameCharacters(row["username"]):
error_list += (f"Line {ln}: The username contains unsupported characters or starts with a number: '"
f"{row['username']}'.\n")
valid = False
if not lib.validator.checkUsernameLength(row["username"]):
error_list += f"Line {ln}: The username '{row['username']}' is either too long(>16) or short(<3)\n"
valid = False
if not lib.validator.checkSSHKey(row["pubkey"]):
error_list += f"Line {ln}: Following SSH-Key from user '{row['username']}' isn't valid: '{row['pubkey']}'."\
f"\n"
valid = False
if not lib.validator.checkEmail(row["email"]):
error_list += f"Line {ln}: The E-Mail address of user '{row['username']}' '{row['email']}' is not valid.\n"
valid = False
if not lib.validator.checkUserExists(row["username"]):
error_list += f"Line {ln}: The user '{row['username']}' already exists.\n"
valid = False
if not lib.validator.checkDatetimeFormat(row["timestamp"]):
error_list += f"Line {ln}: The timestamp '{row['timestamp']}' from user '{row['username']}' is invalid.\n"
valid = False
ln += 1
if valid:
return True
else:
return error_list

Loading…
Cancel
Save