Permalink
Browse files

cmsplugin_csvtable - playing with admin interface

  • Loading branch information...
Marko
Marko committed Mar 14, 2010
1 parent b59e605 commit 45004b14783c374046d059d3a7886c5486cb5516
View
@@ -1,3 +1,8 @@
+#!/usr/bin/env python
+# vim:set fileencoding=utf-8 sw=4 ts=8 et:vim
+# Author: Marko Mahnič
+# Created: mar 2010
+
from django.utils.translation import ugettext as _
from cms.plugin_base import CMSPluginBase
@@ -9,10 +14,17 @@ class CMSCsvTablePlugin(CMSPluginBase):
model = CsvTablePlugin
name = _('CSV Table')
render_template = 'plugins/cmsplugin_csvtable/default.html'
+ change_form_template = 'admin/plugins/cmsplugin_csvtable/plugin_change_form.html'
def render(self, context, instance, placeholder):
+ rows = instance.getRows()
+ headrows = max(0, instance.headrows)
+ has_width = instance.width >= 10
context.update({
'table': instance,
+ 'thead_rows': rows[:headrows],
+ 'tbody_rows': rows[headrows:],
+ 'has_width': has_width,
'placeholder': placeholder
})
return context
@@ -0,0 +1,6 @@
+
+div.csvtable-wraper {
+ height: 12em; overflow: scroll;
+ margin-bottom: 1em;
+ border: 1px solid black;
+}
View
@@ -1,10 +1,34 @@
+#!/usr/bin/env python
+# vim:set fileencoding=utf-8 sw=4 ts=8 et:vim
+# Author: Marko Mahnič
+# Created: mar 2010
+
import csv
-from django.utils.translation import ugettext as _
+from django.utils.translation import ugettext_lazy as _
from django.db import models
+from django.conf import settings
from cms.models import CMSPlugin
+TMP = getattr(settings, "CMSPLUGIN_CSVTABLE_WRAPPER_STYLES", (
+ ('ta-plain', 'Normal'),
+ ('ta-left', 'Align left'),
+ ('ta-right', 'Align right'),
+ ('ta-center', 'Centered'),
+))
+CMSPLUGIN_CSVTABLE_WRAPPER_STYLES = tuple( (item[0], _(item[1])) for item in TMP)
+
+TMP = getattr(settings, "CMSPLUGIN_CSVTABLE_TABLE_STYLES", (
+ ('ts-default', 'Default'),
+))
+CMSPLUGIN_CSVTABLE_TABLE_STYLES = tuple( (item[0], _(item[1])) for item in TMP)
+
+CMSPLUGIN_CSVTABLE_TABLE_WIDTHS = (
+ (0, _('Auto')), (20, '20%'), (30, '30%'), (40, '40%'), (50, '50%'),
+ (60, '60%'), (70, '70%'), (80, '80%'), (90, '90%'), (100, '100%')
+)
+
class CsvCell:
def __init__(self, content, row, order):
self.content = content
@@ -19,29 +43,30 @@ def __init__(self, number, columns):
# Derived from CMSPlugin => doesn't exist outside of a page
class CsvTablePlugin(CMSPlugin):
caption = models.CharField(_('Caption'), max_length=200)
- columns = models.CharField(_('Column layout'), max_length=200,
- help_text=_(
- 'Describe the column width and alignment in a comma separated list.\n' +
- 'Value: 5>,3,10< means: '
- 'column 1 is 5 units wide and right aligned, ' +
- 'column 2 is 3 units wide and centered, ' +
- 'column 3 is 10 units wide and left aligned.'
- ))
+ headrows = models.IntegerField(_('Header rows'))
+ #columns = models.CharField(_('Column layout'), max_length=200, blank=True
+ # help_text=_(
+ # 'Describe the column width and alignment in a comma separated list.\n' +
+ # 'Value: 5>,3,10< means: '
+ # 'column 1 is 5 units wide and right aligned, ' +
+ # 'column 2 is 3 units wide and centered, ' +
+ # 'column 3 is 10 units wide and left aligned.'
+ # ))
csv_data = models.TextField(_('CSV Data'))
- # width = models.IntegerField(_('Table width (%)'), choices=...)
- # border_css = models.CharField(_('Table style'), choices=..., max_length=20)
- # layout_css = models.CharField(_('Table placement'), choices=..., max_length=20)
+ width = models.IntegerField(_('Table width'), choices=CMSPLUGIN_CSVTABLE_TABLE_WIDTHS)
+ table_css = models.CharField(_('Table style'), choices=CMSPLUGIN_CSVTABLE_TABLE_STYLES, max_length=20, blank=True)
+ wrapper_css = models.CharField(_('Table placement'), choices=CMSPLUGIN_CSVTABLE_WRAPPER_STYLES, max_length=20, blank=True)
def __unicode__(self):
return u'%s' % (self.caption)
# TODO: cache rows?
- @property
- def rows(self):
+ def getRows(self):
rows = []
# XXX: Assume data is in unicode!
csvRows = csv.reader(self.csv_data.encode("utf8").split("\n"))
for ir,row in enumerate(csvRows):
rows.append(CsvRow(ir, [c.decode("utf8") for c in row]))
return rows
+
@@ -0,0 +1,8 @@
+{% extends "admin/cms/page/plugin_change_form.html" %}
+{# load i18n admin_modify adminmedia cms_admin #}
+{# load adminmedia #}
+
+{% block extrastyle %}{{ block.super }}
+{# TODO: the /media/ part should be taken from settings! which tag? admin_media_prefix exists, but nothing else #}
+<link rel="stylesheet" type="text/css" href="/media/plugins/cmsplugin_csvtable/csvtable.css" />
+{% endblock %}
@@ -1,21 +1,17 @@
{# TODO: user defined: apend floating/margin class to wrapper #}
{# TODO: user defined: set width style for wrapper #}
-<div class="csvtable-wraper" style="">
+<div class="csvtable-wraper{%if table.wrapper_css %} {{table.wrapper_css}}{% endif %}" style="{%if has_width %}width: {{table.width}}%;{% endif %}">
{# TODO: user defined: replace styling info for table (default: cvstable) #}
- <table class="csvtable" summary="{{ table.caption }}">
+ <table class="csvtable{%if table.table_css %} {{table.table_css}}{% endif %}" style="{%if has_width %}width: 100%;{% endif %}" summary="{{ table.caption }}">
<thead>
- {% for row in table.rows %}<tr>
- {% for cell in row.cells %}
- <th>{{cell.content}}</th>
- {% endfor %}
+ {% for row in thead_rows %}<tr>
+ {% for cell in row.cells %}<th>{{cell.content}}</th>{% endfor %}
</tr>{% endfor %}
</thead>
<tbody>
- {% for row in table.rows %}<tr class={% if row.odd %}"odd"{% else %}"even"{% endif %}>
- {% for cell in row.cells %}
- <td>{{cell.content}}</td>
- {% endfor %}
+ {% for row in tbody_rows %}<tr class={% if row.odd %}"odd"{% else %}"even"{% endif %}>
+ {% for cell in row.cells %}<td>{{cell.content}}</td>{% endfor %}
</tr>{% endfor %}
</tbody>
</table>

0 comments on commit 45004b1

Please sign in to comment.