Ejemplo n.º 1
0
    def normalize_markdown_fields(self,
                                  obj,
                                  text_fields,
                                  old_rich_text,
                                  model_field_map={},
                                  **kwargs):
        if 'rich_text' in kwargs:
            rich_text = kwargs['rich_text']

            # If the caller has changed the rich_text setting, we will need to
            # update any affected fields we already have stored that weren't
            # changed in this request by escaping or unescaping their
            # contents.
            if rich_text != old_rich_text:
                for text_field in text_fields:
                    if text_field not in kwargs:
                        model_field = \
                            model_field_map.get(text_field, text_field)
                        markdown_set_field_escaped(obj, model_field, rich_text)
        elif old_rich_text:
            # The user didn't specify rich-text, but the object may be set for
            # for rich-text, in which case we'll need to pre-escape any text
            # fields that came in.
            for text_field in text_fields:
                if text_field in kwargs:
                    model_field = model_field_map.get(text_field, text_field)
                    markdown_set_field_escaped(obj, model_field, old_rich_text)
Ejemplo n.º 2
0
    def set_extra_data_text_fields(self, obj, text_field, extra_fields,
                                   **kwargs):
        """Normalizes Markdown-capable text fields in extra_data.

        This will check if any Markdown-capable text fields in extra_data
        have been changed (either by changing the text or the text type),
        and handle the saving of the text and type.

        This works just like set_text_fields, but specially handles
        how things are stored in extra_data (text_type vs. rich_text fields,
        possible lack of presence of a text_type field, etc.).
        """
        text_type_field = self._get_text_type_field_name(text_field)
        extra_data = obj.extra_data
        extra_data_text_field = 'extra_data.' + text_field
        extra_data_text_type_field = 'extra_data.' + text_type_field

        if extra_data_text_field in extra_fields:
            # This field was updated in this request. Make sure it's
            # stripped.
            extra_data[text_field] = \
                extra_fields[extra_data_text_field].strip()
        elif extra_data_text_type_field not in extra_fields:
            # Nothing about this field has changed, so bail.
            return

        old_text_type = extra_data.get(text_type_field)
        text_type = extra_fields.get(extra_data_text_type_field,
                                     kwargs.get('text_type'))

        if text_type is not None:
            if old_text_type is None:
                old_text_type = self.DEFAULT_EXTRA_DATA_TEXT_TYPE

            # If the caller has changed the text type for this field, but
            # hasn't provided a new field value, then we will need to update
            # the affected field's existing contents by escaping or
            # unescaping.
            if (text_type != old_text_type and
                extra_data_text_field not in extra_fields):
                markdown_set_field_escaped(
                    extra_data, text_field,
                    text_type == self.TEXT_TYPE_MARKDOWN)
        elif old_text_type:
            # The user didn't specify a text type, but the object may be set
            # for Markdown, in which case we'll need to pre-escape the text
            # field.
            if extra_data_text_field in extra_fields:
                markdown_set_field_escaped(
                    extra_data, text_field,
                    old_text_type == self.TEXT_TYPE_MARKDOWN)

        # Ensure we have a text type set for this field. If one wasn't
        # provided or set above, we'll set it to the default now.
        extra_data[text_type_field] = \
            text_type or self.DEFAULT_EXTRA_DATA_TEXT_TYPE
