import 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 lesmianator.models import Poem, Continuations
12
13
14 def main_page(request):
15     last = Poem.objects.all().order_by('-created_at')[:10]
16     shelves = Tag.objects.filter(user__username='lesmianator')
17
18     return render_to_response('lesmianator/lesmianator.html', 
19                 {"last": last, "shelves": shelves},
20                 context_instance=RequestContext(request))
21
22
23 @cache.never_cache
24 def new_poem(request):
25     user = request.user if request.user.is_authenticated() else None
26     text = Poem.write()
27     p = Poem(slug=get_random_hash(text), text=text, created_by=user)
28     p.save()
29
30     return render_to_response('lesmianator/poem.html', 
31                 {"poem": p},
32                 context_instance=RequestContext(request))
33
34
35 @cache.never_cache
36 def poem_from_book(request, book):
37     kwargs = Book.split_urlid(book)
38     if kwargs is None:
39         raise Http404
40     book = get_object_or_404(Book, **kwargs)
41     user = request.user if request.user.is_authenticated() else None
42     text = Poem.write(Continuations.get(book))
43     p = Poem(slug=get_random_hash(text), text=text, created_by=user)
44     p.set_created_from_value([book.id])
45     p.save()
46
47     return render_to_response('lesmianator/poem.html', 
48                 {"poem": p, "books": [book], "book": book},
49                 context_instance=RequestContext(request))
50
51
52 @cache.never_cache
53 def poem_from_set(request, shelf):
54     user = request.user if request.user.is_authenticated() else None
55     tag = get_object_or_404(Tag, category='set', slug=shelf)
56     text = Poem.write(Continuations.get(tag))
57     p = Poem(slug=get_random_hash(text), text=text, created_by=user)
58     books = Book.tagged.with_any((tag,))
59     p.set_created_from_value([b.id for b in books])
60     p.save()
61
62     book = books[0] if len(books) == 1 else None
63
64     return render_to_response('lesmianator/poem.html', 
65                 {"poem": p, "shelf": tag, "books": books, "book": book},
66                 context_instance=RequestContext(request))
67
68 def get_poem(request, poem):
69     p = get_object_or_404(Poem, slug=poem)
70     p.visit()
71     if p.created_from:
72         books = Book.objects.filter(id__in=p.get_created_from_value())
73         book = books[0] if len(books) == 1 else None
74     else:
75         books = book = None
76
77     return render_to_response('lesmianator/poem.html', 
78                 {"poem": p, "books": books, "book": book},
79                 context_instance=RequestContext(request))
80
81