Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create an avatar tag to be usable anywhere we need an avatar. #1298

Merged
merged 4 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
CustomSignupView,
CustomSocialSignupViewView,
ProfileView,
UserAvatar,
UserViewSet,
)
from versions.api import ImportVersionsView, VersionViewSet
Expand Down Expand Up @@ -99,7 +98,6 @@
path("accounts/", include("allauth.urls")),
path("users/me/", CurrentUserProfileView.as_view(), name="profile-account"),
path("users/<int:pk>/", ProfileView.as_view(), name="profile-user"),
path("users/avatar/", UserAvatar.as_view(), name="user-avatar"),
path("api/v1/users/me/", CurrentUserAPIView.as_view(), name="current-user"),
path(
"api/v1/import-versions/",
Expand Down
10 changes: 7 additions & 3 deletions libraries/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import datetime
from itertools import chain
import re
from types import SimpleNamespace

import structlog
from django.contrib import messages
Expand Down Expand Up @@ -265,9 +266,9 @@ def get_context_data(self, **kwargs):
context["maintainers"] = self.get_maintainers(context["version"])
context["author_tag"] = self.get_author_tag()
exclude_maintainer_ids = [
getattr(x.commitauthor, "id")
x.commitauthor.id
for x in context["maintainers"]
if x.commitauthor
if getattr(x.commitauthor, "id", None)
]
context["top_contributors_release"] = self.get_top_contributors(
version=context["version"],
Expand Down Expand Up @@ -431,7 +432,10 @@ def get_maintainers(self, version):
if author_email := commit_authors.get(user.email.lower(), None):
user.commitauthor = author_email.author
else:
user.commitauthor = None
user.commitauthor = SimpleNamespace(
github_profile_url="",
avatar_url="",
)
return qs

def get_top_contributors(self, version=None, exclude=None):
Expand Down
19 changes: 17 additions & 2 deletions news/tests/fixtures.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import datetime
import io

from PIL import Image
import pytest
from django.db.models import Q
from django.contrib.auth.models import Group, Permission
Expand Down Expand Up @@ -98,15 +100,28 @@ def moderator_user(db, make_user):
# we could use `tp.make_user` but we need this fix to be released
# https://github.com/revsys/django-test-plus/issues/199
user = make_user(email="[email protected]", perms=["news.*"])
user.image = "test.png"

file = io.BytesIO()
filename = "test.png"
file.name = filename
image = Image.new("RGBA", size=(100, 100), color=(155, 0, 0))
image.save(file, "png")
file.seek(0)
user.image.save(filename, file)
user.save()
return user


@pytest.fixture
def regular_user(db, make_user):
user = make_user(email="[email protected]")
user.image = "test.png"
filename = "test.png"
file = io.BytesIO()
file.name = filename
image = Image.new("RGBA", size=(100, 100), color=(155, 0, 0))
image.save(file, "png")
file.seek(0)
user.image.save(filename, file)
user.save()
return user

Expand Down
13 changes: 12 additions & 1 deletion news/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from datetime import date, timedelta
from io import BytesIO

from PIL import Image
import pytest
from django.core import mail
from django.utils.text import slugify
Expand Down Expand Up @@ -377,7 +378,17 @@ def test_news_create_requirements(
url = tp.reverse(url_name)

# Setup user based on parameters
user.image = "test_image.jpg" if has_image else None
if has_image:
file = BytesIO()
filename = "test_image.jpg"
file.name = filename
image = Image.new("RGB", size=(100, 100), color=(155, 0, 0))
image.save(file, "jpeg")
file.seek(0)
user.image.save(filename, file)
else:
user.image = None

user.first_name = "Test" if has_first_name else ""
user.last_name = "User" if has_last_name else ""
user.save()
Expand Down
22 changes: 5 additions & 17 deletions templates/admin/library_report_detail.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% load static humanize %}
{% load static humanize avatar_tags %}
<!DOCTYPE html>
<html>
<head>
Expand Down Expand Up @@ -65,14 +65,8 @@ <h1 class="mx-auto">Boost {{ version.display_name }}</h1>
<div>
<div class="grid grid-cols-5 gap-2">
{% for author in top_contributors_release_overall %}
<div class="flex flex-col gap-y-2 w-20">
{% if author.avatar_url %}
<img src="{{ author.avatar_url }}"
alt="{{ author.name }}"
class="w-8 h-8 rounded mx-auto">
{% else %}
<div class="w-8 h-8 rounded bg-gray-300 mx-auto"></div>
{% endif %}
<div class="flex flex-col gap-y-2 w-20 items-center">
{% avatar commitauthor=author %}
<div class="w-full flex flex-col">
<div class="text-[0.6rem] overflow-ellipsis overflow-hidden whitespace-nowrap w-full text-center">
{{ author.name }}
Expand Down Expand Up @@ -111,14 +105,8 @@ <h4>There were {{ item.version_count.commit_count }} commits in release {{ versi
<div class="mb-2">Top Contributors</div>
<div class="grid grid-cols-5 gap-2 flex-wrap">
{% for author in item.top_contributors_release %}
<div class="flex flex-col gap-y-2 w-20">
{% if author.avatar_url %}
<img src="{{ author.avatar_url }}"
alt="{{ author.name }}"
class="w-8 h-8 rounded mx-auto">
{% else %}
<div class="w-8 h-8 rounded bg-gray-300 mx-auto"></div>
{% endif %}
<div class="flex flex-col gap-y-2 w-20 items-center">
{% avatar commitauthor=author %}
<div class="w-full flex flex-col justify-center items-center">
<div class="text-[0.6rem] overflow-ellipsis overflow-hidden whitespace-nowrap w-full text-center">
{{ author.name }}
Expand Down
18 changes: 3 additions & 15 deletions templates/admin/library_stat_detail.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% extends "admin/base_site.html" %}
{% load static %}
{% load static avatar_tags %}
{% block extrahead %}
<link href="{% static 'css/styles.css' %}" rel="stylesheet" />
{% endblock extrahead %}
Expand Down Expand Up @@ -33,12 +33,7 @@ <h3 class="mb-2">
<div class="flex flex-col gap-y-1">
{% for author in commits_per_author %}
<div class="flex gap-x-1">
{% if author.avatar_url %}
<img src="{{ author.avatar_url }}" alt="github avatar" class="w-8 rounded">
{% else %}
<div class="w-8 h-8 rounded bg-silver">
</div>
{% endif %}
{% avatar commitauthor=author %}
<div>
{{ author.name }}: {{ author.count }}
</div>
Expand All @@ -60,14 +55,7 @@ <h3 class="my-2">
<hr>
{% endifchanged %}
<div class="flex gap-x-1">
{% if item.commit__author__avatar_url %}
<img src="{{ item.commit__author__avatar_url }}"
alt="github avatar"
class="w-8 rounded">
{% else %}
<div class="w-8 h-8 rounded bg-silver">
</div>
{% endif %}
{% base_avatar name=item.commit__author__name image_url=item.commit__author__avatar_url href=None %}
<div>
{{ item.commit__author__name }}: {{ item.count }}
</div>
Expand Down
20 changes: 3 additions & 17 deletions templates/includes/_header.html
Original file line number Diff line number Diff line change
Expand Up @@ -403,23 +403,9 @@
{% if not disable_theme_switcher %}
<i id="light-dark" class="fas fa-sun icon-link " onclick="changeTheme()"></i>
{% endif %}
{% comment %}
The html and context for the user avatar and profile dropdown menu are coming from the view as
and html fragment fetched view htmx get request and inserted into #avatar
{% endcomment %}
<span x-data="{ 'userOpen': false }" class="menu-link" style="position: relative; width: 2rem;">
<div hx-get="/users/avatar/"
hx-trigger="load"
hx-target="#avatar"
hx-indicator=".htmx-indicator">
</div>
<span id="avatar">
<svg aria-hidden="true" viewBox="0 0 100 101" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z" fill="currentColor"/>
<path d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z" fill="currentFill"/>
</svg>
</span>
</span>
<span x-data="{ 'userOpen': false }" class="menu-link w-8 relative">
{% include "users/includes/header_avatar.html" %}
</span>
</div>
</div>
<script>
Expand Down
59 changes: 7 additions & 52 deletions templates/libraries/detail.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{% extends "base.html" %}
{% load i18n %}
{% load static %}
{% load i18n static avatar_tags %}

{% block title %}{{ object.display_name }} ({{ version.display_name }}){% endblock %}
{% block description %}{% trans object.description %}{% endblock %}
Expand Down Expand Up @@ -117,65 +116,21 @@
<!-- Avatars -->
<h2 class="text-2xl">Maintainers &amp; Contributors</h2>
<div class="flex flex-col gap-y-4">
<div class="flex flex-wrap justify-center">
<div class="flex flex-wrap justify-center gap-2">
{% for user in maintainers %}
<a {% if user.commitauthor.github_profile_url %}href="{{ user.commitauthor.github_profile_url }}"{% endif %}>
<div class="p-1 md:p-2 w-min text-center flex flex-col items-center">
<div class="bg-gray-300 dark:bg-slate rounded-lg h-12 w-12">
{% if user.image or user.commitauthor.avatar_url %}
<img src="{% if user.image %}{{ user.image.url}}{% else %}{{ user.commitauthor.avatar_url }}{% endif %}"
title="{{ user.get_full_name }}"
alt="{{ user.get_full_name }}"
class="rounded-lg h-12 w-12 object-cover" />
{% else %}
<i class="h-12 w-12 m-auto text-3xl align-middle fas fa-user text-white dark:text-white/60 " title="{{ user.get_full_name }}"></i>
{% endif %}
</div>
<span class="text-xs">{{ user.get_full_name }}</span>
</div>
</a>
{% avatar user=user commitauthor=user.commitauthor image_size="h-12 w-12" is_show_name=True %}
{% endfor %}
</div>

<div class="flex flex-wrap justify-center">
<div class="flex flex-wrap justify-center gap-2">
{% for author in top_contributors_release %}
<a
{% if author.github_profile_url %}
href="{{ author.github_profile_url }}"
{% endif %}
>
<div class="p-1 md:p-2 flex text-center justify-center" title="{{ author.name }}">
<div class="bg-gray-300 dark:bg-slate rounded-lg h-9 w-9 overflow-hidden">
{% if author.avatar_url %}
<img src="{{ author.avatar_url }}"
title="{{ author.name }}"
alt="{{ author.name }}"
class="rounded-lg h-9 w-9 object-cover" />
{% else %}
<i class="h-9 w-9 m-auto text-3xl fas fa-user text-white dark:text-white/60 " title="{{ author.name }}"></i>
{% endif %}
</div>
</div>
</a>
{% avatar commitauthor=author %}
{% endfor %}
</div>

<div class="flex flex-wrap justify-center">
<div class="flex flex-wrap justify-center gap-2">
{% for author in top_contributors_overall %}
<a {% if author.github_profile_url %}href="{{ author.github_profile_url }}"{% endif %}>
<div class="p-1 md:p-2 flex text-center justify-center" title="{{ author.name }}">
<div class="bg-gray-300 dark:bg-slate rounded-lg h-9 w-9 overflow-hidden">
{% if author.avatar_url %}
<img src="{{ author.avatar_url }}"
title="{{ author.name }}"
alt="{{ author.name }}"
class="rounded-lg h-9 w-9 object-cover" />
{% else %}
<i class="h-9 w-9 m-auto text-3xl fas fa-user text-white dark:text-white/60 " title="{{ author.name }}"></i>
{% endif %}
</div>
</div>
</a>
{% avatar commitauthor=author %}
{% endfor %}
</div>

Expand Down
28 changes: 21 additions & 7 deletions templates/partials/avatar.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
{% if profile.avatar %}
<img class="avatar" src="{{ profile.avatar.url }}" alt="{{ profile.user }}" />
{% elif show_placeholder %}
<span class="avatar empty">
<i class="far fa-user fa-4x" ></i>
</span>
{% endif %}
{# Intended to be used with a django tag: avatar_tags.avatar #}
{% with av_size=av_size|default:"w-9 h-9" av_show_name=av_show_name|default:False av_title=av_title|default:av_name %}
{% with av_alt=av_alt|default:av_name av_icon_size=av_icon_size|default:"text-3xl" %}
<a {% if av_href and av_is_link %}href="{{ av_href }}"{% endif %}>
<div class="w-min text-center flex flex-col justify-center items-center">
<div class="bg-gray-300 dark:bg-slate rounded-lg {{ av_size }} flex items-center justify-center relative" title="{{ av_title }}">
{% if av_image_url %}
<img src="{{ av_image_url }}"
alt="{{ av_alt }}"
class="rounded-lg {{ av_size }} object-cover mx-auto" />
{% else %}
<i class="{{ av_icon_size }} align-middle fas fa-user text-white dark:text-white/60"></i>
{% endif %}
</div>
{% if av_show_name %}
<span class="text-xs">{{ av_name }}</span>
{% endif %}
</div>
</a>
{% endwith %}
{% endwith %}
Loading