Commit 44e0b22f authored by Nicolas Delaby's avatar Nicolas Delaby

Move to new API, detailed properties take precedence

Detailed properties take precedence on coordinate_text (raw string) if at least one of them is defined.
Introduce isDetailed method in coordinate API, which return True if at least one detailed property is defined.
parent 032584d7
...@@ -35,6 +35,8 @@ from Products.ERP5Type.Utils import deprecated ...@@ -35,6 +35,8 @@ from Products.ERP5Type.Utils import deprecated
from Products.CMFDefault.utils import formatRFC822Headers from Products.CMFDefault.utils import formatRFC822Headers
import re import re
_marker = object()
class Coordinate(Base): class Coordinate(Base):
""" """
Coordinates is a mix-in class which is used to store elementary Coordinates is a mix-in class which is used to store elementary
...@@ -138,6 +140,24 @@ class Coordinate(Base): ...@@ -138,6 +140,24 @@ class Coordinate(Base):
""" """
return self.asText() return self.asText()
security.declareProtected(Permissions.AccessContentsInformation, 'getCoordinateText')
def getCoordinateText(self, default=_marker):
"""Fallback on splitted values (old API)
"""
if not self.hasCoordinateText() and self.isDetailed():
# Display old values (parsed ones)
# empty value is None, not ''
value = self.asText()
if value:
return value
if default is _marker:
return None
else:
return default
if default is _marker:
return self._baseGetCoordinateText()
return self._baseGetCoordinateText(default)
security.declareProtected( Permissions.ModifyPortalContent, 'fromText' ) security.declareProtected( Permissions.ModifyPortalContent, 'fromText' )
@deprecated @deprecated
def fromText(self, coordinate_text): def fromText(self, coordinate_text):
...@@ -164,6 +184,10 @@ class Coordinate(Base): ...@@ -164,6 +184,10 @@ class Coordinate(Base):
""" """
pass pass
security.declareProtected(Permissions.AccessContentsInformation, 'isDetailed')
def isDetailed(self):
return False
security.declarePrivate( '_writeFromPUT' ) security.declarePrivate( '_writeFromPUT' )
def _writeFromPUT( self, body ): def _writeFromPUT( self, body ):
headers = {} headers = {}
......
...@@ -92,13 +92,13 @@ class GeographicAddress(Coordinate, Base): ...@@ -92,13 +92,13 @@ class GeographicAddress(Coordinate, Base):
""" """
result = Coordinate.asText(self) result = Coordinate.asText(self)
if result is None: if result is None:
if self.hasData(): if self.isDetailed():
street_address, city, zip_code = self._splitCoordinateText(self.getData(''))
else:
street_address = self.getStreetAddress('') street_address = self.getStreetAddress('')
city = self.getCity('')
zip_code = self.getZipCode('') zip_code = self.getZipCode('')
result = '%s\n%s %s' % (street_address, city, zip_code,) city = self.getCity('')
else:
street_address, zip_code, city = self._splitCoordinateText(self.getCoordinateText(''))
result = '%s\n%s %s' % (street_address, zip_code, city)
if not result.strip(): if not result.strip():
return '' return ''
return result return result
...@@ -109,8 +109,8 @@ class GeographicAddress(Coordinate, Base): ...@@ -109,8 +109,8 @@ class GeographicAddress(Coordinate, Base):
"""Save given data then continue parsing """Save given data then continue parsing
(deprecated because computed values are stored) (deprecated because computed values are stored)
""" """
self._setData(coordinate_text) self._setCoordinateText(coordinate_text)
street_address, city, zip_code = self._splitCoordinateText(coordinate_text) street_address, zip_code, city = self._splitCoordinateText(coordinate_text)
self.setStreetAddress(street_address) self.setStreetAddress(street_address)
self.setZipCode(zip_code) self.setZipCode(zip_code)
self.setCity(city) self.setCity(city)
...@@ -128,3 +128,6 @@ c/o Jean-Paul Sartre ...@@ -128,3 +128,6 @@ c/o Jean-Paul Sartre
""", """,
) )
security.declareProtected(Permissions.AccessContentsInformation, 'isDetailed')
def isDetailed(self):
return self.hasStreetAddress() or self.hasZipCode() or self.hasCity()
...@@ -74,18 +74,18 @@ class InternetProtocolAddress(Base, Coordinate): ...@@ -74,18 +74,18 @@ class InternetProtocolAddress(Base, Coordinate):
""" """
result = Coordinate.asText(self) result = Coordinate.asText(self)
if result is None: if result is None:
if self.hasData(): if self.isDetailed():
result = '\n'.join(('%s:%s' % (k, v) for k, v in\ tmp_list = []
self._splitCoordinateText(self.getData())))
else:
tmp = []
for prop in PropertySheet.InternetProtocolAddress._properties: for prop in PropertySheet.InternetProtocolAddress._properties:
property_id = prop['id'] property_id = prop['id']
getter_name = 'get%s' % convertToUpperCase(property_id) getter_name = 'get%s' % convertToUpperCase(property_id)
getter_method = getattr(self, getter_name) getter_method = getattr(self, getter_name)
value = getter_method() or '' value = getter_method('')
tmp.append('%s:%s' % (property_id, value)) tmp_list.append('%s:%s' % (property_id, value))
result = '\n'.join(tmp) result = '\n'.join(tmp_list)
else:
result = '\n'.join(('%s:%s' % (k, v) for k, v in\
self._splitCoordinateText(self.getCoordinateText())))
return result return result
security.declareProtected(Permissions.ModifyPortalContent, 'fromText') security.declareProtected(Permissions.ModifyPortalContent, 'fromText')
...@@ -94,7 +94,7 @@ class InternetProtocolAddress(Base, Coordinate): ...@@ -94,7 +94,7 @@ class InternetProtocolAddress(Base, Coordinate):
"""Save given data then continue parsing """Save given data then continue parsing
(deprecated because computed values are stored) (deprecated because computed values are stored)
""" """
self._setData(coordinate_text) self._setCoordinateText(coordinate_text)
kw_dict = self._splitCoordinateText(coordinate_text) kw_dict = self._splitCoordinateText(coordinate_text)
for name, value in kw_dict.iteritems(): for name, value in kw_dict.iteritems():
...@@ -116,3 +116,14 @@ broadcast_address:192.168.0.255 ...@@ -116,3 +116,14 @@ broadcast_address:192.168.0.255
dns_server_ip_address:192.168.0.1 dns_server_ip_address:192.168.0.1
gateway_ip_address:192.168.0.1 gateway_ip_address:192.168.0.1
network_interface:eth0""" network_interface:eth0"""
security.declareProtected(Permissions.AccessContentsInformation, 'isDetailed')
def isDetailed(self):
for prop in PropertySheet.InternetProtocolAddress._properties:
property_id = prop['id']
tester_name = 'has%s' % convertToUpperCase(property_id)
tester_method = getattr(self, tester_name)
value = tester_method()
if value:
return True
return False
...@@ -301,7 +301,7 @@ class Telephone(Coordinate, Base): ...@@ -301,7 +301,7 @@ class Telephone(Coordinate, Base):
"""Save given data then continue parsing """Save given data then continue parsing
(deprecated because computed values are stored) (deprecated because computed values are stored)
""" """
self._setData(coordinate_text) self._setCoordinateText(coordinate_text)
method = self._getTypeBasedMethod('fromText') method = self._getTypeBasedMethod('fromText')
if method is not None: if method is not None:
return method(text=coordinate_text) return method(text=coordinate_text)
...@@ -327,18 +327,16 @@ class Telephone(Coordinate, Base): ...@@ -327,18 +327,16 @@ class Telephone(Coordinate, Base):
if script is not None: if script is not None:
return script() return script()
if self.hasData(): if self.isDetailed():
coordinate_text = self.getData()
country, area, city, number, extension =\
self._splitCoordinateText(coordinate_text)
else:
# BBB if one of old slice is stored on current document
# then use old API in order to recompute coordinate string
country = self.getTelephoneCountry('') country = self.getTelephoneCountry('')
area = self.getTelephoneArea('') area = self.getTelephoneArea('')
city = self.getTelephoneCity('') city = self.getTelephoneCity('')
number = self.getTelephoneNumber('') number = self.getTelephoneNumber('')
extension = self.getTelephoneExtension('') extension = self.getTelephoneExtension('')
else:
coordinate_text = self.getCoordinateText()
country, area, city, number, extension =\
self._splitCoordinateText(coordinate_text)
if (country or area or city or number or extension): if (country or area or city or number or extension):
# Define the notation # Define the notation
...@@ -397,6 +395,14 @@ class Telephone(Coordinate, Base): ...@@ -397,6 +395,14 @@ class Telephone(Coordinate, Base):
""" """
return ("+33(0)6-62 05 76 14",) return ("+33(0)6-62 05 76 14",)
security.declareProtected(Permissions.AccessContentsInformation, 'isDetailed')
def isDetailed(self):
return self.hasTelephoneCountry() or\
self.hasTelephoneArea() or\
self.hasTelephoneCity() or\
self.hasTelephoneNumber() or\
self.hasTelephoneExtension()
def _getNotation(self): def _getNotation(self):
""" """
Returns the notation that will be used by asText method. Returns the notation that will be used by asText method.
......
...@@ -71,9 +71,9 @@ class Url(Coordinate, Base, UrlMixin): ...@@ -71,9 +71,9 @@ class Url(Coordinate, Base, UrlMixin):
users just enter www.erp5.com or info@erp5.com rather than users just enter www.erp5.com or info@erp5.com rather than
http://www.erp5.com or mailto:info@erp5.com http://www.erp5.com or mailto:info@erp5.com
""" """
if self.hasData(): if self.isDetailed():
return self.getData('')
return self.getUrlString('') return self.getUrlString('')
return self.getCoordinateText('')
security.declareProtected(Permissions.ModifyPortalContent, 'fromText') security.declareProtected(Permissions.ModifyPortalContent, 'fromText')
@deprecated @deprecated
...@@ -81,7 +81,7 @@ class Url(Coordinate, Base, UrlMixin): ...@@ -81,7 +81,7 @@ class Url(Coordinate, Base, UrlMixin):
""" """
Sets url_string a.k.a. scheme-specific-part of a URL Sets url_string a.k.a. scheme-specific-part of a URL
""" """
self._setData(text) self._setCoordinateText(text)
self.setUrlString(text) self.setUrlString(text)
security.declareProtected(Permissions.AccessContentsInformation, security.declareProtected(Permissions.AccessContentsInformation,
...@@ -97,18 +97,21 @@ class Url(Coordinate, Base, UrlMixin): ...@@ -97,18 +97,21 @@ class Url(Coordinate, Base, UrlMixin):
def getUrlString(self, default=_marker): def getUrlString(self, default=_marker):
if not self.hasUrlString(): if not self.hasUrlString():
if default is _marker: if default is _marker:
return self.getData() return self.getCoordinateText()
else: else:
return self.getData(default) return self.getCoordinateText(default)
else: else:
if default is _marker: if default is _marker:
return self._baseGetUrlString() return self._baseGetUrlString()
else: else:
return self._baseGetUrlString(default) return self._baseGetUrlString(default)
security.declareProtected(Permissions.AccessContentsInformation, 'isDetailed')
def isDetailed(self):
return self.hasUrlString()
security.declareProtected(Permissions.UseMailhostServices, 'send') security.declareProtected(Permissions.UseMailhostServices, 'send')
@deprecated
def send(self, from_url=None, to_url=None, msg=None, def send(self, from_url=None, to_url=None, msg=None,
subject=None, attachment_list=None, extra_headers=None): subject=None, attachment_list=None, extra_headers=None):
""" """
......
...@@ -52,3 +52,7 @@ class ICoordinate(Interface): ...@@ -52,3 +52,7 @@ class ICoordinate(Interface):
string format for that coordinate string format for that coordinate
""" """
pass pass
def isDetailed():
"""return True if one of detailed properties is defined
"""
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment