d8652c7214dabb46f53142949bd4c35072c49c1a
[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 fnpdjango.utils.views import set_current_object
8 from migdal import api
9 from migdal.forms import get_submit_form
10 from migdal.models import Category, Entry
11 from migdal import app_settings
12 from haystack.views import SearchView
13
14
15 def main(request):
16     if app_settings.MAIN_PAGE_ENTRY is not None:
17         main_entry = Entry.objects.get(**app_settings.MAIN_PAGE_ENTRY)
18         return entry(request, entry=main_entry)
19     else:
20         return entry_list(request)
21
22
23 def entry_list(request, type_db=None, category_slug=None):
24     lang = request.LANGUAGE_CODE
25     templates = ["migdal/entry/entry_list.html"]
26
27     if type_db:
28         if app_settings.TYPES_ON_MAIN == (type_db,):
29             return redirect('migdal_main')
30         entry_type = app_settings.TYPES_DICT[type_db]
31         templates = ["migdal/entry/%s/entry_list.html" % type_db] + templates
32         submit = type_db == app_settings.TYPE_SUBMIT
33     else:
34         submit = app_settings.TYPES_ON_MAIN == (app_settings.TYPE_SUBMIT,)
35         entry_type = None
36
37     if category_slug:
38         category = get_object_or_404(Category, **{'slug_%s' % lang: category_slug})
39     else:
40         category = None
41
42     if category:
43         set_current_object(request, category)
44
45     promobox = 5 if entry_type is None and category is None else None
46
47     object_list = api.entry_list(entry_type=entry_type, category=category,
48                     promobox=promobox)
49
50     return render(request, templates, {
51             'object_list': object_list,
52             'category': category,
53             'entry_type': entry_type,
54             'submit': submit,
55         })
56
57
58 def entry(request, type_db=None, slug=None, entry=None):
59     if entry is None:
60         lang = request.LANGUAGE_CODE
61         args = {'type': type_db, 'slug_%s' % lang: slug}
62         entry = get_object_or_404(Entry, **args)
63     if request.user.has_perm('migdal.change_entry') or not entry.published:
64         raise Http404
65     set_current_object(request, entry, in_url=slug is not None)
66
67     templates = ["migdal/entry/entry_detail.html"]
68     if entry.type is not None:
69         templates = ["migdal/entry/%s/entry_detail.html" % type_db] + templates
70     return render(request, templates, {'entry': entry})
71
72
73 def submit(request):
74     if request.method == 'POST':
75         submit_form = get_submit_form(request.POST)
76         if submit_form.is_valid():
77             submit_form.save()
78             return redirect('migdal_submit_thanks')
79     else:
80         submit_form = get_submit_form()
81
82     return render(request, 'migdal/entry/submit.html', {
83             'submit_form': submit_form,
84         })
85
86 def submit_thanks(request):
87     return render(request, "migdal/entry/submit_thanks.html")
88
89
90 class SearchPublishedView(SearchView):
91     def __init__(self, *args, **kwargs):
92         super(SearchPublishedView, self).__init__(*args, **kwargs)
93
94     def get_results(self):
95         results = super(SearchPublishedView, self).get_results()
96         lang_code = get_language()
97         def is_published(entity):
98             if isinstance(entity, Entry):
99                 return getattr(entity, "published_%s" % lang_code) == True
100             else:
101                 return True
102         results = filter(lambda r: is_published(r.object), results)
103         print results
104         return results