Segment value types
These classes are used to represent segment values in Python without needing a model instance.
BaseValue
Source code in wagtail_localize/segments/types.py
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 | |
clone()
Clones this segment. Must be overridden in subclass.
Source code in wagtail_localize/segments/types.py
11 12 13 14 15 | |
unwrap()
Pops a component from the beginning of the path. Reversing .wrap().
For example:
s = StringSegmentValue("wrapped.field", "foo") s.unwrap() 'wrapped', StringSegmentValue('field', 'foo')
Source code in wagtail_localize/segments/types.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | |
with_order(order)
Sets the order of this segment.
Source code in wagtail_localize/segments/types.py
17 18 19 20 21 22 23 | |
wrap(base_path)
Appends a component to the beginning of the path.
For example:
s = StringSegmentValue("field", "foo") s.wrap("wrapped") StringSegmentValue('wrapped.field', 'foo')
Source code in wagtail_localize/segments/types.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | |
OverridableSegmentValue
Bases: BaseValue
Represents a field that can be overridden.
Attributes:
| Name | Type | Description |
|---|---|---|
path |
str
|
The content path of the segment. |
data |
any
|
The value of the field in the source. Must be JSON-serializable. |
order |
int
|
The index that this segment appears on a page. |
Source code in wagtail_localize/segments/types.py
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 | |
__init__(path, data, **kwargs)
Initialises a new RelatedObjectSegmentValue.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path |
str
|
The content path of the segment. |
required |
data |
any
|
The value of the field in the source. Must be JSON-serializable. |
required |
order |
int
|
The index that this segment appears on a page. |
required |
Source code in wagtail_localize/segments/types.py
341 342 343 344 345 346 347 348 349 350 351 352 | |
clone()
Makes an exact copy of this OverridableSegmentValue.
Instead of mutating SegmentValue classes in place, it's recommended to clone them first.
Returns:
| Name | Type | Description |
|---|---|---|
OverridableSegmentValue |
The new segment value that's a copy of this one. |
Source code in wagtail_localize/segments/types.py
354 355 356 357 358 359 360 361 362 363 | |
is_empty()
Returns True if the data is empty.
Returns:
| Name | Type | Description |
|---|---|---|
boolean |
True if the data is empty. |
Source code in wagtail_localize/segments/types.py
365 366 367 368 369 370 371 372 | |
RelatedObjectSegmentValue
Bases: BaseValue
Represents a reference to a foreign translatable object.
Attributes:
| Name | Type | Description |
|---|---|---|
path |
str
|
The content path of the segment. |
content_type |
ContentType
|
The content type of the base model of the foreign object. |
translation_key |
UUID
|
The value of the foreign object's |
order |
int
|
The index that this segment appears on a page. |
Source code in wagtail_localize/segments/types.py
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 318 319 320 321 322 323 324 325 326 327 328 | |
__init__(path, content_type, translation_key, **kwargs)
Initialises a new RelatedObjectSegmentValue.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path |
str
|
The content path of the segment. |
required |
content_type |
ContentType
|
The content type of the base model of the foreign object. |
required |
translation_key |
UUID
|
The value of the foreign object's |
required |
order |
int
|
The index that this segment appears on a page. |
required |
Source code in wagtail_localize/segments/types.py
243 244 245 246 247 248 249 250 251 252 253 254 255 256 | |
clone()
Makes an exact copy of this RelatedObjectSegmentValue.
Instead of mutating SegmentValue classes in place, it's recommended to clone them first.
Returns:
| Name | Type | Description |
|---|---|---|
RelatedObjectSegmentValue |
The new segment value that's a copy of this one. |
Source code in wagtail_localize/segments/types.py
297 298 299 300 301 302 303 304 305 306 307 308 | |
from_instance(path, instance)
classmethod
Initialises a new RelatedObjectSegmentValue from a model instance.
Note: This class only records the type and translation key of the instance. So you will need to save the locale separately if you need to get this same instance back later.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path |
str
|
The content path of the segment. |
required |
instance |
Model
|
An instance of the translatable object that needs to be referenced. |
required |
Raises:
| Type | Description |
|---|---|
AttributeError
|
If the instance is not translatable. |
Returns:
| Name | Type | Description |
|---|---|---|
RelatedObjectSegmentValue |
The new segment value. |
Source code in wagtail_localize/segments/types.py
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | |
get_instance(locale)
Gets an instance of the referenced translatable object for the given locale.
Raises:
| Type | Description |
|---|---|
DoesNotExist
|
If there isn't an instance of the translatable object in the given locale. |
Returns:
| Name | Type | Description |
|---|---|---|
Model |
The instance. |
Source code in wagtail_localize/segments/types.py
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | |
is_empty()
Returns True if the related object is null.
Returns:
| Name | Type | Description |
|---|---|---|
boolean |
True if the related object is null. |
Source code in wagtail_localize/segments/types.py
310 311 312 313 314 315 316 317 | |
StringSegmentValue
Bases: BaseValue
Represents a translatable string segment.
Attributes:
| Name | Type | Description |
|---|---|---|
path |
str
|
The content path of the segment. |
string |
StringValue
|
the value of the segment. |
attrs |
dict
|
A dict of HTML attributes that were stripped out of the string. |
order |
int
|
The index that this segment appears on a page. |
Source code in wagtail_localize/segments/types.py
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 | |
__init__(path, string, attrs=None, **kwargs)
Initialises a new StringSegmentValue.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path |
str
|
The content path of the segment. |
required |
string |
StringValue
|
the value of the segment. |
required |
attrs |
dict
|
A dict of HTML attributes that were stripped out of the string. |
None
|
order |
int
|
The index that this segment appears on a page. |
required |
Source code in wagtail_localize/segments/types.py
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 | |
clone()
Makes an exact copy of this StringSegmentValue.
Instead of mutating SegmentValue classes in place, it's recommended to clone them first.
Returns:
| Name | Type | Description |
|---|---|---|
StringSegmentValue |
The new segment value that's a copy of this one. |
Source code in wagtail_localize/segments/types.py
99 100 101 102 103 104 105 106 107 108 109 110 | |
from_source_html(path, html, **kwargs)
classmethod
Initialises a StringSegmentValue from a HTML string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path |
str
|
The content path of the segment. |
required |
html |
str
|
The HTML value of the segment. |
required |
order |
int
|
The index that this segment appears on a page. |
required |
Source code in wagtail_localize/segments/types.py
112 113 114 115 116 117 118 119 120 121 122 123 | |
is_empty()
Returns True if the StringValue is blank.
Returns:
| Name | Type | Description |
|---|---|---|
boolean |
True if the StringValue is blank. |
Source code in wagtail_localize/segments/types.py
147 148 149 150 151 152 153 154 | |
render_html()
Returns a HTML representation of the segment value.
Note: If the segment value was created from plain text, this escapes special characters.
Returns:
| Name | Type | Description |
|---|---|---|
str |
The HTML representation of the segment value. |
Source code in wagtail_localize/segments/types.py
136 137 138 139 140 141 142 143 144 145 | |
render_text()
Returns a plain text representation of the segment value.
Note: If the segment value was created from HTML, this strips out HTML tags.
Returns:
| Name | Type | Description |
|---|---|---|
str |
The text representation of the segment value. |
Source code in wagtail_localize/segments/types.py
125 126 127 128 129 130 131 132 133 134 | |
TemplateSegmentValue
Bases: BaseValue
Represents an HTML template used to recombine the values of RichTextField/Blocks.
Attributes:
| Name | Type | Description |
|---|---|---|
path |
str
|
The content path of the segment. |
format |
str
|
The format of the template (eg, 'html'). |
template |
str
|
The template. |
string_count |
int
|
The number of translatablle string segments that were extracted from the template. |
order |
int
|
The index that this segment appears on a page. |
Source code in wagtail_localize/segments/types.py
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 | |
__init__(path, format, template, string_count, **kwargs)
Initialises a new TemplateSegmentValue.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path |
str
|
The content path of the segment. |
required |
format |
str
|
The format of the template (eg, 'html'). |
required |
template |
str
|
The template. |
required |
string_count |
int
|
The number of translatablle string segments that were extracted from the template. |
required |
order |
int
|
The index that this segment appears on a page. |
required |
Source code in wagtail_localize/segments/types.py
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | |
clone()
Makes an exact copy of this TemplateSegmentValue.
Instead of mutating SegmentValue classes in place, it's recommended to clone them first.
Returns:
| Name | Type | Description |
|---|---|---|
TemplateSegmentValue |
The new segment value that's a copy of this one. |
Source code in wagtail_localize/segments/types.py
197 198 199 200 201 202 203 204 205 206 207 208 | |
is_empty()
Returns True if the template is blank.
Returns:
| Name | Type | Description |
|---|---|---|
boolean |
True if the template is blank. |
Source code in wagtail_localize/segments/types.py
210 211 212 213 214 215 216 217 | |