[Zope3-checkins] CVS: Zope3/src/zope/products/i18nimage -
__init__.py:1.1.2.1 browser.py:1.1.2.1 configure.zcml:1.1.2.1
edit.pt:1.1.2.1 i18nimage.py:1.1.2.1 interfaces.py:1.1.2.1
tests.py:1.1.2.1
Philipp von Weitershausen
philikon at philikon.de
Wed Feb 11 11:29:24 EST 2004
Update of /cvs-repository/Zope3/src/zope/products/i18nimage
In directory cvs.zope.org:/tmp/cvs-serv21715/i18nimage
Added Files:
Tag: philikon-movecontent-branch
__init__.py browser.py configure.zcml edit.pt i18nimage.py
interfaces.py tests.py
Log Message:
Get rid of zope.products.content and zope.products.codecontent and move
content components in their own packages at zope.products.
See the package geddon proposal: http://dev.zope.org/Zope3/PackageGeddon
=== Added File Zope3/src/zope/products/i18nimage/__init__.py ===
#
# This file is necessary to make this directory a package.
=== Added File Zope3/src/zope/products/i18nimage/browser.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Define view component for image editing.
$Id: browser.py,v 1.1.2.1 2004/02/11 16:29:22 philikon Exp $
"""
from zope.i18n.negotiator import negotiator
from zope.app.i18n import ZopeMessageIDFactory as _
from zope.products.image.browser import ImageData
__metaclass__ = type
class I18nImageEdit:
name = 'editForm'
title = _('Edit Form')
description = _('This edit form allows you to make changes to the ' +
'properties of this image.')
def getImageSize(self, language=None):
# XXX Change to ISizeable adapter
size = self.context.getImageSize(language)
return "%d x %d" % (size[0], size[1])
def action(self, contentType, data, language, defaultLanguage,
selectLanguage=None, removeLanguage=None,
addLanguage=None, newLanguage=None):
if selectLanguage:
pass
elif removeLanguage:
self.context.removeLanguage(language)
language = self.context.getDefaultLanguage()
else:
if addLanguage:
language = newLanguage
self.context.setDefaultLanguage(defaultLanguage)
self.context.edit(data, contentType, language)
return self.request.response.redirect(self.request.URL[-1] +
"/editForm.html?language=%s" % language) # XXX url_quote
class I18nImageData(ImageData):
def __call__(self):
image = self.context
language = None
if self.request is not None:
langs = self.context.getAvailableLanguages()
language = negotiator.getLanguage(langs, self.request)
self.request.response.setHeader('content-type',
image.getContentType())
# XXX: no content-length? See ImageData.__call__
return image.getData(language)
def tag(self, height=None, width=None, **args):
"""See ImageData.tag."""
language = None
if self.request is not None and \
(width is None or height is None):
langs = self.context.getAvailableLanguages()
language = negotiator.getLanguage(langs, self.request)
if width is None:
width = self.context.getImageSize(language)[0]
if height is None:
height = self.context.getImageSize(language)[1]
return ImageData.tag(self, width=width, height=height, **args)
=== Added File Zope3/src/zope/products/i18nimage/configure.zcml ===
<configure
xmlns='http://namespaces.zope.org/zope'
xmlns:browser='http://namespaces.zope.org/browser'
i18n_domain='zope'
>
<interface
interface=".interfaces.II18nImage"
type="zope.app.interfaces.content.IContentType"
/>
<content class=".i18nimage.I18nImage">
<factory
id="I18nImage"
permission="zope.ManageContent"
title="I18n Image"
description="An Internationalized Image"
/>
<require
permission="zope.View"
interface="zope.products.file.interfaces.IReadFile"
attributes="getImageSize"
/>
<require
permission="zope.ManageContent"
interface="zope.products.file.interfaces.IWriteFile"
/>
<require
permission="zope.View"
attributes="getDefaultLanguage getAvailableLanguages"
/>
<require
permission="zope.ManageContent"
attributes="setDefaultLanguage removeLanguage" />
<implements
interface="zope.app.interfaces.annotation.IAttributeAnnotatable"
/>
</content>
<!-- browser directives -->
<browser:page
name="index.html"
for=".interfaces.II18nImage"
permission="zope.View"
allowed_attributes="__call__ tag"
class=".browser.I18nImageData"
/>
<browser:pages
for=".interfaces.II18nImage"
permission="zope.ManageContent"
class=".browser.I18nImageEdit">
<browser:page name="upload.html" template="edit.pt" />
<browser:page name="uploadAction.html" attribute="action" />
</browser:pages>
<browser:menuItems
menu="zmi_views"
for=".interfaces.II18nImage"
>
<!-- Keep the old "edit" form -->
<browser:menuItem title="Edit" action="upload.html"/>
<!-- Suppress upload form (from IFile) -->
<browser:menuItem title="Upload" action="upload.html"
filter="python: False" />
</browser:menuItems>
<browser:menuItem
menu="add_content"
for="zope.app.interfaces.container.IAdding"
title="I18n Image"
action="I18nImage"
description="A multi-locale version of an Image."
permission="zope.ManageContent"
/>
</configure>
=== Added File Zope3/src/zope/products/i18nimage/edit.pt ===
<html metal:use-macro="views/standard_macros/page">
<head>
<style metal:fill-slot="headers" type="text/css">
<!--
.ContentIcon {
width: 20px;
}
.ContentTitle {
text-align: left;
}
-->
</style>
</head>
<body>
<div metal:fill-slot="body">
<p tal:content="options/msg | nothing">
Message will go here.
</p>
<p tal:content="view/description">
Description of the Form.
</p>
<form action="uploadAction.html" method="post"
enctype="multipart/form-data">
<div class="row">
<div class="label" i18n:translate="">Content Type</div>
<div class="field">
<input name="contentType" type="text" size="20"
tal:attributes="value context/getContentType" />
</div>
</div>
<div class="row">
<div class="label" i18n:translate="">Default Language</div>
<div class="field">
<select name="defaultLanguage">
<span tal:repeat="lang context/getAvailableLanguages"
tal:omit-tag="">
<option tal:attributes="
value lang;
selected python:context.getDefaultLanguage() == lang"
tal:content="lang" />
</span>
</select>
</div>
</div>
<hr />
<div class="row">
<div class="label" i18n:translate="">Language</div>
<div class="field">
<select name="language">
<span tal:repeat="lang context/getAvailableLanguages"
tal:omit-tag="">
<option tal:attributes="
value lang;
selected python:request.get('language',
context.getDefaultLanguage()) == lang"
tal:content="lang" />
</span>
</select>
<input type="submit" name="selectLanguage" value="Show"
i18n:attributes="value show-button"/>
<input type="submit" name="removeLanguage"
i18n:attributes="value remove-button"/>
<input type="submit" name="addLanguage"
value="Add new language"
i18n:attributes="value" />
<input type="text" name="newLanguage" size="10" />
</div>
</div>
<div class="row">
<div class="label" i18n:translate="">Data</div>
<div class="field">
<input type="file" name="data" size="20" />
</div>
</div>
<div class="row">
<div class="label" i18n:translate="">Dimensions</div>
<div class="field"
tal:content="python:view.getImageSize(request.get('language'))">
40 x 40
</div>
</div>
<div class="row">
<div class="label" i18n:translate="">Dimensions</div>
<div class="field"
tal:content="python:view.getSize(request.get('language'))">
40 kB
</div>
</div>
<div class="row">
<div class="controls">
<input type="submit" name="edit" value="Save Changes"
i18n:attributes="value save-changes-button"/>
</div>
</div>
</form>
</div>
</body>
</html>
=== Added File Zope3/src/zope/products/i18nimage/i18nimage.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""
Revision Information:
$Id: i18nimage.py,v 1.1.2.1 2004/02/11 16:29:22 philikon Exp $
"""
from zope.interface import implements
from zope.products.image.image import Image, getImageInfo
from zope.products.i18nfile.i18nfile import I18nFile
from interfaces import II18nImage
class I18nImage(I18nFile):
"""An internationalized Image object. Note that images of all
languages share the same content type.
"""
implements(II18nImage)
def _create(self, data):
return Image(data)
def setData(self, data, language=None):
'''See interface IFile'''
super(I18nImage, self).setData(data, language)
if language is None or language == self.getDefaultLanguage():
# Uploading for the default language only overrides content
# type. Note: do not use the argument data here, it doesn't
# work.
contentType = getImageInfo(self.getData(language))[0]
if contentType:
self.setContentType(contentType)
def getImageSize(self, language=None):
'''See interface IImage'''
return self._get(language).getImageSize()
=== Added File Zope3/src/zope/products/i18nimage/interfaces.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""
$Id: interfaces.py,v 1.1.2.1 2004/02/11 16:29:22 philikon Exp $
"""
from zope.products.image.interfaces import IImage
from zope.products.i18nfile.interfaces import II18nFile
class II18nImage(II18nFile, IImage):
"""I18n aware image interface."""
=== Added File Zope3/src/zope/products/i18nimage/tests.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""
$Id: tests.py,v 1.1.2.1 2004/02/11 16:29:22 philikon Exp $
"""
import unittest
from zope.interface.verify import verifyClass
from zope.i18n.tests.testii18naware import TestII18nAware
from i18nimage import I18nImage
def sorted(list):
list.sort()
return list
class Test(TestII18nAware):
def _makeImage(self, *args, **kw):
return I18nImage(*args, **kw)
def _createObject(self):
obj = self._makeImage(defaultLanguage='fr')
obj.setData('', 'lt')
obj.setData('', 'en')
return obj
def testEmpty(self):
file = self._makeImage()
self.assertEqual(file.getContentType(), '')
self.assertEqual(file.getData(), '')
self.assertEqual(file.getDefaultLanguage(), 'en')
def testConstructor(self):
file = self._makeImage('Data')
self.assertEqual(file.getContentType(), '')
self.assertEqual(file.getData(), 'Data')
self.assertEqual(file.getData('en'), 'Data')
self.assertEqual(file.getData('nonexistent'), 'Data')
self.assertEqual(file.getDefaultLanguage(), 'en')
self.assertEqual(sorted(file.getAvailableLanguages()), ['en'])
file = self._makeImage('Data', defaultLanguage='fr')
self.assertEqual(file.getContentType(), '')
self.assertEqual(file.getData(), 'Data')
self.assertEqual(file.getData('en'), 'Data')
self.assertEqual(file.getData('nonexistent'), 'Data')
self.assertEqual(file.getDefaultLanguage(), 'fr')
self.assertEqual(sorted(file.getAvailableLanguages()), ['fr'])
def testMutators(self):
# XXX What's the point of this test? Does it test that data
# contents override content-type? Or not? If the former, then
# real image data should be used.
file = self._makeImage()
file.setContentType('text/plain')
self.assertEqual(file.getContentType(), 'text/plain')
self.assertEqual(sorted(file.getAvailableLanguages()), ['en'])
file.setData('Foobar')
self.assertEqual(file.getData(), 'Foobar')
self.assertEqual(sorted(file.getAvailableLanguages()), ['en'])
file.edit('Blah', 'text/html')
self.assertEqual(file.getContentType(), 'text/html')
self.assertEqual(file.getData(), 'Blah')
self.assertEqual(sorted(file.getAvailableLanguages()), ['en'])
file.setData('Foobar in lt', 'lt')
self.assertEqual(file.getData(), 'Blah')
self.assertEqual(file.getData('lt'), 'Foobar in lt')
self.assertEqual(sorted(file.getAvailableLanguages()), ['en', 'lt'])
file.edit('Blah in fr', 'text/html', 'fr')
self.assertEqual(file.getContentType(), 'text/html')
self.assertEqual(file.getData(), 'Blah')
self.assertEqual(file.getData('lt'), 'Foobar in lt')
self.assertEqual(file.getData('fr'), 'Blah in fr')
self.assertEqual(sorted(file.getAvailableLanguages()),
['en', 'fr', 'lt'])
file.removeLanguage('lt')
self.assertEqual(file.getContentType(), 'text/html')
self.assertEqual(file.getData(), 'Blah')
self.assertEqual(file.getData('fr'), 'Blah in fr')
self.assertEqual(file.getSize(), len('Blah'))
self.assertEqual(file.getSize('fr'), len('Blah in fr'))
self.assertEqual(sorted(file.getAvailableLanguages()), ['en', 'fr'])
self.assertEqual(file.getData('lt'), 'Blah')
self.assertEqual(file.getSize('lt'), len('Blah'))
file.removeLanguage('nonexistent')
self.assertEqual(file.getContentType(), 'text/html')
self.assertEqual(file.getData(), 'Blah')
self.assertEqual(file.getData('fr'), 'Blah in fr')
self.assertEqual(file.getSize(), len('Blah'))
self.assertEqual(file.getSize('fr'), len('Blah in fr'))
self.assertEqual(sorted(file.getAvailableLanguages()), ['en', 'fr'])
self.assertEqual(file.getData('lt'), 'Blah')
self.assertEqual(file.getSize('lt'), len('Blah'))
self.assertRaises(ValueError, file.removeLanguage,
file.getDefaultLanguage())
self.assertRaises(ValueError, file.setDefaultLanguage, 'nonexistent')
# Check that setData updates content type only when updating the
# default language. Need some real images or at least headers
# for that.
gifHdr = 'GIF87a\x20\x00\x10\x00'
file.setData(gifHdr)
self.assertEqual(file.getContentType(), 'image/gif')
pngHdr = '\211PNG\r\n\032\n\0\0\0\x20\0\0\0\x10'
file.setData(pngHdr, 'fr')
self.assertEqual(file.getContentType(), 'image/gif')
file.setData(pngHdr, 'en')
self.assertEqual(file.getContentType(), 'image/png')
def testInterface(self):
from zope.products.image.interfaces import IImage
from zope.products.i18nfile.interfaces import II18nFile
from zope.i18n.interfaces import II18nAware
self.failUnless(IImage.isImplementedByInstancesOf(I18nImage))
self.failUnless(verifyClass(IImage, I18nImage))
self.failUnless(II18nAware.isImplementedByInstancesOf(I18nImage))
self.failUnless(verifyClass(II18nAware, I18nImage))
self.failUnless(II18nFile.isImplementedByInstancesOf(I18nImage))
self.failUnless(verifyClass(II18nFile, I18nImage))
def testSetDefaultLanguage(self):
# getDefaultLanguage and getAvailableLanguages are tested in the
# above tests
file = self._makeImage()
file.setData('', language='lt')
file.setDefaultLanguage('lt')
self.assertEqual(file.getDefaultLanguage(), 'lt')
def test_suite():
loader = unittest.TestLoader()
return loader.loadTestsFromTestCase(Test)
if __name__=='__main__':
unittest.TextTestRunner().run(test_suite())
More information about the Zope3-Checkins
mailing list