app/soc/views/helpers/custom_widgets.py
author Todd Larsen <tlarsen@google.com>
Wed, 24 Sep 2008 02:27:01 +0000
changeset 194 8bdb1d2d0c36
parent 188 27ed1a09c98d
child 211 ec26682053df
permissions -rw-r--r--
Change indentation level to 2 (from 4) and rename MCE_DEF_SETTINGS to DEF_MCE_SETTINGS to match the "default constant" naming convention elsewhere in the code.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
127
70206a64da4a Created custom_widgets.py and added TinyMCE django widget that can be later used for particular form fields.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
70206a64da4a Created custom_widgets.py and added TinyMCE django widget that can be later used for particular form fields.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     2
#
70206a64da4a Created custom_widgets.py and added TinyMCE django widget that can be later used for particular form fields.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     3
# Copyright 2008 the Melange authors.
70206a64da4a Created custom_widgets.py and added TinyMCE django widget that can be later used for particular form fields.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     4
#
70206a64da4a Created custom_widgets.py and added TinyMCE django widget that can be later used for particular form fields.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
70206a64da4a Created custom_widgets.py and added TinyMCE django widget that can be later used for particular form fields.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
70206a64da4a Created custom_widgets.py and added TinyMCE django widget that can be later used for particular form fields.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
70206a64da4a Created custom_widgets.py and added TinyMCE django widget that can be later used for particular form fields.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     8
#
70206a64da4a Created custom_widgets.py and added TinyMCE django widget that can be later used for particular form fields.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
70206a64da4a Created custom_widgets.py and added TinyMCE django widget that can be later used for particular form fields.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    10
#
70206a64da4a Created custom_widgets.py and added TinyMCE django widget that can be later used for particular form fields.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
70206a64da4a Created custom_widgets.py and added TinyMCE django widget that can be later used for particular form fields.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
70206a64da4a Created custom_widgets.py and added TinyMCE django widget that can be later used for particular form fields.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
70206a64da4a Created custom_widgets.py and added TinyMCE django widget that can be later used for particular form fields.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
70206a64da4a Created custom_widgets.py and added TinyMCE django widget that can be later used for particular form fields.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
70206a64da4a Created custom_widgets.py and added TinyMCE django widget that can be later used for particular form fields.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    16
70206a64da4a Created custom_widgets.py and added TinyMCE django widget that can be later used for particular form fields.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    17
"""Custom widgets used for form fields.
70206a64da4a Created custom_widgets.py and added TinyMCE django widget that can be later used for particular form fields.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    18
"""
70206a64da4a Created custom_widgets.py and added TinyMCE django widget that can be later used for particular form fields.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    19
70206a64da4a Created custom_widgets.py and added TinyMCE django widget that can be later used for particular form fields.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    20
__authors__ = [
70206a64da4a Created custom_widgets.py and added TinyMCE django widget that can be later used for particular form fields.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    21
  '"Pawel Solyga" <pawel.solyga@gmail.com>',
70206a64da4a Created custom_widgets.py and added TinyMCE django widget that can be later used for particular form fields.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    22
  ]
70206a64da4a Created custom_widgets.py and added TinyMCE django widget that can be later used for particular form fields.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    23
141
e120c24b89e2 Added Melange front page edit view where you can change title, content, feed url. Created SiteSettings and Document models and some logic for them. Added isFeedURLValid function in soc/logic/feed.py. Created some functions for handling datastore updates of different kinds of Models (soc/logic/model.py). Fixed some typos and too long lines of code.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 127
diff changeset
    24
from django import newforms as forms
127
70206a64da4a Created custom_widgets.py and added TinyMCE django widget that can be later used for particular form fields.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    25
from django.newforms.widgets import flatatt
70206a64da4a Created custom_widgets.py and added TinyMCE django widget that can be later used for particular form fields.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    26
from django.newforms.util import smart_unicode
70206a64da4a Created custom_widgets.py and added TinyMCE django widget that can be later used for particular form fields.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    27
from django.utils.html import escape
70206a64da4a Created custom_widgets.py and added TinyMCE django widget that can be later used for particular form fields.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    28
from django.utils import simplejson
70206a64da4a Created custom_widgets.py and added TinyMCE django widget that can be later used for particular form fields.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    29
from django.utils.safestring import mark_safe
70206a64da4a Created custom_widgets.py and added TinyMCE django widget that can be later used for particular form fields.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    30
141
e120c24b89e2 Added Melange front page edit view where you can change title, content, feed url. Created SiteSettings and Document models and some logic for them. Added isFeedURLValid function in soc/logic/feed.py. Created some functions for handling datastore updates of different kinds of Models (soc/logic/model.py). Fixed some typos and too long lines of code.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 127
diff changeset
    31
