source: products/qPloneResolveUID/tags/0.2.4/transforms/ruid_to_url.py @ 2111

Last change on this file since 2111 was 1, checked in by myroslav, 18 years ago

Building directory structure

File size: 2.0 KB
Line 
1# Author: Melnychuk Taras
2# Contact: fenix@quintagroup.com
3# Date: $Date: 2006-08-11
4# Copyright: quintagroup.com
5
6import re
7from Products.CMFCore.utils import getToolByName
8from Products.PortalTransforms.interfaces import itransform
9
10from Products.qPloneResolveUID.config import *
11
12class ruid_to_url:
13    """Transform which replaces resolve uid into urls"""
14   
15    __implements__ = itransform
16   
17    __name__ = "ruid_to_url"
18    inputs  = ('text/html',)
19    output = 'text/html'
20   
21    def __init__(self, name=None):
22        if name:
23            self.__name__ = name
24        self.tag_regexp = re.compile(TAG_PATTERN ,re.I|re.S)
25        self.ruid_regexp = re.compile(UID_PATTERN ,re.I|re.S)
26
27    def name(self):
28        return self.__name__
29   
30    def find_ruid(self, data):
31        tags_ruid = []
32        unique_ruid = []
33        for m in self.tag_regexp.finditer(data):
34            ruid = re.search(self.ruid_regexp, m.group(0))
35            if ruid:
36                tags_ruid.append({m.group(0):ruid.group('uid')})
37        [unique_ruid.append(tu.values()[0]) for tu in tags_ruid if tu.values()[0] not in unique_ruid]
38        return tags_ruid, unique_ruid
39
40    def mapRUID_URL(self, unique_ruid, portal):
41        ruid_url = {}
42        rc = getToolByName(portal, 'reference_catalog')
43        pu = getToolByName(portal, 'portal_url')
44        for uid in unique_ruid:
45            obj = rc.lookupObject(uid)
46            if obj:
47                ruid_url[uid] = pu.getRelativeUrl(obj)
48        return ruid_url           
49   
50    def convert(self, orig, data, **kwargs):
51        text = orig
52        tags_ruid, unique_ruid = self.find_ruid(text)
53        if unique_ruid:
54            ruid_url = self.mapRUID_URL(unique_ruid, kwargs['context'])
55            for tag_ruid in tags_ruid:
56                t, uid = tag_ruid.items()[0]
57                if ruid_url.has_key(uid):
58                    text = text.replace(t, t.replace('resolveuid/'+uid, ruid_url[uid]))
59       
60        data.setData(text)
61        return data
62
63
64def register():
65    return ruid_to_url()
66   
Note: See TracBrowser for help on using the repository browser.