Compare commits
2 Commits
sketch-red
...
username-f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
66aace247c | ||
|
|
bc3db06960 |
@@ -34,14 +34,22 @@ class TestCustomUser(unittest.TestCase):
|
|||||||
}).insert()
|
}).insert()
|
||||||
self.assertEqual(new_user.username[:8], "username")
|
self.assertEqual(new_user.username[:8], "username")
|
||||||
|
|
||||||
def test_with_hyphen_at_end(self):
|
def test_with_underscore_at_end(self):
|
||||||
new_user = frappe.get_doc({
|
new_user = frappe.get_doc({
|
||||||
"doctype": "User",
|
"doctype": "User",
|
||||||
"email": "test_with_hyphen_at_end@example.com",
|
"email": "test_with_underscore_at_end@example.com",
|
||||||
"first_name": "Username---"
|
"first_name": "Username___"
|
||||||
}).insert()
|
}).insert()
|
||||||
length = len(new_user.username)
|
self.assertNotEqual(new_user.username[-1], "_")
|
||||||
self.assertNotEqual(new_user.username[length-1], "-")
|
|
||||||
|
|
||||||
|
def test_with_short_first_name(self):
|
||||||
|
new_user = frappe.get_doc({
|
||||||
|
"doctype": "User",
|
||||||
|
"email": "test_with_short_first_name@example.com",
|
||||||
|
"first_name": "USN"
|
||||||
|
}).insert()
|
||||||
|
self.assertGreaterEqual(len(new_user.username), 4)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def tearDownClass(cls) -> None:
|
def tearDownClass(cls) -> None:
|
||||||
@@ -49,6 +57,7 @@ class TestCustomUser(unittest.TestCase):
|
|||||||
"test_with_basic_username@example.com",
|
"test_with_basic_username@example.com",
|
||||||
"test-without-username@example.com",
|
"test-without-username@example.com",
|
||||||
"test_with_illegal_characters@example.com",
|
"test_with_illegal_characters@example.com",
|
||||||
"test_with_hyphen_at_end@example.com"
|
"test_with_underscore_at_end@example.com",
|
||||||
|
"test_with_short_first_name@example.com"
|
||||||
]
|
]
|
||||||
frappe.db.delete("User", {"name": ["in", users]})
|
frappe.db.delete("User", {"name": ["in", users]})
|
||||||
|
|||||||
@@ -13,38 +13,45 @@ class CustomUser(User):
|
|||||||
self.validate_username_characters()
|
self.validate_username_characters()
|
||||||
|
|
||||||
def validate_username_characters(self):
|
def validate_username_characters(self):
|
||||||
|
if len(self.username):
|
||||||
|
underscore_condition = self.username[0] == "_" or self.username[-1] == "_"
|
||||||
|
else:
|
||||||
|
underscore_condition = ''
|
||||||
|
|
||||||
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) or underscore_condition:
|
||||||
self.username = self.remove_illegal_characters()
|
self.username = self.remove_illegal_characters()
|
||||||
|
|
||||||
if not self.username:
|
if len(self.username) < 4:
|
||||||
self.username = self.get_username_from_first_name()
|
self.username = self.email.replace("@", "").replace(".", "")
|
||||||
|
|
||||||
if self.username_exists():
|
while 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 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):
|
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