add publication dates, last change date
[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}
49     if not request.user.has_perm('migdal.change_entry'):
50         args['published_%s' % lang] = True
51     entry = get_object_or_404(Entry, **args)
52
53     templates = ["migdal/entry/entry_detail.html"]
54     if type_db is not None:
55         templates = ["migdal/entry/%s/entry_detail.html" % type_db] + templates
56     return render(request, templates, {'entry': entry})
57
58
59 def submit(request):
60     if request.method == 'POST':
61         submit_form = get_submit_form(request.POST)
62         if submit_form.is_valid():
63             submit_form.save()
64             return redirect('migdal_submit_thanks')
65     else:
66         submit_form = get_submit_form()
67
68     return render(request, 'migdal/entry/submit.html', {
69             'submit_form': submit_form,
70         })
71
72 def submit_thanks(request):
73     return render(request, "migdal/entry/submit_thanks.html")
74
75
76 class SearchPublishedView(SearchView):
77     def __init__(self, *args, **kwargs):
78         super(SearchPublishedView, self).__init__(*args, **kwargs)
79
80     def get_results(self):
81         results = super(SearchPublishedView, self).get_results()
82         lang_code = get_language()
83         def is_published(entity):
84             if isinstance(entity, Entry):
85                 return getattr(entity, "published_%s" % lang_code) == True
86             else:
87                 return True
88         results = filter(lambda r: is_published(r.object), results)
89         print results
90         return results