forked from hforge/ikaaro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_groups.py
154 lines (110 loc) · 4.47 KB
/
config_groups.py
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# -*- coding: UTF-8 -*-
# Copyright (C) 2011 Juan David Ibáñez Palomar <[email protected]>
#
# 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/>.
# Import from itools
from itools.core import proto_lazy_property
from itools.datatypes import String
from itools.gettext import MSG
# Import from ikaaro
from autoadd import AutoAdd
from buttons import BrowseButton, Remove_BrowseButton, RenameButton
from config import Configuration
from config_common import NewResource_Local
from messages import MSG_CHANGES_SAVED
from order import OrderedFolder, OrderedFolder_BrowseContent
from resource_ import DBResource
from users_views import BrowseUsers
class Group_BrowseUsers(BrowseUsers):
schema = {'ids': String(multiple=True)}
table_actions = [
BrowseButton(access='is_admin', title=MSG(u'Update'))]
@proto_lazy_property
def _property_name(self):
return self.resource.parent.property_name
def get_item_value(self, resource, context, item, column):
if column == 'checkbox':
user = context.root.get_resource(item.abspath)
groups = user.get_value(self._property_name)
return item.name, str(resource.abspath) in groups
proxy = super(Group_BrowseUsers, self)
return proxy.get_item_value(resource, context, item, column)
def action(self, resource, context, form):
group_id = str(resource.abspath)
root = resource.get_resource('/')
for user in root.get_resources('users'):
groups = set(user.get_value(self._property_name))
if user.name in form['ids']:
groups.add(group_id)
else:
groups.discard(group_id)
user.set_value(self._property_name, list(groups))
context.message = MSG_CHANGES_SAVED
class Group(DBResource):
class_id = 'config-group'
class_title = MSG(u'User Group')
class_description = MSG(u'...')
# Views
class_views = ['browse_users', 'edit']
browse_users = Group_BrowseUsers
new_instance = AutoAdd(fields=['title'])
class BrowseGroups(OrderedFolder_BrowseContent):
access = 'is_admin'
title = MSG(u'Browse groups')
search_schema = {}
search_widgets = []
table_columns = [
('checkbox', None),
('abspath', MSG(u'Name')),
('title', MSG(u'Title')),
('members', MSG(u'Members')),
('mtime', MSG(u'Last Modified')),
('last_author', MSG(u'Last Author')),
('order', MSG(u'Order'))]
table_actions = [Remove_BrowseButton, RenameButton]
@proto_lazy_property
def _property_name(self):
return self.resource.property_name
def get_item_value(self, resource, context, item, column):
if column == 'members':
kw = {self._property_name: str(item.abspath)}
results = context.search(format='user', **kw)
return len(results)
proxy = super(BrowseGroups, self)
return proxy.get_item_value(resource, context, item, column)
class ConfigGroups(OrderedFolder):
class_id = 'config-groups'
class_title = MSG(u'User Groups')
class_description = MSG(u'Manage user groups.')
class_icon48 = 'icons/48x48/groups.png'
# Configuration
config_name = 'groups'
config_group = 'access'
property_name = 'groups'
# Views
class_views = ['browse_content', 'add_group', 'edit', 'commit_log']
browse_content = BrowseGroups
add_group = NewResource_Local(title=MSG(u'Add group'))
default_groups = [
('admins', {'en': u'Admins'}),
('reviewers', {'en': u'Reviewers'}),
('members', {'en': u'Members'})]
def init_resource(self, **kw):
super(ConfigGroups, self).init_resource(**kw)
# Add default groups
for name, title in self.default_groups:
self.make_resource(name, Group, title=title)
def get_document_types(self):
return [Group]
Configuration.register_module(ConfigGroups)