Changeset 2103 in products for quintagroup.canonicalpath/trunk


Ignore:
Timestamp:
Apr 12, 2010 11:44:04 AM (14 years ago)
Author:
mylan
Message:

#190: Extract general part for canonical_link and canonical_path adapters

File:
1 edited

Legend:

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

    r1933 r2103  
    1818 
    1919 
    20 class DefaultCanonicalPathAdapter(object): 
    21     """Adapts base content to canonical path. 
     20class DefaultCanonicalAdapter(object): 
     21    """Generic canonical adapter. 
    2222    """ 
    2323    adapts(ITraversable) 
    24     implements(ICanonicalPath) 
     24 
     25    prop = None 
    2526 
    2627    def __init__(self, context): 
     
    2829        self.purl = getToolByName(self.context,'portal_url') 
    2930 
    30     def _validate_path(self, value): 
     31    def _validate(self, value): 
    3132        value.strip() 
    3233        if not _is_canonical(value): 
     
    3435        return value 
    3536 
    36     def getCanonicalPath(self): 
    37         """ First of all return value from the PROPERTY_PATH, 
    38         if PROPERTY_PATH not exist - return default value 
     37    def getDefault(self): 
     38        """Return default value for the self.prop""" 
     39        raise NotImplementedError() 
     40 
     41    def getProp(self): 
     42        """ First of all return value from the self.prop, 
     43        if self.prop not exist - return default value 
    3944        """ 
    40         if self.context.hasProperty(PROPERTY_PATH): 
    41             return self.context.getProperty(PROPERTY_PATH) 
     45        if self.context.hasProperty(self.prop): 
     46            return self.context.getProperty(self.prop) 
    4247 
     48        return self.getDefault() 
     49 
     50    def setProp(self, value): 
     51        """ First validate value, than add/updater self.prop 
     52        """ 
     53        value = self._validate(value) 
     54 
     55        if self.context.hasProperty(self.prop): 
     56            self.context._updateProperty(self.prop, value) 
     57        else: 
     58            self.context._setProperty(self.prop, value, type="string") 
     59 
     60    def delProp(self): 
     61        """ Delete self.prop customization 
     62        """ 
     63        if self.context.hasProperty(self.prop): 
     64            self.context.manage_delProperties(ids=[self.prop,]) 
     65 
     66 
     67class DefaultCanonicalPathAdapter(DefaultCanonicalAdapter): 
     68    """Adapts base content to canonical path. 
     69    """ 
     70    implements(ICanonicalLink) 
     71 
     72    prop = PROPERTY_PATH 
     73 
     74    def getDefault(self): 
    4375        return '/'+'/'.join(self.purl.getRelativeContentPath(self.context)) 
    4476 
    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     def delCanonicalPath(self): 
    56         """ Delete PROPERTY_PATH customization 
    57         """ 
    58         if self.context.hasProperty(PROPERTY_PATH): 
    59             self.context.manage_delProperties(ids=[PROPERTY_PATH,]) 
    60  
    61     canonical_path = property(getCanonicalPath, setCanonicalPath, delCanonicalPath) 
     77    canonical_path = property(getProp, setProp, delProp) 
    6278 
    6379 
    64 class DefaultCanonicalLinkAdapter(object): 
     80class DefaultCanonicalLinkAdapter(DefaultCanonicalAdapter): 
    6581    """Adapts base content to canonical link. 
    6682    """ 
    67     adapts(ITraversable) 
    6883    implements(ICanonicalLink) 
    6984 
    70     def __init__(self, context): 
    71         self.context = context 
    72         self.purl = getToolByName(self.context,'portal_url') 
     85    prop = PROPERTY_LINK 
    7386 
    74     def _validate_link(self, value): 
    75         value.strip() 
    76         if not _is_canonical(value): 
    77             raise InvalidValue(value) 
    78         return value 
    79          
    80     def getCanonicalLink(self): 
    81         """ First of all return value from the PROPERTY_LINK, 
    82         if PROPERTY_LINK not exist - return default value 
    83         """ 
    84         if self.context.hasProperty(PROPERTY_LINK): 
    85             return self.context.getProperty(PROPERTY_LINK) 
    86  
     87    def getDefault(self): 
    8788        return self.context.absolute_url() 
    8889 
    89     def setCanonicalLink(self, value): 
    90         """ First validate value, than add/updater PROPERTY_LINK 
    91         """ 
    92         value = self._validate_link(value) 
    93  
    94         if self.context.hasProperty(PROPERTY_LINK): 
    95             self.context._updateProperty(PROPERTY_LINK, value) 
    96         else: 
    97             self.context._setProperty(PROPERTY_LINK, value, type="string") 
    98  
    99     def delCanonicalLink(self): 
    100         """ Delete PROPERTY_LINK customization 
    101         """ 
    102         if self.context.hasProperty(PROPERTY_LINK): 
    103             self.context.manage_delProperties(ids=[PROPERTY_LINK,]) 
    104  
    105     canonical_link = property(getCanonicalLink, setCanonicalLink, delCanonicalLink) 
    106  
     90    canonical_link = property(getProp, setProp, delProp) 
Note: See TracChangeset for help on using the changeset viewer.