-
Notifications
You must be signed in to change notification settings - Fork 1
/
popup.py
232 lines (169 loc) · 8.3 KB
/
popup.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# -*- coding: UTF-8 -*-
# Copyright (C) 2010-2011 Taverne Sylvain <[email protected]>
# Copyright (C) 2011 Henry Obein <[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 freeze
from itools.database import NotQuery, OrQuery
# Import from ikaaro
from ikaaro.autoform import SelectWidget
from ikaaro.file import Image
from ikaaro.popup import AddMedia_BrowseContent, AddImage_BrowseContent
from ikaaro.popup import DBResource_AddLink, DBResource_AddMedia
from ikaaro.popup import DBResource_AddImage, AddBase_BrowseContent
from ikaaro.utils import get_base_path_query
# Import from itws
from itws.datatypes import SortBy_Enumerate, Reverse_Enumerate
from itws.feed_views import Feed_View
##################################################
# Some methods
##################################################
def get_itws_namespace(cls, self, resource, context):
namespace = cls.get_namespace(self, resource, context)
# XXX Hack breadcrumb
breadcrumb = []
for x in namespace['breadcrumb']:
x['url'] = x['url'].replace(batch_start=None)
breadcrumb.append(x)
namespace['breadcrumb'] = breadcrumb
return namespace
def itws_get_item_value(self, resource, context, item, column):
brain, item_resource = item
if column == 'js_link':
id = str(resource.get_canonical_path().get_pathto(brain.abspath))
id += self.resource_action
return id
elif column == 'is_selectable':
return isinstance(item_resource, self.get_item_classes())
elif column == 'link':
if self.is_folder(item_resource):
path_to_item = context.site_root.get_pathto(item_resource)
url_dic = {'target': str(path_to_item),
# Avoid search conservation
'search_text': None,
'search_type': None,
# Reset batch
'batch_start': None}
return context.uri.replace(**url_dic)
return None
return Feed_View.get_item_value(self, resource, context, item, column)
def itws_get_table_namespace(self, resource, context, items):
namespace = Feed_View.get_table_namespace(self, resource, context, items)
# Sort by
widget = SelectWidget('sort_by',
datatype=SortBy_Enumerate,
value=context.query['sort_by'],
has_empty_option=False)
namespace['sort_by'] = widget.render()
widget = SelectWidget('reverse',
datatype=Reverse_Enumerate,
value=str(int(context.query['reverse'])),
has_empty_option=False)
namespace['reverse'] = widget.render()
for key in ['target', 'target_id', 'mode']:
namespace[key] = context.get_form_value(key)
# If target is no defined in the query (when we open the popup)
# Get the target value from attributes
if namespace['target'] is None:
popup_root_abspath = self.popup_root.get_abspath()
target = popup_root_abspath.get_pathto(self.target.get_abspath())
namespace['target'] = target
return namespace
def itws_get_additional_args(resource):
args = []
# Current folder
abspath = resource.get_canonical_path()
args.append(get_base_path_query(abspath, depth=1))
# Ignore query
method = getattr(resource, 'get_internal_use_resource_names', None)
if method:
exclude_query = []
resource_abspath = resource.get_abspath()
for name in method():
abspath = resource_abspath.resolve2(name)
q = get_base_path_query(str(abspath), include_container=True)
exclude_query.append(q)
args.append(NotQuery(OrQuery(*exclude_query)))
return args
##################################################
##################################################
class ITWS_AddBase_BrowseContent(Feed_View, AddBase_BrowseContent):
table_template = '/ui/common/popup_browse_content.xml'
content_keys = Feed_View.content_keys + ('js_link', 'link', 'is_selectable')
hidden_fields = freeze(Feed_View.hidden_fields +
AddBase_BrowseContent.hidden_fields + ['target'])
def _get_query_value(self, resource, context, name):
if name == 'target':
# Target is a special case
site_root = resource.get_site_root()
target = self._get_target(resource, context)
return site_root.get_pathto(target)
return Feed_View._get_query_value(self, resource, context, name)
def _get_target(self, resource, context):
query_target = context.get_form_value('target')
if query_target is None:
target = self.target
else:
site_root = resource.get_site_root()
target = site_root.get_resource(query_target)
return target
def get_items(self, resource, context, *args):
target = self._get_target(resource, context)
args = list(args)
args.extend(itws_get_additional_args(self.target))
return AddBase_BrowseContent.get_items(self, target, context, *args)
def get_table_namespace(self, resource, context, items):
return itws_get_table_namespace(self, resource, context, items)
def get_item_value(self, resource, context, item, column):
return itws_get_item_value(self, resource, context, item, column)
class ITWS_AddMedia_BrowseContent(Feed_View, AddMedia_BrowseContent):
table_template = '/ui/common/popup_browse_content.xml'
content_keys = Feed_View.content_keys + ('js_link', 'link', 'is_selectable')
hidden_fields = freeze(Feed_View.hidden_fields +
AddMedia_BrowseContent.hidden_fields + ['target'])
def get_items(self, resource, context, *args):
args = list(args)
args.extend(itws_get_additional_args(self.target))
return AddMedia_BrowseContent.get_items(self, resource, context, *args)
def get_table_namespace(self, resource, context, items):
return itws_get_table_namespace(self, resource, context, items)
def get_item_value(self, resource, context, item, column):
return itws_get_item_value(self, resource, context, item, column)
class ITWS_AddImage_BrowseContent(Feed_View, AddImage_BrowseContent):
table_template = '/ui/common/popup_browse_content.xml'
content_keys = Feed_View.content_keys + ('js_link', 'link', 'is_selectable')
item_classes = (Image,)
hidden_fields = freeze(Feed_View.hidden_fields +
AddImage_BrowseContent.hidden_fields + ['target'])
def get_items(self, resource, context, *args):
args = list(args)
args.extend(itws_get_additional_args(self.target))
return AddImage_BrowseContent.get_items(self, resource, context, *args)
def get_table_namespace(self, resource, context, items):
return itws_get_table_namespace(self, resource, context, items)
def get_item_value(self, resource, context, item, column):
return itws_get_item_value(self, resource, context, item, column)
class ITWS_DBResource_AddLink(DBResource_AddLink):
browse_content_class = ITWS_AddBase_BrowseContent
def get_namespace(self, resource, context):
return get_itws_namespace(DBResource_AddLink, self, resource, context)
class ITWS_DBResource_AddImage(DBResource_AddImage):
browse_content_class = ITWS_AddImage_BrowseContent
def get_namespace(self, resource, context):
return get_itws_namespace(DBResource_AddImage, self, resource, context)
class ITWS_DBResource_AddMedia(DBResource_AddMedia):
browse_content_class = ITWS_AddMedia_BrowseContent
def get_namespace(self, resource, context):
return get_itws_namespace(DBResource_AddMedia, self, resource, context)