import unicodedata

from django.forms import fields


class XMLCompatCharField(fields.CharField):
    """
    Strip 'control characters', as XML 1.0 does not allow them and the API may
    return data in XML.
    """

    def to_python(self, value):
        value = super().to_python(value=value)
        return self.remove_control_characters(value)

    @staticmethod
    def remove_control_characters(str):
        return "".join(ch for ch in str if unicodedata.category(ch)[0] != "C")

import unicodedata

from django.forms import fields


class XMLCompatCharField(fields.CharField):
    """
    Strip 'control characters', as XML 1.0 does not allow them and the API may
    return data in XML.
    """

    def to_python(self, value):
        value = super().to_python(value=value)
        return self.remove_control_characters(value)

    @staticmethod
    def remove_control_characters(input):
        valid_chars = ['\n', '\r']
        return "".join(ch for ch in input if
                       unicodedata.category(ch)[0] != "C" or ch in valid_chars)

Allow linebreaks textareas (should be valid in XML)
Allow linebreaks textareas (should be valid in XML)

  import unicodedata
  
  from django.forms import fields
  
  
  class XMLCompatCharField(fields.CharField):
      """
      Strip 'control characters', as XML 1.0 does not allow them and the API may
      return data in XML.
      """
  
      def to_python(self, value):
          value = super().to_python(value=value)
          return self.remove_control_characters(value)
  
      @staticmethod
-     def remove_control_characters(str):
+     def remove_control_characters(input):
-         return "".join(ch for ch in str if unicodedata.category(ch)[0] != "C")
+         valid_chars = ['\n', '\r']
+         return "".join(ch for ch in input if
+                        unicodedata.category(ch)[0] != "C" or ch in valid_chars)
  
Allow linebreaks textareas (should be valid in XML)
## Code Before:
import unicodedata

from django.forms import fields


class XMLCompatCharField(fields.CharField):
    """
    Strip 'control characters', as XML 1.0 does not allow them and the API may
    return data in XML.
    """

    def to_python(self, value):
        value = super().to_python(value=value)
        return self.remove_control_characters(value)

    @staticmethod
    def remove_control_characters(str):
        return "".join(ch for ch in str if unicodedata.category(ch)[0] != "C")

## Instruction:
Allow linebreaks textareas (should be valid in XML)
## Code After:
import unicodedata

from django.forms import fields


class XMLCompatCharField(fields.CharField):
    """
    Strip 'control characters', as XML 1.0 does not allow them and the API may
    return data in XML.
    """

    def to_python(self, value):
        value = super().to_python(value=value)
        return self.remove_control_characters(value)

    @staticmethod
    def remove_control_characters(input):
        valid_chars = ['\n', '\r']
        return "".join(ch for ch in input if
                       unicodedata.category(ch)[0] != "C" or ch in valid_chars)
