]> git.openstreetmap.org Git - osqa.git/blob - forum/templatetags/user_tags.py
ALteration of the schema to a single content model. As a bonus there is a complete...
[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         try:\r
46             if activity.activity_type == const.TYPE_ACTIVITY_PRIZE:\r
47                 context['badge'] = True\r
48                 context['title'] = activity.content_object.badge.name\r
49                 context['url'] = activity.content_object.badge.get_absolute_url()\r
50                 context['badge_type'] = activity.content_object.badge.type\r
51             else:\r
52                 context['title'] = activity.node.headline\r
53                 context['url'] = activity.node.get_absolute_url()\r
54 \r
55             if activity.activity_type in (const.TYPE_ACTIVITY_UPDATE_ANSWER, const.TYPE_ACTIVITY_UPDATE_QUESTION):\r
56                 context['revision'] = True\r
57                 context['summary'] = activity.content_object.summary or \\r
58                         _('Revision n. %(rev_number)d') % {'rev_number': activity.content_object.revision}\r
59         except Exception, e:\r
60             import sys, traceback\r
61             traceback.print_exc(file=sys.stdout)\r
62             pass\r
63 \r
64 \r
65         return self.template.render(template.Context(context))\r
66 \r
67 @register.tag\r
68 def activity_item(parser, token):\r
69     try:\r
70         tag_name, activity = token.split_contents()\r
71     except ValueError:\r
72         raise template.TemplateSyntaxError, "%r tag requires exactly one arguments" % token.contents.split()[0]\r
73 \r
74     return ActivityNode(activity)\r