summaryrefslogtreecommitdiff
path: root/ishtar_common/menu_base.py
blob: 02e1109103fa0c14c46426f9da70fe8962f846fd (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
#!/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)