@ -156,7 +156,7 @@ def checkName(name: str) -> bool:
return True
return True
def checkImportFile ( path : str , db : str ):
def checkImportFile ( path : str , db : str , test_existence : bool = True ):
""" Checks an CSV file against most of the validators and prints an Error message with the line number corresponding
""" Checks an CSV file against most of the validators and prints an Error message with the line number corresponding
to said failure . . Those includes : checkName , checkUsernameCharacters ,
to said failure . . Those includes : checkName , checkUsernameCharacters ,
ckeckUsernameLength , duplicate usernames ( in the CSV ) , checkSSHKey , checkEmail , checkUserExists , checkUserInDB ,
ckeckUsernameLength , duplicate usernames ( in the CSV ) , checkSSHKey , checkEmail , checkUserExists , checkUserInDB ,
@ -166,54 +166,64 @@ def checkImportFile(path: str, db: str):
: type path : str
: type path : str
: param db : Path to database file ( SQLite )
: param db : Path to database file ( SQLite )
: type db : str
: type db : str
: param test_existence : Flag , checking users existence while true , won ' t when set to false. Default ' s to true .
: type test_existence : bool
: return : Str when Failure , True when success ( All tests passed )
: return : Str when Failure , True when success ( All tests passed )
: rtype : Str or None
: rtype : Str or None
"""
"""
err str = " "
err _ str = " "
valid = True
valid = True
l n = 1 # line number
l i ne = 1 # line number
valid_names_list = [ ]
valid_names_list = [ ]
with open ( path , ' r ' , newline = ' ' ) as f :
with open ( path , ' r ' , newline = ' ' ) as f ile_handle :
reader = csv . DictReader ( f )
reader = csv . DictReader ( f ile_handle )
for row in reader :
for row in reader :
# if any of this fails move on to the next user, just print a relatively helpful message lel
# if any of this fails move on to the next user, just print a relatively helpful message lel
if not lib . Validator . checkName ( row [ " name " ] ) :
if not lib . Validator . checkName ( row [ " name " ] ) :
err str + = f " Line { l n} : Name: ' { row [ ' name ' ] } ' seems not legit. Character followed by character should " \
err _ str + = f " Line { l i ne } : Name: ' { row [ ' name ' ] } ' seems not legit. " \
f " be correct.\n "
f " Character followed by character should be correct.\n "
valid = False
valid = False
if not lib . Validator . checkUsernameCharacters ( row [ " username " ] ) :
if not lib . Validator . checkUsernameCharacters ( row [ " username " ] ) :
errstr + = ( f " Line { ln } : Username contains unsupported characters or starts with a number: ' "
err_str + = ( f " Line { line } : "
f " { row [ ' username ' ] } ' . \n " )
f " Username contains unsupported characters or starts with a number: ' "
f " { row [ ' username ' ] } ' . \n " )
valid = False
valid = False
if not lib . Validator . checkUsernameLength ( row [ " username " ] ) :
if not lib . Validator . checkUsernameLength ( row [ " username " ] ) :
errstr + = f " Line { ln } : Username ' { row [ ' username ' ] } ' is either too long(>16) or short(<3) \n "
err_str + = f " Line { line } : " \
f " Username ' { row [ ' username ' ] } ' is either too long(>16) or short(<3) \n "
valid = False
valid = False
# dup checking
# dup checking
if row [ " username " ] in valid_names_list :
if row [ " username " ] in valid_names_list :
err str + = f " Line { l n} : Duplicate Username { row [ ' username ' ] } ! \n "
err _ str + = f " Line { l i ne } : Duplicate Username { row [ ' username ' ] } ! \n "
valid = False
valid = False
else :
else :
valid_names_list . append ( row [ " username " ] )
valid_names_list . append ( row [ " username " ] )
# dup end
# dup end
if not lib . Validator . checkSSHKey ( row [ " pubkey " ] ) :
if not lib . Validator . checkSSHKey ( row [ " pubkey " ] ) :
errstr + = f " Line { ln } : Following SSH-Key of user ' { row [ ' username ' ] } ' isn ' t valid: " \
err_str + = f " Line { line } : " \
f " ' { row [ ' pubkey ' ] } ' . \n "
f " Following SSH-Key of user ' { row [ ' username ' ] } ' isn ' t valid: " \
f " ' { row [ ' pubkey ' ] } ' . \n "
valid = False
valid = False
if not lib . Validator . checkEmail ( row [ " email " ] ) :
if not lib . Validator . checkEmail ( row [ " email " ] ) :
errstr + = f " Line { ln } : E-Mail address of user ' { row [ ' username ' ] } ' ' { row [ ' email ' ] } ' is not valid. \n "
err_str + = \
f " Line { line } : " \
f " E-Mail address of user ' { row [ ' username ' ] } ' ' { row [ ' email ' ] } ' is not valid. \n "
valid = False
valid = False
if lib . Validator . checkUserExists ( row [ " username " ] ) or checkUserInDB ( row [ " username " ] , db ) :
if lib . Validator . checkUserExists ( row [ " username " ] ) or checkUserInDB ( row [ " username " ] , db ) :
errstr + = f " Line { ln } : User ' { row [ ' username ' ] } ' already exists. \n "
if test_existence :
valid = False
err_str + = f " Line { line } : User ' { row [ ' username ' ] } ' already exists. \n "
valid = False
else :
pass
if not lib . Validator . checkDatetimeFormat ( row [ " timestamp " ] ) :
if not lib . Validator . checkDatetimeFormat ( row [ " timestamp " ] ) :
errstr + = f " Line { ln } : Timestamp ' { row [ ' timestamp ' ] } ' from user ' { row [ ' username ' ] } ' is invalid. \n "
err_str + = f " Line { line } : Timestamp ' { row [ ' timestamp ' ] } ' " \
f " from user ' { row [ ' username ' ] } ' is invalid. \n "
valid = False
valid = False
if int ( row [ " status " ] ) > 1 or int ( row [ " status " ] ) < 0 :
if int ( row [ " status " ] ) > 1 or int ( row [ " status " ] ) < 0 :
err str + = f " Line { l n} : Status ' { row [ ' status ' ] } ' MUST be either 0 or 1. \n "
err _ str + = f " Line { l i ne } : Status ' { row [ ' status ' ] } ' MUST be either 0 or 1. \n "
valid = False
valid = False
l n + = 1
l i ne + = 1
if valid :
if valid :
return True
err_str = True
else :
return err_str
return errstr