Ejemplo n.º 3
0
    def set_extra_data_text_fields(self, obj, text_field, extra_fields,
                                   **kwargs):
        """Normalizes Markdown-capable text fields in extra_data.

        This will check if any Markdown-capable text fields in extra_data
        have been changed (either by changing the text or the text type),
        and handle the saving of the text and type.

        This works just like set_text_fields, but specially handles
        how things are stored in extra_data (text_type vs. rich_text fields,
        possible lack of presence of a text_type field, etc.).
        """
        text_type_field = self._get_text_type_field_name(text_field)
        extra_data = obj.extra_data
        extra_data_text_field = 'extra_data.' + text_field
        extra_data_text_type_field = 'extra_data.' + text_type_field

        if extra_data_text_field in extra_fields:
            # This field was updated in this request. Make sure it's
            # stripped.
            extra_data[text_field] = \
                extra_fields[extra_data_text_field].strip()
        elif extra_data_text_type_field not in extra_fields:
            # Nothing about this field has changed, so bail.
            return

        old_text_type = extra_data.get(text_type_field)
        text_type = extra_fields.get(extra_data_text_type_field,
                                     kwargs.get('text_type'))

        if text_type is not None:
            if old_text_type is None:
                old_text_type = self.DEFAULT_EXTRA_DATA_TEXT_TYPE

            # If the caller has changed the text type for this field, but
            # hasn't provided a new field value, then we will need to update
            # the affected field's existing contents by escaping or
            # unescaping.
            if (text_type != old_text_type and
                extra_data_text_field not in extra_fields):
                markdown_set_field_escaped(
                    extra_data, text_field,
                    text_type == self.TEXT_TYPE_MARKDOWN)
        elif old_text_type:
            # The user didn't specify a text type, but the object may be set
            # for Markdown, in which case we'll need to pre-escape the text
            # field.
            if extra_data_text_field in extra_fields:
                markdown_set_field_escaped(
                    extra_data, text_field,
                    old_text_type == self.TEXT_TYPE_MARKDOWN)

        # Ensure we have a text type set for this field. If one wasn't
        # provided or set above, we'll set it to the default now.
        extra_data[text_type_field] = \
            text_type or self.DEFAULT_EXTRA_DATA_TEXT_TYPE
Ejemplo n.º 4
0
    def set_text_fields(self,
                        obj,
                        text_field,
                        rich_text_field_name=None,
                        text_type_field_name=None,
                        text_model_field=None,
                        **kwargs):
        """Normalizes Markdown-capable text fields that are being saved."""
        if not text_model_field:
            text_model_field = text_field

        if not text_type_field_name:
            text_type_field_name = self._get_text_type_field_name(text_field)

        if not rich_text_field_name:
            rich_text_field_name = self._get_rich_text_field_name(text_field)

        old_rich_text = getattr(obj, rich_text_field_name, None)

        if text_field in kwargs:
            setattr(obj, text_model_field, kwargs[text_field].strip())

        if text_type_field_name in kwargs or 'text_type' in kwargs:
            text_type = kwargs.get(text_type_field_name,
                                   kwargs.get('text_type'))
            rich_text = (text_type == self.TEXT_TYPE_MARKDOWN)

            setattr(obj, rich_text_field_name, rich_text)

            # If the caller has changed the text type for this field, but
            # hasn't provided a new field value, then we will need to update
            # the affected field's existing contents by escaping or
            # unescaping.
            if rich_text != old_rich_text and text_field not in kwargs:
                markdown_set_field_escaped(obj, text_model_field, rich_text)
        elif old_rich_text:
            # The user didn't specify rich-text, but the object may be set
            # for rich-text, in which case we'll need to pre-escape the text
            # field.
            if text_field in kwargs:
                markdown_set_field_escaped(obj, text_model_field,
                                           old_rich_text)
Ejemplo n.º 5
0
    def set_text_fields(self, obj, text_field,
                        rich_text_field_name=None,
                        text_type_field_name=None,
                        text_model_field=None,
                        **kwargs):
        """Normalizes Markdown-capable text fields that are being saved."""
        if not text_model_field:
            text_model_field = text_field

        if not text_type_field_name:
            text_type_field_name = self._get_text_type_field_name(text_field)

        if not rich_text_field_name:
            rich_text_field_name = self._get_rich_text_field_name(text_field)

        old_rich_text = getattr(obj, rich_text_field_name, None)

        if text_field in kwargs:
            setattr(obj, text_model_field, kwargs[text_field].strip())

        if text_type_field_name in kwargs or 'text_type' in kwargs:
            text_type = kwargs.get(text_type_field_name,
                                   kwargs.get('text_type'))
            rich_text = (text_type == self.TEXT_TYPE_MARKDOWN)

            setattr(obj, rich_text_field_name, rich_text)

            # If the caller has changed the text type for this field, but
            # hasn't provided a new field value, then we will need to update
            # the affected field's existing contents by escaping or
            # unescaping.
            if rich_text != old_rich_text and text_field not in kwargs:
                markdown_set_field_escaped(obj, text_model_field,
                                           rich_text)
        elif old_rich_text:
            # The user didn't specify rich-text, but the object may be set
            # for rich-text, in which case we'll need to pre-escape the text
            # field.
            if text_field in kwargs:
                markdown_set_field_escaped(obj, text_model_field,
                                           old_rich_text)
