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.
5 from django.shortcuts import get_object_or_404, render, redirect
6 from django.utils.translation import get_language
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
14 def entry_list(request, type_db=None, category_slug=None):
15 lang = request.LANGUAGE_CODE
16 templates = ["migdal/entry/entry_list.html"]
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
25 submit = app_settings.TYPES_ON_MAIN == (app_settings.TYPE_SUBMIT,)
29 category = get_object_or_404(Category, **{'slug_%s' % lang: category_slug})
33 promobox = 5 if entry_type is None and category is None else None
35 object_list = api.entry_list(entry_type=entry_type, category=category,
38 return render(request, templates, {
39 'object_list': object_list,
41 'entry_type': entry_type,
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)
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})
60 if request.method == 'POST':
61 submit_form = get_submit_form(request.POST)
62 if submit_form.is_valid():
64 return redirect('migdal_submit_thanks')
66 submit_form = get_submit_form()
68 return render(request, 'migdal/entry/submit.html', {
69 'submit_form': submit_form,
72 def submit_thanks(request):
73 return render(request, "migdal/entry/submit_thanks.html")
76 class SearchPublishedView(SearchView):
77 def __init__(self, *args, **kwargs):
78 super(SearchPublishedView, self).__init__(*args, **kwargs)
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
88 results = filter(lambda r: is_published(r.object), results)