-
Notifications
You must be signed in to change notification settings - Fork 5
/
Categories.py
188 lines (146 loc) · 5.34 KB
/
Categories.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# -*- coding: utf-8; mode: python -*-
#
# Cherokee-admin
#
# Authors:
# Alvaro Lopez Ortega <[email protected]>
#
# Copyright (C) 2001-2011 Alvaro Lopez Ortega
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public
# License as published by the Free Software Foundation.
#
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#
import os
import re
import CTK
URL_CAT_LIST_VSRV = '/2wizard/category/vsrv'
URL_CAT_LIST_VSRV_R = r'^/2wizard/category/vsrv/(.*)$'
URL_CAT_LIST_RULE = '/2wizard/category/rule'
URL_CAT_LIST_RULE_R = r'^/2wizard/category/rule/(.*)$'
URL_CAT_APPLY = '/2wizard/category/apply'
TYPE_VSERVER = 1
TYPE_RULE = 1 << 2
# Handle 'click' events inside the Wizard Categories list.
#
JS_WIZARD_LIST = """
$('#%(list_id)s').each(function() {
var box = $(this);
var hidden = box.find('input:hidden');
box.find('li').each(function() {
var li = $(this);
li.bind ('click', function(event) {
box.find('li').removeClass('wizard-list-selected');
$(this).addClass ('wizard-list-selected');
hidden.val (li.attr('wizard'));
});
});
});
"""
# Generates a 'open_wizard' event whenever the dialog is submitted.
#
JS_WIZARD_LIST_SUBMIT = """
var selected = $('#%(hidden)s').val();
if (selected) {
$(this).trigger ({type: 'open_wizard', 'wizard': selected});
}
return false;
"""
_wizards_objs = []
def load_wizards():
# Cache Miss
global _wizards_objs
if not _wizards_objs:
# Locate the directory
wizards_path = os.path.realpath (__file__ + "/../wizards")
wizards = os.listdir (wizards_path)
for wizard in wizards:
# Skip back-ups
if (wizard.startswith ('.')) or ('#' in wizard) or (not wizard.endswith('.py')):
continue
# Load the wizard
wizard_name = wizard.replace('.py', '')
wizard_fp = os.path.join (wizards_path, wizard_name)
mod = CTK.load_module (wizard_name, "wizards2/wizards")
# Update Cache
_wizards_objs.append (mod)
return _wizards_objs
class Icon (CTK.Image):
def __init__ (self, wizard_info, _props={}):
icon = wizard_info['icon_small']
name = wizard_info['name']
props = _props.copy()
props['src'] = '/static/images/wizards2/%s'%(icon)
props['alt'] = "%s logo" %(name)
if 'class' in props:
props['class'] += ' wizard-icon'
else:
props['class'] = 'wizard-icon'
CTK.Image.__init__ (self, props)
class CategoryList_Widget (CTK.Box):
def __init__ (self, category_num, wizards_type):
CTK.Box.__init__ (self)
self.category_num = int(category_num)
self.category_name = get()[self.category_num]
# GUI
wlist = CTK.List({'class': 'wizard-list'})
hidden = CTK.Hidden ('wizard')
submit = CTK.Submitter (URL_CAT_APPLY)
submit += wlist
submit += hidden
submit += CTK.RawHTML (js = JS_WIZARD_LIST %({'list_id': self.id}))
submit.bind ('submit_success', JS_WIZARD_LIST_SUBMIT %({'hidden': hidden.id}))
self += submit
# Fill the list
for wizard_mod in load_wizards():
if not 'software' in wizard_mod.__dict__:
continue
if wizard_mod.software['category'] != self.category_name:
continue
w_name = wizard_mod.software['name']
w_id = wizard_mod.software['id']
w_desc = wizard_mod.software['desc_short']
wlist.Add ([CTK.Box({'class': 'logo'}, Icon(wizard_mod.software)),
CTK.Box({'class': 'title'}, CTK.RawHTML(_(w_name))),
CTK.Box({'class': 'descr'}, CTK.RawHTML(_(w_desc)))],
{'wizard': w_id})
return
def CategoryList_Vsrv():
# Figure the category
category_num = re.findall (URL_CAT_LIST_VSRV_R, CTK.request.url)[0]
content = CategoryList_Widget (category_num, TYPE_VSERVER)
return content.Render().toJSON()
def CategoryList_Rule():
# Figure the category
category_num = re.findall (URL_CAT_LIST_RULE_R, CTK.request.url)[0]
content = CategoryList_Widget (category_num, TYPE_RULE)
return content.Render().toJSON()
def CategoryList_Apply():
return CTK.cfg_reply_ajax_ok()
CTK.publish (URL_CAT_LIST_VSRV_R, CategoryList_Vsrv)
CTK.publish (URL_CAT_LIST_RULE_R, CategoryList_Rule)
CTK.publish ('^%s'%(URL_CAT_APPLY), CategoryList_Apply, method="POST")
#
# Utilities
#
_wizard_categories = []
def get():
global _wizard_categories
if not _wizard_categories:
categories = []
for wizard_mod in load_wizards():
if not 'software' in wizard_mod.__dict__:
continue
categories.append (wizard_mod.software['category'])
_wizard_categories = list(set(categories))
return _wizard_categories