fix: conditions and tests

This commit is contained in:
pateljannat
2021-08-10 10:28:59 +05:30
parent bc3db06960
commit 66aace247c
2 changed files with 28 additions and 9 deletions

View File

@@ -13,17 +13,24 @@ class CustomUser(User):
self.validate_username_characters()
def validate_username_characters(self):
if self.is_new():
if len(self.username):
underscore_condition = self.username[0] == "_" or self.username[-1] == "_"
else:
underscore_condition = ''
if self.is_new():
if not self.username:
self.username = self.get_username_from_first_name()
if self.username.find(" "):
self.username.replace(" ", "")
if not re.match("^[A-Za-z0-9_]*$", self.username):
if not re.match("^[A-Za-z0-9_]*$", self.username) or underscore_condition:
self.username = self.remove_illegal_characters()
if len(self.username) < 4:
self.username = self.email.replace("@", "").replace(".", "")
while self.username_exists():
self.username = self.remove_illegal_characters() + str(random.randint(0, 99))
@@ -34,9 +41,12 @@ class CustomUser(User):
if not re.match("^[A-Za-z0-9_]*$", self.username):
frappe.throw(_("Username can only contain alphabets, numbers and unedrscore."))
if self.username[0] == "_" or self.username[len(self.username) - 1] == "_":
if underscore_condition:
frappe.throw(_("First and Last character of username cannot be Underscore(_)."))
if len(self.username) < 4:
frappe.throw(_("Username cannot be less than 4 characters"))
def get_username_from_first_name(self):
return frappe.scrub(self.first_name) + str(random.randint(0, 99))