Skip to content

Commit

Permalink
Merge pull request #519 from pateljannat/fix-username
Browse files Browse the repository at this point in the history
fix: remove space from username
  • Loading branch information
pateljannat committed May 22, 2023
2 parents dc65bff + a0b06be commit daeeb69
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 89 deletions.
10 changes: 0 additions & 10 deletions lms/overrides/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,6 @@ def test_without_username(self):
user = new_user("Username", "test-without-username@example.com")
self.assertTrue(user.username)

def test_with_illegal_characters(self):
user = new_user("Username$$", "test_with_illegal_characters@example.com")
self.assertEqual(user.username[:8], "username")

def test_with_underscore_at_end(self):
user = new_user("Username___", "test_with_underscore_at_end@example.com")
self.assertNotEqual(user.username[-1], "_")

def test_with_short_first_name(self):
user = new_user("USN", "test_with_short_first_name@example.com")
self.assertGreaterEqual(len(user.username), 4)
Expand All @@ -37,8 +29,6 @@ def tearDownClass(cls) -> None:
users = [
"test_with_basic_username@example.com",
"test-without-username@example.com",
"test_with_illegal_characters@example.com",
"test_with_underscore_at_end@example.com",
"test_with_short_first_name@example.com",
]
frappe.db.delete("User", {"name": ["in", users]})
57 changes: 11 additions & 46 deletions lms/overrides/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,64 +9,29 @@
from frappe.utils import cint, escape_html, random_string
from frappe.website.utils import is_signup_disabled
from lms.lms.utils import validate_image
from frappe.website.utils import cleanup_page_name
from frappe.model.naming import append_number_if_name_exists
from lms.widgets import Widgets


class CustomUser(User):
def validate(self):
super().validate()
self.validate_username_characters()
self.validate_username_duplicates()
self.validate_completion()
self.user_image = validate_image(self.user_image)
self.cover_image = validate_image(self.cover_image)

def validate_username_characters(self):
if self.username and len(self.username):
other_conditions = (
self.username[0] == "_" or self.username[-1] == "_" or "-" in self.username
def validate_username_duplicates(self):
while not self.username or self.username_exists():
self.username = append_number_if_name_exists(
self.doctype, cleanup_page_name(self.full_name), fieldname="username"
)
else:
other_conditions = ""

regex = re.compile(r"[@!#$%^&*()<>?/\|}{~:-]")

if self.is_new():
if not self.username:
self.username = self.get_username_from_first_name()

if self.username.find(" "):
self.username.replace(" ", "")

if len(self.username) < 4:
self.username = self.email.replace("@", "").replace(".", "")

if regex.search(self.username) or other_conditions:
self.username = self.remove_illegal_characters()

while self.username_exists():
self.username = self.remove_illegal_characters() + str(random.randint(0, 99))

else:
if not self.username:
frappe.throw(_("Username already exists."))

if regex.search(self.username):
frappe.throw(_("Username can only contain alphabets, numbers and underscore."))

if other_conditions:
if "-" in self.username:
frappe.throw(_("Username cannot contain a Hyphen(-)"))
else:
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))
if " " in self.username:
self.username = self.username.replace(" ", "")

def remove_illegal_characters(self):
return re.sub(r"[^\w]+", "", self.username).strip("_")
if len(self.username) < 4:
self.username = self.email.replace("@", "").replace(".", "")

def validate_skills(self):
unique_skills = []
Expand Down
8 changes: 4 additions & 4 deletions lms/page_renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ def __init__(self, path, http_status_code):
self.renderer = None

def can_render(self):
if "." in self.path:
return False
"""if "." in self.path:
return False"""

# has prefix and path starts with prefix?
prefix = get_profile_url_prefix().lstrip("/")
Expand All @@ -67,8 +67,8 @@ def can_render(self):

# not a userpage?
username = self.get_username()
if RE_INVALID_USERNAME.search(username):
return False
""" if RE_INVALID_USERNAME.search(username):
return False """
# if there is prefix then we can allow all usernames
if prefix:
return True
Expand Down
51 changes: 26 additions & 25 deletions lms/public/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
--text-3-5xl: 24px;
--text-3-8xl: 34px;
--text-4xl: 36px;
--checkbox-gradient: linear-gradient(180deg, #3d4142 -124.51%, var(--primary) 100%);
}

.nav-link .course-list-count {
Expand Down Expand Up @@ -651,8 +650,12 @@ input[type=checkbox] {
}

.avatar-xl {
width: 112px;
height: 112px;
width: 8rem;
height: 8rem;
}

.avatar-xl .standard-image {
border: 4px solid #ffffff;
}

@media (max-width: 500px) {
Expand Down Expand Up @@ -791,9 +794,9 @@ input[type=checkbox] {
}

.profile-banner {
height: 248px;
background-size: cover;
background-position: center;
height: 248px;
background-size: cover;
background-position: center;
}

@media (max-width: 500px) {
Expand All @@ -803,24 +806,24 @@ input[type=checkbox] {
}

.profile-info {
height: 90px;
background: #ffffff;
border-radius: 0px 0px 8px 8px;
font-size: var(--text-sm);
display: flex;
flex-direction: column;
margin-bottom: 2.5rem;
padding-left: 200px;
padding-right: 1rem;
box-shadow: var(--shadow-sm);
height: 90px;
background: #ffffff;
border-radius: 0px 0px 8px 8px;
font-size: var(--text-sm);
display: flex;
flex-direction: column;
margin-bottom: 2.5rem;
padding-left: 200px;
padding-right: 1rem;
border: 1px solid var(--gray-300);
}

@media (max-width: 550px) {
.profile-info {
align-items: flex-end;
padding-left: 0;
height: 150px;
}
.profile-info {
align-items: flex-end;
padding-left: 0;
height: 150px;
}
}

.profile-avatar {
Expand Down Expand Up @@ -1524,10 +1527,6 @@ pre {
margin: 0 1rem;
}

.profile-page-body {
background-color: var(--gray-50);
}

.profile-column-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
Expand Down Expand Up @@ -1802,6 +1801,8 @@ li {
}

.role {
display: flex;
align-items: center;
margin-bottom: 0;
cursor: pointer;
}
Expand Down
8 changes: 4 additions & 4 deletions lms/www/profiles/profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@

<div class="tab-content">
<div class="tab-pane active" id="profile" role="tabpanel" aria-labelledby="profile">
<div class="common-card-style column-card mt-5">
<div class="">
{{ About(member) }}
{{ EducationDetails(member) }}
{{ WorkDetails(member) }}
Expand Down Expand Up @@ -126,7 +126,7 @@
<div class="container">
<div class="profile-banner" style="background-image: url({{ cover_image | urlencode }})">
<div class="profile-avatar">
{{ widgets.Avatar(member=member, avatar_class="avatar-square") }}
{{ widgets.Avatar(member=member, avatar_class="avatar-xl") }}
</div>
</div>

Expand Down Expand Up @@ -201,9 +201,9 @@
{% macro RoleSettings(member) %}
{% if has_course_moderator_role() %}
<div class="">
<div class="common-card-style column-card">
<div class="">
<div class="course-home-headings"> {{ _("Role Settings") }} </div>
<div class="medium">
<div class="d-flex">
<label class="role">
<input type="checkbox" id="course-creator" data-role="Course Creator"
{% if has_course_instructor_role(member.name) %} checked {% endif %}>
Expand Down

0 comments on commit daeeb69

Please sign in to comment.