Beispiel #1
0
def gravatar(context, user, size=None):
    """
    Outputs the HTML for displaying a user's gravatar.

    This can take an optional size of the image (defaults to 80 if not
    specified).

    This is also influenced by the following settings:

        GRAVATAR_SIZE    - Default size for gravatars
        GRAVATAR_RATING  - Maximum allowed rating (g, pg, r, x)
        GRAVATAR_DEFAULT - Default image set to show if the user hasn't
                           specified a gravatar (identicon, monsterid, wavatar)

    See http://www.gravatar.com/ for more information.
    """
    url = get_gravatar_url(context['request'], user, size)

    if url:
        return format_html(
            '<img src="{0}" width="{1}" height="{1}" alt="{2}" '
            'class="gravatar"/>',
            url, size, user.get_full_name() or user.username)
    else:
        return ''
Beispiel #2
0
def gravatar(context, user, size=None):
    """
    Outputs the HTML for displaying a user's gravatar.

    This can take an optional size of the image (defaults to 80 if not
    specified).

    This is also influenced by the following settings:

        GRAVATAR_SIZE    - Default size for gravatars
        GRAVATAR_RATING  - Maximum allowed rating (g, pg, r, x)
        GRAVATAR_DEFAULT - Default image set to show if the user hasn't
                           specified a gravatar (identicon, monsterid, wavatar)

    See http://www.gravatar.com/ for more information.
    """
    url = get_gravatar_url(context['request'], user, size)

    if url:
        return format_html(
            '<img src="{0}" width="{1}" height="{1}" alt="{2}" '
            'class="gravatar"/>', url, size,
            user.get_full_name() or user.username)
    else:
        return ''
Beispiel #3
0
 def test_get_gravatar_url_for_unicode(self):
     """Testing get_gravatar_url with unicode characters"""
     raw_email = 'こんにちは@example.com'
     encoded_email = raw_email.encode('utf-8')
     user = User.objects.create_user(username='******', email=raw_email)
     self.assertEqual(get_gravatar_url(user=user),
                      get_gravatar_url_for_email(email=encoded_email))
Beispiel #4
0
    def render_user(self, state, user):
        """Render the user's name and gravatar as HTML."""
        gravatar_url = get_gravatar_url(state.datagrid.request, user,
                                        self.GRAVATAR_SIZE)

        if gravatar_url:
            gravatar_html = format_html(
                '<img src="{0}" width="{1}" height="{1}" alt="{2}" '
                'class="gravatar" /> ', gravatar_url, self.GRAVATAR_SIZE,
                user.username)
        else:
            gravatar_html = ''

        return format_html('<a class="user" href="{0}">{1}{2}</a>',
                           user.get_absolute_url(), gravatar_html,
                           user.username)
Beispiel #5
0
    def render_user(self, state, user):
        """Render the user's name and gravatar as HTML."""
        gravatar_url = get_gravatar_url(state.datagrid.request, user,
                                        self.GRAVATAR_SIZE)

        if gravatar_url:
            gravatar_html = format_html(
                '<img src="{0}" width="{1}" height="{1}" alt="{2}" '
                'class="gravatar" /> ',
                gravatar_url, self.GRAVATAR_SIZE, user.username)
        else:
            gravatar_html = ''

        return format_html(
            '<a class="user" href="{0}">{1}{2}</a>',
            user.get_absolute_url(), gravatar_html, user.username)
