|
19 | 19 | from Acquisition import aq_parent
|
20 | 20 |
|
21 | 21 |
|
| 22 | +logger = logging.getLogger('ZPublisher') |
22 | 23 | AC_LOGGER = logging.getLogger('event.AccessControl')
|
23 | 24 |
|
24 | 25 |
|
@@ -100,3 +101,112 @@ def basic_auth_decode(token):
|
100 | 101 | plain = plain.decode('latin-1')
|
101 | 102 | user, password = plain.split(':', 1) # Split at most once
|
102 | 103 | return (user, password)
|
| 104 | + |
| 105 | + |
| 106 | +def _string_tuple(value): |
| 107 | + if not value: |
| 108 | + return () |
| 109 | + return tuple([safe_unicode(element) for element in value]) |
| 110 | + |
| 111 | + |
| 112 | +def fix_properties(obj, path=None): |
| 113 | + """Fix properties on object. |
| 114 | +
|
| 115 | + This does two things: |
| 116 | +
|
| 117 | + 1. Make sure lines contain only strings, instead of bytes, |
| 118 | + or worse: a combination of strings and bytes. |
| 119 | + 2. Replace deprecated ulines, utext, utoken, and ustring properties |
| 120 | + with their non-unicode variant, using native strings. |
| 121 | +
|
| 122 | + See https://github.com/zopefoundation/Zope/issues/987 |
| 123 | +
|
| 124 | + Since Zope 5.3, a lines property stores strings instead of bytes. |
| 125 | + But there is no migration yet. (We do that here.) |
| 126 | + Result is that getProperty on an already created lines property |
| 127 | + will return the old value with bytes, but a newly created lines property |
| 128 | + will return strings. And you might get combinations. |
| 129 | +
|
| 130 | + Also since Zope 5.3, the ulines property type is deprecated. |
| 131 | + You should use a lines property instead. |
| 132 | + Same for a few others: utext, utoken, ustring. |
| 133 | + The unicode variants are planned to be removed in Zope 6. |
| 134 | +
|
| 135 | + Intended usage: |
| 136 | + app.ZopeFindAndApply(app, apply_func=fix_properties) |
| 137 | + """ |
| 138 | + if path is None: |
| 139 | + # When using ZopeFindAndApply, path is always given. |
| 140 | + # But we may be called by other code. |
| 141 | + if hasattr(object, 'getPhysicalPath'): |
| 142 | + path = '/'.join(object.getPhysicalPath()) |
| 143 | + else: |
| 144 | + # Some simple object, for example in tests. |
| 145 | + # We don't care about the path then, it is only shown in logs. |
| 146 | + path = "/dummy" |
| 147 | + |
| 148 | + for prop_info in obj.propertyMap(): |
| 149 | + # Example: {'id': 'title', 'type': 'string', 'mode': 'w'} |
| 150 | + prop_id = prop_info.get("id") |
| 151 | + current = obj.getProperty(prop_id) |
| 152 | + if current is None: |
| 153 | + continue |
| 154 | + new_type = prop_type = prop_info.get("type") |
| 155 | + if prop_type == "lines": |
| 156 | + new = _string_tuple(current) |
| 157 | + elif prop_type == "ulines": |
| 158 | + new_type = "lines" |
| 159 | + new = _string_tuple(current) |
| 160 | + elif prop_type == "utokens": |
| 161 | + new_type = "tokens" |
| 162 | + new = _string_tuple(current) |
| 163 | + elif prop_type == "utext": |
| 164 | + new_type = "text" |
| 165 | + new = safe_unicode(current) |
| 166 | + elif prop_type == "ustring": |
| 167 | + new_type = "string" |
| 168 | + new = safe_unicode(current) |
| 169 | + else: |
| 170 | + continue |
| 171 | + if prop_type != new_type: |
| 172 | + # Replace with non-unicode variant. |
| 173 | + # This could easily lead to: |
| 174 | + # Exceptions.BadRequest: Invalid or duplicate property id. |
| 175 | + # obj._delProperty(prop_id) |
| 176 | + # obj._setProperty(prop_id, new, new_type) |
| 177 | + # So fix it by using internal details. |
| 178 | + for prop in obj._properties: |
| 179 | + if prop.get("id") == prop_id: |
| 180 | + prop["type"] = new_type |
| 181 | + # This is a tuple, so force a safe, just to be sure. |
| 182 | + obj._properties = obj._properties |
| 183 | + break |
| 184 | + else: |
| 185 | + # This probably cannot happen. |
| 186 | + # If it does, we want to know. |
| 187 | + logger.warning( |
| 188 | + "Could not change property %s from %s to %s for %s", |
| 189 | + prop_id, |
| 190 | + prop_type, |
| 191 | + new_type, |
| 192 | + path, |
| 193 | + ) |
| 194 | + continue |
| 195 | + obj._updateProperty(prop_id, new) |
| 196 | + logger.info( |
| 197 | + "Changed property %s from %s to %s for %s", |
| 198 | + prop_id, |
| 199 | + prop_type, |
| 200 | + new_type, |
| 201 | + path, |
| 202 | + ) |
| 203 | + continue |
| 204 | + if current != new: |
| 205 | + obj._updateProperty(prop_id, new) |
| 206 | + logger.info( |
| 207 | + "Changed property %s at %s so value fits the type %s (%r)", |
| 208 | + prop_id, |
| 209 | + path, |
| 210 | + prop_type, |
| 211 | + new, |
| 212 | + ) |
0 commit comments