Commits

Anonymous committed 5b79cbe Draft

#492, null neighborhood for local resources, patch t492_r1465566_null_neighborhood.diff applied (from Olemis)

git-svn-id: https://svn.apache.org/repos/asf/bloodhound/trunk@146678913f79535-47bb-0310-9956-ffa450edef68

Comments (0)

Files changed (1)

trac/trac/resource.py

 
     __slots__ = ('_realm', '_id')
 
+    @property
+    def is_null(self):
+        return (self._realm, self._id) == (None, None)
+
     def __repr__(self):
-        return '<Neighborhood %s:%s>' % (self._realm, self._id)
+        if self.is_null:
+            return '<Neighborhood (null)>'
+        else:
+            return '<Neighborhood %s:%s>' % (self._realm, self._id)
 
     def __eq__(self, other):
         return isinstance(other, Neighborhood) and \
         True
 
         >>> repr(Neighborhood(None))
-        'None'
+        '<Neighborhood (null)>'
         """
         realm = neighborhood_or_realm
         if isinstance(neighborhood_or_realm, Neighborhood):
                 realm = neighborhood_or_realm._realm
         elif id is False:
             id = None
-        if (realm, id) == (None, None):
-            return None
-        else:
-            neighborhood = super(Neighborhood, cls).__new__(cls)
-            neighborhood._realm = realm
-            neighborhood._id = id
-            return neighborhood
+        neighborhood = super(Neighborhood, cls).__new__(cls)
+        neighborhood._realm = realm
+        neighborhood._id = id
+        return neighborhood
 
     def __call__(self, realm=False, id=False, version=False, parent=False):
         """Create a new Resource using the current resource as a template.
 
         >>> repr(nbh(None))
         '<Neighborhood nbh:id>'
+
+        Null neighborhood will be used to put absolute resource
+        references ban into relative form (i.e. `resource.neiighborhood = None`)
+
+        >>> nullnbh = Neighborhood(None, None)
+        >>> repr(nullnbh)
+        '<Neighborhood (null)>'
+
+        >>> repr(nullnbh(main))
+        "<Resource u'wiki:WikiStart'>"
+        >>> repr(nullnbh(main3))
+        "<Resource u'wiki:WikiStart@3'>"
+        >>> repr(nullnbh(main0))
+        "<Resource u'wiki:WikiStart@0'>"
         """
         if (realm, id, version, parent) in ((False, False, False, False),
                                             (None, False, False, False)):
             return resource
 
     def _update_parents(self, resource):
+        if self.is_null and resource.neighborhood is None:
+            return resource
         newresource = Resource(resource.realm, resource.id, resource.version, self)
         current = newresource
         parent = resource.parent
         resource.id = id
         resource.version = version
         resource.parent = parent
+        if neighborhood and neighborhood.is_null:
+            neighborhood = None
         resource.neighborhood = neighborhood
         return resource