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