Add mock Search class for development.
[wolnelektury.git] / apps / lesmianator / views.py
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.
4 #
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
8
9 from catalogue.utils import get_random_hash
10 from catalogue.models import Book, Tag
11 from lesmianator.models import Poem, Continuations
12
13
14 def main_page(request):
15     last = Poem.objects.all().order_by('-created_at')[:10]
16     shelves = Tag.objects.filter(user__username='lesmianator')
17
18     return render_to_response('lesmianator/lesmianator.html',
19                 {"last": last, "shelves": shelves},
20                 context_instance=RequestContext(request))
21
22
23 @cache.never_cache
24 def new_poem(request):
25     user = request.user if request.user.is_authenticated() else None
26     text = Poem.write()
27     p = Poem(slug=get_random_hash(text), text=text, created_by=user)
28     p.save()
29
30     return render_to_response('lesmianator/poem.html',
31                 {"poem": p},
32                 context_instance=RequestContext(request))
33
34
35 @cache.never_cache
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]
42     p.save()
43
44     return render_to_response('lesmianator/poem.html',
45                 {"poem": p, "books": [book], "book": book},
46                 context_instance=RequestContext(request))
47
48
49 @cache.never_cache
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]
57     p.save()
58
59     book = books[0] if len(books) == 1 else None
60
61     return render_to_response('lesmianator/poem.html',
62                 {"poem": p, "shelf": tag, "books": books, "book": book},
63                 context_instance=RequestContext(request))
64
65 def get_poem(request, poem):
66     p = get_object_or_404(Poem, slug=poem)
67     p.visit()
68     if p.created_from:
69         books = Book.objects.filter(id__in=p.created_from)
70         book = books[0] if len(books) == 1 else None
71     else:
72         books = book = None
73
74     return render_to_response('lesmianator/poem.html',
75                 {"poem": p, "books": books, "book": book},
76                 context_instance=RequestContext(request))
77
78