-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added an extensible hook to add different types of variables to
a template
- Loading branch information
Showing
8 changed files
with
264 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from django import forms | ||
from cmsplugin_text_ng.models import TextNG | ||
|
||
class PluginAddForm(forms.ModelForm): | ||
class Meta: | ||
fields = ('template',) | ||
model = TextNG | ||
|
||
|
||
class PluginEditForm(forms.ModelForm): | ||
class Meta: | ||
fields = ('body',) | ||
model = TextNG |
81 changes: 81 additions & 0 deletions
81
...migrations/0002_auto__add_textngvariabletext__add_unique_textngvariabletext_text_ng_la.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# encoding: utf-8 | ||
import datetime | ||
from south.db import db | ||
from south.v2 import SchemaMigration | ||
from django.db import models | ||
|
||
class Migration(SchemaMigration): | ||
|
||
def forwards(self, orm): | ||
|
||
# Adding model 'TextNGVariableText' | ||
db.create_table('cmsplugin_text_ng_textngvariabletext', ( | ||
('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), | ||
('text_ng', self.gf('django.db.models.fields.related.ForeignKey')(related_name='+', to=orm['cmsplugin_text_ng.TextNG'])), | ||
('label', self.gf('django.db.models.fields.CharField')(max_length=20)), | ||
('value', self.gf('django.db.models.fields.TextField')(null=True, blank=True)), | ||
)) | ||
db.send_create_signal('cmsplugin_text_ng', ['TextNGVariableText']) | ||
|
||
# Adding unique constraint on 'TextNGVariableText', fields ['text_ng', 'label'] | ||
db.create_unique('cmsplugin_text_ng_textngvariabletext', ['text_ng_id', 'label']) | ||
|
||
|
||
def backwards(self, orm): | ||
|
||
# Removing unique constraint on 'TextNGVariableText', fields ['text_ng', 'label'] | ||
db.delete_unique('cmsplugin_text_ng_textngvariabletext', ['text_ng_id', 'label']) | ||
|
||
# Deleting model 'TextNGVariableText' | ||
db.delete_table('cmsplugin_text_ng_textngvariabletext') | ||
|
||
|
||
models = { | ||
'cms.cmsplugin': { | ||
'Meta': {'object_name': 'CMSPlugin'}, | ||
'creation_date': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}), | ||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), | ||
'language': ('django.db.models.fields.CharField', [], {'max_length': '15', 'db_index': 'True'}), | ||
'level': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}), | ||
'lft': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}), | ||
'parent': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['cms.CMSPlugin']", 'null': 'True', 'blank': 'True'}), | ||
'placeholder': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['cms.Placeholder']", 'null': 'True'}), | ||
'plugin_type': ('django.db.models.fields.CharField', [], {'max_length': '50', 'db_index': 'True'}), | ||
'position': ('django.db.models.fields.PositiveSmallIntegerField', [], {'null': 'True', 'blank': 'True'}), | ||
'rght': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}), | ||
'tree_id': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}) | ||
}, | ||
'cms.placeholder': { | ||
'Meta': {'object_name': 'Placeholder'}, | ||
'default_width': ('django.db.models.fields.PositiveSmallIntegerField', [], {'null': 'True'}), | ||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), | ||
'slot': ('django.db.models.fields.CharField', [], {'max_length': '50', 'db_index': 'True'}) | ||
}, | ||
'cmsplugin_text_ng.textng': { | ||
'Meta': {'object_name': 'TextNG', 'db_table': "'cmsplugin_textng'"}, | ||
'body': ('django.db.models.fields.TextField', [], {}), | ||
'cmsplugin_ptr': ('django.db.models.fields.related.OneToOneField', [], {'to': "orm['cms.CMSPlugin']", 'unique': 'True', 'primary_key': 'True'}), | ||
'template': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['cmsplugin_text_ng.TextNGTemplate']"}) | ||
}, | ||
'cmsplugin_text_ng.textngtemplate': { | ||
'Meta': {'ordering': "['title']", 'object_name': 'TextNGTemplate'}, | ||
'category': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['cmsplugin_text_ng.TextNGTemplateCategory']", 'null': 'True', 'blank': 'True'}), | ||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), | ||
'path': ('django.db.models.fields.CharField', [], {'max_length': '128'}), | ||
'title': ('django.db.models.fields.CharField', [], {'max_length': '128'}) | ||
}, | ||
'cmsplugin_text_ng.textngtemplatecategory': { | ||
'Meta': {'ordering': "['title']", 'object_name': 'TextNGTemplateCategory'}, | ||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), | ||
'title': ('django.db.models.fields.CharField', [], {'max_length': '128'}) | ||
}, | ||
'cmsplugin_text_ng.textngvariabletext': { | ||
'Meta': {'unique_together': "(('text_ng', 'label'),)", 'object_name': 'TextNGVariableText'}, | ||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), | ||
'label': ('django.db.models.fields.CharField', [], {'max_length': '20'}), | ||
'text_ng': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'+'", 'to': "orm['cmsplugin_text_ng.TextNG']"}), | ||
'value': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}) | ||
} | ||
} | ||
|
||
complete_apps = ['cmsplugin_text_ng'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# -*- coding: utf-8 -*- | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# -*- coding: utf-8 -*- | ||
from django import template | ||
|
||
register = template.Library() | ||
|
||
|
||
class DefineNode(template.Node): | ||
def __init__(self, variable_name, variable_type, optional=False): | ||
self.variable_name = variable_name | ||
self.variable_type = variable_type | ||
self.optional = optional | ||
|
||
def render(self, context): | ||
""" | ||
This template tag does not materialize itself in the template | ||
""" | ||
return '' | ||
|
||
|
||
error_message = 'Syntax: {% define variable_name as variable_type [optional] %}' | ||
|
||
@register.tag(name='define') | ||
def do_define(parser, token): | ||
bits = token.split_contents()[1:] | ||
if len(bits) < 3 and bits[1] != 'as': | ||
raise template.TemplateSyntaxError(error_message) | ||
del bits[1] # "as" | ||
if len(bits) == 2: | ||
variable_name, variable_type = bits | ||
optional = False | ||
elif len(bits) == 3: | ||
if bits[2] == 'optional': | ||
variable_name, variable_type = bits[:2] | ||
optional = True | ||
else: | ||
raise template.TemplateSyntaxError(error_message) | ||
else: | ||
raise template.TemplateSyntaxError(error_message) | ||
return DefineNode(variable_name, variable_type, optional) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
_registry = {} | ||
|
||
class VariableTypeAlreadyRegistered(Exception): | ||
pass | ||
|
||
|
||
class InvalidType(Exception): | ||
pass | ||
|
||
def register_type(type_name, model_class): | ||
from cmsplugin_text_ng.models import TextNGVariableBase | ||
if type_name in _registry: | ||
if _registry[type_name] == model_class: | ||
# already registered | ||
return | ||
else: | ||
raise VariableTypeAlreadyRegistered( | ||
'The type "%s" is already registered by %s' % (type_name, _registry[type_name].__name__) | ||
) | ||
if not issubclass(model_class, TextNGVariableBase): | ||
raise InvalidType('%s is not a subclass of TextNGVariableBase' % model_class.__name__) | ||
# TODO: validate that there is a "value" field that is nullable | ||
_registry[type_name] = model_class | ||
|
||
def get_type(type_name): | ||
return _registry[type_name] | ||
|
||
def get_type_list(): | ||
return _registry.values() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# -*- coding: utf-8 -*- | ||
from django.template import loader | ||
|
||
from cmsplugin_text_ng.templatetags.text_ng_tags import DefineNode | ||
from cmsplugin_text_ng.type_registry import get_type | ||
|
||
def get_variables_from_template(template): | ||
if isinstance(template, basestring): | ||
template = loader.get_template(template) | ||
variable_nodes = [n for n in template.nodelist if isinstance(n, DefineNode)] | ||
variables = {} | ||
for node in variable_nodes: | ||
variables[node.variable_name] = { | ||
'type': get_type(node.variable_type), | ||
'optional': node.optional, | ||
} | ||
return variables |