1 from __future__ import absolute_import
3 from django.db.models import Count
4 from django.core.urlresolvers import reverse
5 from django.contrib.comments.models import Comment
6 from django.template.defaultfilters import stringfilter
7 from django import template
8 from django.utils.translation import ugettext as _
10 from wiki.models import Book, Chunk
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', _('My page'), 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('create', _('Add'), reverse("wiki_create_missing")))
38 tabs.append(Tab('upload', _('Upload'), reverse("wiki_upload")))
41 tabs.append(Tab('admin', _('Admin'), reverse("admin:index")))
43 return {"tabs": tabs, "active_tab": active}
46 class WallItem(object):
54 def __init__(self, tag):
59 return self.user.email
64 def changes_wall(max_len):
65 qs = Chunk.change_model.objects.filter(revision__gt=-1).order_by('-created_at')
66 qs = qs.defer('patch')
67 qs = qs.select_related('author', 'tree', 'tree__book__title')
70 tag = 'stage' if item.tags.count() else 'change'
73 w.title = chunk.pretty_name()
74 w.summary = item.description
75 w.url = reverse('wiki_editor',
76 args=[chunk.book.slug, chunk.slug]) + '?diff=%d' % item.revision
77 w.timestamp = item.created_at
79 w.email = item.author_email
83 def published_wall(max_len):
84 qs = Book.objects.exclude(last_published=None).order_by('-last_published')
87 w = WallItem('publish')
89 w.summary = item.title
90 w.url = chunk.book.get_absolute_url()
91 w.timestamp = item.last_published
92 w.user = item.last_published_by
96 def comments_wall(max_len):
97 qs = Comment.objects.filter(is_public=True).select_related().order_by('-submit_date')
100 w = WallItem('comment')
101 w.title = item.content_object
102 w.summary = item.comment
103 w.url = item.content_object.get_absolute_url()
104 w.timestamp = item.submit_date
106 w.email = item.user_email
110 def big_wall(max_len, *args):
112 Takes some WallItem iterators and zips them into one big wall.
113 Input iterators must already be sorted by timestamp.
118 subwalls.append([next(w), w])
119 except StopIteration:
122 while max_len and subwalls:
123 i, next_item = max(enumerate(subwalls), key=lambda x: x[1][0].timestamp)
127 next_item[0] = next(next_item[1])
128 except StopIteration:
132 @register.inclusion_tag("wiki/wall.html", takes_context=True)
133 def wall(context, max_len=10):
135 "request": context['request'],
136 "STATIC_URL": context['STATIC_URL'],
137 "wall": big_wall(max_len,
138 changes_wall(max_len),
139 published_wall(max_len),
140 comments_wall(max_len),