#!/usr/bin/env python # -*- coding: utf-8 -*- # Copyright (C) 2010 É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. """ Menus """ from django.utils.translation import ugettext_lazy as _ class SectionItem: def __init__(self, idx, label, childs=[]): self.idx = idx self.label = label self.childs = childs self.available = False class MenuItem: def __init__(self, idx, label, access_controls=[]): self.idx = idx self.label = label self.access_controls = access_controls self.available = False def can_be_available(self, user): if not self.access_controls: return True for access_control in self.access_controls: if user.has_perm('furnitures.' + access_control): return True return False def is_available(self, user, obj=None): if not self.access_controls: return True for access_control in self.access_controls: if user.has_perm('furnitures.' + access_control, obj): return True return False class Menu: def __init__(self, user): self.user = user self.initialized = False self.childs = [ SectionItem('administration', _(u"Administration"), childs=[ MenuItem('person_creation', _(u"Person creation"), access_controls=['add_person', 'add_own_person']), ]), SectionItem('file_management', _(u"File management"), childs=[ MenuItem('file_creation', _(u"File creation"), access_controls=['add_file', 'add_own_file']), MenuItem('file_modification', _(u"File modification"), access_controls=['change_file', 'change_own_file']), MenuItem('file_deletion', _(u"File deletion"), access_controls=['delete_file', 'delete_own_file']), ]), SectionItem('operation_management', _(u"Operation management"), childs=[ MenuItem('operation_creation', _(u"Operation creation"), access_controls=['add_operation', 'add_own_operation']), MenuItem('operation_modification', _(u"Operation modification"), access_controls=['change_operation', 'change_own_operation']), ]), ] self.items = {} def init(self): if self.initialized: return for main_menu in self.childs: main_menu.available = False for child in main_menu.childs: if self.user: child.available = child.can_be_available(self.user) if child.available: main_menu.available = True self.items[child.idx] = child self.initialized = True menu = Menu(None) menu.init()