1ece4cc0384baa718f19f8b50ccc7995642d1437
[prawokultury.git] / migdal / views.py
1 # -*- coding: utf-8 -*-
2 # This file is part of PrawoKultury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 #
5 from django.shortcuts import get_object_or_404, render, redirect
6 from django.utils.translation import get_language
7 from migdal import api
8 from migdal.forms import get_submit_form
9 from migdal.models import Category, Entry
10 from migdal import app_settings
11 from haystack.views import SearchView
12
13
14 def entry_list(request, type_db=None, category_slug=None):
15     lang = request.LANGUAGE_CODE
16     templates = ["migdal/entry/entry_list.html"]
17
18     if type_db:
19         if app_settings.TYPES_ON_MAIN == (type_db,):
20             return redirect('migdal_main')
21         entry_type = app_settings.TYPES_DICT[type_db]
22         templates = ["migdal/entry/%s/entry_list.html" % type_db] + templates
23         submit = type_db == app_settings.TYPE_SUBMIT
24     else:
25         submit = app_settings.TYPES_ON_MAIN == (app_settings.TYPE_SUBMIT,)
26         entry_type = None
27
28     if category_slug:
29         category = get_object_or_404(Category, **{'slug_%s' % lang: category_slug})
30     else:
31         category = None
32
33     promobox = 5 if entry_type is None and category is None else None
34
35     object_list = api.entry_list(entry_type=entry_type, category=category,
36                     promobox=promobox)
37
38     return render(request, templates, {
39             'object_list': object_list,
40             'category': category,
41             'entry_type': entry_type,
42             'submit': submit,
43         })
44
45
46 def entry(request, type_db, slug):
47     lang = request.LANGUAGE_CODE
48     args = {'type': type_db, 'slug_%s' % lang: slug, 'published_%s' % lang: True}
49     # TODO: preview for admins
50     entry = get_object_or_404(Entry, **args)
51
52     templates = ["migdal/entry/entry_detail.html"]
53     if type_db is not None:
54         templates = ["migdal/entry/%s/entry_detail.html" % type_db] + templates
55     return render(request, templates, {'entry': entry})
56
57
58 def submit(request):
59     if request.method == 'POST':
60         submit_form = get_submit_form(request.POST)
61         if submit_form.is_valid():
62             submit_form.save()
63             return redirect('migdal_submit_thanks')
64     else:
65         submit_form = get_submit_form()
66
67     return render(request, 'migdal/entry/submit.html', {
68             'submit_form': submit_form,
69         })
70
71 def submit_thanks(request):
72     return render(request, "migdal/entry/submit_thanks.html")
73
74
75 class SearchPublishedView(SearchView):
76     def __init__(self, *args, **kwargs):
77         super(SearchPublishedView, self).__init__(*args, **kwargs)
78
79     def get_results(self):
80         results = super(SearchPublishedView, self).get_results()
81         lang_code = get_language()
82         def is_published(entity):
83             if isinstance(entity, Entry):
84                 return getattr(entity, "published_%s" % lang_code) == True
85             else:
86                 return True
87         results = filter(lambda r: is_published(r.object), results)
88         print results
89         return results