[Checkins] SVN: lovely.tag/trunk/src/lovely/tag/ implemented
IUserTagging adapter for easy property access to tags
Bernd Dorn
bernd.dorn at fhv.at
Tue Aug 15 11:11:36 EDT 2006
Log message for revision 69509:
implemented IUserTagging adapter for easy property access to tags
Changed:
U lovely.tag/trunk/src/lovely/tag/README.txt
U lovely.tag/trunk/src/lovely/tag/__init__.py
U lovely.tag/trunk/src/lovely/tag/configure.zcml
U lovely.tag/trunk/src/lovely/tag/interfaces.py
U lovely.tag/trunk/src/lovely/tag/tagging.py
-=-
Modified: lovely.tag/trunk/src/lovely/tag/README.txt
===================================================================
--- lovely.tag/trunk/src/lovely/tag/README.txt 2006-08-15 13:18:24 UTC (rev 69508)
+++ lovely.tag/trunk/src/lovely/tag/README.txt 2006-08-15 15:11:36 UTC (rev 69509)
@@ -291,7 +291,36 @@
>>> sorted(tagging.getUsers(tags=(u'USA',)))
[u'jodok', u'srichter']
+IUserTagging
+------------
+There is also an adapter for ITaggable objects which provides a simple
+tag attribute which accepts a list of tags defined for the ITaggable
+by the current principal.
+
+ >>> zope.component.provideAdapter(tag.UserTagging)
+ >>> userTagging = tag.interfaces.IUserTagging(image)
+ >>> userTagging.tags
+ Traceback (most recent call last):
+ ...
+ ValueError: User not found
+
+We get a ValueError because we have no interaction in this test, and
+therefore the implementation cannot find the principal. We have to
+create a principal and a participation.
+
+ >>> from zope.security.testing import Principal, Participation
+ >>> from zope.security import management
+ >>> p = Principal(u'srichter')
+ >>> participation = Participation(p)
+ >>> management.endInteraction()
+ >>> management.newInteraction(participation)
+ >>> sorted(userTagging.tags)
+ [u'USA', u'home']
+ >>> userTagging.tags = [u'zope3', u'guru']
+ >>> sorted(userTagging.tags)
+ [u'guru', u'zope3']
+
Tag Clouds
----------
@@ -310,14 +339,13 @@
>>> engine.update(2, u'michael', [u'lovely', u'USA'])
>>> engine.update(1, u'jodok', [u'USA',])
-
The most common use-case is to generate a global tag cloud.
>>> sorted(engine.getCloud())
- [(u'Austria', 2), (u'Bizau', 1), (u'USA', 4), (u'austria', 1), (u'home', 1),
- (u'lovely', 2), (u'personal', 1), (u'vacation', 1), (u'work', 1)]
+ [(u'Austria', 2), (u'Bizau', 1), (u'USA', 3), (u'austria', 1),
+ (u'guru', 1), (u'lovely', 2), (u'personal', 1), (u'vacation', 1),
+ (u'work', 1), (u'zope3', 1)]
-
Of course you can generate clouds on item basis. You can't pass a tuple of
items, only a single one is allowed:
@@ -327,7 +355,7 @@
The same applies to queries by user:
>>> sorted(engine.getCloud(user=u'srichter'))
- [(u'USA', 1), (u'home', 1)]
+ [(u'guru', 1), (u'zope3', 1)]
It makes no sense to combine user and item. This will never be a cloud.
@@ -355,7 +383,7 @@
>>> engine.update(4, u'jodok', [u'lovely', u'dornbirn', u'personal'])
>>> sorted(engine.getRelatedTags(u'austria', degree=3))
- [u'Austria', u'USA', u'dornbirn', u'home', u'lovely', u'personal',
+ [u'Austria', u'USA', u'dornbirn', u'lovely', u'personal',
u'vacation', u'work']
Frequency Of Tags
@@ -364,10 +392,11 @@
If we have a list of tags we can ask for the frequencies of the tags.
>>> sorted(engine.getFrequency([u'Austria', u'USA']))
- [(u'Austria', 2), (u'USA', 4)]
+ [(u'Austria', 2), (u'USA', 3)]
We get a frequency of 0 if we ask for a tag which is not in the engine.
>>> sorted(engine.getFrequency([u'Austria', u'jukart', u'USA']))
- [(u'Austria', 2), (u'USA', 4), (u'jukart', 0)]
+ [(u'Austria', 2), (u'USA', 3), (u'jukart', 0)]
+
Modified: lovely.tag/trunk/src/lovely/tag/__init__.py
===================================================================
--- lovely.tag/trunk/src/lovely/tag/__init__.py 2006-08-15 13:18:24 UTC (rev 69508)
+++ lovely.tag/trunk/src/lovely/tag/__init__.py 2006-08-15 15:11:36 UTC (rev 69509)
@@ -3,7 +3,7 @@
import zope.i18nmessageid
from engine import TaggingEngine
-from tagging import Tagging
+from tagging import Tagging, UserTagging
_ = zope.i18nmessageid.MessageFactory('feed')
Modified: lovely.tag/trunk/src/lovely/tag/configure.zcml
===================================================================
--- lovely.tag/trunk/src/lovely/tag/configure.zcml 2006-08-15 13:18:24 UTC (rev 69508)
+++ lovely.tag/trunk/src/lovely/tag/configure.zcml 2006-08-15 15:11:36 UTC (rev 69509)
@@ -51,4 +51,24 @@
locate="True"
/>
+
+ <!-- UserTagging adapter for taggable objects. -->
+
+ <class class=".tagging.UserTagging">
+ <require
+ permission="lovely.tag.UpdateTag"
+ set_schema=".interfaces.IUserTagging"
+ />
+ <require
+ permission="lovely.tag.AccessTag"
+ interface=".interfaces.IUserTagging"
+ />
+ </class>
+
+ <adapter
+ factory=".tagging.UserTagging"
+ trusted="True"
+ locate="True"
+ />
+
</configure>
Modified: lovely.tag/trunk/src/lovely/tag/interfaces.py
===================================================================
--- lovely.tag/trunk/src/lovely/tag/interfaces.py 2006-08-15 13:18:24 UTC (rev 69508)
+++ lovely.tag/trunk/src/lovely/tag/interfaces.py 2006-08-15 15:11:36 UTC (rev 69509)
@@ -126,3 +126,12 @@
See ``ITaggingEngine.getUsers()`` for more information.
"""
+
+class IUserTagging(zope.interface.Interface):
+
+ """Provides easy tagging of objects based on the current
+ principal"""
+
+ tags = zope.schema.Set(title=u'Tags',
+ description=u'Tags for the current User')
+
Modified: lovely.tag/trunk/src/lovely/tag/tagging.py
===================================================================
--- lovely.tag/trunk/src/lovely/tag/tagging.py 2006-08-15 13:18:24 UTC (rev 69508)
+++ lovely.tag/trunk/src/lovely/tag/tagging.py 2006-08-15 15:11:36 UTC (rev 69509)
@@ -19,7 +19,7 @@
import zope.component
import zope.interface
from zope.app import intid
-
+from zope.security.management import getInteraction
from lovely.tag import interfaces
class Tagging(object):
@@ -48,3 +48,35 @@
def getUsers(self, tags=None):
"""See interfaces.ITagging"""
return self._engine.getUsers(items=(self._id,), tags=tags)
+
+class UserTagging(object):
+
+ zope.interface.implements(interfaces.IUserTagging)
+ zope.component.adapts(interfaces.ITaggable)
+
+ def __init__(self, context):
+ self.context = context
+ ids = zope.component.getUtility(intid.interfaces.IIntIds)
+ self._id = ids.queryId(self.context)
+ if self._id is None:
+ ids.register(self.context)
+ self._id = ids.getId(self.context)
+ self._engine = zope.component.getUtility(interfaces.ITaggingEngine)
+
+ @property
+ def _pid(self):
+ participations = getInteraction().participations
+ if participations:
+ return participations[0].principal.id
+ else:
+ raise ValueError, "User not found"
+
+ @apply
+ def tags():
+ def fget(self):
+ return self._engine.getTags(items=(self._id,),
+ users=(self._pid,))
+ def fset(self, value):
+ return self._engine.update(self._id, self._pid, value)
+ return property(**locals())
+
More information about the Checkins
mailing list