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.
5 from django.shortcuts import render_to_response, get_object_or_404
6 from django.template import RequestContext
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, slug):
37 book = get_object_or_404(Book, slug=slug)
38 user = request.user if request.user.is_authenticated() else None
39 text = Poem.write(Continuations.get(book))
40 p = Poem(slug=get_random_hash(text), text=text, created_by=user)
41 p.created_from = [book.id]
44 return render_to_response('lesmianator/poem.html',
45 {"poem": p, "books": [book], "book": book},
46 context_instance=RequestContext(request))
50 def poem_from_set(request, shelf):
51 user = request.user if request.user.is_authenticated() else None
52 tag = get_object_or_404(Tag, category='set', slug=shelf)
53 text = Poem.write(Continuations.get(tag))
54 p = Poem(slug=get_random_hash(text), text=text, created_by=user)
55 books = Book.tagged.with_any((tag,))
56 p.created_from = [b.id for b in books]
59 book = books[0] if len(books) == 1 else None
61 return render_to_response('lesmianator/poem.html',
62 {"poem": p, "shelf": tag, "books": books, "book": book},
63 context_instance=RequestContext(request))
65 def get_poem(request, poem):
66 p = get_object_or_404(Poem, slug=poem)
69 books = Book.objects.filter(id__in=p.created_from)
70 book = books[0] if len(books) == 1 else None
74 return render_to_response('lesmianator/poem.html',
75 {"poem": p, "books": books, "book": book},
76 context_instance=RequestContext(request))