d705d34d8743dc7d1643fe0999683590f2a9e39e
[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
6 from migdal import api
7 from migdal.models import Category, Entry
8 from migdal.settings import TYPES_DICT
9
10
11 def entry_list(request, type_db=None, category_slug=None):
12     lang = request.LANGUAGE_CODE
13     templates = ["migdal/entry/entry_list.html"]
14
15     if category_slug:
16         category = get_object_or_404(Category, **{'slug_%s' % lang: category_slug})
17     else:
18         category = None
19     if type_db:
20         entry_type = TYPES_DICT[type_db]
21         # TODO: if it's the only on main, redirect to main
22         templates = ["migdal/entry/%s/entry_list.html" % type_db] + templates
23     else:
24         entry_type = None
25
26     object_list = api.entry_list(entry_type=entry_type, category=category)
27
28     return render(request, templates, {
29             'object_list': object_list,
30             'category': category,
31             'entry_type': entry_type,
32         })
33
34
35 def entry(request, type_db, slug):
36     lang = request.LANGUAGE_CODE
37     args = {'type': type_db, 'slug_%s' % lang: slug, 'published_%s' % lang: True}
38     # TODO: preview for admins
39     entry = get_object_or_404(Entry, **args)
40
41     templates = ["migdal/entry/entry_detail.html"]
42     if type_db is not None:
43         templates = ["migdal/entry/%s/entry_detail.html" % type_db] + templates
44     return render(request, templates, {'entry': entry})