Ignore:
Timestamp:
Mar 17, 2010 4:41:03 PM (14 years ago)
Author:
mylan
Message:

#177: Add ICanonicalLink interface and default adapter

File:
1 edited

Legend:

Unmodified
Added
Removed
  • quintagroup.canonicalpath/trunk/quintagroup/canonicalpath/adapters.py

    r1921 r1927  
    88 
    99from quintagroup.canonicalpath.interfaces import ICanonicalPath 
     10from quintagroup.canonicalpath.interfaces import ICanonicalLink 
    1011 
    11 PROPERTY = "canonical_path" 
     12PROPERTY_PATH = "canonical_path" 
     13PROPERTY_LINK = "canonical_link" 
    1214 
    1315_is_canonical = re.compile( 
     
    2426    def __init__(self, context): 
    2527        self.context = context 
     28        self.purl = getToolByName(self.context,'portal_url') 
    2629 
    27     def _validate(self, value): 
     30    def _validate_path(self, value): 
     31        value.strip() 
     32        if not _is_canonical(value): 
     33            raise InvalidValue(value) 
     34        return value 
     35 
     36    def getCanonicalPath(self): 
     37        """ First of all return value from the PROPERTY_PATH, 
     38        if PROPERTY_PATH not exist - return default value 
     39        """ 
     40        if self.context.hasProperty(PROPERTY_PATH): 
     41            return self.context.getProperty(PROPERTY_PATH) 
     42 
     43        return '/'+'/'.join(self.purl.getRelativeContentPath(self.context)) 
     44 
     45    def setCanonicalPath(self, value): 
     46        """ First validate value, than add/updater PROPERTY_PATH 
     47        """ 
     48        value = self._validate_path(value) 
     49 
     50        if self.context.hasProperty(PROPERTY_PATH): 
     51            self.context._updateProperty(PROPERTY_PATH, value) 
     52        else: 
     53            self.context._setProperty(PROPERTY_PATH, value, type="string") 
     54 
     55    canonical_path = property(getCanonicalPath, setCanonicalPath) 
     56 
     57 
     58class DefaultCanonicalLinkAdapter(object): 
     59    """Adapts base content to canonical link. 
     60    """ 
     61    adapts(ITraversable) 
     62    implements(ICanonicalLink) 
     63 
     64    def __init__(self, context): 
     65        self.context = context 
     66        self.purl = getToolByName(self.context,'portal_url') 
     67 
     68    def _validate_link(self, value): 
    2869        value.strip() 
    2970        if not _is_canonical(value): 
     
    3172        return value 
    3273         
     74    def getCanonicalLink(self): 
     75        """ First of all return value from the PROPERTY_LINK, 
     76        if PROPERTY_LINK not exist - return default value 
     77        """ 
     78        if self.context.hasProperty(PROPERTY_LINK): 
     79            return self.context.getProperty(PROPERTY_LINK) 
    3380 
    34     def getCanonicalPath(self): 
    35         """ First of all return value from the PROPERTY, 
    36         if PROPERTY not exist - return default value 
     81        return self.context.absolute_url() 
     82 
     83    def setCanonicalLink(self, value): 
     84        """ First validate value, than add/updater PROPERTY_LINK 
    3785        """ 
    38         if self.context.hasProperty(PROPERTY): 
    39             return self.context.getProperty(PROPERTY) 
     86        value = self._validate_link(value) 
    4087 
    41         purl = getToolByName(self.context,'portal_url') 
    42         return '/'+'/'.join(purl.getRelativeContentPath(self.context)) 
     88        if self.context.hasProperty(PROPERTY_LINK): 
     89            self.context._updateProperty(PROPERTY_LINK, value) 
     90        else: 
     91            self.context._setProperty(PROPERTY_LINK, value, type="string") 
    4392 
    44     def setCanonicalPath(self, value): 
    45         """ First validate value, than add/updater PROPERTY 
    46         """ 
    47         value = self._validate(value) 
     93    canonical_link = property(getCanonicalLink, setCanonicalLink) 
    4894 
    49         if self.context.hasProperty(PROPERTY): 
    50             self.context._updateProperty(PROPERTY, value) 
    51         else: 
    52             self.context._setProperty(PROPERTY, value, type="string") 
    53  
    54     canonical_path = property(getCanonicalPath, setCanonicalPath) 
Note: See TracChangeset for help on using the changeset viewer.