3a1cc172e539ee5809ab8e0861541a76b3771678
[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', _('My page'), 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('create', _('Add'), reverse("wiki_create_missing")))
38     tabs.append(Tab('upload', _('Upload'), reverse("wiki_upload")))
39
40     if user.is_staff:
41         tabs.append(Tab('admin', _('Admin'), reverse("admin:index")))
42
43     return {"tabs": tabs, "active_tab": active}
44
45
46 class WallItem(object):
47     title = ''
48     summary = ''
49     url = ''
50     timestamp = ''
51     user = None
52     email = ''
53
54     def __init__(self, tag):
55         self.tag = tag
56
57     def get_email(self):
58         if self.user:
59             return self.user.email
60         else:
61             return self.email
62
63
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')
68     qs = qs[:max_len]
69     for item in qs:
70         tag = 'stage' if item.tags.count() else 'change'
71         chunk = item.tree
72         w  = WallItem(tag)
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
78         w.user = item.author
79         w.email = item.author_email
80         yield w
81
82
83 def published_wall(max_len):
84     qs = Book.objects.exclude(last_published=None).order_by('-last_published')
85     qs = qs[:max_len]
86     for item in qs:
87         w  = WallItem('publish')
88         w.title = item.title
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
93         yield w
94
95
96 def comments_wall(max_len):
97     qs = Comment.objects.filter(is_public=True).select_related().order_by('-submit_date')
98     qs = qs[:max_len]
99     for item in qs:
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
105         w.user = item.user
106         w.email = item.user_email
107         yield w
108
109
110 def big_wall(max_len, *args):
111     """
112         Takes some WallItem iterators and zips them into one big wall.
113         Input iterators must already be sorted by timestamp.
114     """
115     subwalls = []
116     for w in args:
117         try:
118             subwalls.append([next(w), w])
119         except StopIteration:
120             pass
121
122     while max_len and subwalls:
123         i, next_item = max(enumerate(subwalls), key=lambda x: x[1][0].timestamp)
124         yield next_item[0]
125         max_len -= 1
126         try:
127             next_item[0] = next(next_item[1])
128         except StopIteration:
129             del subwalls[i]
130
131
132 @register.inclusion_tag("wiki/wall.html", takes_context=True)
133 def wall(context, max_len=10):
134     return {
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),
141         )}