Source code for mezzanine.accounts.templatetags.accounts_tags
from __future__ import unicode_literals
from collections import OrderedDict
from django.contrib.auth import get_user_model
from mezzanine import template
from mezzanine.conf import settings
from mezzanine.accounts import (get_profile_form, get_profile_user_fieldname,
get_profile_for_user, ProfileNotConfigured)
from mezzanine.accounts.forms import LoginForm
register = template.Library()
User = get_user_model()
[docs]@register.filter
def profile_fields(user):
"""
Returns profile fields as a dict for the given user. Used in the
profile view template when the ``ACCOUNTS_PROFILE_VIEWS_ENABLED``
setting is set to ``True``, and also in the account approval emails
sent to administrators when the ``ACCOUNTS_APPROVAL_REQUIRED``
setting is set to ``True``.
"""
fields = OrderedDict()
try:
profile = get_profile_for_user(user)
user_fieldname = get_profile_user_fieldname()
exclude = tuple(settings.ACCOUNTS_PROFILE_FORM_EXCLUDE_FIELDS)
for field in profile._meta.get_fields():
if field.name not in ("id", user_fieldname) + exclude:
value = getattr(profile, field.name)
fields[field.verbose_name.title()] = value
except ProfileNotConfigured:
pass
return list(fields.items())
[docs]@register.filter
def username_or(user, attr):
"""
Returns the user's username for display, or an alternate attribute
if ``ACCOUNTS_NO_USERNAME`` is set to ``True``.
"""
if not settings.ACCOUNTS_NO_USERNAME:
attr = "username"
value = getattr(user, attr)
if callable(value):
value = value()
return value