Ejemplo n.º 6
0
    def normalize_markdown_fields(self, obj, text_fields, old_rich_text,
                                  model_field_map={}, **kwargs):
        if 'rich_text' in kwargs:
            rich_text = kwargs['rich_text']

            # If the caller has changed the rich_text setting, we will need to
            # update any affected fields we already have stored that weren't
            # changed in this request by escaping or unescaping their
            # contents.
            if rich_text != old_rich_text:
                for text_field in text_fields:
                    if text_field not in kwargs:
                        model_field = \
                            model_field_map.get(text_field, text_field)
                        markdown_set_field_escaped(obj, model_field, rich_text)
        elif old_rich_text:
            # The user didn't specify rich-text, but the object may be set for
            # for rich-text, in which case we'll need to pre-escape any text
            # fields that came in.
            for text_field in text_fields:
                if text_field in kwargs:
                    model_field = model_field_map.get(text_field, text_field)
                    markdown_set_field_escaped(obj, model_field, old_rich_text)
Ejemplo n.º 7
0
    def set_extra_data_text_fields(self, obj, text_field, extra_fields,
                                   **kwargs):
        """Normalize Markdown-capable text fields in extra_data.

        This will check if any Markdown-capable text fields in extra_data
        have been changed (either by changing the text or the text type),
        and handle the saving of the text and type.

        This works just like :py:meth:`set_text_fields`, but specially handles
        how things are stored in extra_data (text_type vs. rich_text fields,
        possible lack of presence of a text_type field, etc.).

        Args:
            obj (django.db.models.Model):
                The object containing Markdown-capable fields to modify.

            text_field (unicode):
                The key from ``kwargs`` containing the new value for the
                text fields.

            extra_fields (dict):
                Fields passed to the API resource that aren't natively handled
                by that resource. These may contain ``extra_data.``-prefixed
                keys.

            **kwargs (dict):
                Any fields passed by the client. This may be checked for a
                legacy ``text_type`` field.

        Returns:
            set:
            The keys in ``extra_data`` that were set based on the request.
        """
        modified_fields = set()

        text_type_field = self._get_text_type_field_name(text_field)
        extra_data = obj.extra_data
        extra_data_text_field = 'extra_data.' + text_field
        extra_data_text_type_field = 'extra_data.' + text_type_field

        if extra_data_text_field in extra_fields:
            # This field was updated in this request. Make sure it's
            # stripped.
            extra_data[text_field] = \
                extra_fields[extra_data_text_field].strip()
            modified_fields.add(text_field)
        elif extra_data_text_type_field not in extra_fields:
            # Nothing about this field has changed, so bail.
            return modified_fields

        old_text_type = extra_data.get(text_type_field)
        text_type = extra_fields.get(extra_data_text_type_field,
                                     kwargs.get('text_type'))

        if text_type is not None:
            if old_text_type is None:
                old_text_type = self.DEFAULT_EXTRA_DATA_TEXT_TYPE

            # If the caller has changed the text type for this field, but
            # hasn't provided a new field value, then we will need to update
            # the affected field's existing contents by escaping or
            # unescaping.
            if (text_type != old_text_type
                    and extra_data_text_field not in extra_fields):
                markdown_set_field_escaped(
                    extra_data, text_field,
                    text_type == self.TEXT_TYPE_MARKDOWN)
                modified_fields.add(text_field)
        elif old_text_type:
            # The user didn't specify a text type, but the object may be set
            # for Markdown, in which case we'll need to pre-escape the text
            # field.
            if extra_data_text_field in extra_fields:
                markdown_set_field_escaped(
                    extra_data, text_field,
                    old_text_type == self.TEXT_TYPE_MARKDOWN)
                modified_fields.add(text_field)

        # Ensure we have a text type set for this field. If one wasn't
        # provided or set above, we'll set it to the default now.
        extra_data[text_type_field] = \
            text_type or self.DEFAULT_EXTRA_DATA_TEXT_TYPE
        modified_fields.add(text_type_field)

        return modified_fields
