1 from __future__ import absolute_import
3 from django.core.urlresolvers import reverse
4 from django.contrib.comments.models import Comment
5 from django.template.defaultfilters import stringfilter
6 from django import template
7 from django.utils.translation import ugettext as _
9 from wiki.models import Book
10 from dvcs.models import Change
12 register = template.Library()
20 def __init__(self, slug, caption, url):
22 self.caption = caption
26 @register.inclusion_tag("wiki/main_tabs.html", takes_context=True)
27 def main_tabs(context):
28 active = getattr(context['request'], 'wiki_active_tab', None)
31 user = context['user']
32 if user.is_authenticated():
33 tabs.append(Tab('my', _('Assigned to me'), reverse("wiki_user")))
35 tabs.append(Tab('unassigned', _('Unassigned'), reverse("wiki_unassigned")))
36 tabs.append(Tab('users', _('Users'), reverse("wiki_users")))
37 tabs.append(Tab('all', _('All'), reverse("wiki_document_list")))
38 tabs.append(Tab('create', _('Add'), reverse("wiki_create_missing")))
39 tabs.append(Tab('upload', _('Upload'), reverse("wiki_upload")))
42 tabs.append(Tab('admin', _('Admin'), reverse("admin:index")))
44 return {"tabs": tabs, "active_tab": active}
47 class WallItem(object):
55 def __init__(self, tag):
60 return self.user.email
65 def changes_wall(max_len):
66 qs = Change.objects.filter(revision__gt=-1).order_by('-created_at').select_related()
69 tag = 'stage' if item.tags.count() else 'change'
70 chunk = item.tree.chunk
72 w.title = chunk.pretty_name()
73 w.summary = item.description
74 w.url = reverse('wiki_editor',
75 args=[chunk.book.slug, chunk.slug]) + '?diff=%d' % item.revision
76 w.timestamp = item.created_at
78 w.email = item.author_email
82 def published_wall(max_len):
83 qs = Book.objects.exclude(last_published=None).order_by('-last_published')
86 w = WallItem('publish')
88 w.summary = item.title
89 w.url = chunk.book.get_absolute_url()
90 w.timestamp = item.last_published
91 w.user = item.last_published_by
95 def comments_wall(max_len):
96 qs = Comment.objects.filter(is_public=True).select_related().order_by('-submit_date')
99 w = WallItem('comment')
100 w.title = item.content_object
101 w.summary = item.comment
102 w.url = item.content_object.get_absolute_url()
103 w.timestamp = item.submit_date
105 w.email = item.user_email
109 def big_wall(max_len, *args):
111 Takes some WallItem iterators and zips them into one big wall.
112 Input iterators must already be sorted by timestamp.
117 subwalls.append([next(w), w])
118 except StopIteration:
121 while max_len and subwalls:
122 i, next_item = max(enumerate(subwalls), key=lambda x: x[1][0].timestamp)
126 next_item[0] = next(next_item[1])
127 except StopIteration:
131 @register.inclusion_tag("wiki/wall.html", takes_context=True)
132 def wall(context, max_len=10):
134 "request": context['request'],
135 "STATIC_URL": context['STATIC_URL'],
136 "wall": big_wall(max_len,
137 changes_wall(max_len),
138 published_wall(max_len),
139 comments_wall(max_len),