diff options
author | Étienne Loks <etienne.loks@peacefrogs.net> | 2012-10-19 20:19:44 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@peacefrogs.net> | 2012-10-19 20:19:44 +0200 |
commit | f30dc48d37e90a3bab3d5264bd16c628eb9568ab (patch) | |
tree | d985cbed42ab34efed0689a102d27e275dd43de5 | |
parent | a25486cb11be6c3033793e2df1cf02eee8ab47f9 (diff) | |
download | Ishtar-f30dc48d37e90a3bab3d5264bd16c628eb9568ab.tar.bz2 Ishtar-f30dc48d37e90a3bab3d5264bd16c628eb9568ab.zip |
Dynamicaly manage menus by loaded applications
-rw-r--r-- | ishtar_common/menu_base.py | 86 | ||||
-rw-r--r-- | ishtar_common/menus.py | 102 |
2 files changed, 114 insertions, 74 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) + diff --git a/ishtar_common/menus.py b/ishtar_common/menus.py index 79355a6be..8bc2087c1 100644 --- a/ishtar_common/menus.py +++ b/ishtar_common/menus.py @@ -1,6 +1,6 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -# Copyright (C) 2010-2011 Étienne Loks <etienne.loks_AT_peacefrogsDOTnet> +# Copyright (C) 2010-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 @@ -21,82 +21,15 @@ Menus """ +from django.conf import settings from django.utils.translation import ugettext_lazy as _ +from menu_base import SectionItem, MenuItem import models -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) - -class Menu: - def __init__(self, user): - self.user = user - self.initialized = False - self.childs = [ +_extra_menus = [( + 10, + [ SectionItem('administration', _(u"Administration"), childs=[SectionItem('person', _(u"Person"), childs=[ @@ -110,7 +43,21 @@ class Menu: MenuItem('account_management', _(u"Account management"), model=models.IshtarUser, access_controls=['add_ishtaruser',]), - ]), + ]) + ] +)] + +for app in settings.INSTALLED_APPS: + mod = __import__(app, fromlist=['ishtar_menu']) + if hasattr(mod, 'ishtar_menu'): + menu = getattr(mod, 'ishtar_menu') + _extra_menus.append((menu.ORDER, menu.MENU_SECTIONS)) + +_section_items = [] +for order, menu in sorted(_extra_menus, key=lambda x:x[0]): + _section_items += menu + +""" SectionItem('file_management', _(u"Archaeological file"), childs=[ MenuItem('file_search', _(u"Search"), @@ -317,6 +264,13 @@ class Menu: # access_controls=['change_warehouse',]), ]), ] +""" + +class Menu: + childs = _section_items + def __init__(self, user): + self.user = user + self.initialized = False self.items = {} def init(self): |