mirror of
https://github.com/jazzband/django-avatar.git
synced 2026-03-16 22:20:30 +00:00
Merge pull request #139 from reidransom/tmploverride
Allow provider to override template_name and context
This commit is contained in:
commit
b986082ae5
3 changed files with 47 additions and 6 deletions
|
|
@ -24,7 +24,7 @@ class DefaultAvatarProvider(object):
|
|||
"""
|
||||
|
||||
@classmethod
|
||||
def get_avatar_url(self, user, size):
|
||||
def get_avatar_url(cls, user, size):
|
||||
return get_default_avatar_url()
|
||||
|
||||
|
||||
|
|
@ -34,7 +34,7 @@ class PrimaryAvatarProvider(object):
|
|||
"""
|
||||
|
||||
@classmethod
|
||||
def get_avatar_url(self, user, size):
|
||||
def get_avatar_url(cls, user, size):
|
||||
avatar = get_primary_avatar(user, size)
|
||||
if avatar:
|
||||
return avatar.avatar_url(size)
|
||||
|
|
@ -46,7 +46,7 @@ class GravatarAvatarProvider(object):
|
|||
"""
|
||||
|
||||
@classmethod
|
||||
def get_avatar_url(self, user, size):
|
||||
def get_avatar_url(cls, user, size):
|
||||
params = {"s": str(size)}
|
||||
if settings.AVATAR_GRAVATAR_DEFAULT:
|
||||
params["d"] = settings.AVATAR_GRAVATAR_DEFAULT
|
||||
|
|
@ -68,8 +68,30 @@ class FacebookAvatarProvider(object):
|
|||
"""
|
||||
|
||||
@classmethod
|
||||
def get_avatar_url(self, user, size):
|
||||
def get_avatar_url(cls, user, size):
|
||||
fb_id = get_facebook_id(user)
|
||||
if fb_id:
|
||||
url = "https://graph.facebook.com/{fb_id}/picture?type=square&width={size}&height={size}"
|
||||
return url.format(fb_id=fb_id, size=size)
|
||||
|
||||
|
||||
class InitialsAvatarProvider(object):
|
||||
"""
|
||||
Returns a tuple with template_name and context for rendering the given user's avatar as their
|
||||
initials in white against a background with random hue based on their primary key.
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def get_avatar_url(cls, user, size):
|
||||
initials = user.first_name[:1] + user.last_name[:1]
|
||||
if not initials:
|
||||
initials = user.username[:1]
|
||||
initials = initials.upper()
|
||||
context = {
|
||||
"fontsize": (size * 1.1) / 2,
|
||||
"initials": initials,
|
||||
"hue": user.pk % 360,
|
||||
"saturation": "65%",
|
||||
"lightness": "60%",
|
||||
}
|
||||
return ("avatar/initials.html", context)
|
||||
|
|
|
|||
11
avatar/templates/avatar/initials.html
Normal file
11
avatar/templates/avatar/initials.html
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
<span style='background-color: hsla({{ hue }}, {{ saturation }}, {{ lightness }}, 1);
|
||||
width: {{ size }}px;
|
||||
height: {{ size }}px;
|
||||
font-size: {{ fontsize }}px;
|
||||
text-align: center;
|
||||
color: white;
|
||||
display: inline-block;
|
||||
line-height: {{ size }}px;
|
||||
{{ kwargs.style }}'
|
||||
{% for key, value in kwargs.items %}{% if key != 'style' %}{{ key }}="{{ value }}" {% endif %}{% endfor %}>
|
||||
{{ initials }}</span>
|
||||
|
|
@ -45,11 +45,19 @@ def avatar(user, size=settings.AVATAR_DEFAULT_SIZE, **kwargs):
|
|||
|
||||
context = {
|
||||
"user": user,
|
||||
"url": url,
|
||||
"alt": alt,
|
||||
"size": size,
|
||||
"kwargs": kwargs,
|
||||
}
|
||||
return render_to_string("avatar/avatar_tag.html", context)
|
||||
template_name = "avatar/avatar_tag.html"
|
||||
ext_context = None
|
||||
try:
|
||||
template_name, ext_context = url
|
||||
except ValueError:
|
||||
context["url"] = url
|
||||
if ext_context:
|
||||
context = dict(context, **ext_context)
|
||||
return render_to_string(template_name, context)
|
||||
|
||||
|
||||
@register.filter
|
||||
|
|
|
|||
Loading…
Reference in a new issue