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