]> git.openstreetmap.org Git - osqa.git/blob - forum/templatetags/user_tags.py
cd22f26bcb6913828577335ccaaea3540a9b7720
[osqa.git] / forum / templatetags / user_tags.py
1 from django import template\r
2 from django.utils.translation import ugettext as _\r
3 from django.utils.safestring import mark_safe\r
4 import logging\r
5 \r
6 register = template.Library()\r
7 \r
8 class UserSignatureNode(template.Node):\r
9     template = template.loader.get_template('users/signature.html')\r
10 \r
11     def __init__(self, user, format):\r
12         self.user = template.Variable(user)\r
13         self.format = template.Variable(format)\r
14 \r
15     def render(self, context):\r
16         return self.template.render(template.Context({\r
17             'user': self.user.resolve(context),\r
18             'format': self.format.resolve(context)\r
19         }))\r
20 \r
21 @register.tag        \r
22 def user_signature(parser, token):\r
23     try:\r
24         tag_name, user, format = token.split_contents()\r
25     except ValueError:\r
26         raise template.TemplateSyntaxError, "%r tag requires exactly two arguments" % token.contents.split()[0]\r
27 \r
28     return UserSignatureNode(user, format)\r
29 \r
30 \r
31 class ActivityNode(template.Node):\r
32     template = template.loader.get_template('users/activity.html')\r
33 \r
34     def __init__(self, activity, viewer):\r
35         self.activity = template.Variable(activity)\r
36         self.viewer = template.Variable(viewer)\r
37 \r
38     def render(self, context):\r
39         try:\r
40             action = self.activity.resolve(context).leaf()\r
41             viewer = self.viewer.resolve(context)\r
42             describe = mark_safe(action.describe(viewer))\r
43             return self.template.render(template.Context(dict(action=action, describe=describe)))\r
44         except Exception, e:\r
45             logging.error("Error in %s action describe: %s" % (action.action_type, str(e)))\r
46 \r
47 @register.tag\r
48 def activity_item(parser, token):\r
49     try:\r
50         tag_name, activity, viewer = token.split_contents()\r
51     except ValueError:\r
52         raise template.TemplateSyntaxError, "%r tag requires exactly two arguments" % token.contents.split()[0]\r
53 \r
54     return ActivityNode(activity, viewer)\r
55 \r
56 \r
57 @register.inclusion_tag('users/menu.html')\r
58 def user_menu(request, user):\r
59     return dict(viewer=request.user, user=user)\r
60 \r