1 # Create your views here.
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
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
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')
19 return render_to_response('lesmianator/lesmianator.html',
20 {"last": last, "form": form, "shelves": shelves},
21 context_instance=RequestContext(request))
25 def new_poem(request):
26 user = request.user if request.user.is_authenticated() else None
28 p = Poem(slug=get_random_hash(text), text=text, created_by=user)
31 return render_to_response('lesmianator/poem.html',
33 context_instance=RequestContext(request))
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])
45 return render_to_response('lesmianator/poem.html',
46 {"poem": p, "books": [book], "book": book},
47 context_instance=RequestContext(request))
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])
60 book = books[0] if len(books) == 1 else None
62 return render_to_response('lesmianator/poem.html',
63 {"poem": p, "shelf": tag, "books": books, "book": book},
64 context_instance=RequestContext(request))
66 def get_poem(request, poem):
67 p = get_object_or_404(Poem, slug=poem)
69 books = Book.objects.filter(id__in=p.get_created_from_value())
70 book = books[0] if len(books) == 1 else None
72 return render_to_response('lesmianator/poem.html',
73 {"poem": p, "books": books, "book": book},
74 context_instance=RequestContext(request))