tagging

changeset 1:4ab9b65273b0

fixed double-tagging bug, made metaclass reload-safe
* before, tag_add didn't properly check whether the tag was already used
* if tagging.py is reloaded, TagCNode is re-created.
author Thomas Jollans <thomas@jollans.com>
date Mon Aug 20 18:00:54 2007 +0200 (2007-08-20)
parents 51dfdd611e50
children e660c8b40367
files modules/tagging.py
line diff
     1.1 --- a/modules/tagging.py	Wed Aug 15 02:34:38 2007 +0200
     1.2 +++ b/modules/tagging.py	Mon Aug 20 18:00:54 2007 +0200
     1.3 @@ -20,6 +20,9 @@
     1.4  simple generic tagging interface for content.py-using modules
     1.5  """
     1.6  
     1.7 +from random import randint
     1.8 +TAG_NODE_TYPE_NAME = 'TagCNode%d' % randint(0, 2**15)
     1.9 +
    1.10  from types import MethodType
    1.11  
    1.12  def get_tag_id(cnode, tag):
    1.13 @@ -74,7 +77,7 @@
    1.14  def tag_add(self, tag, magic=False):
    1.15      if isinstance(tag, type(self)):
    1.16          if 'tags' not in self: self['tags'] = []
    1.17 -        if tag.get_id not in self['tags']: self['tags'].append(tag.get_id())
    1.18 +        if tag.get_id() not in self['tags']: self['tags'].append(tag.get_id())
    1.19          tag.link(self)
    1.20  
    1.21      elif isinstance(tag, tuple):
    1.22 @@ -87,7 +90,7 @@
    1.23              tagnode['type'] = type_
    1.24              tagnode['tagname'] = tag[1]
    1.25              if magic:
    1.26 -                tagnode['addr'] = tag[1]
    1.27 +                tagnode['addr'] = self._tag_req['content'].mk_addr(tag[1])
    1.28          else:
    1.29              if len(nodes) > 1:
    1.30                  self._tag_req.log('warning: multiple tags %s' % tag)
    1.31 @@ -116,7 +119,7 @@
    1.32          cnode_class.__init__(self, *args, **kwargs)
    1.33          self._tag_req = req
    1.34  
    1.35 -    newcnode = type('TagCNode', (cnode_class,), {})
    1.36 +    newcnode = type(TAG_NODE_TYPE_NAME, (cnode_class,), {})
    1.37      newcnode.__init__   = MethodType(init_,      None, newcnode)
    1.38      newcnode.tag_getall = MethodType(tag_getall, None, newcnode)
    1.39      newcnode.tag_add    = MethodType(tag_add,    None, newcnode)
    1.40 @@ -156,8 +159,14 @@
    1.41            - C{tag_getall}
    1.42          """
    1.43          module = self._r.import_mdl(self._r['content'].__module__)
    1.44 -        if 'TagCNode' in str(module.CNode): return 
    1.45 -        module.CNode = withtags(self._r, module.CNode)
    1.46 +        if 'TagCNode' in str(module.CNode):
    1.47 +            # if this module is reloaded, I want to re-hack
    1.48 +            if TAG_NODE_TYPE_NAME in str(module.CNode):
    1.49 +                return
    1.50 +            else:
    1.51 +                module.CNode = withtags(self._r, module.CNode.__bases__[0])
    1.52 +        else:
    1.53 +            module.CNode = withtags(self._r, module.CNode)
    1.54          self.hacked = True
    1.55  
    1.56      def add_to_template(self, tpl):