Changes between Version 5 and Version 6 of RichTextField


Ignore:
Timestamp:
08/22/2006 01:42:39 PM (9 years ago)
Author:
[530]
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • RichTextField

    v5 v6  
    22
    33A attempt at intergrating tinymce completely as a own field
    4 
    54
    65== Adding the field type ==
     
    109{{{
    1110
    12 from django.db.models import fields
    13 from django.utils.html import escape
    14 from django import forms
    15 import settings 
    16    
    17 class LargeRichTextField(forms.TextField):
    18     def __init__(self, field_name, rows=10, cols=40, is_required=False, validator_list=[], maxlength=None):
    19         self.field_name = field_name
    20         self.rows, self.cols, self.is_required = rows, cols, is_required
    21         self.validator_list = validator_list[:]
    22         if maxlength:
    23             self.validator_list.append(self.isValidLength)
    24             self.maxlength = maxlength
     11rom django.utils.html import escape
     12from django.forms import LargeTextField
     13from django.db.models.fields import Field
     14import settings
    2515
    26         # should be able to add the admin js here somehow
    27 
     16class TextFieldWithClass(LargeTextField):
     17    def __init__(self, css_class='', *args, **kwargs):
     18        self.css_class = css_class
     19        LargeTextField.__init__(self, *args, **kwargs)
    2820
    2921    def render(self, data):
     
    3123            data = ''
    3224        if isinstance(data, unicode):
    33             data = data.encode(settings.DEFAULT_CHARSET) 
     25            data = data.encode(settings.DEFAULT_CHARSET)
    3426
    35         # adds a class to activate editor via editor_selector
     27        css_class = ''
     28        if self.css_class != '':
     29            css_class = ' ' + self.css_class
    3630
    37         return '<textarea id="%s" class="v%s%s mceEditor" name="%s" rows="%s" cols="%s">%s</textarea>' % \
     31        return '<textarea id="%s" class="v%s%s%s" name="%s" rows="%s" cols="%s">%s</textarea>' % \
    3832            (self.get_id(), self.__class__.__name__, self.is_required and ' required' or '',
    39             self.field_name, self.rows, self.cols, escape(data))
     33            css_class, self.field_name, self.rows, self.cols, escape(data))
    4034
    41 class RichTextField(fields.Field):
     35class ClassTextField(Field):
     36
     37    def __init__(self, css_class='', *args, **kwargs):
     38        self.css_class = css_class
     39        Field.__init__(self, *args, **kwargs)
     40
     41    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
     44        return (field_objs, params)
     45
    4246    def get_manipulator_field_objs(self):
    43         return [LargeRichTextField]
     47        return [TextFieldWithClass]
     48
     49    def get_internal_type(self):
     50        return 'TextField'
     51
    4452
    4553
     
    8391
    8492from django.db import models
    85 import local_models       
     93from local_models import ClassTextField
    8694
    8795class Page(models.Model):
     
    8997    label = models.CharField(maxlength=255)
    9098
    91     text = local_models.RichTextField()
     99    text = local_models.ClassTextField(css_class='mceEditor')
    92100   
    93101    def __repr__(self):
     
    96104    class Admin:
    97105        js=['../js/tinymce/jscripts/tiny_mce/tiny_mce.js', '../js/tmce.js']
    98         # this should be added automatically by the field somehow, but only once, not per field instance
    99106
    100107}}}
Back to Top