Skip to content

Commit

Permalink
apply default configuration to tiles at initialization (closes #100)
Browse files Browse the repository at this point in the history
now we can get rid of a lot of ugly code
  • Loading branch information
hvelarde committed Apr 26, 2013
1 parent cc35a15 commit 706b8bb
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 3 deletions.
4 changes: 4 additions & 0 deletions docs/HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ Because you have to know where your towel is.
1.0a3 (unreleased)
^^^^^^^^^^^^^^^^^^

- Apply default configuration to tiles at initialization (closes `#100`_).
[hvelarde]

- Store basic tile data in unicode format to avoid UnicodeDecodeError.
[hvelarde]

Expand Down Expand Up @@ -89,6 +92,7 @@ Because you have to know where your towel is.

.. _`PloneFormGen`: https://pypi.python.org/pypi/Products.PloneFormGen
.. _`#48`: https://github.com/collective/collective.cover/issues/48
.. _`#100`: https://github.com/collective/collective.cover/issues/100
.. _`#114`: https://github.com/collective/collective.cover/issues/114
.. _`#121`: https://github.com/collective/collective.cover/issues/121
.. _`#123`: https://github.com/collective/collective.cover/issues/123
Expand Down
77 changes: 74 additions & 3 deletions src/collective/cover/tiles/configuration.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,65 @@
# -*- coding: utf-8 -*-

from persistent.dict import PersistentDict

from plone.namedfile.interfaces import INamedBlobImageField
from plone.tiles.interfaces import ITileType
from zope.annotation.interfaces import IAnnotations

from zope.component import getUtility
from zope.interface import implements
from zope.interface import Interface
from zope.schema import getFieldNamesInOrder
from zope.schema import getFieldsInOrder
from zope.schema.interfaces import ITextLine


ANNOTATIONS_KEY_PREFIX = u'plone.tiles.configuration'


class ITilesConfigurationScreen(Interface):

def _set_default_configuration():
"""Return a default configuration based on fields defined on the
schema; all fields must have, at least, the following attributes:
visibility: (u'on', u'off')
is the field visible? defaults to u'on'
XXX: this could be a boolean in the future
order: (u'1', u'2', ...)
in which order we are going to display the field?
XXX: until we fix the UI, we must start with 1 instead of 0
because is most intuitive for end users
the following attribute is available only in TextLine fields:
htmltag: (u'h1', u'h2', ...)
defaults to u'h2'
XXX: this name is a bad one, but I have no proposal for now
the following attributes are available only in NamedImage fields:
position: (u'inline', u'left', u'right')
defaults to u'left'
XXX: this should be named 'alignment' probably
imgsize: (any of the available sizes for scaled down images)
defaults to u'mini 200:200' or first available size
XXX: this should be named 'scale' probably
final result will be something like this:
{'date': {'order': u'3', 'visibility': u'on'},
'description': {'order': u'1', 'visibility': u'on'},
'image': {'imgsize': u'mini 200:200',
'order': u'2',
'position': u'left',
'visibility': u'on'},
'subjects': {'order': u'4', 'visibility': u'on'},
'title': {'htmltag': u'h2', 'order': u'0', 'visibility': u'on'},
'uuid': {'htmltag': u'h2', 'order': u'5', 'visibility': u'on'}}
obviously some of the fields are not ment to be displayed, but that's
another story.
"""

def get_configuration():
"""
Get the configuration for a given tile
Expand Down Expand Up @@ -40,11 +88,34 @@ def __init__(self, context, request, tile):
self.request = request
self.tile = tile
self.annotations = IAnnotations(self.context)
self.key = "%s.%s" % (ANNOTATIONS_KEY_PREFIX, tile.id,)
self.key = "%s.%s" % (ANNOTATIONS_KEY_PREFIX, tile.id)

def _set_default_configuration(self):
defaults = {}
tile_type = getUtility(ITileType, name=self.tile.__name__)
fields = getFieldNamesInOrder(tile_type.schema)

for name, field in getFieldsInOrder(tile_type.schema):
order = unicode(fields.index(name))
# default configuration attributes for all fields
defaults[name] = {'order': order, 'visibility': u'on'}
if ITextLine.providedBy(field):
# field is TextLine, we should add 'htmltag'
defaults[name]['htmltag'] = u'h2'
elif INamedBlobImageField.providedBy(field):
# field is an image, we should add 'position' and 'imgsize'
defaults[name]['position'] = u'left'
defaults[name]['imgsize'] = u'mini 200:200'

return defaults

def get_configuration(self):
data = dict(self.annotations.get(self.key, {}))

if not data:
# tile has no configuration; let's apply the default one
data = self._set_default_configuration()

return data

def set_configuration(self, configuration):
Expand Down

0 comments on commit 706b8bb

Please sign in to comment.