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', _('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 = Chunk.change_model.objects.filter(revision__gt=-1).order_by('-created_at')
67 qs = qs.defer('patch')
68 qs = qs.select_related('author', 'tree')
69 #qs = qs.annotate(book_length=Count('chunk__book__chunk'))
72 tag = 'stage' if item.tags.count() else 'change'
75 w.title = chunk.pretty_name()
76 w.summary = item.description
77 w.url = reverse('wiki_editor',
78 args=[chunk.book.slug, chunk.slug]) + '?diff=%d' % item.revision
79 w.timestamp = item.created_at
81 w.email = item.author_email
85 def published_wall(max_len):
86 qs = Book.objects.exclude(last_published=None).order_by('-last_published')
89 w = WallItem('publish')
91 w.summary = item.title
92 w.url = chunk.book.get_absolute_url()
93 w.timestamp = item.last_published
94 w.user = item.last_published_by
98 def comments_wall(max_len):
99 qs = Comment.objects.filter(is_public=True).select_related().order_by('-submit_date')
102 w = WallItem('comment')
103 w.title = item.content_object
104 w.summary = item.comment
105 w.url = item.content_object.get_absolute_url()
106 w.timestamp = item.submit_date
108 w.email = item.user_email
112 def big_wall(max_len, *args):
114 Takes some WallItem iterators and zips them into one big wall.
115 Input iterators must already be sorted by timestamp.
120 subwalls.append([next(w), w])
121 except StopIteration:
124 while max_len and subwalls:
125 i, next_item = max(enumerate(subwalls), key=lambda x: x[1][0].timestamp)
129 next_item[0] = next(next_item[1])
130 except StopIteration:
134 @register.inclusion_tag("wiki/wall.html", takes_context=True)
135 def wall(context, max_len=10):
137 "request": context['request'],
138 "STATIC_URL": context['STATIC_URL'],
139 "wall": big_wall(max_len,
140 changes_wall(max_len),
141 published_wall(max_len),
142 comments_wall(max_len),