Skip to content
This repository has been archived by the owner on Feb 1, 2019. It is now read-only.

Commit

Permalink
Add fallback to manually defined class
Browse files Browse the repository at this point in the history
  • Loading branch information
Krzysiek Szularz committed May 17, 2013
1 parent 0b813d9 commit a18812c
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 12 deletions.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
include LICENSE.txt
recursive-include aldryn_style/templates *
recursive-include aldryn_style/static *
recursive-include aldryn_style/locale *
recursive-include aldryn_style/migrations *
recursive-exclude * *.pyc
2 changes: 1 addition & 1 deletion aldryn_style/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# -*- coding: utf-8 -*-
__version__ = '0.0.1'
__version__ = '0.0.5'
2 changes: 2 additions & 0 deletions aldryn_style/cms_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from cms.plugin_pool import plugin_pool

from aldryn_style import models
from aldryn_style.forms import StyleForm


class StylePlugin(CMSPluginBase):
Expand All @@ -13,6 +14,7 @@ class StylePlugin(CMSPluginBase):
name = _('Style')
model = models.StylePlugin
allow_children = True
form = StyleForm

def render(self, context, instance, placeholder):
context['instance'] = instance
Expand Down
43 changes: 43 additions & 0 deletions aldryn_style/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# -*- coding: utf-8 -*-
from django import forms
from django.conf import settings
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _


from aldryn_style.models import StylePlugin


class ClassNameWidget(forms.MultiWidget):

def __init__(self):
self.options = getattr(settings, 'STYLE_CLASS_NAMES', [])
choices = [(x, x) for x in self.options]
choices.append(('', _('Enter manually')))
widgets = [forms.Select(choices=choices), forms.TextInput()]
super(ClassNameWidget, self).__init__(widgets)

class Media:
js = ['aldryn_style/js/class_name_widget.js']

def decompress(self, value):
select_visible = value in self.options
return (value, '') if select_visible else ('', value)

def value_from_datadict(self, *args, **kwargs):
value = super(ClassNameWidget, self).value_from_datadict(*args, **kwargs)
(select_value, text_input_value) = value
return select_value or text_input_value

def format_output(self, rendered_widgets):
return mark_safe(' '.join(rendered_widgets))


class StyleForm(forms.ModelForm):

def __init__(self, *args, **kwargs):
super(StyleForm, self).__init__(*args, **kwargs)
self.fields['class_name'].widget = ClassNameWidget()

class Meta:
model = StylePlugin
8 changes: 3 additions & 5 deletions aldryn_style/models.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
# -*- coding: utf-8 -*-
from django.core.validators import RegexValidator
from django.db import models
from django.conf import settings
from django.utils.translation import ugettext_lazy as _

from cms.models.pluginmodel import CMSPlugin


def get_class_choices():
return [(x, x) for x in getattr(settings, 'STYLE_CLASS_NAMES', [''])]
from aldryn_style.utils import CLASS_NAME_FORMAT


class StylePlugin(CMSPlugin):

class_name = models.CharField(_('Class name'), choices=get_class_choices(), max_length=50)
class_name = models.CharField(_('Class name'), max_length=50, validators=[RegexValidator(CLASS_NAME_FORMAT)])

def __unicode__(self):
return self.class_name
21 changes: 21 additions & 0 deletions aldryn_style/static/aldryn_style/js/class_name_widget.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
(function($) {
function init() {
var class_name_field = $(this);
var select = class_name_field.find('select');
var text_input = class_name_field.find('input');

select.change(function() {
if (select.val()) {
text_input.hide();
} else {
text_input.show();
}
});

select.change();
}

$(document).ready(function() {
$('.field-class_name').each(init);
});
}(django.jQuery));
5 changes: 5 additions & 0 deletions aldryn_style/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# -*- coding: utf-8 -*-
import re


CLASS_NAME_FORMAT = re.compile(r'^\w[\w_-]*$')
2 changes: 1 addition & 1 deletion app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ author:
url: https://www.divio.ch
installed-apps:
- aldryn_style
version: 0.0.1
version: 0.0.5
description: Wraps inner plugin with a classy div.
license:
name: BSD
Expand Down
8 changes: 3 additions & 5 deletions cmscloud_config.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
# -*- coding: utf-8 -*-
from cmscloud_client import forms

import re
from aldryn_style.utils import CLASS_NAME_FORMAT


class ClassNamesField(forms.CharField):

CLASS_NAME_RE = re.compile(r'^\w[\w-_]*$')

def clean(self, value):
value = super(ClassNamesField, self).clean(value)
value = filter(bool, map(lambda x: x.strip(), value.split(',')))
for class_name in value:
if not self.CLASS_NAME_RE.match(class_name):
if not CLASS_NAME_FORMAT.match(class_name):
raise forms.ValidationError(u'%s is not a proper class name.' % (class_name, ))
return value


class Form(forms.BaseForm):

class_names = ClassNamesField('Class names')
class_names = ClassNamesField('Class names', max_length=100)

def to_settings(self, data, settings):
settings['STYLE_CLASS_NAMES'] = data['class_names']
Expand Down

0 comments on commit a18812c

Please sign in to comment.