befa9505997f7ce3d7808e77728af9450c441d9a
[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 migdal import api
7 from migdal.forms import get_submit_form
8 from migdal.models import Category, Entry
9 from migdal import app_settings
10
11
12 def entry_list(request, type_db=None, category_slug=None):
13     lang = request.LANGUAGE_CODE
14     templates = ["migdal/entry/entry_list.html"]
15
16     if type_db:
17         if app_settings.TYPES_ON_MAIN == (type_db,):
18             return redirect('migdal_main')
19         entry_type = app_settings.TYPES_DICT[type_db]
20         templates = ["migdal/entry/%s/entry_list.html" % type_db] + templates
21         submit = type_db == app_settings.TYPE_SUBMIT
22     else:
23         submit = app_settings.TYPES_ON_MAIN == (app_settings.TYPE_SUBMIT,)
24         entry_type = None
25
26     if category_slug:
27         category = get_object_or_404(Category, **{'slug_%s' % lang: category_slug})
28     else:
29         category = None
30
31     promobox = 5 if entry_type is None and category is None else None
32
33     object_list = api.entry_list(entry_type=entry_type, category=category,
34                     promobox=promobox)
35
36     return render(request, templates, {
37             'object_list': object_list,
38             'category': category,
39             'entry_type': entry_type,
40             'submit': submit,
41         })
42
43
44 def entry(request, type_db, slug):
45     lang = request.LANGUAGE_CODE
46     args = {'type': type_db, 'slug_%s' % lang: slug, 'published_%s' % lang: True}
47     # TODO: preview for admins
48     entry = get_object_or_404(Entry, **args)
49
50     templates = ["migdal/entry/entry_detail.html"]
51     if type_db is not None:
52         templates = ["migdal/entry/%s/entry_detail.html" % type_db] + templates
53     return render(request, templates, {'entry': entry})
54
55
56 def submit(request):
57     if request.method == 'POST':
58         submit_form = get_submit_form(request.POST)
59         if submit_form.is_valid():
60             submit_form.save()
61             return redirect('migdal_submit_thanks')
62     else:
63         submit_form = get_submit_form()
64
65     return render(request, 'migdal/entry/submit.html', {
66             'submit_form': submit_form,
67         })
68
69 def submit_thanks(request):
70     return render(request, "migdal/entry/submit_thanks.html")