Changes between Version 6 and Version 7 of RichTextField


Ignore:
Timestamp:
10/06/06 15:58:27 (8 years ago)
Author:
anonymous
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • RichTextField

    v6 v7  
    99{{{
    1010
    11 rom django.utils.html import escape
    12 from django.forms import LargeTextField
     11from django.utils.html import escape
     12from django.forms import LargeTextField 
    1313from django.db.models.fields import Field
    14 import settings
     14from django.dispatch import dispatcher
     15from django.db.models import signals
     16from django.conf import settings
    1517
    1618class TextFieldWithClass(LargeTextField):
    17     def __init__(self, css_class='', *args, **kwargs):
    18         self.css_class = css_class
     19    def __init__(self, css_class='', plaintext_field=None, *args, **kwargs):
     20        self.css_class, self.plaintext_field = css_class, plaintext_field
    1921        LargeTextField.__init__(self, *args, **kwargs)
    2022
     
    3335            css_class, self.field_name, self.rows, self.cols, escape(data))
    3436
    35 class ClassTextField(Field):
     37class RichTextFieldWithClass(Field):
    3638
    37     def __init__(self, css_class='', *args, **kwargs):
    38         self.css_class = css_class
     39    def __init__(self, css_class='', plaintext_field=None, *args, **kwargs):
     40        self.css_class, self.plaintext_field = css_class, plaintext_field
    3941        Field.__init__(self, *args, **kwargs)
    4042
     43    def set_plaintext_field(self, instance=None):
     44        from genshi.core import Markup
     45        plain = Markup(getattr(instance, self.attname)).striptags().stripentities().encode('utf-8')
     46        setattr(instance, self.plaintext_field, plain)
     47
     48    def contribute_to_class(self, cls, name):
     49        super(RichTextFieldWithClass, self).contribute_to_class(cls, name)
     50        if self.plaintext_field:
     51            dispatcher.connect(self.set_plaintext_field, signals.pre_save, sender=cls)
     52
    4153    def prepare_field_objs_and_params(self, manipulator, name_prefix):
    42         field_objs, params = super(ClassTextField, self).prepare_field_objs_and_params(manipulator, name_prefix)
    43         params['css_class'] = self.css_class
     54        field_objs, params = super(RichTextFieldWithClass, self).prepare_field_objs_and_params(manipulator, name_prefix)
     55        params['css_class'], params['plaintext_field'] = self.css_class, self.plaintext_field
    4456        return (field_objs, params)
    4557
     
    4961    def get_internal_type(self):
    5062        return 'TextField'
    51 
    52 
    5363
    5464}}}
     
    91101
    92102from django.db import models
    93 from local_models import ClassTextField
     103from local_models import RichTextFieldWithClass
    94104
    95105class Page(models.Model):
     
    97107    label = models.CharField(maxlength=255)
    98108
    99     text = local_models.ClassTextField(css_class='mceEditor')
    100    
     109    text = RichTextFieldWithClass(css_class='mceEditor', plaintext_field='plaintext')
     110    plaintext = models.TextField() # optional            optional argument, requires genshi from edgewall
     111
    101112    def __repr__(self):
    102113        return self.label
Back to Top