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