source: products/quintagroup.canonicalpath/trunk/quintagroup/canonicalpath/upgrades.py @ 2362

Last change on this file since 2362 was 2362, checked in by mylan, 14 years ago

Added basic of canonical link to path and vice versa convertor.

  • Property svn:eol-style set to native
File size: 2.9 KB
Line 
1import logging
2from zope.component import queryAdapter
3#from zope.component import queryMultiAdapter
4from Acquisition import aq_base, aq_inner
5#from Products.CMFCore.utils import getToolByName
6
7#from quintagroup.canonicalpath.adapters import PROPERTY_LINK
8#from quintagroup.canonicalpath.adapters import PROPERTY_PATH
9from quintagroup.canonicalpath.interfaces  import ICanonicalPath
10from quintagroup.canonicalpath.interfaces  import ICanonicalLink
11
12class CanonicalConvertor(object):
13    """Convert canonical link to canonical path and vice versa."""
14
15    def __init__(self, portal_url, logger_name="quintagroup.canonicalpath"):
16        """Create instanse of convertor.
17           - *portal_url* (string), add in the front of canonical path property
18             value for get canonical link.
19           - *logger_name* - name of the logger
20        """
21        self.initLogger(logger_name)
22        self.portal_url = portal_url
23
24    def initLogger(self, lname):
25        self.logger = logging.getLogger(lname)
26
27    def convertLinkToPath(self, obj):
28        """Convert canonical link to canonical path"""
29        return self._convert(obj, ICanonicalLink, ICanonicalPath,
30                             self._convertL2P)
31
32    def convertPathToLink(self, obj):
33        """Convert canonical path to canonical link"""
34        return self._convert(obj, ICanonicalPath, ICanonicalLink,
35                             self._convertP2L)
36
37    def _convert(self, obj, src_iface, dst_iface, converter):
38        """Convert canonical from source canonical interface
39           to destination canonical interface.
40
41           Return True is successfull, False otherwise.
42           Log results in logger.
43        """
44        src = queryAdapter(obj, src_iface)
45        dst = queryAdapter(obj, dst_iface)
46        # XXX: Check is this correct work XXX
47        obj = aq_base(aq_inner(obj))
48        # XXX
49        if src is not None \
50           and dst is not None:
51            msg = "Migrate %s into %s for %s object: " \
52                   % (src_iface, dst_iface, obj.absolute_url())
53            try:
54                converter(src, dst)
55            except Exception, e:
56                lev = logging.ERROR
57                msg += "WITH ERROR: %s" % str(e)
58            else:
59                lev = logging.INFO
60                msg += "SUCCESSFUL"
61
62            self.logger.log(lev, msg)
63           
64            return lev == logging.INFO and True or False
65   
66    def _convertP2L(self, src, dst):
67        """Convert canonical path to canonical link."""
68        cpath = src.canonical_path
69        cpath = cpath.startswith('/') and cpath or '/' + cpath
70        dst.canonical_link = self.portal_url + cpath
71        del dst.canonical_path
72
73    def _convertL2P(self, src, dst):
74        """Convert canonical link to canonical path."""
75        raise NotImplementedError(
76            "Convertion from canonical link to canonical path not implemented")
77
78
Note: See TracBrowser for help on using the repository browser.