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, 'published_%s' % lang: True}
49 # TODO: preview for admins
50 entry = get_object_or_404(Entry, **args)
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})
59 if request.method == 'POST':
60 submit_form = get_submit_form(request.POST)
61 if submit_form.is_valid():
63 return redirect('migdal_submit_thanks')
65 submit_form = get_submit_form()
67 return render(request, 'migdal/entry/submit.html', {
68 'submit_form': submit_form,
71 def submit_thanks(request):
72 return render(request, "migdal/entry/submit_thanks.html")
75 class SearchPublishedView(SearchView):
76 def __init__(self, *args, **kwargs):
77 super(SearchPublishedView, self).__init__(*args, **kwargs)
79 def get_results(self):
80 results = super(SearchPublishedView, self).get_results()
81 lang_code = get_language()
82 def is_published(entity):
83 print "is published? %s, %s" % (entity.published_pl, entity.published_en)
84 if isinstance(entity, Entry):
85 return getattr(entity, "published_%s" % lang_code) == True
88 results = filter(lambda r: is_published(r.object), results)