merge new librarian changes with custom pdf fix
[wolnelektury.git] / apps / lesmianator / views.py
1 # Create your views here.
2
3 from django.http import Http404
4 from django.shortcuts import render_to_response, get_object_or_404
5 from django.template import RequestContext
6 from django.contrib.auth.decorators import login_required
7 from django.views.decorators import cache
8
9 from catalogue.utils import get_random_hash
10 from catalogue.models import Book, Tag
11 from catalogue import forms
12 from lesmianator.models import Poem, Continuations
13
14
15 def main_page(request):
16     last = Poem.objects.all().order_by('-created_at')[:10]
17     form = forms.SearchForm()
18     shelves = Tag.objects.filter(user__username='lesmianator')
19
20     return render_to_response('lesmianator/lesmianator.html', 
21                 {"last": last, "form": form, "shelves": shelves},
22                 context_instance=RequestContext(request))
23
24
25 @cache.never_cache
26 def new_poem(request):
27     user = request.user if request.user.is_authenticated() else None
28     text = Poem.write()
29     p = Poem(slug=get_random_hash(text), text=text, created_by=user)
30     p.save()
31
32     return render_to_response('lesmianator/poem.html', 
33                 {"poem": p},
34                 context_instance=RequestContext(request))
35
36
37 @cache.never_cache
38 def poem_from_book(request, book):
39     kwargs = Book.split_urlid(book)
40     if kwargs is None:
41         raise Http404
42     book = get_object_or_404(Book, **kwargs)
43     user = request.user if request.user.is_authenticated() else None
44     text = Poem.write(Continuations.get(book))
45     p = Poem(slug=get_random_hash(text), text=text, created_by=user)
46     p.set_created_from_value([book.id])
47     p.save()
48
49     return render_to_response('lesmianator/poem.html', 
50                 {"poem": p, "books": [book], "book": book},
51                 context_instance=RequestContext(request))
52
53
54 @cache.never_cache
55 def poem_from_set(request, shelf):
56     user = request.user if request.user.is_authenticated() else None
57     tag = get_object_or_404(Tag, category='set', slug=shelf)
58     text = Poem.write(Continuations.get(tag))
59     p = Poem(slug=get_random_hash(text), text=text, created_by=user)
60     books = Book.tagged.with_any((tag,))
61     p.set_created_from_value([b.id for b in books])
62     p.save()
63
64     book = books[0] if len(books) == 1 else None
65
66     return render_to_response('lesmianator/poem.html', 
67                 {"poem": p, "shelf": tag, "books": books, "book": book},
68                 context_instance=RequestContext(request))
69
70 def get_poem(request, poem):
71     p = get_object_or_404(Poem, slug=poem)
72     p.visit()
73     if p.created_from:
74         books = Book.objects.filter(id__in=p.get_created_from_value())
75         book = books[0] if len(books) == 1 else None
76     else:
77         books = book = None
78
79     return render_to_response('lesmianator/poem.html', 
80                 {"poem": p, "books": books, "book": book},
81                 context_instance=RequestContext(request))
82
83