Coverage for pages.widgets: 74%

# Coverage for pages.widgets : 74%

## 178 statements 131 run 0 excluded 47 missing

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 | # -*- coding: utf-8 -*- """Django CMS come with a set of ready to use widgets that you can enable in the admin via a placeholder tag in your template.""" from pages.settings import PAGES_MEDIA_URL, PAGE_TAGGING from pages.settings import PAGE_TINYMCE, PAGE_LANGUAGES from pages.models import Page from pages.widgets_registry import register_widget from django.conf import settings from django.forms import TextInput, Textarea, HiddenInput from django.forms import MultiWidget, FileInput from django.contrib.admin.widgets import AdminTextInputWidget from django.contrib.admin.widgets import AdminTextareaWidget from django.utils.safestring import mark_safe from django.template.loader import render_to_string from django.core.exceptions import ObjectDoesNotExist from django.utils.translation import ugettext as _ from os.path import join register_widget(TextInput) register_widget(Textarea) register_widget(AdminTextInputWidget) register_widget(AdminTextareaWidget) if "filebrowser" in getattr(settings, 'INSTALLED_APPS', []): from filebrowser.fields import FileBrowseWidget class FileBrowseInput(FileBrowseWidget): """FileBrowseInput widget.""" def __init__(self, attrs={}): super(FileBrowseInput, self).__init__(attrs) register_widget(FileBrowseInput) if PAGE_TAGGING: from tagging.models import Tag from django.utils import simplejson class AutoCompleteTagInput(TextInput): """An autocompete widget""" class Media: js = [join(PAGES_MEDIA_URL, path) for path in ( 'javascript/jquery.js', 'javascript/jquery.bgiframe.min.js', 'javascript/jquery.ajaxQueue.js', 'javascript/jquery.autocomplete.min.js' )] def __init__(self, language=None, attrs=None, **kwargs): self.language = language super(AutoCompleteTagInput, self).__init__(attrs) def render(self, name, value, language=None, attrs=None, **kwargs): rendered = super(AutoCompleteTagInput, self).render( name, value, attrs) page_tags = Tag.objects.usage_for_model(Page) context = { 'name': name, 'tags': simplejson.dumps([tag.name for tag in page_tags], ensure_ascii=False), } return rendered + mark_safe(render_to_string( 'pages/widgets/autocompletetaginput.html', context)) register_widget(AutoCompleteTagInput) class RichTextarea(Textarea): """A RichTextarea widget.""" class Media: js = [join(PAGES_MEDIA_URL, path) for path in ( 'javascript/jquery.js', )] css = { 'all': [join(PAGES_MEDIA_URL, path) for path in ( 'css/rte.css', )] } def __init__(self, language=None, attrs=None, **kwargs): attrs = {'class': 'rte'} self.language = language super(RichTextarea, self).__init__(attrs) def render(self, name, value, attrs=None, **kwargs): rendered = super(RichTextarea, self).render(name, value, attrs) context = { 'name': name, 'PAGES_MEDIA_URL': PAGES_MEDIA_URL, } return rendered + mark_safe(render_to_string( 'pages/widgets/richtextarea.html', context)) register_widget(RichTextarea) if PAGE_TINYMCE: from tinymce import widgets as tinymce_widgets class TinyMCE(tinymce_widgets.TinyMCE): """TinyMCE widget.""" def __init__(self, language=None, attrs=None, mce_attrs=None, **kwargs): self.language = language if mce_attrs is None: mce_attrs = {} self.mce_attrs = mce_attrs self.mce_attrs.update({ 'mode': "exact", 'theme': "advanced", 'width': 640, 'height': 400, 'theme_advanced_toolbar_location': "top", 'theme_advanced_toolbar_align': "left" }) # take into account the default settings, don't allow # the above hard coded ones overriding them self.mce_attrs.update( getattr(settings, 'TINYMCE_DEFAULT_CONFIG', {})) super(TinyMCE, self).__init__(language, attrs, mce_attrs) register_widget(TinyMCE) class CKEditor(Textarea): """CKEditor widget.""" class Media: js = [join(PAGES_MEDIA_URL, 'ckeditor/ckeditor.js'), join(settings.MEDIA_URL, 'filebrowser/js/FB_CKEditor.js'), ] def __init__(self, language=None, attrs=None, **kwargs): self.language = language self.filebrowser = "filebrowser" in getattr(settings, 'INSTALLED_APPS', []) self.attrs = {'class': 'ckeditor'} if attrs: self.attrs.update(attrs) super(CKEditor, self).__init__(attrs) def render(self, name, value, attrs=None, **kwargs): rendered = super(CKEditor, self).render(name, value, attrs) context = { 'name': name, 'filebrowser': self.filebrowser, } return rendered + mark_safe(render_to_string( 'pages/widgets/ckeditor.html', context)) register_widget(CKEditor) class WYMEditor(Textarea): """WYMEditor widget.""" class Media: js = [join(PAGES_MEDIA_URL, path) for path in ( 'javascript/jquery.js', 'javascript/jquery.ui.js', 'javascript/jquery.ui.resizable.js', 'wymeditor/jquery.wymeditor.js', 'wymeditor/plugins/resizable/jquery.wymeditor.resizable.js', )] if "filebrowser" in getattr(settings, 'INSTALLED_APPS', []): js.append(join(PAGES_MEDIA_URL, 'wymeditor/plugins/filebrowser/jquery.wymeditor.filebrowser.js')) def __init__(self, language=None, attrs=None, **kwargs): self.language = language or getattr(settings, 'LANGUAGE_CODE') self.attrs = {'class': 'wymeditor'} if attrs: self.attrs.update(attrs) super(WYMEditor, self).__init__(attrs) def render(self, name, value, attrs=None, **kwargs): rendered = super(WYMEditor, self).render(name, value, attrs) context = { 'name': name, 'lang': self.language[:2], 'language': self.language, 'PAGES_MEDIA_URL': PAGES_MEDIA_URL, } context['page_link_wymeditor'] = 1 context['page_list'] = Page.objects.all().order_by('tree_id','lft') context['filebrowser'] = 0 if "filebrowser" in getattr(settings, 'INSTALLED_APPS', []): context['filebrowser'] = 1 return rendered + mark_safe(render_to_string( 'pages/widgets/wymeditor.html', context)) register_widget(WYMEditor) class markItUpMarkdown(Textarea): """markItUpMarkdown widget.""" class Media: js = [join(PAGES_MEDIA_URL, path) for path in ( 'javascript/jquery.js', 'markitup/jquery.markitup.js', 'markitup/sets/markdown/set.js', )] css = { 'all': [join(PAGES_MEDIA_URL, path) for path in ( 'markitup/skins/simple/style.css', 'markitup/sets/markdown/style.css', )] } def render(self, name, value, attrs=None, **kwargs): rendered = super(markItUpMarkdown, self).render(name, value, attrs) context = { 'name': name, } return rendered + mark_safe(render_to_string( 'pages/widgets/markitupmarkdown.html', context)) register_widget(markItUpMarkdown) class markItUpHTML(Textarea): """markItUpHTML widget.""" class Media: js = [join(PAGES_MEDIA_URL, path) for path in ( 'javascript/jquery.js', 'markitup/jquery.markitup.js', 'markitup/sets/default/set.js', )] css = { 'all': [join(PAGES_MEDIA_URL, path) for path in ( 'markitup/skins/simple/style.css', 'markitup/sets/default/style.css', )] } def render(self, name, value, attrs=None, **kwargs): rendered = super(markItUpHTML, self).render(name, value, attrs) context = { 'name': name, } return rendered + mark_safe(render_to_string( 'pages/widgets/markituphtml.html', context)) register_widget(markItUpHTML) class EditArea(Textarea): """EditArea is a html syntax coloured widget.""" class Media: js = [join(PAGES_MEDIA_URL, path) for path in ( 'edit_area/edit_area_full.js', )] def __init__(self, language=None, attrs=None, **kwargs): self.language = language self.attrs = {'class': 'editarea'} if attrs: self.attrs.update(attrs) super(EditArea, self).__init__(attrs) def render(self, name, value, attrs=None, **kwargs): rendered = super(EditArea, self).render(name, value, attrs) context = { 'name': name, 'language': self.language, 'PAGES_MEDIA_URL': PAGES_MEDIA_URL, } return rendered + mark_safe(render_to_string( 'pages/widgets/editarea.html', context)) register_widget(EditArea) class ImageInput(FileInput): def __init__(self, page=None, language=None, attrs=None, **kwargs): self.language = language self.page = page super(ImageInput, self).__init__(attrs) def render(self, name, value, attrs=None, **kwargs): if not self.page: field_content = _('Please save the page to show the image field') else: field_content = '' if value: field_content = _('Current file: %s<br/>') % value field_content += super(ImageInput, self).render(name, attrs) return mark_safe(field_content) register_widget(ImageInput) class VideoWidget(MultiWidget): '''A youtube `Widget` for the admin.''' def __init__(self, attrs=None, page=None, language=None, video_url=None, weight=None, height=None): widgets = [ TextInput(attrs=attrs), TextInput(attrs=attrs), TextInput(attrs=attrs) ] super(VideoWidget, self).__init__(widgets, attrs) def decompress(self, value): # backslashes are forbidden in URLs if value: return value.split('\\') return (None, None, None) def value_from_datadict(self, data, files, name): value = [u'', u'', u''] for da in filter(lambda x: x.startswith(name), data): index = int(da[len(name)+1:]) value[index] = data[da] if value[0] == value[1] == value[2] == u'': return None return u'%s\\%s\\%s' % tuple(value) def _has_changed(self, initial, data): """Need to be reimplemented to be correct.""" if data == initial: return False return bool(initial) != bool(data) def format_output(self, rendered_widgets): """ Given a list of rendered widgets (as strings), it inserts an HTML linebreak between them. Returns a Unicode string representing the HTML for the whole lot. """ return u"""<table> <tr><td>url</td><td>%s</td></tr> <tr><td>width</td><td>%s</td></tr> <tr><td>weight</td><td>%s</td></tr> </table>""" % tuple(rendered_widgets) register_widget(VideoWidget) class LanguageChoiceWidget(TextInput): def __init__(self, language=None, attrs=None, **kwargs): self.language = language self.page = kwargs.get('page') # page is None super(LanguageChoiceWidget, self).__init__(attrs) def render(self, name, value, attrs=None, **kwargs): context = { 'name': name, 'value':value, 'page':self.page, 'language': value, 'page_languages':PAGE_LANGUAGES } return mark_safe(render_to_string( 'pages/widgets/languages.html', context))