db2eca3e6a08f21bf26598a2a029cbe7ed0ecbfd
[redakcja.git] / apps / wiki / templatetags / wiki.py
1 from __future__ import absolute_import
2
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 _
9
10 from wiki.models import Book, Chunk
11
12 register = template.Library()
13
14
15 class Tab(object):
16     slug = None
17     caption = None
18     url = None
19
20     def __init__(self, slug, caption, url):
21         self.slug = slug
22         self.caption = caption
23         self.url = url
24
25
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)
29
30     tabs = []
31     user = context['user']
32     if user.is_authenticated():
33         tabs.append(Tab('my', _('Assigned to me'), reverse("wiki_user")))
34
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")))
40
41     if user.is_staff:
42         tabs.append(Tab('admin', _('Admin'), reverse("admin:index")))
43
44     return {"tabs": tabs, "active_tab": active}
45
46
47 class WallItem(object):
48     title = ''
49     summary = ''
50     url = ''
51     timestamp = ''
52     user = None
53     email = ''
54
55     def __init__(self, tag):
56         self.tag = tag
57
58     def get_email(self):
59         if self.user:
60             return self.user.email
61         else:
62             return self.email
63
64
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'))
70     qs = qs[:max_len]
71     for item in qs:
72         tag = 'stage' if item.tags.count() else 'change'
73         chunk = item.tree
74         w  = WallItem(tag)
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
80         w.user = item.author
81         w.email = item.author_email
82         yield w
83
84
85 def published_wall(max_len):
86     qs = Book.objects.exclude(last_published=None).order_by('-last_published')
87     qs = qs[:max_len]
88     for item in qs:
89         w  = WallItem('publish')
90         w.title = item.title
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
95         yield w
96
97
98 def comments_wall(max_len):
99     qs = Comment.objects.filter(is_public=True).select_related().order_by('-submit_date')
100     qs = qs[:max_len]
101     for item in qs:
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
107         w.user = item.user
108         w.email = item.user_email
109         yield w
110
111
112 def big_wall(max_len, *args):
113     """
114         Takes some WallItem iterators and zips them into one big wall.
115         Input iterators must already be sorted by timestamp.
116     """
117     subwalls = []
118     for w in args:
119         try:
120             subwalls.append([next(w), w])
121         except StopIteration:
122             pass
123
124     while max_len and subwalls:
125         i, next_item = max(enumerate(subwalls), key=lambda x: x[1][0].timestamp)
126         yield next_item[0]
127         max_len -= 1
128         try:
129             next_item[0] = next(next_item[1])
130         except StopIteration:
131             del subwalls[i]
132
133
134 @register.inclusion_tag("wiki/wall.html", takes_context=True)
135 def wall(context, max_len=10):
136     return {
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),
143         )}