Source

django-redactormedia / redactormedia / widgets.py

Full commit
# -*- coding: utf-8 -*-
"""
RedactorMedia widgets
"""
from django.core.urlresolvers import reverse
from django.forms import Media
from django.utils.translation.trans_real import get_language

__author__ = "Frank Becker <fb@alien8.de>"
__version__ = "0.1"
__date__ = "Date: 12/12/12"
__copyright__ = "Copyright (c) 2012 Frank Becker"
__license__ = "Python"

from redactor.widgets import RedactorEditor, AdminRedactorEditor


class RedactorWithMediaEditor(RedactorEditor):
    """
    subclass to add upload config
    """

    def __init__(self, attrs=None, redactor_css=None, redactor_settings=None):
        super(RedactorWithMediaEditor, self).__init__(attrs=attrs)
        language = get_language().split('-')[0]
        default_settings = {
            'lang': language,
            'load': True,
            'path': False,
            'autoresize': True,
            'imageUpload': reverse('upload_files'),
            'imageGetJson': reverse('recent_files'),
            'focus': False,
            #'plugins': ["fullscreen"]
        }
        self.redactor_settings = redactor_settings or default_settings

    @property
    def media(self):
        js = (
            'django-redactor/lib/jquery-1.7.min.js',
            'django-redactor/redactor/redactor.min.js',
            'django-redactor/redactor/setup.js',
            'redactor-media/redactor-media.js',
        )
        if self.redactor_settings['lang'] != 'en':
            js += ('django-redactor/redactor/langs/%s.js' %
                   self.redactor_settings['lang'],)
        css = {
            'screen': [
                'django-redactor/redactor/css/redactor.css',
            ]
        }
        return Media(css=css, js=js)


class AdminRedactorWithMediaEditor(AdminRedactorEditor):
    """
    subclass to add upload config
    """
    def __init__(self, attrs=None, redactor_css=None, redactor_settings=None):
        super(AdminRedactorWithMediaEditor, self).__init__(attrs=attrs)
        language = get_language().split('-')[0]
        default_settings = {
            'lang': language,
            'load': True,
            'path': False,
            'autoresize': True,
            'imageUpload': reverse('upload_files'),
            'imageGetJson': reverse('recent_files'),
            'focus': False,
            #'plugins': ["fullscreen"]
        }
        self.redactor_settings = redactor_settings or default_settings

    @property
    def media(self):
        js = (
            'django-redactor/lib/jquery-1.7.min.js',
            'django-redactor/redactor/redactor.min.js',
            #'redactor-media/fullscreen.js'
            'django-redactor/redactor/setup.js',
            'redactor-media/redactor-media.js',
        )
        css = {
            'screen': [
                'django-redactor/redactor/css/redactor.css',
                'django-redactor/redactor/css/django_admin.css',
                'redactor-media/css/django_admin.css',
            ]
        }
        return Media(css=css, js=js)