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))
52 def poem_from_set(request, shelf):
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.set_created_from_value([b.id for b in books])
61 book = books[0] if len(books) == 1 else None
63 return render_to_response('lesmianator/poem.html',
64 {"poem": p, "shelf": tag, "books": books, "book": book},
65 context_instance=RequestContext(request))
67 def get_poem(request, poem):
68 p = get_object_or_404(Poem, slug=poem)
70 books = Book.objects.filter(id__in=p.get_created_from_value())
71 book = books[0] if len(books) == 1 else None
73 return render_to_response('lesmianator/poem.html',
74 {"poem": p, "books": books, "book": book},
75 context_instance=RequestContext(request))