[Checkins] SVN: zc.sourcefactory/trunk/ - added an extended the term policy for contextual sources to allow access to

Christian Theune ct at gocept.com
Mon May 7 06:45:42 EDT 2007


Log message for revision 75607:
   - added an extended the term policy for contextual sources to allow access to
     the context in getTitle and createTerm
  

Changed:
  A   zc.sourcefactory/trunk/CHANGES.txt
  U   zc.sourcefactory/trunk/src/zc/sourcefactory/browser/README.txt
  U   zc.sourcefactory/trunk/src/zc/sourcefactory/browser/configure.zcml
  U   zc.sourcefactory/trunk/src/zc/sourcefactory/browser/source.py
  U   zc.sourcefactory/trunk/src/zc/sourcefactory/configure-z2.zcml
  U   zc.sourcefactory/trunk/src/zc/sourcefactory/interfaces.py
  U   zc.sourcefactory/trunk/src/zc/sourcefactory/policies.py

-=-
Added: zc.sourcefactory/trunk/CHANGES.txt
===================================================================
--- zc.sourcefactory/trunk/CHANGES.txt	2007-05-07 08:11:06 UTC (rev 75606)
+++ zc.sourcefactory/trunk/CHANGES.txt	2007-05-07 10:45:41 UTC (rev 75607)
@@ -0,0 +1,11 @@
+=======
+Changes
+=======
+
+
+0.2
+===
+
+    - Added a contextual term policy interface that allow createTerm and
+      getTitle to access the context for contextual sources.
+


Property changes on: zc.sourcefactory/trunk/CHANGES.txt
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: zc.sourcefactory/trunk/src/zc/sourcefactory/browser/README.txt
===================================================================
--- zc.sourcefactory/trunk/src/zc/sourcefactory/browser/README.txt	2007-05-07 08:11:06 UTC (rev 75606)
+++ zc.sourcefactory/trunk/src/zc/sourcefactory/browser/README.txt	2007-05-07 10:45:41 UTC (rev 75607)
@@ -9,7 +9,7 @@
 Simple use
 ==========
 
-Let's start with a simple source factory:
+Let's start with a simple source factory::
 
   >>> import zc.sourcefactory.basic
   >>> class DemoSource(zc.sourcefactory.basic.BasicSourceFactory):
@@ -19,7 +19,7 @@
   >>> list(source)
   ['a', 'b', 'c', 'd']
 
-We need a request first, then we can adapt the source to ITerms:
+We need a request first, then we can adapt the source to ITerms::
 
   >>> from zope.publisher.browser import TestRequest
   >>> import zope.app.form.browser.interfaces
@@ -30,7 +30,7 @@
   >>> terms
   <zc.sourcefactory.browser.source.FactoredTerms object at 0x...>
 
-For each value we get a factored term:
+For each value we get a factored term::
 
   >>> terms.getTerm('a')
   <zc.sourcefactory.browser.source.FactoredTerm object at 0x...>
@@ -41,7 +41,7 @@
   >>> terms.getTerm('d')
   <zc.sourcefactory.browser.source.FactoredTerm object at 0x...>
 
