]> git.openstreetmap.org Git - osqa.git/blob - forum/templatetags/user_tags.py
0e9ae27abba45387bffd87a5aad79c6ae9d367c5
[osqa.git] / forum / templatetags / user_tags.py
1 from django import template\r
2 from django.utils.translation import ugettext as _\r
3 from forum import const\r
4 \r
5 register = template.Library()\r
6 \r
7 class UserSignatureNode(template.Node):\r
8     template = template.loader.get_template('users/signature.html')\r
9 \r
10     def __init__(self, user, format):\r
11         self.user = template.Variable(user)\r
12         self.format = template.Variable(format)\r
13 \r
14     def render(self, context):\r
15         return self.template.render(template.Context({\r
16             'user': self.user.resolve(context),\r
17             'format': self.format.resolve(context)\r
18         }))\r
19 \r
20 @register.tag        \r
21 def user_signature(parser, token):\r
22     try:\r
23         tag_name, user, format = token.split_contents()\r
24     except ValueError:\r
25         raise template.TemplateSyntaxError, "%r tag requires exactly two arguments" % token.contents.split()[0]\r
26 \r
27     return UserSignatureNode(user, format)\r
28 \r
29 \r
30 class ActivityNode(template.Node):\r
31     template = template.loader.get_template('users/activity.html')\r
32 \r
33     def __init__(self, activity):\r
34         self.activity = template.Variable(activity)\r
35 \r
36     def render(self, context):\r
37         activity = self.activity.resolve(context)\r
38 \r
39         context = {\r
40             'active_at': activity.active_at,\r
41             'description': activity.type_as_string,\r
42             'type': activity.activity_type,\r
43         }\r
44 \r
45         if activity.activity_type == const.TYPE_ACTIVITY_PRIZE:\r
46             context['badge'] = True\r
47             context['title'] = activity.content_object.badge.name\r
48             context['url'] = activity.content_object.badge.get_absolute_url()\r
49             context['badge_type'] = activity.content_object.badge.type\r
50         else:\r
51             context['title'] = activity.node.headline\r
52             context['url'] = activity.node.get_absolute_url()\r
53 \r
54         if activity.activity_type in (const.TYPE_ACTIVITY_UPDATE_ANSWER, const.TYPE_ACTIVITY_UPDATE_QUESTION):\r
55             context['revision'] = True\r
56             context['summary'] = activity.content_object.summary or \\r
57                     _('Revision n. %(rev_number)d') % {'rev_number': activity.content_object.revision}\r
58 \r
59         return self.template.render(template.Context(context))\r
60 \r
61 @register.tag\r
62 def activity_item(parser, token):\r
63     try:\r
64         tag_name, activity = token.split_contents()\r
65     except ValueError:\r
66         raise template.TemplateSyntaxError, "%r tag requires exactly one arguments" % token.contents.split()[0]\r
67 \r
68     return ActivityNode(activity)\r