CKEditorPlaceholderNode.get_widget()   B
last analyzed

Complexity

Conditions 5

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 8.5454
c 0
b 0
f 0
cc 5
1
from django import template
2
from django.forms import Textarea
3
from django.utils import translation
4
from django.conf import settings
5
from django.contrib.admin.widgets import AdminTextareaWidget
6
7
from pages.placeholders import PlaceholderNode
8
from pages.placeholders import parse_placeholder
9
10
try:
11
    from ckeditor.widgets import CKEditorWidget
12
except ImportError:
13
    CKEditorWidget = AdminTextareaWidget
14
15
register = template.Library()
16
17
class CKEditorPlaceholderNode(PlaceholderNode):
18
    def get_widget(self, page, language, fallback=Textarea):
19
        if 'ckeditor' not in settings.INSTALLED_APPS:
20
            return fallback
21
22
        with_stmt = self.widget # name of the widget as called in template like...
23
        # {% ckeditor_placeholder "welcome" with text_wysiwym_widget:default%}
24
        splitted = with_stmt.split(":")
25
26
        if len(splitted) == 1:
27
            ck = CKEditorWidget(config_name='default')
28
        elif len(splitted) > 1:
29
            ck = CKEditorWidget(config_name=splitted[1])
30
31
        if not ck.config.get('language'):
32
            ck.config['language'] = translation.get_language()
33
34
        return ck
35
36
def do_ckeditorplaceholder(parser, token):
37
    name, params = parse_placeholder(parser, token)
38
    return CKEditorPlaceholderNode(name, **params)
39
40
register.tag('ckeditor_placeholder', do_ckeditorplaceholder)
41