~akl-web-site-team/akl-web-site/django

« back to all changes in this revision

Viewing changes to src/akllt/templatetags/user.py

  • Committer: Marius Gedminas
  • Date: 2010-06-05 16:28:18 UTC
  • Revision ID: marius@gedmin.as-20100605162818-avmndg2a33tuqmhl
Make the username a link to the user's openid URL.

This is mostly proof-of-concept code to see how to get to the openid URL.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from django import template
 
2
from django.utils.safestring import mark_safe
 
3
from django.utils.html import conditional_escape
 
4
from django_openid_auth.models import UserOpenID
 
5
from akllt import settings
 
6
 
 
7
register = template.Library()
 
8
 
 
9
@register.tag
 
10
def user(parser, token):
 
11
    """{% user %}"""
 
12
    try:
 
13
        tag_name, = token.split_contents()
 
14
    except ValueError:
 
15
        raise template.TemplateSyntaxError(
 
16
            "%r: invalid syntax. Format is %s" % (
 
17
                token.contents.split()[0],
 
18
                user.__doc__
 
19
            ))
 
20
 
 
21
    return UserNode()
 
22
 
 
23
 
 
24
class UserNode(template.Node):
 
25
    """
 
26
    Returns prettified/linkified user name
 
27
    """
 
28
 
 
29
    def render(self, context):
 
30
        user = context['user']
 
31
        name = user.get_full_name()
 
32
        if not name:
 
33
            name = user.username
 
34
        try:
 
35
            url = user.useropenid_set.get().claimed_id
 
36
        except UserOpenID.DoesNotExist:
 
37
            url = ''
 
38
        if context.autoescape:
 
39
            name = conditional_escape(name)
 
40
            url = conditional_escape(url)
 
41
        if url:
 
42
            html = u'<a href="%s">%s</a>' % (url, name)
 
43
        else:
 
44
            html = name
 
45
        return mark_safe(html)
 
46