class TinyMCE(forms.widgets.Textarea):
188
27ed1a09c98d Changes in TinyMCE widget in custom_widgets.py based on review comments.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 141
diff changeset
    32
    """TinyMCE widget. 
27ed1a09c98d Changes in TinyMCE widget in custom_widgets.py based on review comments.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 141
diff changeset
    33
    
27ed1a09c98d Changes in TinyMCE widget in custom_widgets.py based on review comments.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 141
diff changeset
    34
    Requires to include tiny_mce_src.js in your template. Widget can be
27ed1a09c98d Changes in TinyMCE widget in custom_widgets.py based on review comments.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 141
diff changeset
    35
    customized by overwriting or adding extra options to mce_settings
27ed1a09c98d Changes in TinyMCE widget in custom_widgets.py based on review comments.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 141
diff changeset
    36
    dictionary
127
70206a64da4a Created custom_widgets.py and added TinyMCE django widget that can be later used for particular form fields.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    37
188
27ed1a09c98d Changes in TinyMCE widget in custom_widgets.py based on review comments.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 141
diff changeset
    38
    You can set TinyMCE widget for particular form field using code below:
27ed1a09c98d Changes in TinyMCE widget in custom_widgets.py based on review comments.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 141
diff changeset
    39
      class ExampleForm(forms_helpers.DbModelForm):
27ed1a09c98d Changes in TinyMCE widget in custom_widgets.py based on review comments.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 141
diff changeset
    40
        content = forms.fields.CharField(widget=custom_widgets.TinyMCE())
27ed1a09c98d Changes in TinyMCE widget in custom_widgets.py based on review comments.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 141
diff changeset
    41
    
27ed1a09c98d Changes in TinyMCE widget in custom_widgets.py based on review comments.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 141
diff changeset
    42
    You can include tiny_mce_src.js in your template using:
27ed1a09c98d Changes in TinyMCE widget in custom_widgets.py based on review comments.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 141
diff changeset
    43
      {% block scripts %}
27ed1a09c98d Changes in TinyMCE widget in custom_widgets.py based on review comments.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 141
diff changeset
    44
    	  <script type="text/javascript" src="/tiny_mce/tiny_mce_src.js"></script>
27ed1a09c98d Changes in TinyMCE widget in custom_widgets.py based on review comments.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 141
diff changeset
    45
      {% endblock %}
27ed1a09c98d Changes in TinyMCE widget in custom_widgets.py based on review comments.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 141
diff changeset
    46
    """ 
194
8bdb1d2d0c36 Change indentation level to 2 (from 4) and rename MCE_DEF_SETTINGS to
Todd Larsen <tlarsen@google.com>
parents: 188
diff changeset
    47
    DEF_MCE_SETTINGS = { 'mode': "exact",
188
27ed1a09c98d Changes in TinyMCE widget in custom_widgets.py based on review comments.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 141
diff changeset
    48
                         'theme': "simple",
27ed1a09c98d Changes in TinyMCE widget in custom_widgets.py based on review comments.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 141
diff changeset
    49
                         'theme_advanced_toolbar_location': "top",
27ed1a09c98d Changes in TinyMCE widget in custom_widgets.py based on review comments.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 141
diff changeset
    50
                         'theme_advanced_toolbar_align': "center"}
127
70206a64da4a Created custom_widgets.py and added TinyMCE django widget that can be later used for particular form fields.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    51
194
8bdb1d2d0c36 Change indentation level to 2 (from 4) and rename MCE_DEF_SETTINGS to
Todd Larsen <tlarsen@google.com>
parents: 188
diff changeset
    52
    mce_settings = DEF_MCE_SETTINGS
