diff --git a/passbook/core/templates/base/page.html b/passbook/core/templates/base/page.html index 6bb61d962..ebcee481a 100644 --- a/passbook/core/templates/base/page.html +++ b/passbook/core/templates/base/page.html @@ -53,7 +53,7 @@ {{ user.username }} - + {% block page_content %} diff --git a/passbook/core/templates/login/form_with_user.html b/passbook/core/templates/login/form_with_user.html index dfe6213a2..da9defe35 100644 --- a/passbook/core/templates/login/form_with_user.html +++ b/passbook/core/templates/login/form_with_user.html @@ -7,7 +7,7 @@
- + {{ user.username }}
diff --git a/passbook/lib/default.yml b/passbook/lib/default.yml index eb2e61bf3..5d911a651 100644 --- a/passbook/lib/default.yml +++ b/passbook/lib/default.yml @@ -22,6 +22,7 @@ error_reporting: send_pii: false passbook: + avatars: gravatar # gravatar or none branding: title: passbook title_show: true diff --git a/passbook/lib/templatetags/passbook_utils.py b/passbook/lib/templatetags/passbook_utils.py index 3cda3388a..d64482b0f 100644 --- a/passbook/lib/templatetags/passbook_utils.py +++ b/passbook/lib/templatetags/passbook_utils.py @@ -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 [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 = [ - p - for p in ( - ("s", size or "158"), - ("r", rating or "g"), +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 = [ + ("s", "158"), + ("r", "g"), + ] + # 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)}" ) - if p[1] - ] - - if parameters: - gravatar_url += "?" + urlencode(parameters, doseq=True) - - return escape(gravatar_url) + return escape(gravatar_url) + raise ValueError(f"Invalid avatar mode {mode}") @register.filter diff --git a/passbook/root/test_runner.py b/passbook/root/test_runner.py index 17d4bbf07..6e9e5db60 100644 --- a/passbook/root/test_runner.py +++ b/passbook/root/test_runner.py @@ -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. diff --git a/passbook/static/static/passbook/user-default.png b/passbook/static/static/passbook/user-default.png new file mode 100644 index 000000000..9da19eaca Binary files /dev/null and b/passbook/static/static/passbook/user-default.png differ