-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
get_gravatar_url: allow custom default #11077
base: main
Are you sure you want to change the base?
Conversation
To allow users get a clear 404 or a variety of generated images based on the hash. While here, fix default mm into mp, ref. https://gravatar.com/site/implement/images/
Manage this branch in SquashTest this branch here: https://nim65smain-dnil3.squash.io |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume this is to use this for your own custom code?
May I suggest we take a different approach, what if we just make the support for WAGTAIL_GRAVATAR_PROVIDER_URL
more robust.
e.g. if you provide a config like this
WAGTAIL_GRAVATAR_PROVIDER_URL = "//www.gravatar.com/avatar?d=robohash"
The util will parse that URL, pull out the query string params, and then merge them with the default params. (e.g. with import urllib.parse as urlparse
).
These tests would be suitable to add in wagtail/admin/tests/test_templatetags.py
as this is where the template tag is tested and/or the existing test file you have created.
This way we have a more flexible approach that supports qs params for other non-gravatar usage.
e.g. currently if you do something like this:
WAGTAIL_GRAVATAR_PROVIDER_URL = "//robohash.org/?set=4"
I am pretty sure it will not work, as the 'hash' part will be added after the qs and output something like //robohash.org/?set=4/123456hash?d=mm&size=100
.
I still think the update to default=mm
makes sense but this way we get a more flexible solution, no need to change kwargs on the util and we can even add documentation to suggest ways to change the hash to your favourite in docs/reference/settings.md
.
Rough idea, not sure if this will work but might be a good starting point.
from urllib.parse import parse_qs, urlparse
# ...
def get_gravatar_url(email, size=50):
"""
See https://gravatar.com/site/implement/images/ for Gravatar image options
"""
gravatar_provider_url = getattr(
settings, "WAGTAIL_GRAVATAR_PROVIDER_URL", "//www.gravatar.com/avatar"
)
if (not email) or (gravatar_provider_url is None):
return None
default = "mp"
# requested at retina size by default and scaled down at point of use with css
size = int(size) * 2
url = urlparse(gravatar_provider_url)
email_bytes = email.lower().encode("utf-8")
hash = safe_md5(email_bytes, usedforsecurity=False).hexdigest()
gravatar_url = "{base_url}/{hash}?{params}".format(
gravatar_provider_url="".join(url[:3]).rstrip("/"),
hash=hash,
params={"z": size, "d": default, **parse_qs(url.query)},
)
return gravatar_url |
|
Hi, Thanks for the review ! Yes, this is for use in my custom code. But your approach wouldn't work in my use case, as I need some calls with But we could easily merge your urlparse ideas with the Would this work for you ? |
Gravatar will ignore the However, I think adding the kwarg also is fine but calling this directly may not ever be officially supported. Thank you for taking on the feedback. |
I have raised the PR #11800, Please have a look. Thanks. |
Hi,
This allow users get a clear 404 or a variety of generated images based on the hash.
While here, fix default mm into mp,
ref. https://gravatar.com/site/implement/images/
Also add a test for this.
Please check the following:
make lint
from the Wagtail root.Please describe additional details for testing this change.
Footnotes
Development Testing ↩
Browser and device support ↩
Accessibility Target ↩