Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Enhanced structure to allow better definition of widgets

  • Loading branch information...
commit 7f3b93e274003a22d4dde1d68d153c22c18c8c8e 1 parent f43a34d
@pauloprea pauloprea authored
View
43 smartsnippets/admin.py
@@ -2,14 +2,15 @@
from django.db.models import Q
from django.contrib.sites.models import Site
from django.forms import ModelForm
+from django.forms.models import BaseInlineFormSet
from django.core.exceptions import ValidationError
from django.template import Template, TemplateSyntaxError, \
TemplateDoesNotExist, loader
+from django.forms.widgets import Select
-
-from models import SmartSnippet, SmartSnippetVariable
+from models import SmartSnippet, SmartSnippetVariable, DropDownVariable
from settings import shared_sites, include_orphan, restrict_user
-
+from widgets_pool import widget_pool
class SnippetForm(ModelForm):
include_orphan = include_orphan
@@ -43,13 +44,31 @@ def clean_template_path(self):
raise ValidationError(e)
return path
+
+class SnippetVariablesFormSet(BaseInlineFormSet):
+ def get_queryset(self):
+ if not hasattr(self, '_queryset'):
+ available_widgets = [widget.__name__ for widget in widget_pool.get_all_widgets()]
+ qs = super(SnippetVariablesFormSet, self).get_queryset().filter(widget__in=available_widgets)
+ self._queryset = qs
+ return self._queryset
+
+
class SnippetVariablesAdmin(admin.StackedInline):
model = SmartSnippetVariable
- extra = 1
+ extra = 0
+
+ def formfield_for_dbfield(self, db_field, **kwargs):
+ if db_field.name == 'widget':
+ kwargs['widget'] = Select(choices=tuple([(x.__name__, x.name) for x in widget_pool.get_all_widgets()]))
+ return super(SnippetVariablesAdmin,self).formfield_for_dbfield(db_field, **kwargs)
+
+class RegularSnippetVariablesAdmin(SnippetVariablesAdmin):
+ formset = SnippetVariablesFormSet
class SnippetAdmin(admin.ModelAdmin):
- inlines = [SnippetVariablesAdmin, ]
+ inlines = [RegularSnippetVariablesAdmin,]
shared_sites = shared_sites
include_orphan = include_orphan
restrict_user = restrict_user
@@ -105,4 +124,16 @@ def queryset(self, request):
return q.filter(f).distinct()
-admin.site.register(SmartSnippet, SnippetAdmin)
+
+class DropDownVariableAdmin(SnippetVariablesAdmin):
+ model = DropDownVariable
+ exclude = ('widget',)
+
+
+class ExtendedSnippetAdmin(SnippetAdmin):
+ inlines = [RegularSnippetVariablesAdmin, DropDownVariableAdmin]
+
+
+admin.site.register(SmartSnippet, SnippetAdmin)
+admin.site.unregister(SmartSnippet)
+admin.site.register(SmartSnippet, ExtendedSnippetAdmin)
View
2  smartsnippets/cms_plugins.py
@@ -27,7 +27,7 @@ def change_view(self, request, object_id, extra_context=None):
pointer = SmartSnippetPointer.objects.get(pk=object_id)
variables = pointer.variables.all()
extra_context.update({'variables':
- [var.get_widget() for var in variables]
+ [widget_pool.get_widget(var.widget)(var) for var in variables]
})
return (super(SmartSnippetPlugin, self)
.change_view(request, object_id, extra_context))
View
2  smartsnippets/migrations/0005_auto__add_smartsnippetvariable__chg_field_variable_value.py
@@ -13,7 +13,6 @@ def forwards(self, orm):
('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
('name', self.gf('django.db.models.fields.CharField')(max_length=50)),
('widget', self.gf('django.db.models.fields.CharField')(max_length=50)),
- ('choices', self.gf('django.db.models.fields.CharField')(max_length=512, null=True, blank=True)),
('snippet', self.gf('django.db.models.fields.related.ForeignKey')(related_name='variables', to=orm['smartsnippets.SmartSnippet'])),
))
db.send_create_signal('smartsnippets', ['SmartSnippetVariable'])
@@ -70,7 +69,6 @@ def backwards(self, orm):
},
'smartsnippets.smartsnippetvariable': {
'Meta': {'ordering': "['name']", 'object_name': 'SmartSnippetVariable'},
- 'choices': ('django.db.models.fields.CharField', [], {'max_length': '512', 'null': 'True', 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '50'}),
'snippet': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'variables'", 'to': "orm['smartsnippets.SmartSnippet']"}),
View
1  smartsnippets/migrations/0006_populate_variables.py
@@ -74,7 +74,6 @@ def get_variables_list(self, snippet):
},
'smartsnippets.smartsnippetvariable': {
'Meta': {'ordering': "['name']", 'object_name': 'SmartSnippetVariable'},
- 'choices': ('django.db.models.fields.CharField', [], {'max_length': '512', 'null': 'True', 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '50'}),
'snippet': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'variables'", 'to': "orm['smartsnippets.SmartSnippet']"}),
View
1  smartsnippets/migrations/0007_relate_variable_to_smartsnippet.py
@@ -66,7 +66,6 @@ def backwards(self, orm):
},
'smartsnippets.smartsnippetvariable': {
'Meta': {'ordering': "['name']", 'object_name': 'SmartSnippetVariable'},
- 'choices': ('django.db.models.fields.CharField', [], {'max_length': '512', 'null': 'True', 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '50'}),
'snippet': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'variables'", 'to': "orm['smartsnippets.SmartSnippet']"}),
View
1  smartsnippets/migrations/0008_populate_relation_variable_to_smartsnippet.py
@@ -67,7 +67,6 @@ def backwards(self, orm):
},
'smartsnippets.smartsnippetvariable': {
'Meta': {'ordering': "['name']", 'object_name': 'SmartSnippetVariable'},
- 'choices': ('django.db.models.fields.CharField', [], {'max_length': '512', 'null': 'True', 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '50'}),
'snippet': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'variables'", 'to': "orm['smartsnippets.SmartSnippet']"}),
View
1  smartsnippets/migrations/0009_auto__del_field_variable_name.py
@@ -60,7 +60,6 @@ def backwards(self, orm):
},
'smartsnippets.smartsnippetvariable': {
'Meta': {'ordering': "['name']", 'object_name': 'SmartSnippetVariable'},
- 'choices': ('django.db.models.fields.CharField', [], {'max_length': '512', 'null': 'True', 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '50'}),
'snippet': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'variables'", 'to': "orm['smartsnippets.SmartSnippet']"}),
View
1  smartsnippets/migrations/0010_auto__add_unique_together_constraints.py
@@ -66,7 +66,6 @@ def backwards(self, orm):
},
'smartsnippets.smartsnippetvariable': {
'Meta': {'ordering': "['name']", 'unique_together': "(('snippet', 'name'),)", 'object_name': 'SmartSnippetVariable'},
- 'choices': ('django.db.models.fields.CharField', [], {'max_length': '512', 'null': 'True', 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '50'}),
'snippet': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'variables'", 'to': "orm['smartsnippets.SmartSnippet']"}),
View
26 smartsnippets/models.py
@@ -6,7 +6,6 @@
from django.utils.translation import ugettext_lazy as _
from cms.models import CMSPlugin
-from smartsnippets.widgets_pool import widget_pool
class SmartSnippet(models.Model):
name = models.CharField(unique=True, max_length=255)
@@ -44,9 +43,7 @@ def __unicode__(self):
class SmartSnippetVariable(models.Model):
name = models.CharField(max_length=50)
- widget = models.CharField(max_length=50,
- choices = tuple([(x.__name__, x.name) for x in widget_pool.get_all_widgets()]))
- choices = models.CharField(max_length=512, blank=True, null=True)
+ widget = models.CharField(max_length=50)
snippet = models.ForeignKey(SmartSnippet, related_name="variables")
class Meta:
@@ -76,7 +73,7 @@ def __unicode__(self):
class Variable(models.Model):
- snippet_variable = models.ForeignKey(SmartSnippetVariable, related_name='variables', null=True)
+ snippet_variable = models.ForeignKey(SmartSnippetVariable, related_name='variables')
value = models.CharField(max_length=1024)
snippet = models.ForeignKey(SmartSnippetPointer, related_name='variables')
@@ -88,9 +85,18 @@ def name(self):
return self.snippet_variable.name
@property
- def choices(self):
- return ([choice.strip() for choice in self.snippet_variable.choices.split(',') if choice.strip()]
- if self.snippet_variable.choices else [])
+ def widget(self):
+ return self.snippet_variable.widget
- def get_widget(self):
- return widget_pool.get_widget(self.snippet_variable.widget)(self.name, self.value, choices=self.choices)
+
+
+class DropDownVariable(SmartSnippetVariable):
+ choices = models.CharField(max_length=512)
+
+ @property
+ def choices_list(self):
+ return [choice.strip() for choice in self.choices.split(',')]
+
+ def save(self, *args, **kwargs):
+ self.widget = 'DropDownField'
+ super(DropDownVariable, self).save(*args, **kwargs)
View
7 smartsnippets/settings.py
@@ -3,10 +3,3 @@
shared_sites = getattr(settings, 'SMARTSNIPPETS_SHARED_SITES', [])
include_orphan = getattr(settings, 'SMARTSNIPPETS_INCLUDE_ORPHAN', True)
restrict_user = getattr(settings, 'SMARTSNIPPETS_RESTRICT_USER', False)
-installed_widgets = getattr(settings, 'SMARTSNIPPETS_INSTALLED_WIDGETS',
- [
- 'smartsnippets.widgets.textfield',
- 'smartsnippets.widgets.textareafield',
- 'smartsnippets.widgets.dropdownfield'
- ]
- )
View
6 smartsnippets/templates/smartsnippets/widgets/dropdownfield/widget.html
@@ -1,8 +1,8 @@
-<td><label for="var_{{ field.label|lower }}">{{ field.label }}</label></td>
+<td><label for="var_{{ field.name|lower }}">{{ field.name }}</label></td>
<td>
- <select id="var_{{ field.label }}" name="_{{ field.label }}_">
+ <select id="var_{{ field.name }}" name="_{{ field.name }}_">
<option value="">---------</option>
- {% for value in field.choices %}
+ {% for value in field.snippet_variable.dropdownvariable.choices_list %}
<option {% if value == field.value %} selected="selected" {% endif %} value="{{ value }}" >{{ value }}</option>
{% endfor %}
</select>
View
4 smartsnippets/templates/smartsnippets/widgets/textareafield/widget.html
@@ -1,2 +1,2 @@
-<td><label for="var_{{ field.label|lower }}">{{ field.label }}</label></td>
-<td><textarea id="var_{{ field.label }}" name="_{{ field.label }}_" rows="6" cols="72">{{ field.value }}</textarea></td>
+<td><label for="var_{{ field.name|lower }}">{{ field.name }}</label></td>
+<td><textarea id="var_{{ field.name }}" name="_{{ field.name }}_" rows="6" cols="72">{{ field.value }}</textarea></td>
View
4 smartsnippets/templates/smartsnippets/widgets/textfield/widget.html
@@ -1,2 +1,2 @@
-<td><label for="var_{{ field.label|lower }}">{{ field.label }}</label></td>
-<td><input id="var_{{ field.label }}" type="text" name="_{{ field.label }}_" value="{{ field.value }}" size="100"/></td>
+<td><label for="var_{{ field.name|lower }}">{{ field.name }}</label></td>
+<td><input id="var_{{ field.name }}" type="text" name="_{{ field.name }}_" value="{{ field.value }}" size="100"/></td>
View
19 smartsnippets/widgets.py
@@ -1,34 +1,31 @@
from django.template.loader import render_to_string
from smartsnippets.widgets_pool import widget_pool
from smartsnippets.widgets_base import SmartSnippetWidgetBase
-
+from models import DropDownVariable
class TextField(SmartSnippetWidgetBase):
name = 'Text Field'
-
+
def render(self):
return render_to_string('smartsnippets/widgets/textfield/widget.html',
- {'field': self})
+ {'field': self.variable})
class TextAreaField(SmartSnippetWidgetBase):
name = 'TextArea Field'
-
+
def render(self):
return render_to_string('smartsnippets/widgets/textareafield/widget.html',
- {'field': self})
+ {'field': self.variable})
class DropDownField(SmartSnippetWidgetBase):
name = 'DropDown Field'
-
- def __init__(self, label, value, **kwargs):
- super(DropDownField, self).__init__(label, value, **kwargs)
- self.choices = kwargs.get('choices')
-
+ model = DropDownVariable
+
def render(self):
return render_to_string('smartsnippets/widgets/dropdownfield/widget.html',
- {'field': self})
+ {'field': self.variable})
widget_pool.register_widget(TextField)
widget_pool.register_widget(TextAreaField)
View
9 smartsnippets/widgets_base.py
@@ -1,8 +1,11 @@
+from models import SmartSnippetVariable
+
class SmartSnippetWidgetBase(object):
+ name = 'Base Widget'
+ model = SmartSnippetVariable
- def __init__(self, label, value, **kwargs):
- self.label = label
- self.value = value
+ def __init__(self, variable, **kwargs):
+ self.variable = variable
def render(self, context):
raise NotImplementedError("render needs to be implemented")
View
8 smartsnippets/widgets_pool.py
@@ -1,8 +1,8 @@
from django.core.exceptions import ImproperlyConfigured
-from utils.widgets_load import load
+from cms.utils.django_load import load
from smartsnippets.exceptions import WidgetAlreadyRegistered, WidgetNotRegistered
from smartsnippets.widgets_base import SmartSnippetWidgetBase
-
+from models import SmartSnippetVariable
class WidgetPool(object):
def __init__(self):
@@ -43,9 +43,9 @@ def get_widget(self, name):
self.discover_widgets()
return self.widgets[name]
- def get_all_widgets(self, snippet=None):
+ def get_all_widgets(self, has_model=False, snippet=None):
self.discover_widgets()
- widgets = self.widgets.values()
+ widgets = [x for x in self.widgets.values() if x.model==SmartSnippetVariable]
widgets.sort(key=lambda obj: unicode(obj.name))
return widgets
Please sign in to comment.
Something went wrong with that request. Please try again.