127
70206a64da4a Created custom_widgets.py and added TinyMCE django widget that can be later used for particular form fields.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    53
194
8bdb1d2d0c36 Change indentation level to 2 (from 4) and rename MCE_DEF_SETTINGS to
Todd Larsen <tlarsen@google.com>
parents: 188
diff changeset
    54
    TINY_MCE_HTML_FMT = u'''\
8bdb1d2d0c36 Change indentation level to 2 (from 4) and rename MCE_DEF_SETTINGS to
Todd Larsen <tlarsen@google.com>
parents: 188
diff changeset
    55
<textarea %(attrs)s>%(value)s</textarea>
8bdb1d2d0c36 Change indentation level to 2 (from 4) and rename MCE_DEF_SETTINGS to
Todd Larsen <tlarsen@google.com>
parents: 188
diff changeset
    56
<script type="text/javascript">
8bdb1d2d0c36 Change indentation level to 2 (from 4) and rename MCE_DEF_SETTINGS to
Todd Larsen <tlarsen@google.com>
parents: 188
diff changeset
    57
 tinyMCE.init(%(settings_json)s)
8bdb1d2d0c36 Change indentation level to 2 (from 4) and rename MCE_DEF_SETTINGS to
Todd Larsen <tlarsen@google.com>
parents: 188
diff changeset
    58
</script>'''
188
27ed1a09c98d Changes in TinyMCE widget in custom_widgets.py based on review comments.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 141
diff changeset
    59
    
127
70206a64da4a Created custom_widgets.py and added TinyMCE django widget that can be later used for particular form fields.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    60
    def render(self, name, value, attrs=None):
194
8bdb1d2d0c36 Change indentation level to 2 (from 4) and rename MCE_DEF_SETTINGS to
Todd Larsen <tlarsen@google.com>
parents: 188
diff changeset
    61
      """Render TinyMCE widget as HTML.
8bdb1d2d0c36 Change indentation level to 2 (from 4) and rename MCE_DEF_SETTINGS to
Todd Larsen <tlarsen@google.com>
parents: 188
diff changeset
    62
      """
8bdb1d2d0c36 Change indentation level to 2 (from 4) and rename MCE_DEF_SETTINGS to
Todd Larsen <tlarsen@google.com>
parents: 188
diff changeset
    63
      if value is None:
8bdb1d2d0c36 Change indentation level to 2 (from 4) and rename MCE_DEF_SETTINGS to
Todd Larsen <tlarsen@google.com>
parents: 188
diff changeset
    64
        value = ''
8bdb1d2d0c36 Change indentation level to 2 (from 4) and rename MCE_DEF_SETTINGS to
Todd Larsen <tlarsen@google.com>
parents: 188
diff changeset
    65
      value = smart_unicode(value)
8bdb1d2d0c36 Change indentation level to 2 (from 4) and rename MCE_DEF_SETTINGS to
Todd Larsen <tlarsen@google.com>
parents: 188
diff changeset
    66
      final_attrs = self.build_attrs(attrs, name=name)
127
70206a64da4a Created custom_widgets.py and added TinyMCE django widget that can be later used for particular form fields.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    67
194
8bdb1d2d0c36 Change indentation level to 2 (from 4) and rename MCE_DEF_SETTINGS to
Todd Larsen <tlarsen@google.com>
parents: 188
diff changeset
    68
      self.mce_settings['elements'] = "id_%s" % name
188
27ed1a09c98d Changes in TinyMCE widget in custom_widgets.py based on review comments.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 141
diff changeset
    69
        
194
8bdb1d2d0c36 Change indentation level to 2 (from 4) and rename MCE_DEF_SETTINGS to
Todd Larsen <tlarsen@google.com>
parents: 188
diff changeset
    70
      # convert mce_settings from dict to JSON
8bdb1d2d0c36 Change indentation level to 2 (from 4) and rename MCE_DEF_SETTINGS to
Todd Larsen <tlarsen@google.com>
parents: 188
diff changeset
    71
      mce_json = simplejson.JSONEncoder().encode(self.mce_settings)
127
70206a64da4a Created custom_widgets.py and added TinyMCE django widget that can be later used for particular form fields.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    72
194
8bdb1d2d0c36 Change indentation level to 2 (from 4) and rename MCE_DEF_SETTINGS to
Todd Larsen <tlarsen@google.com>
parents: 188
diff changeset
    73
      return mark_safe(self.TINY_MCE_HTML_FMT % 
8bdb1d2d0c36 Change indentation level to 2 (from 4) and rename MCE_DEF_SETTINGS to
Todd Larsen <tlarsen@google.com>
parents: 188
diff changeset
    74
          {'attrs': flatatt(final_attrs),
8bdb1d2d0c36 Change indentation level to 2 (from 4) and rename MCE_DEF_SETTINGS to
Todd Larsen <tlarsen@google.com>
parents: 188
diff changeset
    75
           'value': escape(value), 
8bdb1d2d0c36 Change indentation level to 2 (from 4) and rename MCE_DEF_SETTINGS to
Todd Larsen <tlarsen@google.com>
parents: 188
diff changeset
    76
           'settings_json':  mce_json})