summaryrefslogtreecommitdiff
path: root/ishtar_common/menu_base.py
diff options
context:
space:
mode:
Diffstat (limited to 'ishtar_common/menu_base.py')
-rw-r--r--ishtar_common/menu_base.py86
1 files changed, 86 insertions, 0 deletions
diff --git a/ishtar_common/menu_base.py b/ishtar_common/menu_base.py
new file mode 100644
index 000000000..02e110910
--- /dev/null
+++ b/ishtar_common/menu_base.py
@@ -0,0 +1,86 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# Copyright (C) 2012 É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 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 <http://www.gnu.org/licenses/>.
+
+# 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)
+