122 lines
4.2 KiB
Python
122 lines
4.2 KiB
Python
from pickle import FALSE
|
|
import re
|
|
from models.database import Database
|
|
from models.user import User
|
|
|
|
class Validator:
|
|
@staticmethod
|
|
def check_email(input):
|
|
regex = re.compile(r'([A-Za-z0-9]+[.-_])*[A-Za-z0-9]+@[A-Za-z0-9-]+(\.[A-Z|a-z]{2,})+')
|
|
return re.fullmatch(regex, input)
|
|
|
|
@staticmethod
|
|
def check_username(input):
|
|
if len(input) < 6 or len(input) > 10:
|
|
return False
|
|
|
|
if not f"{input[0]}".isalpha():
|
|
return False
|
|
|
|
regex = re.compile(r'^[0-9A-Za-z_.-]+$')
|
|
if not re.fullmatch(regex, input):
|
|
return False
|
|
|
|
user = User(Database.connection, None, input)
|
|
user.load_by_username()
|
|
|
|
if user.id != None:
|
|
return False
|
|
|
|
return True
|
|
|
|
@staticmethod
|
|
def check_password(input):
|
|
regex = re.compile(r'^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!%*#?&]{8,31}$')
|
|
return re.fullmatch(regex, input)
|
|
|
|
@staticmethod
|
|
def check_postcode(input):
|
|
regex = re.compile(r'\b\d{4}[A-Z]{2}\b')
|
|
return re.fullmatch(regex, input)
|
|
|
|
class InputMenu:
|
|
|
|
def __init__(self, title):
|
|
self._title = title
|
|
self._fields = []
|
|
|
|
def add_option(self, key, title, type, value, min, max, validator):
|
|
self._fields.append({"key": key, "title": title, "type": type, "value": value, "min": min, "max": max, "validator": validator})
|
|
return self
|
|
|
|
def do_input(self):
|
|
print("")
|
|
print(f"/--[ {self._title} ]-------------")
|
|
|
|
for i in range(len(self._fields)):
|
|
data = None
|
|
|
|
while True:
|
|
data = ""
|
|
|
|
if self._fields[i]['value'] == None:
|
|
data = input(f"| ({self._fields[i]['title']}) => ")
|
|
else:
|
|
data = input(f"| ({self._fields[i]['title']}) [{self._fields[i]['value']}] => ")
|
|
|
|
if self._fields[i]['type'] == "STR":
|
|
|
|
# Check if value was preset and entered value was not changed
|
|
if self._fields[i]['value'] != None and data == "":
|
|
break
|
|
|
|
# Check length
|
|
if len(data) >= self._fields[i]['min'] and len(data) <= self._fields[i]['max']:
|
|
|
|
# Check regex validator id used
|
|
if self._fields[i]['validator'] != None:
|
|
if self._fields[i]['validator'](data):
|
|
# Set value of current field
|
|
self._fields[i]['value'] = data
|
|
break
|
|
else:
|
|
print("| Invalid input! Try again. \n|")
|
|
continue
|
|
|
|
# Set value of current field
|
|
self._fields[i]['value'] = data
|
|
break
|
|
|
|
|
|
print("| Invalid input! Try again. \n|")
|
|
continue
|
|
|
|
elif self._fields[i]['type'] == "INT":
|
|
|
|
# Check if value was preset and entered value was not changed
|
|
if self._fields[i]['value'] != None and data == "":
|
|
break
|
|
|
|
# Try and parse string into int, set value if no errors occur
|
|
try:
|
|
num = int(data)
|
|
if num >= self._fields[i]['min'] and num <= self._fields[i]['max']:
|
|
self._fields[i]['value'] = num
|
|
break
|
|
else:
|
|
print("| Invalid input! Try again. \n|")
|
|
except:
|
|
print("| Invalid input! Try again. \n|")
|
|
else:
|
|
exit("Invalid input type!")
|
|
|
|
print(f"\-------------------------------")
|
|
print("")
|
|
return self
|
|
|
|
def get_value(self, key):
|
|
for i in range(len(self._fields)):
|
|
if self._fields[i]["key"] == key:
|
|
return self._fields[i]["value"]
|
|
return None
|