-Our terms are ITitledTokenizedTerm-compatible:
+Our terms are ITitledTokenizedTerm-compatible::
 
   >>> import zope.schema.interfaces
   >>> zope.schema.interfaces.ITitledTokenizedTerm.providedBy(
@@ -49,15 +49,13 @@
   True
 
 In the most simple case, the title of a term is the string representation of
-the object:
+the object::
 
-XXX this should be unicode!
-
-  >>> terms.getTerm('a').title
+  >>> terms.getTerm('a').title    # XXX This should return unicode.
   'a'
 
 If an adapter from the value to IDCDescriptiveProperties exists, the title
-will be retrieved from this adapter:
+will be retrieved from this adapter::
 
   >>> import persistent
   >>> class MyObject(persistent.Persistent):
@@ -79,7 +77,7 @@
 
 Instead of relying on string representation or IDCDescriptiveProperties
 adapters you can specify the `getTitle` method on the source factory to
-determine the title for a value:
+determine the title for a value::
 
   >>> class DemoSourceWithTitles(DemoSource):
   ...     def getTitle(self, value):
@@ -101,7 +99,7 @@
 
 Instead of relying on default adapters to generate tokens for your values, you
 can override the `getToken` method on the source factory to determine the
-token for a value:
+token for a value::
 
   >>> class DemoObjectWithToken(object):
   ...     token = None
@@ -126,7 +124,7 @@
   >>> terms3.getTerm(o2).token
   'two'
 
-Looking up by the custom tokens works as well:
+Looking up by the custom tokens works as well::
 
   >>> terms3.getValue("one") is o1
   True
@@ -143,9 +141,45 @@
   XXX to come
 
 
-Iterfaces
-=========
+Contextual sources
+==================
 
+Let's start with an object that we can use as the context::
+
+  >>> zip_to_city = {'06112': 'Halle',
+  ...                '06844': 'Dessau'}
+  >>> import zc.sourcefactory.contextual
+  >>> class DemoContextualSource(
+  ...     zc.sourcefactory.contextual.BasicContextualSourceFactory):
+  ...     def getValues(self, context):
+  ...         return context.keys()
+  ...     def getTitle(self, context, value):
+  ...         return context[value]
+  >>> source = DemoContextualSource()(zip_to_city)
+  >>> sorted(list(source))
+  ['06112', '06844']
+
+Let's look at the terms::
+
+  >>> terms = zope.component.getMultiAdapter(
+  ...     (source, request), zope.app.form.browser.interfaces.ITerms)
+  >>> terms
+  <zc.sourcefactory.browser.source.FactoredContextualTerms object at 0x...>
+
+For each value we get a factored term with the right title from the context::
+
+  >>> terms.getTerm('06112')
+  <zc.sourcefactory.browser.source.FactoredTerm object at 0x...>
+  >>> terms.getTerm('06112').title
+  'Halle'
+  >>> terms.getTerm('06844')
+  <zc.sourcefactory.browser.source.FactoredTerm object at 0x...>
+  >>> terms.getTerm('06844').title
+  'Dessau'
+
+Interfaces
+==========
+
 Both the FactoredSource and FactoredContextualSource have associated
 interfaces.
 

Modified: zc.sourcefactory/trunk/src/zc/sourcefactory/browser/configure.zcml
===================================================================
--- zc.sourcefactory/trunk/src/zc/sourcefactory/browser/configure.zcml	2007-05-07 08:11:06 UTC (rev 75606)
+++ zc.sourcefactory/trunk/src/zc/sourcefactory/browser/configure.zcml	2007-05-07 10:45:41 UTC (rev 75607)
@@ -5,6 +5,8 @@
     i18n_domain="zc.sourcefactory">
 
   <adapter factory=".source.FactoredTerms"/>
+  <adapter factory=".source.FactoredContextualTerms"/>
+
   <adapter factory=".mapping.MappedTerms"/>
 
   <adapter factory=".token.fromString"/>

Modified: zc.sourcefactory/trunk/src/zc/sourcefactory/browser/source.py
===================================================================
--- zc.sourcefactory/trunk/src/zc/sourcefactory/browser/source.py	2007-05-07 08:11:06 UTC (rev 75606)
+++ zc.sourcefactory/trunk/src/zc/sourcefactory/browser/source.py	2007-05-07 10:45:41 UTC (rev 75607)
@@ -52,6 +52,23 @@
         return self.source.factory.getValue(self.source, token)
 
 
+class FactoredContextualTerms(FactoredTerms):
+    """A terms implementation that knows þwo to handle a source that was
+    created through a contextual source factory.
+    """
+
+    zope.component.adapts(
+        zc.sourcefactory.source.FactoredContextualSource,
+        zope.publisher.interfaces.browser.IBrowserRequest)
+
+    def getTerm(self, value):
+        title = self.source.factory.getTitle(self.source.context, value)
+        token = self.source.factory.getToken(value)
+        return self.source.factory.createTerm(
+            self.source.context, self.source, value, title, token,
+            self.request)
+
+
 class FactoredTerm(object):
     """A title tokenized term."""
 

Modified: zc.sourcefactory/trunk/src/zc/sourcefactory/configure-z2.zcml
===================================================================
--- zc.sourcefactory/trunk/src/zc/sourcefactory/configure-z2.zcml	2007-05-07 08:11:06 UTC (rev 75606)
+++ zc.sourcefactory/trunk/src/zc/sourcefactory/configure-z2.zcml	2007-05-07 10:45:41 UTC (rev 75607)
@@ -23,3 +23,28 @@
   <include package=".browser"/>
 
 </configure>
+<?xml version="1.0" encoding="utf-8"?>
+<configure
+    xmlns="http://namespaces.zope.org/zope"
+    >
+  <!-- Configuration for usage with Zope 2/Five -->
+
+  <class class=".source.FactoredSource">
+    <require
+        interface=".interfaces.IFactoredSource"
+        permission="zope2.View"
+        />
+  </class>
+
+  <class class=".source.FactoredContextualSource">
+    <require
+        interface=".interfaces.IContextualSource"
+        permission="zope2.View"
+        />
+  </class>
+
+  <adapter factory=".adapters.getSourceQueriables"/>
+
+  <include package=".browser"/>
+
+</configure>

Modified: zc.sourcefactory/trunk/src/zc/sourcefactory/interfaces.py
===================================================================
--- zc.sourcefactory/trunk/src/zc/sourcefactory/interfaces.py	2007-05-07 08:11:06 UTC (rev 75606)
+++ zc.sourcefactory/trunk/src/zc/sourcefactory/interfaces.py	2007-05-07 10:45:41 UTC (rev 75607)
@@ -75,6 +75,26 @@
         """
 
 
+class IContextualTermPolicy(zope.interface.Interface):
+    """The contextual term policy creates terms and provides data for terms.
+
+    It allows access to the context.
+
+    """
+
+    def createTerm(context, source, value, title, token, request):
+        """Create and return a term object."""
+
+    def getTitle(context, value):
+        """Return a title for the value.
+
+        The return value should not be localized; that is the
+        responsibility of the user.  The title may be an
+        internationalized message value.
+
+        """
+
+
 class IValuePolicy(zope.interface.Interface):
     """The value policy retrieves and filters values for a source."""
 
@@ -104,5 +124,5 @@
 
 
 class IContextualSourcePolicy(
-    ITokenPolicy, ITermPolicy, IContextualValuePolicy):
+    ITokenPolicy, IContextualTermPolicy, IContextualValuePolicy):
     pass

Modified: zc.sourcefactory/trunk/src/zc/sourcefactory/policies.py
===================================================================
--- zc.sourcefactory/trunk/src/zc/sourcefactory/policies.py	2007-05-07 08:11:06 UTC (rev 75606)
+++ zc.sourcefactory/trunk/src/zc/sourcefactory/policies.py	2007-05-07 10:45:41 UTC (rev 75607)
@@ -59,6 +59,24 @@
         return title
 
 
+class BasicContextualTermPolicy(BasicTermPolicy):
+    """A basic contextual term policy.
+
+    All methods are deferred to the BasicTermPolicy by removing the context
+    argument.
+
+    """
+
+    zope.interface.implements(zc.sourcefactory.interfaces.ITermPolicy)
+
+    def createTerm(self, context, source, value, title, token, request):
+        return super(BasicContextualTermPolicy, self).createTerm(
+            source, value, title, token, request)
+
+    def getTitle(self, context, value):
+        return super(BasicContextualTermPolicy, self).getTitle(value)
+
+
 # Token policies
 
 class BasicTokenPolicy(object):
@@ -146,7 +164,7 @@
 
 
 class BasicContextualSourcePolicy(
-    BasicContextualValuePolicy, BasicTokenPolicy, BasicTermPolicy):
+    BasicContextualValuePolicy, BasicTokenPolicy, BasicContextualTermPolicy):
     pass
 
 



More information about the Checkins mailing list