249
249
text_type_field_name=None,
250
250
text_model_field=None,
252
"""Normalizes Markdown-capable text fields that are being saved."""
252
"""Normalize Markdown-capable text fields that are being saved.
255
obj (django.db.models.Model):
256
The object containing Markdown-capable fields to modify.
258
text_field (unicode):
259
The key from ``kwargs`` containing the new value for the
262
rich_text_field_name (unicode, optional):
263
The name of the boolean field on ``obj`` representing the
266
If not provided, a name will be generated based on the value
267
of ``text_field`` (``'rich_text'`` if ``text_field`` is
268
``'text'``, or :samp:`{text_field}_rich_text` otherwise).
270
text_type_field_name (unicode, optional):
271
The key from ``kwargs`` containing the text type.
273
If not provided, a name will be generated based on the value
274
of ``text_field`` (``'text_type'`` if ``text_field`` is
275
``'text'``, or :samp:`{text_field}_text_type` otherwise).
277
text_model_field (unicode, optional):
278
The name of the text field on ``obj``.
280
If not provided, ``text_field`` will be used for the value.
283
Any fields passed by the client. These should be values
284
corresponding to ``text_type_field_name`` and ``text_field``
287
This may also contain a legacy value of ``'text_type'``,
288
which will be used if ``text_type_field_name`` is not present.
292
The fields on ``obj`` that were set based on the request.
294
modified_fields = set()
253
296
if not text_model_field:
254
297
text_model_field = text_field
260
303
rich_text_field_name = self._get_rich_text_field_name(text_field)
262
305
old_rich_text = getattr(obj, rich_text_field_name, None)
264
if text_field in kwargs:
265
setattr(obj, text_model_field, kwargs[text_field].strip())
267
if text_type_field_name in kwargs or 'text_type' in kwargs:
268
text_type = kwargs.get(text_type_field_name,
269
kwargs.get('text_type'))
306
text = kwargs.get(text_field)
309
setattr(obj, text_model_field, text.strip())
310
modified_fields.add(text_model_field)
312
legacy_text_type = kwargs.get('text_type')
313
text_type = kwargs.get(text_type_field_name, legacy_text_type)
315
if text_type is not None:
270
316
rich_text = (text_type == self.TEXT_TYPE_MARKDOWN)
272
318
setattr(obj, rich_text_field_name, rich_text)
319
modified_fields.add(rich_text_field_name)
274
321
# If the caller has changed the text type for this field, but
275
322
# hasn't provided a new field value, then we will need to update
276
323
# the affected field's existing contents by escaping or
278
if rich_text != old_rich_text and text_field not in kwargs:
279
markdown_set_field_escaped(obj, text_model_field,
325
if rich_text != old_rich_text and text is None:
326
markdown_set_field_escaped(obj, text_model_field, rich_text)
327
modified_fields.add(text_model_field)
281
328
elif old_rich_text:
282
329
# The user didn't specify rich-text, but the object may be set
283
330
# for rich-text, in which case we'll need to pre-escape the text
285
if text_field in kwargs:
286
333
markdown_set_field_escaped(obj, text_model_field,
335
modified_fields.add(text_model_field)
337
return modified_fields
289
339
def set_extra_data_text_fields(self, obj, text_field, extra_fields,
291
"""Normalizes Markdown-capable text fields in extra_data.
341
"""Normalize Markdown-capable text fields in extra_data.
293
343
This will check if any Markdown-capable text fields in extra_data
294
344
have been changed (either by changing the text or the text type),
295
345
and handle the saving of the text and type.
297
This works just like set_text_fields, but specially handles
347
This works just like :py:meth:`set_text_fields`, but specially handles
298
348
how things are stored in extra_data (text_type vs. rich_text fields,
299
349
possible lack of presence of a text_type field, etc.).
352
obj (django.db.models.Model):
353
The object containing Markdown-capable fields to modify.
355
text_field (unicode):
356
The key from ``kwargs`` containing the new value for the
360
Fields passed to the API resource that aren't natively handled
361
by that resource. These may contain ``extra_data.``-prefixed
365
Any fields passed by the client. This may be checked for a
366
legacy ``text_type`` field.
370
The keys in ``extra_data`` that were set based on the request.
372
modified_fields = set()
301
374
text_type_field = self._get_text_type_field_name(text_field)
302
375
extra_data = obj.extra_data
303
376
extra_data_text_field = 'extra_data.' + text_field
337
412
markdown_set_field_escaped(
338
413
extra_data, text_field,
339
414
old_text_type == self.TEXT_TYPE_MARKDOWN)
415
modified_fields.add(text_field)
341
417
# Ensure we have a text type set for this field. If one wasn't
342
418
# provided or set above, we'll set it to the default now.
343
419
extra_data[text_type_field] = \
344
420
text_type or self.DEFAULT_EXTRA_DATA_TEXT_TYPE
421
modified_fields.add(text_type_field)
423
return modified_fields
346
425
def _get_text_type_field_name(self, text_field_name):
347
426
if text_field_name == 'text':