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 catalogue import forms
12 from lesmianator.models import Poem, Continuations
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')
20 return render_to_response('lesmianator/lesmianator.html',
21 {"last": last, "form": form, "shelves": shelves},
22 context_instance=RequestContext(request))
26 def new_poem(request):
27 user = request.user if request.user.is_authenticated() else None
29 p = Poem(slug=get_random_hash(text), text=text, created_by=user)
32 return render_to_response('lesmianator/poem.html',
34 context_instance=RequestContext(request))
38 def poem_from_book(request, book):
39 kwargs = Book.split_urlid(book)
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])
49 return render_to_response('lesmianator/poem.html',
50 {"poem": p, "books": [book], "book": book},
51 context_instance=RequestContext(request))
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])
64 book = books[0] if len(books) == 1 else None
66 return render_to_response('lesmianator/poem.html',
67 {"poem": p, "shelf": tag, "books": books, "book": book},
68 context_instance=RequestContext(request))
70 def get_poem(request, poem):
71 p = get_object_or_404(Poem, slug=poem)
74 books = Book.objects.filter(id__in=p.get_created_from_value())
75 book = books[0] if len(books) == 1 else None
79 return render_to_response('lesmianator/poem.html',
80 {"poem": p, "books": books, "book": book},
81 context_instance=RequestContext(request))