Beispiel #6
0
def gravatar(user, size=None):
    """Return HTML for displaying a user's Gravatar.

    This is also influenced by the following Django settings:

    ``GRAVATAR_SIZE``:
        The default size for Gravatars.

    ``GRAVATAR_RATING``:
        The maximum allowed rating (one of ``'g'``, ``'pg'``, ``'r'``, or
        ``'x'``).

    ``GRAVATAR_DEFAULT``:
        The default image to show if the user hasn't specified a Gravatar (one
        of ``identicon``, ``monsterid``, or ``wavatar``).

    See https://gravatar.com for more information.

    Note that callers adhering to the GDPR should check for a user's consent
    before displaying a Gravatar on their behalf. This is checked automatically
    if using :py:mod:`djblets.avatars`.

    Args:
        user (django.contrib.auth.models.User):
            The user whose gravatar is to be displayed.

        size (int):
            An optional height and width for the image (in pixels). This will
            default to 80 if not specified.

    Returns:
        django.utils.safestring.SafeText:
        HTML for rendering the Gravatar.
    """
    url = get_gravatar_url(user=user, size=size)

    if url:
        return format_html(
            '<img src="{0}" width="{1}" height="{1}" alt="{2}" '
            'class="gravatar"/>',
            url, size, user.get_full_name() or user.username)
    else:
        return ''
Beispiel #7
0
    def render_user(self, state, user):
        """Render the user's name and gravatar as HTML."""
        siteconfig = SiteConfiguration.objects.get_current()

        if siteconfig.get('integration_gravatars'):
            gravatar_url = get_gravatar_url(state.datagrid.request, user,
                                            self.GRAVATAR_SIZE)
        else:
            gravatar_url = None

        if gravatar_url:
            gravatar_html = format_html(
                '<img src="{0}" width="{1}" height="{1}" alt="{2}" '
                'class="gravatar" /> ', gravatar_url, self.GRAVATAR_SIZE,
                user.username)
        else:
            gravatar_html = ''

        return format_html('<a class="user" href="{0}">{1}{2}</a>',
                           user.get_absolute_url(), gravatar_html,
                           user.username)
Beispiel #8
0
    def render_user(self, state, user):
        """Render the user's name and gravatar as HTML."""
        siteconfig = SiteConfiguration.objects.get_current()

        if siteconfig.get('integration_gravatars'):
            gravatar_url = get_gravatar_url(state.datagrid.request, user,
                                            self.GRAVATAR_SIZE)
        else:
            gravatar_url = None

        if gravatar_url:
            gravatar_html = format_html(
                '<img src="{0}" width="{1}" height="{1}" alt="{2}" '
                'class="gravatar" /> ',
                gravatar_url, self.GRAVATAR_SIZE, user.username)
        else:
            gravatar_html = ''

        return format_html(
            '<a class="user" href="{0}">{1}{2}</a>',
            user.get_absolute_url(), gravatar_html, user.username)
Beispiel #9
0
def gravatar(user, size=None):
    """Return HTML for displaying a user's Gravatar.

    This is also influenced by the following Django settings:

    ``GRAVATAR_SIZE``:
        The default size for Gravatars.

    ``GRAVATAR_RATING``:
        The maximum allowed rating (one of ``'g'``, ``'pg'``, ``'r'``, or
        ``'x'``).

    ``GRAVATAR_DEFAULT``:
        The default image to show if the user hasn't specified a Gravatar (one
        of ``identicon``, ``monsterid``, or ``wavatar``).

    See https://gravatar.com for more information.

    Args:
        user (django.contrib.auth.models.User):
            The user whose gravatar is to be displayed.

        size (int):
            An optional height and width for the image (in pixels). This will
            default to 80 if not specified.

    Returns:
        django.utils.safestring.SafeText:
        HTML for rendering the Gravatar.
    """
    url = get_gravatar_url(user=user, size=size)

    if url:
        return format_html(
            '<img src="{0}" width="{1}" height="{1}" alt="{2}" '
            'class="gravatar"/>', url, size,
            user.get_full_name() or user.username)
    else:
        return ''
Beispiel #10
0
 def serialize_avatar_url_field(self, user, request=None, **kwargs):
     return get_gravatar_url(request, user)
