fix: username issues

This commit is contained in:
pateljannat
2021-08-09 16:54:02 +05:30
parent d5067a4bcd
commit bc3db06960

View File

@@ -15,36 +15,33 @@ class CustomUser(User):
def validate_username_characters(self):
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):
self.username = self.remove_illegal_characters()
if not self.username:
self.username = self.get_username_from_first_name()
if self.username_exists():
while self.username_exists():
self.username = self.remove_illegal_characters() + str(random.randint(0, 99))
else:
if not re.match("^[A-Za-z0-9_-]*$", self.username):
frappe.throw(_("Username can only contain alphabets, numbers, hyphen and underscore."))
if not self.username:
frappe.throw(_("Username already exists."))
if self.username[0] == "-" or self.username[len(self.username) - 1] == "-":
frappe.throw(_("First and Last character of username cannot be Hyphen(-)."))
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] == "_":
frappe.throw(_("First and Last character of username cannot be Underscore(_)."))
def get_username_from_first_name(self):
return frappe.scrub(self.first_name) + str(random.randint(0, 99))
def remove_illegal_characters(self):
username = ''.join([c for c in self.username if c.isalnum() or c in ['-', '_']])
while username[0] == "-" or username[len(username) - 1] == "-":
if username[0] == "-":
username = username[1:]
if username[len(username) - 1]:
username = username[:1]
return username
return re.sub("[^\w]+", "", self.username).strip("_")
def get_authored_courses(self) -> int:
"""Returns the number of courses authored by this user.