Ejemplo n.º 8
0
    def set_text_fields(self,
                        obj,
                        text_field,
                        rich_text_field_name=None,
                        text_type_field_name=None,
                        text_model_field=None,
                        **kwargs):
        """Normalize Markdown-capable text fields that are being saved.

        Args:
            obj (django.db.models.Model):
                The object containing Markdown-capable fields to modify.

            text_field (unicode):
                The key from ``kwargs`` containing the new value for the
                text fields.

            rich_text_field_name (unicode, optional):
                The name of the boolean field on ``obj`` representing the
                rich text state.

                If not provided, a name will be generated based on the value
                of ``text_field`` (``'rich_text'`` if ``text_field`` is
                ``'text'``, or :samp:`{text_field}_rich_text` otherwise).

            text_type_field_name (unicode, optional):
                The key from ``kwargs`` containing the text type.

                If not provided, a name will be generated based on the value
                of ``text_field`` (``'text_type'`` if ``text_field`` is
                ``'text'``, or :samp:`{text_field}_text_type` otherwise).

            text_model_field (unicode, optional):
                The name of the text field on ``obj``.

                If not provided, ``text_field`` will be used for the value.

            **kwargs (dict):
                Any fields passed by the client. These should be values
                corresponding to ``text_type_field_name`` and ``text_field``
                in here.

                This may also contain a legacy value of ``'text_type'``,
                which will be used if ``text_type_field_name`` is not present.

        Returns:
            set:
            The fields on ``obj`` that were set based on the request.
        """
        modified_fields = set()

        if not text_model_field:
            text_model_field = text_field

        if not text_type_field_name:
            text_type_field_name = self._get_text_type_field_name(text_field)

        if not rich_text_field_name:
            rich_text_field_name = self._get_rich_text_field_name(text_field)

        old_rich_text = getattr(obj, rich_text_field_name, None)
        text = kwargs.get(text_field)

        if text is not None:
            setattr(obj, text_model_field, text.strip())
            modified_fields.add(text_model_field)

        legacy_text_type = kwargs.get('text_type')
        text_type = kwargs.get(text_type_field_name, legacy_text_type)

        if text_type is not None:
            rich_text = (text_type == self.TEXT_TYPE_MARKDOWN)

            setattr(obj, rich_text_field_name, rich_text)
            modified_fields.add(rich_text_field_name)

            # If the caller has changed the text type for this field, but
            # hasn't provided a new field value, then we will need to update
            # the affected field's existing contents by escaping or
            # unescaping.
            if rich_text != old_rich_text and text is None:
                markdown_set_field_escaped(obj, text_model_field, rich_text)
                modified_fields.add(text_model_field)
        elif old_rich_text:
            # The user didn't specify rich-text, but the object may be set
            # for rich-text, in which case we'll need to pre-escape the text
            # field.
            if text is not None:
                markdown_set_field_escaped(obj, text_model_field,
                                           old_rich_text)
                modified_fields.add(text_model_field)

        return modified_fields
Ejemplo n.º 9
0
    def set_extra_data_text_fields(self, obj, text_field, extra_fields,
                                   **kwargs):
        """Normalize Markdown-capable text fields in extra_data.

        This will check if any Markdown-capable text fields in extra_data
        have been changed (either by changing the text or the text type),
        and handle the saving of the text and type.

        This works just like :py:meth:`set_text_fields`, but specially handles
        how things are stored in extra_data (text_type vs. rich_text fields,
        possible lack of presence of a text_type field, etc.).

        Args:
            obj (django.db.models.Model):
                The object containing Markdown-capable fields to modify.

            text_field (unicode):
                The key from ``kwargs`` containing the new value for the
                text fields.

            extra_fields (dict):
                Fields passed to the API resource that aren't natively handled
                by that resource. These may contain ``extra_data.``-prefixed
                keys.

            **kwargs (dict):
                Any fields passed by the client. This may be checked for a
                legacy ``text_type`` field.

        Returns:
            set:
            The keys in ``extra_data`` that were set based on the request.
        """
        modified_fields = set()

        text_type_field = self._get_text_type_field_name(text_field)
        extra_data = obj.extra_data
        extra_data_text_field = 'extra_data.' + text_field
        extra_data_text_type_field = 'extra_data.' + text_type_field

        if extra_data_text_field in extra_fields:
            # This field was updated in this request. Make sure it's
            # stripped.
            extra_data[text_field] = \
                extra_fields[extra_data_text_field].strip()
            modified_fields.add(text_field)
        elif extra_data_text_type_field not in extra_fields:
            # Nothing about this field has changed, so bail.
            return modified_fields

        old_text_type = extra_data.get(text_type_field)
        text_type = extra_fields.get(extra_data_text_type_field,
                                     kwargs.get('text_type'))

        if text_type is not None:
            if old_text_type is None:
                old_text_type = self.DEFAULT_EXTRA_DATA_TEXT_TYPE

            # If the caller has changed the text type for this field, but
            # hasn't provided a new field value, then we will need to update
            # the affected field's existing contents by escaping or
            # unescaping.
            if (text_type != old_text_type and
                extra_data_text_field not in extra_fields):
                markdown_set_field_escaped(
                    extra_data, text_field,
                    text_type == self.TEXT_TYPE_MARKDOWN)
                modified_fields.add(text_field)
        elif old_text_type:
            # The user didn't specify a text type, but the object may be set
            # for Markdown, in which case we'll need to pre-escape the text
            # field.
            if extra_data_text_field in extra_fields:
                markdown_set_field_escaped(
                    extra_data, text_field,
                    old_text_type == self.TEXT_TYPE_MARKDOWN)
                modified_fields.add(text_field)

        # Ensure we have a text type set for this field. If one wasn't
        # provided or set above, we'll set it to the default now.
        extra_data[text_type_field] = \
            text_type or self.DEFAULT_EXTRA_DATA_TEXT_TYPE
        modified_fields.add(text_type_field)

        return modified_fields
