[Checkins] SVN: z3c.reference/ initial reference product import
Oliver Petznick
oliver at mopa.at
Thu Feb 1 13:02:24 EST 2007
Log message for revision 72303:
initial reference product import
Changed:
A z3c.reference/
A z3c.reference/trunk/
A z3c.reference/trunk/src/
A z3c.reference/trunk/src/z3c/
A z3c.reference/trunk/src/z3c/reference/
A z3c.reference/trunk/src/z3c/reference/README.txt
A z3c.reference/trunk/src/z3c/reference/__init__.py
A z3c.reference/trunk/src/z3c/reference/browser/
A z3c.reference/trunk/src/z3c/reference/browser/__init__.py
A z3c.reference/trunk/src/z3c/reference/browser/configure.zcml
A z3c.reference/trunk/src/z3c/reference/browser/resources/
A z3c.reference/trunk/src/z3c/reference/browser/resources/noimage.jpg
A z3c.reference/trunk/src/z3c/reference/browser/resources/z3c_reference.js
A z3c.reference/trunk/src/z3c/reference/browser/tests.py
A z3c.reference/trunk/src/z3c/reference/browser/views.py
A z3c.reference/trunk/src/z3c/reference/browser/widget.py
A z3c.reference/trunk/src/z3c/reference/configure.zcml
A z3c.reference/trunk/src/z3c/reference/interfaces.py
A z3c.reference/trunk/src/z3c/reference/reference.py
A z3c.reference/trunk/src/z3c/reference/schema.py
A z3c.reference/trunk/src/z3c/reference/tests.py
A z3c.reference/trunk/src/z3c/reference/z3c.reference-configure.zcml
-=-
Added: z3c.reference/trunk/src/z3c/reference/README.txt
===================================================================
--- z3c.reference/trunk/src/z3c/reference/README.txt 2007-02-01 17:06:08 UTC (rev 72302)
+++ z3c.reference/trunk/src/z3c/reference/README.txt 2007-02-01 18:02:22 UTC (rev 72303)
@@ -0,0 +1,97 @@
+=========================
+ Refererences to Objects
+=========================
+
+Referenced objects must be adaptable to IKeyReference.
+
+ >>> from z3c.reference.reference import ViewReference
+ >>> from zope.annotation.interfaces import IAttributeAnnotatable
+ >>> from zope.app.container.contained import Contained
+ >>> from zope import interface
+ >>> class O(Contained):
+ ... interface.implements(IAttributeAnnotatable)
+ ... def __init__(self,title):
+ ... if title is not None:
+ ... self.title = title
+
+ >>> o = O(u"first")
+ >>> o.title
+ u'first'
+
+ >>> ref = ViewReference(target=o)
+ >>> ref.target is o
+ True
+
+ If we do not define a target the view is supposed to be absolute
+ url. No further checks are done.
+
+ >>> ref = ViewReference(view=u'abc')
+ >>> ref.target is None
+ True
+
+
+
+Reference Fields
+================
+
+ >>> from zope.schema.fieldproperty import FieldProperty
+ >>> from z3c.reference.schema import ViewReferenceField
+ >>>
+ >>> class IContent(interface.Interface):
+ ... ref = ViewReferenceField(title=u"Reference")
+ >>> class Content(object):
+ ... interface.implements(IContent)
+ ... ref = FieldProperty(IContent['ref'])
+
+ >>> c = Content()
+ >>> c.ref is None
+ True
+ >>> c.ref = object()
+ Traceback (most recent call last):
+ ...
+ SchemaNotProvided
+ >>> o2 = O(u'O2 Title')
+ >>> c.ref = ViewReference(o2)
+ >>> c.ref.target is o2
+ True
+
+Image Reference Fields
+======================
+
+ImageReferenceField is a special ViewReferenceField, which constraints
+the size and the object type to IImage
+
+ >>> from z3c.reference.reference import ImageReference
+ >>> from z3c.reference.schema import ImageReferenceField
+ >>> from zope.schema.fieldproperty import FieldProperty
+
+ On Image references the size is required.
+
+ >>> class IContent(interface.Interface):
+ ... img = ImageReferenceField(title=u"Image",size=(10,10))
+ >>> class Content(object):
+ ... interface.implements(IContent)
+ ... img = FieldProperty(IContent['img'])
+
+ >>> c = Content()
+ >>> c.img is None
+ True
+ >>> c.img = object()
+ Traceback (most recent call last):
+ ...
+ SchemaNotProvided
+ >>> from zope.app.file.image import Image
+ >>> from zope.app.file.interfaces import IImage
+ >>> img = Image()
+ >>> imgRef = ImageReference(img)
+ >>> imgRef
+ <z3c.reference.reference.ImageReference object at ...>
+
+ >>> IContent['img'].schema.providedBy(imgRef)
+ True
+
+ >>> c.img = imgRef
+ >>> c.img.target is img
+ True
+
+
Property changes on: z3c.reference/trunk/src/z3c/reference/README.txt
___________________________________________________________________
Name: svn:eol-style
+ native
Added: z3c.reference/trunk/src/z3c/reference/__init__.py
===================================================================
--- z3c.reference/trunk/src/z3c/reference/__init__.py 2007-02-01 17:06:08 UTC (rev 72302)
+++ z3c.reference/trunk/src/z3c/reference/__init__.py 2007-02-01 18:02:22 UTC (rev 72303)
@@ -0,0 +1 @@
+#
Property changes on: z3c.reference/trunk/src/z3c/reference/__init__.py
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added: z3c.reference/trunk/src/z3c/reference/browser/__init__.py
===================================================================
--- z3c.reference/trunk/src/z3c/reference/browser/__init__.py 2007-02-01 17:06:08 UTC (rev 72302)
+++ z3c.reference/trunk/src/z3c/reference/browser/__init__.py 2007-02-01 18:02:22 UTC (rev 72303)
@@ -0,0 +1 @@
+#
Property changes on: z3c.reference/trunk/src/z3c/reference/browser/__init__.py
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added: z3c.reference/trunk/src/z3c/reference/browser/configure.zcml
===================================================================
--- z3c.reference/trunk/src/z3c/reference/browser/configure.zcml 2007-02-01 17:06:08 UTC (rev 72302)
+++ z3c.reference/trunk/src/z3c/reference/browser/configure.zcml 2007-02-01 18:02:22 UTC (rev 72303)
@@ -0,0 +1,65 @@
+<configure
+ xmlns="http://namespaces.zope.org/zope"
+ xmlns:browser="http://namespaces.zope.org/browser">
+
+<view
+ type="zope.publisher.interfaces.browser.IBrowserRequest"
+ for="..interfaces.IViewReferenceField"
+ provides="zope.app.form.interfaces.IInputWidget"
+ factory=".widget.ViewReferenceWidget"
+ permission="zope.Public"
+ />
+
+<view
+ type="zope.publisher.interfaces.browser.IBrowserRequest"
+ for="..interfaces.IObjectReferenceField"
+ provides="zope.app.form.interfaces.IInputWidget"
+ factory=".widget.ObjectReferenceWidget"
+ permission="zope.Public"
+ />
+
+<view
+ type="zope.publisher.interfaces.browser.IBrowserRequest"
+ for="..interfaces.IImageReferenceField"
+ provides="zope.app.form.interfaces.IInputWidget"
+ factory=".widget.ImageReferenceWidget"
+ permission="zope.Public"
+ />
+
+
+<view
+ for="..interfaces.IViewReference"
+ name="absolute_url"
+ factory=".views.ViewReferenceAbsoluteURL"
+ type="zope.publisher.interfaces.http.IHTTPRequest"
+ permission="zope.Public"
+ allowed_interface="zope.traversing.browser.interfaces.IAbsoluteURL"
+ />
+
+<view
+ for="..interfaces.IViewReference"
+ factory=".views.ViewReferenceAbsoluteURL"
+ type="zope.publisher.interfaces.http.IHTTPRequest"
+ permission="zope.Public"
+ provides="zope.traversing.browser.interfaces.IAbsoluteURL"
+ />
+
+<browser:page
+ for="..interfaces.IViewReference"
+ name="absolute_url"
+ class=".views.ViewReferenceAbsoluteURL"
+ permission="zope.Public"
+ allowed_interface="zope.traversing.browser.interfaces.IAbsoluteURL"
+ />
+
+ <resourceLibrary name="z3c.reference"
+ require="z3c.javascript.jquery">
+ <directory source="resources" include="z3c_reference.js"/>
+ </resourceLibrary>
+
+ <browser:resourceDirectory
+ name="z3c.reference.resources"
+ directory="resources"
+ />
+
+</configure>
\ No newline at end of file
Property changes on: z3c.reference/trunk/src/z3c/reference/browser/configure.zcml
___________________________________________________________________
Name: svn:eol-style
+ native
Added: z3c.reference/trunk/src/z3c/reference/browser/resources/noimage.jpg
===================================================================
(Binary files differ)
Property changes on: z3c.reference/trunk/src/z3c/reference/browser/resources/noimage.jpg
___________________________________________________________________
Name: svn:mime-type
+ image/jpeg
Added: z3c.reference/trunk/src/z3c/reference/browser/resources/z3c_reference.js
===================================================================
--- z3c.reference/trunk/src/z3c/reference/browser/resources/z3c_reference.js 2007-02-01 17:06:08 UTC (rev 72302)
+++ z3c.reference/trunk/src/z3c/reference/browser/resources/z3c_reference.js 2007-02-01 18:02:22 UTC (rev 72303)
@@ -0,0 +1,112 @@
+var Z3C_REFERENCE = true;
+var z3cReferenceCurrent = null;
+var z3cExplorerWindow = null;
+
+function z3cReferenceLinkChoosen(current,ref){
+ var current = $(current);
+ var ref = $(ref);
+ var href = ref.attr('href');
+ var title = ref.attr('title');
+ current.attr('href',href);
+ if(title!=null){
+ current.attr('title',title);
+ current.html(title);
+ }
+
+ // remove the ".tag" suffix
+ var fieldId = current[0].id.substr(0,current[0].id.length-4);
+
+ // cant get element with jquery sorry
+ var f = document.getElementById(fieldId);
+ f.value = href;
+}
+
+function z3cReferenceImageChoosen(current,ref){
+ var current = $(current);
+ var ref = $(ref);
+ var src = ref.attr('src');
+ var title = ref.attr('title');
+ current.attr('src',src);
+ if(title!=null){
+ current.attr('title',title);
+ }
+
+ // remove the ".tag" suffix
+ var fieldId = current[0].id.substr(0,current[0].id.length-4);
+
+ // cant get element with jquery sorry
+ var f = document.getElementById(fieldId);
+ f.value=src;
+}
+
+
+function z3cReferenceChoosen(ref){
+ if (!z3cReferenceCurrent){
+ alert("No Object selected!");
+ }
+ var current = z3cReferenceCurrent;
+ z3cReferenceCurrent=null;
+ switch (current.tagName) {
+ case "A":
+ return z3cReferenceLinkChoosen(current,ref);
+ case "IMG":
+ return z3cReferenceImageChoosen(current,ref);
+ }
+ alert("Wrong Object returned: " + ref);
+}
+
+function z3cReferenceOpenWindow(theURL) {
+ var str = "left=0,screenX=0,top=0,screenY=0";
+
+ if (window.screen) {
+ var ah = screen.availHeight;
+ var aw = screen.availWidth;
+ str += ",height=" + ah;
+ str += ",innerHeight=" + ah;
+ str += ",width=" + aw;
+ str += ",innerWidth=" + aw;
+ }
+ else {
+ str += ",resizable"; // so the user can resize the window manually
+ }
+
+ return window.open(theURL, 'explorer', str);
+}
+
+
+
+function z3cReferenceLinkHandler(obj) {
+ var linkElm, imageElm, inst;
+ var explorerLink = obj.getAttribute('z3c:explorerLink');
+ switch (obj.tagName) {
+ case "A":
+ return z3cReferenceOpenWindow
+ (explorerLink);
+ case "IMG":
+ var url = obj.getAttribute('src');
+ // todo check image on placeholder
+ if (url=='/@@/z3c.reference.resources/noimage.jpg' || url=='undefined' || url=='Deleted Object') {
+
+ return z3cReferenceOpenWindow
+ ('@@explorer.html?link=0');
+ } else {
+ var url = url.split('/processed?');
+ var win = z3cReferenceOpenWindow
+ (url[0]+'/@@imgedit.html?'+url[1]);
+ }
+ }
+ return false; // Pass to next handler in chain
+}
+
+
+function z3cReferenceOnClick(obj,command){
+ if (z3cExplorerWindow){
+ z3cExplorerWindow.close();
+ z3cExplorerWindow=null;
+ z3cReferenceCurrent=null;
+ }
+ z3cExplorerWindow = z3cReferenceLinkHandler(obj);
+ z3cReferenceCurrent=obj;
+ return false;
+}
+
Added: z3c.reference/trunk/src/z3c/reference/browser/tests.py
===================================================================
--- z3c.reference/trunk/src/z3c/reference/browser/tests.py 2007-02-01 17:06:08 UTC (rev 72302)
+++ z3c.reference/trunk/src/z3c/reference/browser/tests.py 2007-02-01 18:02:22 UTC (rev 72303)
@@ -0,0 +1,52 @@
+import doctest
+import unittest
+from zope.testing.doctestunit import DocFileSuite, DocTestSuite
+from zope.app.testing import setup
+from zope import component
+from zope.dublincore.annotatableadapter import ZDCAnnotatableAdapter
+from zope.dublincore.interfaces import IWriteZopeDublinCore
+from zope.app.keyreference.testing import SimpleKeyReference
+from zope.traversing.testing import browserView
+from zope.traversing.browser.interfaces import IAbsoluteURL
+from z3c.reference.interfaces import IViewReference
+from zope.traversing.browser.absoluteurl import AbsoluteURL
+from zope.publisher.browser import BrowserPage
+
+from views import ViewReferenceAbsoluteURL
+
+class TestPage(BrowserPage):
+
+ def __call__(self):
+ return "testpage"
+
+def setUp(test):
+ test.globs['site'] = setup.placefulSetUp(True)
+ component.provideAdapter(SimpleKeyReference)
+ component.provideAdapter(ZDCAnnotatableAdapter,
+ provides=IWriteZopeDublinCore)
+ browserView(IViewReference, '', ViewReferenceAbsoluteURL,
+ providing=IAbsoluteURL)
+ browserView(None,'index.html',TestPage)
+
+def tearDown(test):
+ setup.placefulTearDown()
+
+
+def test_suite():
+
+ return unittest.TestSuite(
+ (
+ DocTestSuite('z3c.reference.browser.widget',
+ setUp=setUp,tearDown=tearDown,
+ optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS,
+ ),
+ DocTestSuite('z3c.reference.browser.views',
+ setUp=setUp,tearDown=tearDown,
+ optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS,
+ ),
+ ))
+
+
+if __name__ == '__main__':
+ unittest.main(defaultTest='test_suite')
+
Property changes on: z3c.reference/trunk/src/z3c/reference/browser/tests.py
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added: z3c.reference/trunk/src/z3c/reference/browser/views.py
===================================================================
--- z3c.reference/trunk/src/z3c/reference/browser/views.py 2007-02-01 17:06:08 UTC (rev 72302)
+++ z3c.reference/trunk/src/z3c/reference/browser/views.py 2007-02-01 18:02:22 UTC (rev 72303)
@@ -0,0 +1,69 @@
+from zope.traversing.browser.absoluteurl import absoluteURL,AbsoluteURL
+import urllib
+from zope.traversing.browser.interfaces import IAbsoluteURL
+from zope import component
+from zope.traversing.interfaces import IContainmentRoot
+noImage = '/@@/z3c.reference.resources/noimage.jpg'
+
+class ViewReferenceAbsoluteURL(AbsoluteURL):
+
+ """adapts a view reference to IAbsoluteURL
+
+ >>> from z3c.reference.reference import ViewReference
+ >>> from zope.publisher.browser import TestRequest
+ >>> ref = ViewReference(view=u'http://www.zope.org/')
+ >>> request = TestRequest()
+ >>> view = ViewReferenceAbsoluteURL(ref,request)
+ >>> view
+ <z3c.reference.browser.views.ViewReferenceAbsoluteURL ...>
+ >>> view()
+ 'http://www.zope.org/'
+
+ >>> ref = ViewReference(target=site)
+ >>> view = ViewReferenceAbsoluteURL(ref,request)
+ >>> view()
+ 'http://127.0.0.1'
+
+ >>> ref = ViewReference(target=site,view=u'index.html?x=1&y=2')
+ >>> view = ViewReferenceAbsoluteURL(ref,request)
+ >>> view()
+ 'http://127.0.0.1/index.html?x=1&y=2'
+
+ """
+
+ def __init__(self,context,request):
+ self.context = context.target
+ self.view = context.view
+ self.request = request
+
+ def __str__(self):
+ if self.context is not None:
+ if self.context.__name__ or IContainmentRoot.providedBy(
+ self.context):
+ view = component.getMultiAdapter((self.context, self.request),
+ IAbsoluteURL)
+ try:
+ url = view()
+ except TypeError:
+ return noImage
+ if self.view is not None:
+ url = '%s/%s' % (url,self.view.encode('utf8'))
+ return url
+ else:
+ # the target ist lost TODO:
+ return noImage
+ elif self.view is not None:
+ return self.view.encode('utf8')
+
+ raise TypeError("Can't get absolute url of reference,"
+ "because there is no target or view "
+ "specified.")
+ __call__=__str__
+ def breadcrumbs(self):
+ if self.context is not None:
+ view = component.getMultiAdapter((self.context, self.request),
+ IAbsoluteURL)
+ return view.breadcrumbs()
+ raise TypeError("Can't get breadcrumbs of external reference")
+
+
Property changes on: z3c.reference/trunk/src/z3c/reference/browser/views.py
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added: z3c.reference/trunk/src/z3c/reference/browser/widget.py
===================================================================
--- z3c.reference/trunk/src/z3c/reference/browser/widget.py 2007-02-01 17:06:08 UTC (rev 72302)
+++ z3c.reference/trunk/src/z3c/reference/browser/widget.py 2007-02-01 18:02:22 UTC (rev 72303)
@@ -0,0 +1,266 @@
+from zope.app.form.browser.widget import SimpleInputWidget
+from zope.app.form.browser.textwidgets import TextWidget
+from z3c.reference.reference import ViewReference,ImageReference
+from zope.traversing.browser.absoluteurl import absoluteURL
+from zope.app.component import hooks
+from zope import traversing
+from zope.app.form.browser.widget import renderElement
+from zc import resourcelibrary
+from xml.dom.minidom import parse, parseString
+from zope.traversing.interfaces import TraversalError
+import urlparse, cgi, urllib
+from zope.cachedescriptors.property import Lazy
+
+
+untitled = u'No Link defined'
+undefined = u'Undefined'
+
+emptyViewReference = ViewReference(view=u'#')
+emptyImageReference = ImageReference(
+ view=u'/@@/z3c.reference.resources/noimage.jpg')
+
+def getText(nodelist):
+ rc = ""
+ for node in nodelist:
+ if node.nodeType == node.TEXT_NODE:
+ rc = rc + node.data
+ return rc
+
+
+def referenceFromURL(url,request,factory):
+
+ site=hooks.getSite()
+ siteURL = absoluteURL(site,request)
+ if not url.startswith(siteURL):
+ return ViewReference(view=url)
+ url = url[len(siteURL)+1:]
+ scheme,location,path,query,fragment = urlparse.urlsplit(url)
+ tPath = map(lambda x: urllib.unquote(x.encode('utf-8')).decode('utf-8'),
+ path.split('/'))
+ # get the nearest traversable
+ views = []
+ while tPath:
+ try:
+ target = traversing.api.traverse(site,tPath)
+ break
+ except TraversalError:
+ views.append(tPath.pop())
+
+ query = query and u'?' + query or u''
+ if views:
+ views.reverse()
+ views = u'/'.join(views)
+ view = views + query
+ else:
+ view = query or None
+ return factory(target=target,view=view)
+
+
+class ViewReferenceWidget(TextWidget):
+
+ """renders an "a" tag with the title and href attributes
+
+ if no target
+
+ >>> from zope.publisher.browser import TestRequest
+ >>> from z3c.reference.schema import ViewReferenceField
+ >>> from zope.app.folder import Folder
+ >>> f = Folder()
+ >>> site['folder'] = f
+ >>> field = ViewReferenceField(title=(u'Title of Field'),
+ ... __name__='ref')
+ >>> request = TestRequest()
+ >>> w = ViewReferenceWidget(field,request)
+ >>> w()
+ u'<input class="hiddenType" id="field.ref" .../>...</a>'
+ >>> request.form['field.ref']=u'http://127.0.0.1/folder'
+ >>> res = w.getInputValue()
+ >>> res
+ <z3c.reference.reference.ViewReference object at ...>
+
+ >>> res.view is None
+ True
+
+ >>> res.target is f
+ True
+
+ >>> request.form['field.ref']=u'http://127.0.0.1/folder/index.html'
+ >>> res = w.getInputValue()
+ >>> res.target is f
+ True
+ >>> res.view
+ u'index.html'
+ >>> absoluteURL(res,request)
+ 'http://127.0.0.1/folder/index.html'
+
+ >>> request.form['field.ref']=u'http://127.0.0.1/folder/index.html?x=1&y=2'
+ >>> res = w.getInputValue()
+ >>> res.target is f
+ True
+ >>> res.view
+ u'index.html?x=1&y=2'
+ >>> absoluteURL(res,request)
+ 'http://127.0.0.1/folder/index.html?x=1&y=2'
+
+ >>> print w()
+ <input class="hiddenType" .../><a href="..." ...</a>
+
+ >>> ff = Folder()
+ >>> f[u'second'] = ff
+ >>> request.form['field.ref']=u'http://127.0.0.1/folder/second/index.html?x=1&y=2'
+ >>> res = w.getInputValue()
+ >>> res.target is ff
+ True
+
+
+ """
+
+ tag = u'input'
+ type = u'hidden'
+ cssClass = u''
+ extra = u'z3c:explorerLink="@@explorer.html?link=1"'
+ refTag = u'a'
+ refTagOnClick="z3cReferenceOnClick(this); return false;"
+ _emptyReference = emptyViewReference
+
+
+ def __init__(self, *args):
+ resourcelibrary.need('z3c.reference')
+ super(ViewReferenceWidget, self).__init__(*args)
+
+
+ def __call__(self):
+ resourcelibrary.need('z3c.reference')
+ hidden = super(ViewReferenceWidget,self).__call__()
+ if self._renderedValueSet():
+ ref = self._data
+ else:
+ ref = self.context.default
+ if not ref:
+ try:
+ ref = self.context.get(self.context.context)
+ except:
+ ref = None
+ if ref is None:
+ ref = self._emptyReference
+ url = absoluteURL(ref,self.request)
+ if ref.target is not None:
+ contents = getattr(ref.target,'title',None) or \
+ ref.target.__name__
+ else:
+ contents = untitled
+ tag = renderElement(self.refTag,
+ href=url,
+ name=self.name,
+ id=self.name + '.tag',
+ title=contents,
+ onclick=self.refTagOnClick,
+ contents=contents,
+ style=self.style,
+ extra=self.extra)
+ return hidden + tag
+
+ def _getFormValue(self):
+ res = super(ViewReferenceWidget,self)._getFormValue()
+ return res
+
+ def _toFormValue(self, value):
+ if value == self.context.missing_value:
+ return self._missing
+ try:
+ url = absoluteURL(value,self.request)
+ except TypeError:
+ return self._missing
+ return url
+
+ def _toFieldValue(self, input):
+ if input == self._missing:
+ return self.context.missing_value
+ return referenceFromURL(input,self.request,
+ self._emptyReference.__class__)
+
+
+
+class ObjectReferenceWidget(ViewReferenceWidget):
+
+ @Lazy
+ def extra(self):
+ iface = self.context.refSchema
+ name = u'%s.%s' % (iface.__module__,iface.__name__)
+ return u'z3c:explorerLink="@@explorer.html?link=1&schema=%s"' % \
+ name
+
+
+class ImageReferenceWidget(ViewReferenceWidget):
+
+ """renders an "a" tag with the title and href attributes
+
+ if no target
+
+ >>> from zope.publisher.browser import TestRequest
+ >>> from z3c.reference.schema import ImageReferenceField
+ >>> from zope.app.folder import Folder
+ >>> f = Folder()
+ >>> site['folder'] = f
+ >>> field = ImageReferenceField(title=(u'Title of Field'),
+ ... __name__='ref',size=(10,10))
+ >>> request = TestRequest()
+ >>> w = ImageReferenceWidget(field,request)
+ >>> print w()
+ <input .../><img ...height="10" id="field.ref.tag" .../>
+
+ """
+
+
+
+ refTag = u'img'
+ _emptyReference = emptyImageReference
+ extra = u''
+
+
+ def __call__(self):
+ hidden = super(ViewReferenceWidget,self).__call__()
+ if self._renderedValueSet():
+ ref = self._data
+ else:
+ ref = self.context.default
+ if not ref:
+ try:
+ ref = self.context.get(self.context.context)
+ except:
+ ref = None
+ if ref is None:
+ ref = self._emptyReference
+ url = absoluteURL(ref, self.request)
+ else:
+ # return uid instead of absoluteURL cuz of umlaut problems
+ url = str(absoluteURL(ref.target,self.request)) + '/' + ref.view
+ if ref.target is not None:
+ title = getattr(ref.target,'title',None) or \
+ ref.target.__name__
+ else:
+ title = untitled
+ width,height = self.context.size
+ kwords = dict(src=url,
+ name=self.name,
+ id=self.name + '.tag',
+ title=title,
+ alt=title,
+ width=width,
+ height=height,
+ onclick=self.refTagOnClick,
+ style=self.style,
+ extra=self.extra)
+ tag = renderElement(self.refTag,**kwords)
+ return hidden + tag
+
+
+ def _toFormValue(self, value):
+ if value == self.context.missing_value:
+ return self._missing
+ try:
+ # return uid instead of absoluteURL cuz of umlaut problems
+ url = str(absoluteURL(value.target,self.request)) + '/' + value.view
+ except TypeError:
+ return self._missing
+ return url
Property changes on: z3c.reference/trunk/src/z3c/reference/browser/widget.py
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added: z3c.reference/trunk/src/z3c/reference/configure.zcml
===================================================================
--- z3c.reference/trunk/src/z3c/reference/configure.zcml 2007-02-01 17:06:08 UTC (rev 72302)
+++ z3c.reference/trunk/src/z3c/reference/configure.zcml 2007-02-01 18:02:22 UTC (rev 72303)
@@ -0,0 +1,16 @@
+<configure
+ xmlns="http://namespaces.zope.org/zope"
+ i18n_domain="z3c.reference"
+ >
+
+ <class class="z3c.reference.reference.ViewReference">
+ <allow interface=".interfaces.IViewReference"/>
+ </class>
+
+ <class class="z3c.reference.reference.ImageReference">
+ <allow interface=".interfaces.IImageReference"/>
+ </class>
+
+ <include package=".browser"/>
+
+</configure>
\ No newline at end of file
Property changes on: z3c.reference/trunk/src/z3c/reference/configure.zcml
___________________________________________________________________
Name: svn:eol-style
+ native
Added: z3c.reference/trunk/src/z3c/reference/interfaces.py
===================================================================
--- z3c.reference/trunk/src/z3c/reference/interfaces.py 2007-02-01 17:06:08 UTC (rev 72302)
+++ z3c.reference/trunk/src/z3c/reference/interfaces.py 2007-02-01 18:02:22 UTC (rev 72303)
@@ -0,0 +1,39 @@
+from zope import interface,schema
+from zope.location.interfaces import ILocation
+from zope.app.file.interfaces import IImage
+from zope.interface.interfaces import IInterface
+
+class IViewReference(interface.Interface):
+
+ """a reference to a view of an object, by storing the name of the
+ view. If the target is None, the view name is supposed to be an
+ absolute url to an external target"""
+
+ target = schema.Object(ILocation,required=False,
+ title=u'Target Object')
+ view = schema.TextLine(required=False,title=u'View')
+
+class IViewReferenceField(schema.interfaces.IObject):
+ """a view reference field"""
+
+class IImageReference(IViewReference):
+
+ """a reference to an image with optional size constraints"""
+ target = schema.Object(IImage,required=False,
+ title=u'Target Image')
+
+class IImageReferenceField(schema.interfaces.IObject):
+
+ """an image reference field"""
+
+ size = schema.Tuple(title=u'Forced Size',
+ value_type=schema.Int(),
+ required=True,
+ min_length=2,max_length=2)
+
+class IObjectReferenceField(IViewReferenceField):
+
+ """a schema based reference field"""
+
+ refSchema = schema.Object(IInterface,
+ title=u'Reference Schema')
Property changes on: z3c.reference/trunk/src/z3c/reference/interfaces.py
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added: z3c.reference/trunk/src/z3c/reference/reference.py
===================================================================
--- z3c.reference/trunk/src/z3c/reference/reference.py 2007-02-01 17:06:08 UTC (rev 72302)
+++ z3c.reference/trunk/src/z3c/reference/reference.py 2007-02-01 18:02:22 UTC (rev 72303)
@@ -0,0 +1,52 @@
+from interfaces import *
+from persistent import Persistent
+from zope.schema.fieldproperty import FieldProperty
+from zope.cachedescriptors.property import readproperty
+from zope.dublincore.interfaces import IZopeDublinCore
+from zope.app.keyreference.interfaces import IKeyReference
+from zope import interface
+from zope.traversing.browser.absoluteurl import absoluteURL
+import urlparse,cgi
+
+class ViewReference(Persistent):
+
+ interface.implements(IViewReference)
+ _key = None
+ view = FieldProperty(IViewReference['view'])
+
+ def __init__(self,target=None,view=None):
+ if target is not None:
+ self.target = target
+ self.view = view
+
+ @apply
+ def target():
+ def fget(self):
+ if self._key is not None:
+ return self._key()
+ return None
+ def fset(self, value):
+ self._key = IKeyReference(value)
+ return property(**locals())
+
+ def __eq__(self,other):
+ if not other:
+ return False
+ if IViewReference.providedBy(other):
+ return (self.view == other.view) and \
+ (self.target is other.target)
+ return False
+
+ def __ne__(self,other):
+ if not other:
+ return True
+ if IViewReference.providedBy(other):
+ return (self.view != other.view) or \
+ (self.target != other.target)
+ return True
+
+
+class ImageReference(ViewReference):
+
+ interface.implements(IImageReference)
+
Property changes on: z3c.reference/trunk/src/z3c/reference/reference.py
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added: z3c.reference/trunk/src/z3c/reference/schema.py
===================================================================
--- z3c.reference/trunk/src/z3c/reference/schema.py 2007-02-01 17:06:08 UTC (rev 72302)
+++ z3c.reference/trunk/src/z3c/reference/schema.py 2007-02-01 18:02:22 UTC (rev 72303)
@@ -0,0 +1,27 @@
+from zope import schema,interface
+from interfaces import *
+
+class ViewReferenceField(schema.Object):
+ interface.implements(IViewReferenceField)
+
+ def __init__(self,**kw):
+ super(ViewReferenceField,self).__init__(IViewReference,
+ **kw)
+
+class ImageReferenceField(schema.Object):
+ interface.implements(IImageReferenceField)
+ size = schema.fieldproperty.FieldProperty(IImageReferenceField['size'])
+
+ def __init__(self,**kw):
+ self.size = kw.pop('size',None)
+ super(ImageReferenceField,self).__init__(IImageReference,
+ **kw)
+
+class ObjectReferenceField(ViewReferenceField):
+
+ interface.implements(IObjectReferenceField)
+
+ def __init__(self,refSchema,**kw):
+ self.refSchema = refSchema
+ super(ObjectReferenceField,self).__init__(**kw)
+
Property changes on: z3c.reference/trunk/src/z3c/reference/schema.py
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added: z3c.reference/trunk/src/z3c/reference/tests.py
===================================================================
--- z3c.reference/trunk/src/z3c/reference/tests.py 2007-02-01 17:06:08 UTC (rev 72302)
+++ z3c.reference/trunk/src/z3c/reference/tests.py 2007-02-01 18:02:22 UTC (rev 72303)
@@ -0,0 +1,32 @@
+import doctest
+import unittest
+from zope.testing.doctestunit import DocFileSuite, DocTestSuite
+from zope.app.testing import setup
+from zope import component
+from zope.dublincore.annotatableadapter import ZDCAnnotatableAdapter
+from zope.dublincore.interfaces import IWriteZopeDublinCore
+from zope.app.keyreference.testing import SimpleKeyReference
+def setUp(test):
+ setup.placefulSetUp()
+ component.provideAdapter(SimpleKeyReference)
+ component.provideAdapter(ZDCAnnotatableAdapter,
+ provides=IWriteZopeDublinCore)
+
+def tearDown(test):
+ setup.placefulTearDown()
+
+
+def test_suite():
+
+ return unittest.TestSuite(
+ (
+ DocFileSuite('README.txt',
+ setUp=setUp,tearDown=tearDown,
+ optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS,
+ ),
+ ))
+
+
+if __name__ == '__main__':
+ unittest.main(defaultTest='test_suite')
+
Property changes on: z3c.reference/trunk/src/z3c/reference/tests.py
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added: z3c.reference/trunk/src/z3c/reference/z3c.reference-configure.zcml
===================================================================
--- z3c.reference/trunk/src/z3c/reference/z3c.reference-configure.zcml 2007-02-01 17:06:08 UTC (rev 72302)
+++ z3c.reference/trunk/src/z3c/reference/z3c.reference-configure.zcml 2007-02-01 18:02:22 UTC (rev 72303)
@@ -0,0 +1 @@
+<include package="z3c.reference" />
\ No newline at end of file
Property changes on: z3c.reference/trunk/src/z3c/reference/z3c.reference-configure.zcml
___________________________________________________________________
Name: svn:eol-style
+ native
More information about the Checkins
mailing list