Beispiel #11
0
    def render(self, name, value, attrs=None):
        """Render the widget.

        Args:
            name (unicode):
                The name of the field.

            value (list or None):
                The current value of the field.

            attrs (dict):
                Attributes for the HTML element.

        Returns:
            django.utils.safestring.SafeText:
            The rendered HTML.
        """
        if value:
            value = [v for v in value if v]
            input_value = ','.join(force_text(v) for v in value)
            existing_users = (User.objects.filter(pk__in=value).order_by(
                'first_name', 'last_name', 'username'))
        else:
            input_value = None
            existing_users = []

        final_attrs = self.build_attrs(attrs, name=name)

        input_html = super(RelatedUserWidget,
                           self).render(name, input_value, attrs)

        # The Gravatar API in Djblets currently uses the request to determine
        # whether or not to use https://secure.gravatar.com or
        # http://gravatar.com. Unfortunately, it's hard enough to get a copy of
        # the request in a form, much less in a form widget. Instead, we fake
        # the request here and just always use the HTTPS one. This will be
        # dramatically better in 3.0+ with the new avatar services.
        class FakeRequest(object):
            def is_secure(self):
                return True

        fake_request = FakeRequest()
        siteconfig = SiteConfiguration.objects.get_current()
        use_gravatars = siteconfig.get('integration_gravatars')
        user_data = []

        for user in existing_users:
            data = {
                'fullname': user.get_full_name(),
                'id': user.pk,
                'username': user.username,
            }

            if use_gravatars:
                data['avatar_url'] = get_gravatar_url(fake_request, user, 40)

            user_data.append(data)

        extra_html = render_to_string(
            'admin/related_user_widget.html', {
                'input_id': final_attrs['id'],
                'use_gravatars': use_gravatars,
                'users': user_data,
            })

        return mark_safe(input_html + extra_html)
Beispiel #12
0
 def test_get_gravatar_url_whitespace(self):
     """Testing get_gravatar_url with whitespace characters"""
     user = User.objects.create_user(username='******',
                                     email=' [email protected] ')
     self.assertEqual(get_gravatar_url(user=user),
                      get_gravatar_url_for_email(email='*****@*****.**'))
Beispiel #13
0
 def test_get_gravatar_url_uppercase(self):
     """Testing get_gravatar_url with uppercase characters"""
     user = User.objects.create_user(username='******',
                                     email='*****@*****.**')
     self.assertEqual(get_gravatar_url(user=user),
                      get_gravatar_url_for_email(email='*****@*****.**'))
    def render(self, name, value, attrs=None):
        """Render the widget.

        Args:
            name (unicode):
                The name of the field.

            value (list or None):
                The current value of the field.

            attrs (dict):
                Attributes for the HTML element.

        Returns:
            django.utils.safestring.SafeText:
            The rendered HTML.
        """
        if value:
            value = [v for v in value if v]
            input_value = ','.join(force_text(v) for v in value)
            existing_users = (
                User.objects
                .filter(pk__in=value)
                .order_by('first_name', 'last_name', 'username')
            )
        else:
            input_value = None
            existing_users = []

        final_attrs = self.build_attrs(attrs, name=name)

        input_html = super(RelatedUserWidget, self).render(
            name, input_value, attrs)

        # The Gravatar API in Djblets currently uses the request to determine
        # whether or not to use https://secure.gravatar.com or
        # http://gravatar.com. Unfortunately, it's hard enough to get a copy of
        # the request in a form, much less in a form widget. Instead, we fake
        # the request here and just always use the HTTPS one. This will be
        # dramatically better in 3.0+ with the new avatar services.
        class FakeRequest(object):
            def is_secure(self):
                return True

        fake_request = FakeRequest()
        siteconfig = SiteConfiguration.objects.get_current()
        use_gravatars = siteconfig.get('integration_gravatars')
        user_data = []

        for user in existing_users:
            data = {
                'fullname': user.get_full_name(),
                'id': user.pk,
                'username': user.username,
            }

            if use_gravatars:
                data['avatar_url'] = get_gravatar_url(fake_request, user, 40)

            user_data.append(data)

        extra_html = render_to_string('admin/related_user_widget.html', {
            'input_id': final_attrs['id'],
            'use_gravatars': use_gravatars,
            'users': user_data,
        })

        return mark_safe(input_html + extra_html)