Skip to content

Commit

Permalink
Use display_name for "username" instead of first_name, last_name (#1640)
Browse files Browse the repository at this point in the history
  • Loading branch information
daveoconnor committed Feb 20, 2025
1 parent ffc545b commit 092764b
Show file tree
Hide file tree
Showing 28 changed files with 134 additions and 162 deletions.
10 changes: 8 additions & 2 deletions core/githubhelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,8 +646,7 @@ def extract_contributor_data(self, contributor: str) -> dict:
data["email"] = None
data["valid_email"] = False

first_name, last_name = self.extract_names(contributor)
data["first_name"], data["last_name"] = first_name[:30], last_name[:30]
data["display_name"] = self.extract_name(contributor)

return data

Expand Down Expand Up @@ -681,6 +680,13 @@ def extract_email(self, val: str) -> str:
return
return email

def extract_name(self, val: str) -> str:
email = re.search("<.+>", val)
if email:
val = val.replace(email.group(), "")

return val.strip()

def extract_names(self, val: str) -> list:
"""
Returns a list of first, last names for the val argument.
Expand Down
36 changes: 30 additions & 6 deletions core/tests/test_githubhelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,33 @@ def test_parse_tag():
assert result == expected


def test_extract_name():
sample = "Tester Testerson <tester -at- gmail.com>"
expected = "Tester Testerson"
result = GithubDataParser().extract_name(sample)
assert expected == result

sample = "Tester Testerson"
expected = "Tester Testerson"
result = GithubDataParser().extract_name(sample)
assert expected == result

sample = "Tester de Testerson <tester -at- gmail.com>"
expected = "Tester de Testerson"
result = GithubDataParser().extract_name(sample)
assert expected == result

sample = "Tester de Testerson"
expected = "Tester de Testerson"
result = GithubDataParser().extract_name(sample)
assert expected == result

sample = "Various"
expected = "Various"
result = GithubDataParser().extract_name(sample)
assert expected == result


def test_extract_names():
sample = "Tester Testerson <tester -at- gmail.com>"
expected = ["Tester", "Testerson"]
Expand Down Expand Up @@ -255,20 +282,17 @@ def test_extract_contributor_data():
expected = {
"valid_email": True,
"email": "[email protected]",
"first_name": "Tester",
"last_name": "Testerson",
"display_name": "Tester Testerson",
}
result = GithubDataParser().extract_contributor_data(sample)
assert expected == result

sample = "Tester Testerson"
expected = {
"valid_email": False,
"first_name": "Tester",
"last_name": "Testerson",
"display_name": "Tester Testerson",
}
result = GithubDataParser().extract_contributor_data(sample)
assert expected["valid_email"] is False
assert expected["first_name"] == result["first_name"]
assert expected["last_name"] == result["last_name"]
assert expected["display_name"] == result["display_name"]
assert "email" in result
3 changes: 1 addition & 2 deletions libraries/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ def filter_queryset(self, queryset):
Q(name__icontains=value)
| Q(description__icontains=value)
| Q(categories__name__icontains=value)
| Q(authors__first_name__icontains=value)
| Q(authors__last_name__icontains=value)
| Q(authors__display_name__icontains=value)
)
return Library.objects.filter(f).distinct()[:5]

Expand Down
13 changes: 4 additions & 9 deletions libraries/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,22 +303,18 @@ def update_authors(self, obj: Library | LibraryVersion, authors=None):

if isinstance(authors, str):
authors = [authors]

for author in authors:
person_data = self.parser.extract_contributor_data(author)
email = person_data["email"]
user = User.objects.find_contributor(
email=person_data["email"],
first_name=person_data["first_name"],
last_name=person_data["last_name"],
display_name=person_data["display_name"],
)

if not user:
email = person_data.pop("email")
if not email:
email = generate_fake_email(
f"{person_data['first_name']} {person_data['last_name']}"
)
email = generate_fake_email(person_data["display_name"])
# With a new email, we may have a user record
user = User.objects.find_contributor(email=email)

Expand Down Expand Up @@ -346,13 +342,12 @@ def update_maintainers(self, obj, maintainers=None):
person_data = self.parser.extract_contributor_data(maintainer)
user = User.objects.find_contributor(
email=person_data["email"],
first_name=person_data["first_name"],
last_name=person_data["last_name"],
display_name=person_data["display_name"],
)

if not user:
email = person_data.pop("email") or generate_fake_email(
f"{person_data['first_name']} {person_data['last_name']}"
person_data["display_name"]
)
if not (user := User.objects.filter(email=email).first()):
user = User.objects.create_stub_user(email.lower(), **person_data)
Expand Down
2 changes: 1 addition & 1 deletion libraries/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ def get_commit_data_by_release(self):
def get_author_tag(self):
"""Format the authors for the author meta tag in the template."""
authors = self.object.authors.all()
author_names = [author.get_full_name() for author in authors]
author_names = [author.display_name for author in authors]
if len(author_names) > 1:
final_output = ", ".join(author_names[:-1]) + " and " + author_names[-1]
else:
Expand Down
3 changes: 2 additions & 1 deletion news/tests/test_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ def test_send_email_news_needs_moderation(
assert "news entry needs moderation" in msg.subject.lower()
assert entry.title in msg.body
assert escape(entry.title) not in msg.body
assert entry.author.get_display_name in msg.body
if entry.author.display_name:
assert entry.author.display_name in msg.body
assert entry.author.email in msg.body
assert request.build_absolute_uri(entry.get_absolute_url()) in msg.body
assert request.build_absolute_uri(reverse("news-moderate")) in msg.body
Expand Down
17 changes: 7 additions & 10 deletions news/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,18 +360,16 @@ def test_news_create_multiplexer(tp, user_type, request):


@pytest.mark.parametrize(
"has_image, has_first_name, has_last_name, should_redirect",
"has_image, has_display_name, should_redirect",
[
(True, True, False, False), # Has image and first name
(True, False, True, False), # Has image and last name
(True, True, True, False), # Has image, first name, and last name
(False, True, True, True), # Missing image
(True, False, False, True), # Missing names
(False, False, False, True), # Missing everything
(True, True, False), # Has image, display name
(False, True, True), # Missing image
(True, False, True), # Missing names
(False, False, True), # Missing everything
],
)
def test_news_create_requirements(
tp, user, has_image, has_first_name, has_last_name, should_redirect
tp, user, has_image, has_display_name, should_redirect
):
"""Users must have a profile photo and at least one of the names: first or last."""
url_name = "news-create"
Expand All @@ -389,8 +387,7 @@ def test_news_create_requirements(
else:
user.image = None

user.first_name = "Test" if has_first_name else ""
user.last_name = "User" if has_last_name else ""
user.display_name = "Test User" if has_display_name else ""
user.save()

with tp.login(user):
Expand Down
2 changes: 1 addition & 1 deletion news/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def dispatch(self, request, *args, **kwargs):
if request.user.is_authenticated:
missing_data = []

if not request.user.first_name and not request.user.last_name:
if not request.user.display_name:
missing_data.append("your name")

if not request.user.image:
Expand Down
6 changes: 3 additions & 3 deletions templates/admin/release_report_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,14 @@ <h2 class="mt-0">From the Fiscal Sponsorship Committee</h2>
</svg>
<div class="sponsor_message_copy dynamic-text p-[30px] mr-16 absolute z-20 max-h-[340px]">{{ version.sponsor_message|safe }}</div>
<div class="committee_members flex flex-wrap mt-2 text-sm text-center absolute" style="">
{% for user in committee_members|dictsort:"first_name" %}
{% for user in committee_members|dictsort:"display_name" %}
<figure class="w-32 m-2">
{% if user.hq_image %}
<img src="{{ user.hq_image_render.url }}" alt="{{ user.first_name }} {{ user.last_name }}" />
<img src="{{ user.hq_image_render.url }}" alt="{{ user.display_name }}" />
{% else %}
<img src="{% static 'img/Boost_Symbol_Transparent.svg' %}" alt="" />
{% endif %}
<figcaption class="p-1">{{user.first_name}} {{user.last_name}}</figcaption>
<figcaption class="p-1">{{user.display_name}}</figcaption>
</figure>
{% endfor %}
</div>
Expand Down
10 changes: 5 additions & 5 deletions templates/homepage.html
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,14 @@ <h3 class="pb-2 mb-4 text-lg md:text-2xl capitalize border-b border-gray-400 tex
<div class="bg-gray-300 dark:bg-slate rounded-lg w-[36px] h-[36px]">
{% if author.image %}
<img src="{{ author.image.url }}"
title="{{ author.get_full_name }}"
alt="{{ author.get_full_name }}"
title="{{ author.display_name }}"
alt="{{ author.display_name }}"
class="rounded-lg w-[36px] h-[36px]" />
{% else %}
<i class="text-3xl fas fa-user text-white dark:text-white/60" title="{{ author.get_full_name }}"></i>
<i class="text-3xl fas fa-user text-white dark:text-white/60" title="{{ author.display_name }}"></i>
{% endif %}
</div>
<span class="text-xs">{{ author.get_full_name }}</span>
<span class="text-xs">{{ author.display_name }}</span>
</div>
{% endfor %}
</div>
Expand Down Expand Up @@ -230,7 +230,7 @@ <h2 class="flex items-center pb-2 mb-3 text-lg md:text-2xl lg:text-2xl font-semi
</a>
</h2>

<p class="pt-0 pb-4 mx-auto w-full text-xs md:text-sm align-left">Posted on {{ entry.publish_at|date:"M jS, Y" }} by {{ entry.author.get_full_name }}</p>
<p class="pt-0 pb-4 mx-auto w-full text-xs md:text-sm align-left">Posted on {{ entry.publish_at|date:"M jS, Y" }} by {{ entry.author.display_name }}</p>
{% if entry.content %}
<div class="md:ml-[40px]">
<span class="text-sm md:text-base text-gray-500 dark:text-white/70">{{ entry.content|urlize|url_target_blank:'text-sky-600 dark:text-sky-300 hover:text-orange dark:hover:text-orange'|linebreaksbr|multi_truncate_middle:30|truncatechars_html:500 }}</span>
Expand Down
2 changes: 1 addition & 1 deletion templates/libraries/_library_grid_list_item.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ <h3 class="pb-2 text-xl md:text-2xl capitalize border-b border-gray-700">
<a class="link-header" href="{% url 'library-detail' library_slug=library_version.library.slug version_slug=version_str %}">{{ library_version.library.name }}</a>
{% for author in library.authors.all %}
{% if author.image %}
<img src="{{ author.image.url }}" class="inline float-right rounded w-[30px] ml-1" alt="{{ author.get_display_name }}" />
<img src="{{ author.image.url }}" class="inline float-right rounded w-[30px] ml-1" alt="{{ author.display_name }}" />
{% endif %}
{% endfor %}
</h3>
Expand Down
5 changes: 4 additions & 1 deletion templates/libraries/includes/library_preferences.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
<div class="flex relative space-x-3 justify-start">
{# Search #}

{% comment %} {% include "libraries/includes/search_input.html" %} {% endcomment %}
{% comment %}
LibrarySearchView can be removed if this isn't ever used again
{% include "libraries/includes/search_input.html" %}
{% endcomment %}

{# Display options #}
<div class="flex space-x-3">
Expand Down
8 changes: 4 additions & 4 deletions templates/news/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ <h1 class="text-3xl">{{ entry.title }}</h1>
<div class="space-x-3 mt-3 flex items-center">
{% if entry.author.image %}
<span class="inline-block h-[30px] w-[30px] overflow-hidden rounded border border-gray-400 dark:border-gray-500">
<img src="{{ entry.author.image_thumbnail.url }}" alt="{{ entry.author.get_full_name }}" class="h-full w-full object-cover">
<img src="{{ entry.author.image_thumbnail.url }}" alt="{{ entry.author.display_name }}" class="h-full w-full object-cover">
</span>
{% else %}
<span class="inline-block h-[30px] w-[30px] bg-white rounded dark:text-white dark:bg-slate border border-gray-400 dark:border-gray-500">
<i class="text-2xl fas fa-user ml-1" title="{{ entry.author.get_full_name }}"></i>
<i class="text-2xl fas fa-user ml-1" title="{{ entry.author.display_name }}"></i>
</span>
{% endif %}
{% if entry.author.get_full_name %}
{% if entry.author.display_name %}
<div class="inline-block p-0 m-0">
{{ entry.author.get_full_name }}<br />
{{ entry.author.display_name }}<br />
<span class="block py-0 text-xs">{{ entry.publish_at|date:'M jS, Y' }}</span>
</div>
{% endif %}
Expand Down
2 changes: 1 addition & 1 deletion templates/news/emails/needs_moderation.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ <h2 style="padding-top: 0; padding-bottom: 0; font-weight: 800 !important; verti
</table>
<p class="" style="line-height: 24px; font-size: 16px; width: 100%; margin: 0;" align="left">
Hello! You are receiving this email because you are a Boost news moderator. <br/>
The user <strong>{{ entry.author.get_display_name|default:entry.author.email }}</strong> has submitted a new {{ entry.tag }} that requires moderation:
The user <strong>{{ entry.author.display_name|default:entry.author.email }}</strong> has submitted a new {{ entry.tag }} that requires moderation:
</p>
</div>

Expand Down
2 changes: 1 addition & 1 deletion templates/news/emails/needs_moderation.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Hello! You are receiving this email because you are a Boost news moderator.

The user {{ entry.author.get_display_name|default:entry.author.email }} has submitted a new {{ entry.tag }} that requires moderation:
The user {{ entry.author.display_name|default:entry.author.email }} has submitted a new {{ entry.tag }} that requires moderation:

---
{% autoescape off %}
Expand Down
10 changes: 5 additions & 5 deletions templates/news/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ <h6 class="pb-1 text-base">Moderate</h6>
</div>
<div class="hidden pt-2 mt-4 text-xs text-left md:block min-w-[70px] relative group">
{% avatar user=entry.author %}
<span class="group-hover:opacity-100 transition-opacity bg-slate px-1 text-xs text-gray-100 rounded-sm absolute top-5 left-1/2 -translate-x-1/2 translate-y-full opacity-0 m-0 mx-auto w-auto">{{ entry.author.first_name}}&nbsp;{{ entry.author.last_name }}</span>
<span class="group-hover:opacity-100 transition-opacity bg-slate px-1 text-xs text-gray-100 rounded-sm absolute top-5 left-1/2 -translate-x-1/2 translate-y-full opacity-0 m-0 mx-auto w-auto">{{ entry.author.display_name }}</span>
</div>
<div class="block p-6 mb-3 md:mb-10 w-full bg-white md:rounded-lg md:shadow-lg md:ml-6 dark:bg-charcoal overflow-hidden">
{% can_edit news_item=entry as editable %}
Expand All @@ -134,7 +134,7 @@ <h2 class="py-0 my-0 text-xl font-semibold mr-4">
</h2>
</div>
<div class="md:block text-xs hidden ml-auto text-right w-[150px]">
{{ entry.author.get_full_name }}<br />
{{ entry.author.display_name }}<br />
<span class="text-xs">{{ entry.publish_at|date:"M jS, Y" }}</span>
</div>
</div>
Expand All @@ -147,7 +147,7 @@ <h2 class="py-0 my-0 text-xl font-semibold mr-4">
<!-- footer of cards only for mobile devices -->
<div class="flex border-t mt-4 border-gray-300 md:hidden">
<div class="mt-6 w-1/3 text-xs text-left capitalize">
{{ entry.author.get_full_name }}<br />
{{ entry.author.display_name }}<br />
{{ entry.display_publish_at }}
</div>
<div class="pt-6 w-1/3 text-center md:border-l-2 border-white dark:border-slate">
Expand Down Expand Up @@ -183,11 +183,11 @@ <h2 class="py-0 my-0 text-xl font-semibold mr-4">
<div class="pt-2 mt-4 w-1/3 text-xs text-right">
{% if entry.author.image %}
<span class="inline-block h-[36px] w-[36px] overflow-hidden rounded-lg ">
<img src="{{ entry.author.image.url }}" alt="{{ entry.author.get_full_name }}" class="h-full w-full object-cover">
<img src="{{ entry.author.image.url }}" alt="{{ entry.author.display_name }}" class="h-full w-full object-cover">
</span>
{% else %}
<span class="inline-block h-[36px] w-[36px] bg-white rounded-lg dark:text-white dark:bg-slate border">
<i class="text-2xl fas fa-user mr-1" title="{{ entry.author.get_full_name }}"></i>
<i class="text-2xl fas fa-user mr-1" title="{{ entry.author.display_name }}"></i>
</span>
{% endif %}
</div>
Expand Down
2 changes: 1 addition & 1 deletion templates/users/profile_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<div class="flex items-center pt-6 px-4 border-b md:px-0 md:border-0 border-slate">
{% avatar user=user image_size='h-[80px] w-[80px]' icon_size='text-7xl' %}
<div class="ml-4 text-base">
<span class="block">{{ user.get_full_name }}</span>
<span class="block">{{ user.display_name }}</span>
<span class="text-xs text-slate dark:text-white/60">Joined {{ user.date_joined }}</span>
</div>
</div>
Expand Down
8 changes: 3 additions & 5 deletions users/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ class EmailUserAdmin(UserAdmin):
_("Personal info"),
{
"fields": (
"first_name",
"last_name",
"display_name",
"github_username",
"valid_email",
"claimed",
Expand Down Expand Up @@ -54,13 +53,12 @@ class EmailUserAdmin(UserAdmin):
ordering = ("email",)
list_display = (
"email",
"first_name",
"last_name",
"display_name",
"is_staff",
"valid_email",
"claimed",
)
search_fields = ("email", "first_name", "last_name")
search_fields = ("email", "display_name")


admin.site.register(User, EmailUserAdmin)
8 changes: 7 additions & 1 deletion users/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,13 @@ class Meta:
class UserProfileForm(forms.ModelForm):
class Meta:
model = User
fields = ["email", "first_name", "last_name", "indicate_last_login_method"]
fields = ["email", "display_name", "indicate_last_login_method"]
labels = {
"display_name": "Username",
}
help_texts = {
"display_name": "Your name as it will be displayed across the site.",
}


class CustomClearableFileInput(forms.ClearableFileInput):
Expand Down
Loading

0 comments on commit 092764b

Please sign in to comment.