summaryrefslogtreecommitdiff
path: root/ishtar/furnitures/menus.py
blob: 5b03e01563c4a27c7bdb71e08f50ff67010dbdba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2010  É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 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 <http://www.gnu.org/licenses/>.

# 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()