Ejemplo n.º 10
0
    def set_text_fields(self, obj, text_field,
                        rich_text_field_name=None,
                        text_type_field_name=None,
                        text_model_field=None,
                        **kwargs):
        """Normalize Markdown-capable text fields that are being saved.

        Args:
            obj (django.db.models.Model):
                The object containing Markdown-capable fields to modify.

            text_field (unicode):
                The key from ``kwargs`` containing the new value for the
                text fields.

            rich_text_field_name (unicode, optional):
                The name of the boolean field on ``obj`` representing the
                rich text state.

                If not provided, a name will be generated based on the value
                of ``text_field`` (``'rich_text'`` if ``text_field`` is
                ``'text'``, or :samp:`{text_field}_rich_text` otherwise).

            text_type_field_name (unicode, optional):
                The key from ``kwargs`` containing the text type.

                If not provided, a name will be generated based on the value
                of ``text_field`` (``'text_type'`` if ``text_field`` is
                ``'text'``, or :samp:`{text_field}_text_type` otherwise).

            text_model_field (unicode, optional):
                The name of the text field on ``obj``.

                If not provided, ``text_field`` will be used for the value.

            **kwargs (dict):
                Any fields passed by the client. These should be values
                corresponding to ``text_type_field_name`` and ``text_field``
                in here.

                This may also contain a legacy value of ``'text_type'``,
                which will be used if ``text_type_field_name`` is not present.

        Returns:
            set:
            The fields on ``obj`` that were set based on the request.
        """
        modified_fields = set()

        if not text_model_field:
            text_model_field = text_field

        if not text_type_field_name:
            text_type_field_name = self._get_text_type_field_name(text_field)

        if not rich_text_field_name:
            rich_text_field_name = self._get_rich_text_field_name(text_field)

        old_rich_text = getattr(obj, rich_text_field_name, None)
        text = kwargs.get(text_field)

        if text is not None:
            setattr(obj, text_model_field, text.strip())
            modified_fields.add(text_model_field)

        legacy_text_type = kwargs.get('text_type')
        text_type = kwargs.get(text_type_field_name, legacy_text_type)

        if text_type is not None:
            rich_text = (text_type == self.TEXT_TYPE_MARKDOWN)

            setattr(obj, rich_text_field_name, rich_text)
            modified_fields.add(rich_text_field_name)

            # If the caller has changed the text type for this field, but
            # hasn't provided a new field value, then we will need to update
            # the affected field's existing contents by escaping or
            # unescaping.
            if rich_text != old_rich_text and text is None:
                markdown_set_field_escaped(obj, text_model_field, rich_text)
                modified_fields.add(text_model_field)
        elif old_rich_text:
            # The user didn't specify rich-text, but the object may be set
            # for rich-text, in which case we'll need to pre-escape the text
            # field.
            if text is not None:
                markdown_set_field_escaped(obj, text_model_field,
                                           old_rich_text)
                modified_fields.add(text_model_field)

        return modified_fields