finished add members

This commit is contained in:
2022-10-04 20:53:29 +02:00
parent 688867250b
commit 45ad63fe3c
5 changed files with 86 additions and 24 deletions

View File

@@ -1,3 +1,6 @@
import re
EMAIL_VALIDATOR = re.compile(r'([A-Za-z0-9]+[.-_])*[A-Za-z0-9]+@[A-Za-z0-9-]+(\.[A-Z|a-z]{2,})+')
class InputMenu:
@@ -5,8 +8,8 @@ class InputMenu:
self._title = title
self._fields = []
def add_option(self, key, type, title, min, max):
self._fields.append({"key": key, "title": title, "type": type, "value": None, "min": min, "max": max})
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):
@@ -17,18 +20,50 @@ class InputMenu:
data = None
while True:
data = input(f"| ({self._fields[i]['title']}) => ")
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":
if len(data) > self._fields[i]['min'] and len(data) <= self._fields[i]['max']:
# 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 re.fullmatch(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
else:
print("| Invalid input! Try again. \n|")
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']:
if num >= self._fields[i]['min'] and num <= self._fields[i]['max']:
self._fields[i]['value'] = data
break
else: