lib: add configurable avatars, set to none mode for tests

This commit is contained in:
Jens Langhammer 2020-10-22 13:55:37 +02:00
parent 7b5a0964b2
commit bbcf58705f
6 changed files with 26 additions and 32 deletions

View File

@ -53,7 +53,7 @@
{{ user.username }}
</a>
</div>
<img class="pf-c-avatar" src="{% gravatar user.email %}" alt="">
<img class="pf-c-avatar" src="{% avatar user %}" alt="">
</div>
</header>
{% block page_content %}

View File

@ -7,7 +7,7 @@
<div class="pf-c-form__group">
<div class="form-control-static">
<div class="left">
<img class="pf-c-avatar" src="{% gravatar user.email %}" alt="">
<img class="pf-c-avatar" src="{% avatar user %}" alt="">
{{ user.username }}
</div>
<div class="right">

View File

@ -22,6 +22,7 @@ error_reporting:
send_pii: false
passbook:
avatars: gravatar # gravatar or none
branding:
title: passbook
title_show: true

View File

@ -6,15 +6,19 @@ from django import template
from django.db.models import Model
from django.http.request import HttpRequest
from django.template import Context
from django.templatetags.static import static
from django.utils.html import escape, mark_safe
from structlog import get_logger
from passbook.core.models import User
from passbook.lib.config import CONFIG
from passbook.lib.utils.urls import is_url_absolute
register = template.Library()
LOGGER = get_logger()
GRAVATAR_URL = "https://secure.gravatar.com"
@register.simple_tag(takes_context=True)
def back(context: Context) -> str:
@ -54,37 +58,23 @@ def css_class(field, css):
@register.simple_tag
def gravatar(email, size=None, rating=None):
"""
Generates a Gravatar URL for the given email address.
Syntax::
{% gravatar <email> [size] [rating] %}
Example::
{% gravatar someone@example.com 48 pg %}
"""
# gravatar uses md5 for their URLs, so md5 can't be avoided
gravatar_url = "%savatar/%s" % (
"https://secure.gravatar.com/",
md5(email.encode("utf-8")).hexdigest(), # nosec
)
def avatar(user: User) -> str:
"""Get avatar, depending on passbook.avatar setting"""
mode = CONFIG.raw.get("passbook").get("avatars")
if mode == "none":
return static("passbook/user-default.png")
if mode == "gravatar":
parameters = [
p
for p in (
("s", size or "158"),
("r", rating or "g"),
)
if p[1]
("s", "158"),
("r", "g"),
]
if parameters:
gravatar_url += "?" + urlencode(parameters, doseq=True)
# gravatar uses md5 for their URLs, so md5 can't be avoided
mail_hash = md5(user.email.encode("utf-8")).hexdigest() # nosec
gravatar_url = (
f"{GRAVATAR_URL}/avatar/{mail_hash}?{urlencode(parameters, doseq=True)}"
)
return escape(gravatar_url)
raise ValueError(f"Invalid avatar mode {mode}")
@register.filter

View File

@ -1,6 +1,8 @@
"""Integrate ./manage.py test with pytest"""
from django.conf import settings
from passbook.lib.config import CONFIG
class PytestTestRunner:
"""Runs pytest to discover and run tests."""
@ -11,6 +13,7 @@ class PytestTestRunner:
self.keepdb = keepdb
settings.TEST = True
settings.CELERY_TASK_ALWAYS_EAGER = True
CONFIG.raw.get("passbook")["avatars"] = "none"
def run_tests(self, test_labels):
"""Run pytest and return the exitcode.

Binary file not shown.

After

Width:  |  Height:  |  Size: 68 B