42a2a39d502f51160305263a8dab48c2e1e9719a
[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.settings import TYPES_DICT, TYPES_ON_MAIN, TYPE_SUBMIT
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 TYPES_ON_MAIN == (type_db,):
18             return redirect('migdal_main')
19         entry_type = TYPES_DICT[type_db]
20         templates = ["migdal/entry/%s/entry_list.html" % type_db] + templates
21         submit = type_db == TYPE_SUBMIT
22     else:
23         submit = TYPES_ON_MAIN == (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     object_list = api.entry_list(entry_type=entry_type, category=category)
32
33     return render(request, templates, {
34             'object_list': object_list,
35             'category': category,
36             'entry_type': entry_type,
37             'submit': submit,
38         })
39
40
41 def entry(request, type_db, slug):
42     lang = request.LANGUAGE_CODE
43     args = {'type': type_db, 'slug_%s' % lang: slug, 'published_%s' % lang: True}
44     # TODO: preview for admins
45     entry = get_object_or_404(Entry, **args)
46
47     templates = ["migdal/entry/entry_detail.html"]
48     if type_db is not None:
49         templates = ["migdal/entry/%s/entry_detail.html" % type_db] + templates
50     return render(request, templates, {'entry': entry})
51
52
53 def submit(request):
54     if request.method == 'POST':
55         submit_form = get_submit_form(request.POST)
56         if submit_form.is_valid():
57             submit_form.save()
58             return redirect('migdal_submit_thanks')
59     else:
60         submit_form = get_submit_form()
61
62     return render(request, 'migdal/entry/submit.html', {
63             'submit_form': submit_form,
64         })
65
66 def submit_thanks(request):
67     return render(request, "migdal/entry/submit_thanks.html")