跳转到主要内容

图片小部件(带缩略图)用于 z3c.form,使用 Grok

项目描述

示例

我们将在这里开发一个小示例,以演示 dolmen.widget.image 的使用。首先,我们实例化我们的测试模型并在对象中添加一个图片字段

>>> from persistent import Persistent

>>> class Mammoth(Persistent):
...     pass

>>> import dolmen.file
>>> import grokcore.component as grok
>>> from zope.interface import Interface, alsoProvides
>>> from zope.schema.fieldproperty import FieldProperty

>>> class IMammothId(Interface):
...   """Even mammoths need an ID card"""
...   picture = dolmen.file.ImageField(title=u'Luggages')

>>> manfred = Mammoth()
>>> manfred.picture = None
>>> alsoProvides(manfred, IMammothId)

图片现在设置在我们的长毛象上。我们创建一个表单来尝试编辑图片字段

>>> from megrok.z3cform.base import EditForm

>>> class EditMammoth(EditForm):
...    grok.name('edit')
...    grok.context(IMammothId)

>>> grok.testing.grok_component('edit', EditMammoth)
True

我们持久化我们的长毛象以获取具有 URL 的定位长毛象

>>> from zope.component.hooks import getSite
>>> root = getSite()
>>> root['manfred'] = manfred
>>> manfred = root['manfred']

我们可以在持久化对象上调用编辑表单

>>> from zope.component import getMultiAdapter
>>> from zope.publisher.browser import TestRequest

>>> request = TestRequest()

>>> form = getMultiAdapter((manfred, request), name='edit')
>>> form.updateWidgets()
>>> print form.widgets['picture'].render()
<div id="form-widgets-picture"
     class="image-widget required imagefield-field">
  <input type="file" id="form-widgets-picture-input"
         name="form.widgets.picture" />
</div>

现在,让我们尝试一个假值

>>> manfred.picture = "some fake image"
>>> form = getMultiAdapter((manfred, request), name='edit')
>>> form.updateWidgets()

>>> print form.widgets['picture'].render()
<div id="form-widgets-picture"
     class="image-widget required imagefield-field">
  <div class="image-widget-preview">
    <img src="http://127.0.0.1/manfred/++thumbnail++picture.preview" />
  </div>
  <span>
    <a href="http://127.0.0.1/manfred/++download++picture"></a> &mdash;
  </span>
  <div style="padding-top: 1em;">
    <input type="radio" value="nochange" checked="checked"
           class="noborder"
           name="form.widgets.picture.nochange"
           onclick="document.getElementById('form-widgets-picture-input').disabled=true"
           id="form-widgets-picture-nochange" />
<BLANKLINE>
    <label for="form-widgets-picture-nochange">Keep existing file</label>
    <br />
    <input type="radio" value="delete" class="noborder"
           name="form.widgets.picture.nochange"
           onclick="document.getElementById('form-widgets-picture-input').disabled=true"
           id="form-widgets-picture-delete" />
    <label for="form-widgets-picture-delete">Delete existing file</label>
    <br />
    <input type="radio" name="form.widgets.picture.nochange"
           onclick="document.getElementById('form-widgets-picture-input').disabled=false"
           id="form-widgets-picture-replace" />
    <label for="form-widgets-picture-replace">Replace with new file</label>
  </div>
  <input type="file" id="form-widgets-picture-input"
         name="form.widgets.picture" />
<BLANKLINE>
  <script type="text/javascript">document.getElementById('form-widgets-picture-input').disabled=true;</script>
</div>
<BLANKLINE>

对于非持久化对象(它们不解析为 URL),不会显示预览。如果我们无法解析为 URL,我们无法提供缩略图或下载数据

>>> judith = Mammoth()
>>> judith.picture = "Fake image data"
>>> alsoProvides(judith, IMammothId)

>>> form = getMultiAdapter((judith, request), name='edit')
>>> form.updateWidgets()
>>> print form.widgets['picture'].render()
<div id="form-widgets-picture"
     class="image-widget required imagefield-field">
<BLANKLINE>
<BLANKLINE>
<BLANKLINE>
<BLANKLINE>
  <div style="padding-top: 1em;">
    <input type="radio" value="nochange" checked="checked"
           class="noborder"
           name="form.widgets.picture.nochange"
           onclick="document.getElementById('form-widgets-picture-input').disabled=true"
           id="form-widgets-picture-nochange" />
<BLANKLINE>
    <label for="form-widgets-picture-nochange">Keep existing file</label>
    <br />
<BLANKLINE>
    <label for="form-widgets-picture-delete">Delete existing file</label>
    <br />
    <input type="radio" name="form.widgets.picture.nochange"
           onclick="document.getElementById('form-widgets-picture-input').disabled=false"
           id="form-widgets-picture-replace" />
    <label for="form-widgets-picture-replace">Replace with new file</label>
  </div>
  <input type="file" id="form-widgets-picture-input"
         name="form.widgets.picture" />
<BLANKLINE>
  <script type="text/javascript">document.getElementById('form-widgets-picture-input').disabled=true;</script>
</div>
<BLANKLINE>

变更日志

0.2 (2010-03-01)

  • 更新了代码以使用 dolmen.widget.file 0.2 dolmen.file 0.5.1

  • 更新了模板以使用 zope.size.ISized 适配器来显示文件大小。

  • 更新了测试以去除不必要的 zope.app 软件包。

0.1 (2009-10-22)

  • 首次发布

由以下机构支持