prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Static page editing save HTML outcome instead of raw text
  To reproduce:
  
  1. Server using Markdown for static pages and an existing static page written using Markdown
  2. Minor edit the content of the static page (editing the title also seems to do this)
  3. Saves fine
  4. Edit the page again.  Static page text is now all in HTML. 
  5. View static page on site.  Static page is now all garbled HTML
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `pootle/core/markup/fields.py`
  Content:
  ```
  1 # -*- coding: utf-8 -*-
  2 #
  3 # Copyright (C) Pootle contributors.
  4 #
  5 # This file is a part of the Pootle project. It is distributed under the GPL3
  6 # or later license. See the LICENSE file for a copy of the license and the
  7 # AUTHORS file for copyright and authorship information.
  8 
  9 import logging
  10 
  11 from lxml.etree import ParserError
  12 from lxml.html.clean import clean_html
  13 
  14 from django.conf import settings
  15 from django.core.cache import cache
  16 from django.db import models
  17 from django.utils.safestring import mark_safe
  18 
  19 from .filters import apply_markup_filter
  20 from .widgets import MarkupTextarea
  21 
  22 
  23 __all__ = ('Markup', 'MarkupField',)
  24 
  25 
  26 logger = logging.getLogger('pootle.markup')
  27 
  28 
  29 _rendered_cache_key = lambda obj, pk, field: '_%s_%s_%s_rendered' % \
  30                                              (obj, pk, field)
  31 
  32 
  33 class Markup(object):
  34 
  35     def __init__(self, instance, field_name, rendered_cache_key):
  36         self.instance = instance
  37         self.field_name = field_name
  38         self.cache_key = rendered_cache_key
  39 
  40     @property
  41     def raw(self):
  42         return self.instance.__dict__[self.field_name]
  43 
  44     @raw.setter
  45     def raw(self, value):
  46         setattr(self.instance, self.field_name, value)
  47 
  48     @property
  49     def rendered(self):
  50         rendered = cache.get(self.cache_key)
  51 
  52         if not rendered:
  53             logger.debug(u'Caching rendered output of %r', self.cache_key)
  54             rendered = apply_markup_filter(self.raw)
  55             cache.set(self.cache_key, rendered,
  56                       settings.POOTLE_CACHE_TIMEOUT)
  57 
  58         return rendered
  59 
  60     def __unicode__(self):
  61         try:
  62             return mark_safe(clean_html(self.rendered))
  63         except ParserError:
  64             return u''
  65 
  66     def __nonzero__(self):
  67         return self.raw.strip() != '' and self.raw is not None
  68 
  69 
  70 class MarkupDescriptor(object):
  71 
  72     def __init__(self, field):
  73         self.field = field
  74 
  75     def __get__(self, obj, owner):
  76         if obj is None:
  77             raise AttributeError('Can only be accessed via an instance.')
  78 
  79         markup = obj.__dict__[self.field.name]
  80         if markup is None:
  81             return None
  82 
  83         cache_key = _rendered_cache_key(obj.__class__.__name__,
  84                                         obj.pk,
  85                                         self.field.name)
  86         return Markup(obj, self.field.name, cache_key)
  87 
  88     def __set__(self, obj, value):
  89         if isinstance(value, Markup):
  90             obj.__dict__[self.field.name] = value.raw
  91         else:
  92             obj.__dict__[self.field.name] = value
  93 
  94 
  95 class MarkupField(models.TextField):
  96 
  97     description = 'Text field supporting different markup formats.'
  98 
  99     def contribute_to_class(self, cls, name):
  100         super(MarkupField, self).contribute_to_class(cls, name)
  101         setattr(cls, self.name, MarkupDescriptor(self))
  102 
  103     def pre_save(self, model_instance, add):
  104         value = super(MarkupField, self).pre_save(model_instance, add)
  105 
  106         if not add:
  107             # Invalidate cache to force rendering upon next retrieval
  108             cache_key = _rendered_cache_key(model_instance.__class__.__name__,
  109                                             model_instance.pk,
  110                                             self.name)
  111             logger.debug('Invalidating cache for %r', cache_key)
  112             cache.delete(cache_key)
  113 
  114         return value.raw
  115 
  116     def get_prep_value(self, value):
  117         if isinstance(value, Markup):
  118             return value.raw
  119 
  120         return value
  121 
  122     def value_to_string(self, obj):
  123         value = self._get_val_from_obj(obj)
  124         return self.get_prep_value(value)
  125 
  126     def formfield(self, **kwargs):
  127         defaults = {'widget': MarkupTextarea}
  128         defaults.update(kwargs)
  129         return super(MarkupField, self).formfield(**defaults)
  130 
  131     def deconstruct(self):
  132         name, path, args, kwargs = super(MarkupField, self).deconstruct()
  133         kwargs.pop('help_text', None)
  134         return name, path, args, kwargs
  135
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/pootle/core/markup/fields.py b/pootle/core/markup/fields.py
  --- a/pootle/core/markup/fields.py
  +++ b/pootle/core/markup/fields.py
  @@ -119,8 +119,11 @@
   
           return value
   
  +    def to_python(self, value):
  +        return self.get_prep_value(value)
  +
       def value_to_string(self, obj):
  -        value = self._get_val_from_obj(obj)
  +        value = self.value_from_object(obj)
           return self.get_prep_value(value)
   
       def formfield(self, **kwargs):
  
verification_info: {"golden_diff": "diff --git a/pootle/core/markup/fields.py b/pootle/core/markup/fields.py\n--- a/pootle/core/markup/fields.py\n+++ b/pootle/core/markup/fields.py\n@@ -119,8 +119,11 @@\n \n         return value\n \n+    def to_python(self, value):\n+        return self.get_prep_value(value)\n+\n     def value_to_string(self, obj):\n-        value = self._get_val_from_obj(obj)\n+        value = self.value_from_object(obj)\n         return self.get_prep_value(value)\n \n     def formfield(self, **kwargs):\n", "issue": "Static page editing save HTML outcome instead of raw text\nTo reproduce:\r\n\r\n1. Server using Markdown for static pages and an existing static page written using Markdown\r\n2. Minor edit the content of the static page (editing the title also seems to do this)\r\n3. Saves fine\r\n4. Edit the page again.  Static page text is now all in HTML. \r\n5. View static page on site.  Static page is now all garbled HTML\n", "before_files": [{"content": "# -*- coding: utf-8 -*-\n#\n# Copyright (C) Pootle contributors.\n#\n# This file is a part of the Pootle project. It is distributed under the GPL3\n# or later license. See the LICENSE file for a copy of the license and the\n# AUTHORS file for copyright and authorship information.\n\nimport logging\n\nfrom lxml.etree import ParserError\nfrom lxml.html.clean import clean_html\n\nfrom django.conf import settings\nfrom django.core.cache import cache\nfrom django.db import models\nfrom django.utils.safestring import mark_safe\n\nfrom .filters import apply_markup_filter\nfrom .widgets import MarkupTextarea\n\n\n__all__ = ('Markup', 'MarkupField',)\n\n\nlogger = logging.getLogger('pootle.markup')\n\n\n_rendered_cache_key = lambda obj, pk, field: '_%s_%s_%s_rendered' % \\\n                                             (obj, pk, field)\n\n\nclass Markup(object):\n\n    def __init__(self, instance, field_name, rendered_cache_key):\n        self.instance = instance\n        self.field_name = field_name\n        self.cache_key = rendered_cache_key\n\n    @property\n    def raw(self):\n        return self.instance.__dict__[self.field_name]\n\n    @raw.setter\n    def raw(self, value):\n        setattr(self.instance, self.field_name, value)\n\n    @property\n    def rendered(self):\n        rendered = cache.get(self.cache_key)\n\n        if not rendered:\n            logger.debug(u'Caching rendered output of %r', self.cache_key)\n            rendered = apply_markup_filter(self.raw)\n            cache.set(self.cache_key, rendered,\n                      settings.POOTLE_CACHE_TIMEOUT)\n\n        return rendered\n\n    def __unicode__(self):\n        try:\n            return mark_safe(clean_html(self.rendered))\n        except ParserError:\n            return u''\n\n    def __nonzero__(self):\n        return self.raw.strip() != '' and self.raw is not None\n\n\nclass MarkupDescriptor(object):\n\n    def __init__(self, field):\n        self.field = field\n\n    def __get__(self, obj, owner):\n        if obj is None:\n            raise AttributeError('Can only be accessed via an instance.')\n\n        markup = obj.__dict__[self.field.name]\n        if markup is None:\n            return None\n\n        cache_key = _rendered_cache_key(obj.__class__.__name__,\n                                        obj.pk,\n                                        self.field.name)\n        return Markup(obj, self.field.name, cache_key)\n\n    def __set__(self, obj, value):\n        if isinstance(value, Markup):\n            obj.__dict__[self.field.name] = value.raw\n        else:\n            obj.__dict__[self.field.name] = value\n\n\nclass MarkupField(models.TextField):\n\n    description = 'Text field supporting different markup formats.'\n\n    def contribute_to_class(self, cls, name):\n        super(MarkupField, self).contribute_to_class(cls, name)\n        setattr(cls, self.name, MarkupDescriptor(self))\n\n    def pre_save(self, model_instance, add):\n        value = super(MarkupField, self).pre_save(model_instance, add)\n\n        if not add:\n            # Invalidate cache to force rendering upon next retrieval\n            cache_key = _rendered_cache_key(model_instance.__class__.__name__,\n                                            model_instance.pk,\n                                            self.name)\n            logger.debug('Invalidating cache for %r', cache_key)\n            cache.delete(cache_key)\n\n        return value.raw\n\n    def get_prep_value(self, value):\n        if isinstance(value, Markup):\n            return value.raw\n\n        return value\n\n    def value_to_string(self, obj):\n        value = self._get_val_from_obj(obj)\n        return self.get_prep_value(value)\n\n    def formfield(self, **kwargs):\n        defaults = {'widget': MarkupTextarea}\n        defaults.update(kwargs)\n        return super(MarkupField, self).formfield(**defaults)\n\n    def deconstruct(self):\n        name, path, args, kwargs = super(MarkupField, self).deconstruct()\n        kwargs.pop('help_text', None)\n        return name, path, args, kwargs\n", "path": "pootle/core/markup/fields.py"}], "after_files": [{"content": "# -*- coding: utf-8 -*-\n#\n# Copyright (C) Pootle contributors.\n#\n# This file is a part of the Pootle project. It is distributed under the GPL3\n# or later license. See the LICENSE file for a copy of the license and the\n# AUTHORS file for copyright and authorship information.\n\nimport logging\n\nfrom lxml.etree import ParserError\nfrom lxml.html.clean import clean_html\n\nfrom django.conf import settings\nfrom django.core.cache import cache\nfrom django.db import models\nfrom django.utils.safestring import mark_safe\n\nfrom .filters import apply_markup_filter\nfrom .widgets import MarkupTextarea\n\n\n__all__ = ('Markup', 'MarkupField',)\n\n\nlogger = logging.getLogger('pootle.markup')\n\n\n_rendered_cache_key = lambda obj, pk, field: '_%s_%s_%s_rendered' % \\\n                                             (obj, pk, field)\n\n\nclass Markup(object):\n\n    def __init__(self, instance, field_name, rendered_cache_key):\n        self.instance = instance\n        self.field_name = field_name\n        self.cache_key = rendered_cache_key\n\n    @property\n    def raw(self):\n        return self.instance.__dict__[self.field_name]\n\n    @raw.setter\n    def raw(self, value):\n        setattr(self.instance, self.field_name, value)\n\n    @property\n    def rendered(self):\n        rendered = cache.get(self.cache_key)\n\n        if not rendered:\n            logger.debug(u'Caching rendered output of %r', self.cache_key)\n            rendered = apply_markup_filter(self.raw)\n            cache.set(self.cache_key, rendered,\n                      settings.POOTLE_CACHE_TIMEOUT)\n\n        return rendered\n\n    def __unicode__(self):\n        try:\n            return mark_safe(clean_html(self.rendered))\n        except ParserError:\n            return u''\n\n    def __nonzero__(self):\n        return self.raw.strip() != '' and self.raw is not None\n\n\nclass MarkupDescriptor(object):\n\n    def __init__(self, field):\n        self.field = field\n\n    def __get__(self, obj, owner):\n        if obj is None:\n            raise AttributeError('Can only be accessed via an instance.')\n\n        markup = obj.__dict__[self.field.name]\n        if markup is None:\n            return None\n\n        cache_key = _rendered_cache_key(obj.__class__.__name__,\n                                        obj.pk,\n                                        self.field.name)\n        return Markup(obj, self.field.name, cache_key)\n\n    def __set__(self, obj, value):\n        if isinstance(value, Markup):\n            obj.__dict__[self.field.name] = value.raw\n        else:\n            obj.__dict__[self.field.name] = value\n\n\nclass MarkupField(models.TextField):\n\n    description = 'Text field supporting different markup formats.'\n\n    def contribute_to_class(self, cls, name):\n        super(MarkupField, self).contribute_to_class(cls, name)\n        setattr(cls, self.name, MarkupDescriptor(self))\n\n    def pre_save(self, model_instance, add):\n        value = super(MarkupField, self).pre_save(model_instance, add)\n\n        if not add:\n            # Invalidate cache to force rendering upon next retrieval\n            cache_key = _rendered_cache_key(model_instance.__class__.__name__,\n                                            model_instance.pk,\n                                            self.name)\n            logger.debug('Invalidating cache for %r', cache_key)\n            cache.delete(cache_key)\n\n        return value.raw\n\n    def get_prep_value(self, value):\n        if isinstance(value, Markup):\n            return value.raw\n\n        return value\n\n    def to_python(self, value):\n        return self.get_prep_value(value)\n\n    def value_to_string(self, obj):\n        value = self.value_from_object(obj)\n        return self.get_prep_value(value)\n\n    def formfield(self, **kwargs):\n        defaults = {'widget': MarkupTextarea}\n        defaults.update(kwargs)\n        return super(MarkupField, self).formfield(**defaults)\n\n    def deconstruct(self):\n        name, path, args, kwargs = super(MarkupField, self).deconstruct()\n        kwargs.pop('help_text', None)\n        return name, path, args, kwargs\n", "path": "pootle/core/markup/fields.py"}]}