#!/usr/bin/env python # -*- coding: utf-8 -*- # Copyright (C) 2012-2013 Étienne Loks # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero 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 Affero General Public License for more details. # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . # See the file COPYING for details. class SectionItem: def __init__(self, idx, label, childs=[]): self.idx = idx self.label = label self.childs = childs self.available = False self.items = {} def can_be_available(self, user): for child in self.childs: if child.can_be_available(user): return True return False def is_available(self, user, obj=None): for child in self.childs: if child.is_available(user, obj): return True return False def set_items(self, user, items): if user: self.available = self.can_be_available(user) for child in self.childs: child.set_items(user, items) items[child.idx] = child class MenuItem: def __init__(self, idx, label, model=None, access_controls=[]): self.idx = idx self.label = label self.model = model 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: access_control = self.model._meta.app_label + '.' + access_control if user.has_perm(access_control, self.model): return True # manage by person type if hasattr(user, 'ishtaruser'): person_type = user.ishtaruser.person.person_type if person_type.rights.filter(wizard__url_name=self.idx).count(): 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: access_control = self.model._meta.app_label + '.' + access_control if user.has_perm(access_control, self.model, obj): return True # manage by person type if hasattr(user, 'ishtaruser'): person_type = user.ishtaruser.person.person_type if person_type.rights.filter(wizard__url_name=self.idx).count(): return True return False def set_items(self, user, items): if user: self.available = self.can_be_available(user)