[Grok-dev] What's the best practise to store images / files in blob storage
Jan-Wijbrand Kolman
janwijbrand at gmail.com
Tue Mar 20 08:11:06 UTC 2012
Somewhat late to this aprty :)
On 2/27/12 16:53 , lzdych wrote:
> Finally I sort it out wapp a great help on IRC from trollfot and goschtl (thx
> a lot guys!).
>
> In my almost vanila environment created by grokproject (grok 1.4) I just had
> to:
>
> 1. pin these versions (IMPORTANT):
> megrok.chameleon = 0.5.2
> Chameleon = 1.2.13
>
> 2. add the following deps into setup.py (install_requires):
> 'zeam.form.base',
> zeam.form.ztk
> dolmen.blob
> dolmen.widget.file
> dolmen.widget.image
>
> 3. rewrite my form from grokcore.formlib based into zeam.form.ztk based
> as you can see in the following example:
We are using blobfile wapp grokcore.formlib. Something like so:
interfaces.py:
class ILogo(dolmen.blob.IBlobFile):
pass
class ILayoutSettings(ISettings):
logo = zope.schema.Object(
schema=ILogo, title=u'Logo', required=False,
description=_(u'Image file'))
settings.py:
class Logo(dolmen.blob.BlobFile):
grok.implements(app.interfaces.ILogo)
def __init__(self, data, filename):
super(Logo, self).__init__(data, filename=filename)
class LayoutSettings(Settings):
grok.implements(app.interfaces.ILayoutSettings)
grok.name('layout')
grok.provides(app.interfaces.ILayoutSettings)
grok.title(_(u'Layout'))
grok.traversable('logo')
grok.order(30)
# the located property ensures the __parent__/__name__
# contract for the logo.
logo = app.interfaces.LocatedProperty('logo')
browser.py:
class LogoInputWidget(zope.formlib.widgets.FileWidget):
def _toFieldValue(self, input):
if not isinstance(input, zope.publisher.browser.FileUpload):
return self.context.missing_value
return app.configuration.Logo(
input.read(), filename=input.filename)
class EditLayoutSettings(Edit):
grok.context(app.interfaces.ILayoutSettings)
def update(self):
self.form_fields = grok.Fields(app.interfaces.ILayoutSettings)
self.form_fields['logo'].custom_widget = LogoInputWidget
HTH,
regards, jw
More information about the Grok-dev
mailing list