summaryrefslogtreecommitdiff
path: root/chimere/actions.py
blob: 392c5d3f3c0d95b921802db14229327b9dd3c215 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2008-2016  Étienne Loks  <etienne.loks_AT_peacefrogsDOTnet>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# See the file COPYING for details.

"""
Actions available in the main interface
"""
from django.conf import settings
from django.http import Http404
from django.urls import reverse, NoReverseMatch
from django.utils.translation import ugettext_lazy as _

from chimere.models import Page, Area, AreaPages


class Action:
    def __init__(self, id, path, label, extra_url_args=[]):
        self.id, self.path, self.label = id, path, label
        self.extra_url_args, self.url = extra_url_args, None

    def update_url(self, area_name):
        try:
            self.url = reverse(
                self.path,
                args=[area_name + '/' if area_name else '']
                + self.extra_url_args)
        except NoReverseMatch:
            # backward url management
            self.url = reverse(
                self.path,
                args=[area_name if area_name else ''] + self.extra_url_args)

default_actions = [
    (Action('view', settings.MAIN_INDEX, _('View')), []),
    (Action('contribute', 'chimere:edit', _('Contribute')),
     (Action('edit-marker', 'chimere:editmarker',
      _('Add a new point of interest')),
      Action('edit-route', 'chimere:editroute', _('Add a new route')),
      Action('edit-polygon', 'chimere:editpolygon', _('Add a new polygon'))),
     )]

if settings.CHIMERE_FEEDS:
    default_actions.append((Action('rss', 'chimere:feeds-form',
                                   _('RSS feeds')), []))

if settings.EMAIL_HOST:
    default_actions.append((Action('contact', 'chimere:contact',
                                   _('Contact us')), []),)


def actions(area_name=''):
    acts = default_actions[:]
    area, q = None, None
    if area_name:
        q = Area.objects.filter(urn=area_name)
        if not q.count():
            q = Area.objects.filter(name=area_name)
    if not q or not q.count():
        q = Area.objects.filter(default=True)
    if q.count():
        area = q.all()[0]
        if area.show_directory:
            acts.insert(
                len(acts) - 3,
                (Action('categories', 'chimere:category-directory',
                                   _('Directory')), [])
            )
    else:
        raise Http404

    real_acts = []
    for act, childs in acts:
        real_childs = []
        for child_act in childs:
            if area:
                if (child_act.id == 'edit-marker'
                    and not area.allow_point_edition) \
                    or (child_act.id == 'edit-route'
                        and not area.allow_route_edition) \
                    or (child_act.id == 'edit-polygon'
                        and not area.allow_polygon_edition):
                    continue
            real_childs.append(child_act)
        if childs and not real_childs:
            continue
        act.update_url(area_name)
        for child_act in real_childs:
            child_act.update_url(area_name)
        real_acts.append((act, real_childs))
    for page in Page.objects.filter(areas__urn=area.urn, available=True):
        act = Action(page.mnemonic, 'chimere:extra_page', page.title,
                     [page.mnemonic])
        act.update_url(area_name)
        real_acts.append((act, []))
    return real_acts


def get_extra_pages(area):
    if not area:
        return []
    extra_pages = []
    for area_page in AreaPages.objects.filter(
            area__urn=area.urn, page__available=True).order_by('order').all():
        extra_pages.append(area_page.page)
    return extra_pages