~vcs-imports/reviewboard/trunk

« back to all changes in this revision

Viewing changes to reviewboard/webapi/mixins.py

  • Committer: Christian Hammond
  • Date: 2019-01-24 01:59:00 UTC
  • mfrom: (4677.1.178)
  • Revision ID: git-v1:40cabd7ed191e9805fb095aca6808008834bf7d0
Merge branch 'release-4.0.x'

Show diffs side-by-side

added added

removed removed

Lines of Context:
249
249
                        text_type_field_name=None,
250
250
                        text_model_field=None,
251
251
                        **kwargs):
252
 
        """Normalizes Markdown-capable text fields that are being saved."""
 
252
        """Normalize Markdown-capable text fields that are being saved.
 
253
 
 
254
        Args:
 
255
            obj (django.db.models.Model):
 
256
                The object containing Markdown-capable fields to modify.
 
257
 
 
258
            text_field (unicode):
 
259
                The key from ``kwargs`` containing the new value for the
 
260
                text fields.
 
261
 
 
262
            rich_text_field_name (unicode, optional):
 
263
                The name of the boolean field on ``obj`` representing the
 
264
                rich text state.
 
265
 
 
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).
 
269
 
 
270
            text_type_field_name (unicode, optional):
 
271
                The key from ``kwargs`` containing the text type.
 
272
 
 
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).
 
276
 
 
277
            text_model_field (unicode, optional):
 
278
                The name of the text field on ``obj``.
 
279
 
 
280
                If not provided, ``text_field`` will be used for the value.
 
281
 
 
282
            **kwargs (dict):
 
283
                Any fields passed by the client. These should be values
 
284
                corresponding to ``text_type_field_name`` and ``text_field``
 
285
                in here.
 
286
 
 
287
                This may also contain a legacy value of ``'text_type'``,
 
288
                which will be used if ``text_type_field_name`` is not present.
 
289
 
 
290
        Returns:
 
291
            set:
 
292
            The fields on ``obj`` that were set based on the request.
 
293
        """
 
294
        modified_fields = set()
 
295
 
253
296
        if not text_model_field:
254
297
            text_model_field = text_field
255
298
 
260
303
            rich_text_field_name = self._get_rich_text_field_name(text_field)
261
304
 
262
305
        old_rich_text = getattr(obj, rich_text_field_name, None)
263
 
 
264
 
        if text_field in kwargs:
265
 
            setattr(obj, text_model_field, kwargs[text_field].strip())
266
 
 
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)
 
307
 
 
308
        if text is not None:
 
309
            setattr(obj, text_model_field, text.strip())
 
310
            modified_fields.add(text_model_field)
 
311
 
 
312
        legacy_text_type = kwargs.get('text_type')
 
313
        text_type = kwargs.get(text_type_field_name, legacy_text_type)
 
314
 
 
315
        if text_type is not None:
270
316
            rich_text = (text_type == self.TEXT_TYPE_MARKDOWN)
271
317
 
272
318
            setattr(obj, rich_text_field_name, rich_text)
 
319
            modified_fields.add(rich_text_field_name)
273
320
 
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
277
324
            # unescaping.
278
 
            if rich_text != old_rich_text and text_field not in kwargs:
279
 
                markdown_set_field_escaped(obj, text_model_field,
280
 
                                           rich_text)
 
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
284
331
            # field.
285
 
            if text_field in kwargs:
 
332
            if text is not None:
286
333
                markdown_set_field_escaped(obj, text_model_field,
287
334
                                           old_rich_text)
 
335
                modified_fields.add(text_model_field)
 
336
 
 
337
        return modified_fields
288
338
 
289
339
    def set_extra_data_text_fields(self, obj, text_field, extra_fields,
290
340
                                   **kwargs):
291
 
        """Normalizes Markdown-capable text fields in extra_data.
 
341
        """Normalize Markdown-capable text fields in extra_data.
292
342
 
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.
296
346
 
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.).
 
350
 
 
351
        Args:
 
352
            obj (django.db.models.Model):
 
353
                The object containing Markdown-capable fields to modify.
 
354
 
 
355
            text_field (unicode):
 
356
                The key from ``kwargs`` containing the new value for the
 
357
                text fields.
 
358
 
 
359
            extra_fields (dict):
 
360
                Fields passed to the API resource that aren't natively handled
 
361
                by that resource. These may contain ``extra_data.``-prefixed
 
362
                keys.
 
363
 
 
364
            **kwargs (dict):
 
365
                Any fields passed by the client. This may be checked for a
 
366
                legacy ``text_type`` field.
 
367
 
 
368
        Returns:
 
369
            set:
 
370
            The keys in ``extra_data`` that were set based on the request.
300
371
        """
 
372
        modified_fields = set()
 
373
 
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
308
381
            # stripped.
309
382
            extra_data[text_field] = \
310
383
                extra_fields[extra_data_text_field].strip()
 
384
            modified_fields.add(text_field)
311
385
        elif extra_data_text_type_field not in extra_fields:
312
386
            # Nothing about this field has changed, so bail.
313
 
            return
 
387
            return modified_fields
314
388
 
315
389
        old_text_type = extra_data.get(text_type_field)
316
390
        text_type = extra_fields.get(extra_data_text_type_field,
329
403
                markdown_set_field_escaped(
330
404
                    extra_data, text_field,
331
405
                    text_type == self.TEXT_TYPE_MARKDOWN)
 
406
                modified_fields.add(text_field)
332
407
        elif old_text_type:
333
408
            # The user didn't specify a text type, but the object may be set
334
409
            # for Markdown, in which case we'll need to pre-escape the text
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)
340
416
 
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)
 
422
 
 
423
        return modified_fields
345
424
 
346
425
    def _get_text_type_field_name(self, text_field_name):
347
426
        if text_field_name == 'text':