1 # Create your views here.
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
9 from catalogue.utils import get_random_hash
10 from catalogue.models import Book, Tag
11 from lesmianator.models import Poem, Continuations
14 def main_page(request):
15 last = Poem.objects.all().order_by('-created_at')[:10]
16 shelves = Tag.objects.filter(user__username='lesmianator')
18 return render_to_response('lesmianator/lesmianator.html',
19 {"last": last, "shelves": shelves},
20 context_instance=RequestContext(request))
24 def new_poem(request):
25 user = request.user if request.user.is_authenticated() else None
27 p = Poem(slug=get_random_hash(text), text=text, created_by=user)
30 return render_to_response('lesmianator/poem.html',
32 context_instance=RequestContext(request))
36 def poem_from_book(request, book):
37 kwargs = Book.split_urlid(book)
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])
47 return render_to_response('lesmianator/poem.html',
48 {"poem": p, "books": [book], "book": book},
49 context_instance=RequestContext(request))
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])
62 book = books[0] if len(books) == 1 else None
64 return render_to_response('lesmianator/poem.html',
65 {"poem": p, "shelf": tag, "books": books, "book": book},
66 context_instance=RequestContext(request))
68 def get_poem(request, poem):
69 p = get_object_or_404(Poem, slug=poem)
72 books = Book.objects.filter(id__in=p.get_created_from_value())
73 book = books[0] if len(books) == 1 else None
77 return render_to_response('lesmianator/poem.html',
78 {"poem": p, "books": books, "book": book},
79 context_instance=RequestContext(request))