-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLocalPropertyManager.py
317 lines (244 loc) · 10.7 KB
/
LocalPropertyManager.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# -*- coding: UTF-8 -*-
# Copyright (C) 2000-2004 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 the Standard Library
from urllib import quote
from time import time
# Import from Zope
from AccessControl import ClassSecurityInfo
from Acquisition import aq_base
from App.class_init import InitializeClass
# Import from Localizer
from LanguageManager import LanguageManager
from LocalAttributes import LocalAttribute, LocalAttributesBase
from LocalFiles import LocalDTMLFile
# FIXME
# For backwards compatibility (<= 0.8.0): other classes import 'LocalProperty'
# Since this may be stored as a persistent value, we cannot remove it, first
# we must provide an upgrade facility
LocalProperty = LocalAttribute
class LocalPropertyManager(LanguageManager, LocalAttributesBase):
"""
Mixin class that allows to manage localized properties.
Somewhat similar to OFS.PropertyManager.
"""
security = ClassSecurityInfo()
# Metadata for local properties
# Example: ({'id': 'title', 'type': 'string'},)
_local_properties_metadata = ()
# Local properties are stored here
# Example: {'title': {'en': ('Title', timestamp), 'es': ('Títul', timestamp)}}
_local_properties = {}
# Useful to find or index all LPM instances
isLocalPropertyManager = 1
def getLocalPropertyManager(self):
"""
Returns the instance, useful to get the object through acquisition.
"""
return self
manage_options = (
{'action': 'manage_localPropertiesForm',
'label': u'Local properties',
'help': ('Localizer', 'LPM_properties.stx')},
{'action': 'manage_transPropertiesForm',
'label': u'Translate properties',
'help': ('Localizer', 'LPM_translate.stx')}) \
+ LanguageManager.manage_options
security.declarePublic('hasLocalProperty')
def hasLocalProperty(self, id):
"""Return true if object has a property 'id'"""
for property in self._local_properties_metadata:
if property['id'] == id:
return 1
return 0
security.declareProtected('View management screens',
'manage_localPropertiesForm')
manage_localPropertiesForm = LocalDTMLFile('ui/LPM_properties', globals())
security.declareProtected('View management screens',
'manage_transPropertiesForm')
manage_transPropertiesForm = LocalDTMLFile('ui/LPM_translations', globals())
security.declareProtected('Manage properties', 'set_localpropvalue')
def set_localpropvalue(self, id, lang, value):
# Get previous value
old_value, timestamp = self.get_localproperty(id, lang)
if old_value is None:
old_value = ''
# Update value only if it is different
if value != old_value:
properties = self._local_properties.copy()
if not properties.has_key(id):
properties[id] = {}
properties[id][lang] = (value, time())
self._local_properties = properties
def get_localproperty(self, name, language):
if name not in self._local_properties:
return None, None
property = self._local_properties[name]
if language not in property:
return None, None
value = property[language]
if isinstance(value, tuple):
return value
return value, None
security.declareProtected('Manage properties', 'set_localproperty')
def set_localproperty(self, id, type, lang=None, value=None):
"""Adds a new local property"""
if not self.hasLocalProperty(id):
self._local_properties_metadata += ({'id': id, 'type': type},)
setattr(self, id, LocalAttribute(id))
if lang is not None:
self.set_localpropvalue(id, lang, value)
security.declareProtected('Manage properties', 'del_localproperty')
def del_localproperty(self, id):
"""Deletes a property"""
# Update properties metadata
p = [ x for x in self._local_properties_metadata if x['id'] != id ]
self._local_properties_metadata = tuple(p)
# delete attribute
try:
del self._local_properties[id]
except KeyError:
pass
try:
delattr(self, id)
except KeyError:
pass
security.declareProtected('Manage properties', 'manage_addLocalProperty')
def manage_addLocalProperty(self, id, type, REQUEST=None, RESPONSE=None):
"""Adds a new local property"""
self.set_localproperty(id, type)
if RESPONSE is not None:
url = "%s/manage_localPropertiesForm?manage_tabs_message=Saved changes." % REQUEST['URL1']
RESPONSE.redirect(url)
security.declareProtected('Manage properties', 'manage_editLocalProperty')
def manage_editLocalProperty(self, REQUEST, RESPONSE=None):
"""Edit a property"""
def_lang = self.get_default_language()
form = REQUEST.form
for prop in self.getLocalProperties():
name = prop['id']
if form.has_key(name):
value = form[name].strip()
self.set_localpropvalue(name, def_lang, value)
if REQUEST is not None:
url = "%s/%s?manage_tabs_message=Saved changes." \
% (REQUEST['URL1'], REQUEST['destination'])
REQUEST.RESPONSE.redirect(url)
security.declareProtected('Manage properties', 'manage_delLocalProperty')
def manage_delLocalProperty(self, ids=[], REQUEST=None, RESPONSE=None):
"""Deletes a property"""
for id in ids:
self.del_localproperty(id)
if RESPONSE is not None:
url = "%s/manage_localPropertiesForm?manage_tabs_message=Saved changes." % REQUEST['URL1']
RESPONSE.redirect(url)
security.declareProtected('Manage properties', 'manage_transLocalProperty')
def manage_transLocalProperty(self, id, code, value, REQUEST,
RESPONSE=None):
"""Translate a property."""
self.set_localpropvalue(id, code, value.strip())
if RESPONSE is not None:
url = "%s/%s?lang=%s&prop=%s&manage_tabs_message=Saved changes." \
% (REQUEST['URL1'], REQUEST['destination'], code, id)
RESPONSE.redirect(url)
security.declareProtected('Manage properties', 'is_obsolete')
def is_obsolete(self, prop, lang):
default_language = self.get_default_language()
value, t0 = self.get_localproperty(prop, default_language)
value, t1 = self.get_localproperty(prop, lang)
if t0 is None:
return False
if t1 is None:
return True
return t1 < t0
security.declarePublic('getTargetLanguages')
def get_targetLanguages(self):
"""Get all languages except the default one."""
def_lang = self.get_default_language()
all_langs = self.get_languages_mapping()
for record in all_langs:
if def_lang == record['code']:
all_langs.remove(record)
return all_langs
security.declarePublic('getLocalProperties')
def getLocalProperties(self):
"""Returns a copy of the properties metadata."""
return tuple([ x.copy() for x in self._local_properties_metadata ])
security.declarePublic('getLocalAttribute')
def getLocalAttribute(self, id, lang=None):
"""Returns a local property"""
# No language, look for the first non-empty available version
if lang is None:
lang = self.get_selected_language(property=id)
value, timestamp = self.get_localproperty(id, lang)
if value is None:
return ''
return value
# Languages logic
security.declarePublic('get_available_languages')
def get_available_languages(self, **kw):
""" """
languages = self.get_languages()
id = kw.get('property', None)
if id is None:
# Is this thing right??
return languages
else:
if id in self._local_properties:
property = self._local_properties[id]
return [ x for x in languages if property.get(x, None) ]
else:
return []
security.declarePublic('get_default_language')
def get_default_language(self):
""" """
if self._default_language:
return self._default_language
languages = self.get_languages()
if languages:
return languages[0]
return None
# Upgrading..
def _needs_upgrade(self):
return hasattr(aq_base(self), 'original_language')
def _upgrade(self):
# In version 0.7 the language management logic moved to the
# mixin class LanguageManager, as a consequence the attribute
# "original_language" changes its name to "_default_language".
if hasattr(aq_base(self), 'original_languge'):
self._default_language = self.original_language
del self.original_language
# XXX With version 1.1.0b5 (as of patch 14) the '_local_properties'
# data structure keeps a timestamp to mark obsolete translations.
# The upgrade code below must be activated once the new upgrade
# framework is deployed, something that should happen for the 1.2
# release.
## for k, v in self._local_properties.items():
## for i, j in v.items():
## if type(j) is not tuple:
## # XXX add the timestamp for every property
## self._local_properties[k][i] = (j, time())
## self._p_changed = 1
# Define <id>_<lang> attributes, useful for example to catalog
def __getattr__(self, name):
try:
index = name.rfind('_')
id, lang = name[:index], name[index+1:]
property = self._local_properties[id]
except:
raise AttributeError, "%s instance has no attribute '%s'" \
% (self.__class__.__name__, name)
return self.getLocalAttribute(id, lang)
InitializeClass(LocalPropertyManager)