Cleaning: timezone issues, deprecated urls.py imports, missing notes.
[wolnelektury.git] / apps / lesmianator / views.py
1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 #
5 from django.http import Http404
6 from django.shortcuts import render_to_response, get_object_or_404
7 from django.template import RequestContext
8 from django.contrib.auth.decorators import login_required
9 from django.views.decorators import cache
10
11 from catalogue.utils import get_random_hash
12 from catalogue.models import Book, Tag
13 from lesmianator.models import Poem, Continuations
14
15
16 def main_page(request):
17     last = Poem.objects.all().order_by('-created_at')[:10]
18     shelves = Tag.objects.filter(user__username='lesmianator')
19
20     return render_to_response('lesmianator/lesmianator.html', 
21                 {"last": last, "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, slug):
39     book = get_object_or_404(Book, slug=slug)
40     user = request.user if request.user.is_authenticated() else None
41     text = Poem.write(Continuations.get(book))
42     p = Poem(slug=get_random_hash(text), text=text, created_by=user)
43     p.created_from = [book.id]
44     p.save()
45
46     return render_to_response('lesmianator/poem.html', 
47                 {"poem": p, "books": [book], "book": book},
48                 context_instance=RequestContext(request))
49
50
51 @cache.never_cache
52 def poem_from_set(request, shelf):
53     user = request.user if request.user.is_authenticated() else None
54     tag = get_object_or_404(Tag, category='set', slug=shelf)
55     text = Poem.write(Continuations.get(tag))
56     p = Poem(slug=get_random_hash(text), text=text, created_by=user)
57     books = Book.tagged.with_any((tag,))
58     p.created_from = [b.id for b in books]
59     p.save()
60
61     book = books[0] if len(books) == 1 else None
62
63     return render_to_response('lesmianator/poem.html', 
64                 {"poem": p, "shelf": tag, "books": books, "book": book},
65                 context_instance=RequestContext(request))
66
67 def get_poem(request, poem):
68     p = get_object_or_404(Poem, slug=poem)
69     p.visit()
70     if p.created_from:
71         books = Book.objects.filter(id__in=p.created_from)
72         book = books[0] if len(books) == 1 else None
73     else:
74         books = book = None
75
76     return render_to_response('lesmianator/poem.html', 
77                 {"poem": p, "books": books, "book": book},
78                 context_instance=RequestContext(request))
79
80