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 }} {{ user.username }}
</a> </a>
</div> </div>
<img class="pf-c-avatar" src="{% gravatar user.email %}" alt=""> <img class="pf-c-avatar" src="{% avatar user %}" alt="">
</div> </div>
</header> </header>
{% block page_content %} {% block page_content %}

View File

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

View File

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

View File

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

View File

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 68 B