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