#!/usr/bin/env python # -*- coding: utf-8 -*- # Copyright (C) 2008-2016 Étienne Loks # 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 . # 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