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
from Products.CMFDefault.utils import formatRFC822Headers
import re
_marker = object()
class Coordinate(Base):
"""
Coordinates is a mix-in class which is used to store elementary
......@@ -138,6 +140,24 @@ class Coordinate(Base):
"""
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' )
@deprecated
def fromText(self, coordinate_text):
......@@ -164,6 +184,10 @@ class Coordinate(Base):
"""
pass
security.declareProtected(Permissions.AccessContentsInformation, 'isDetailed')
def isDetailed(self):
return False
security.declarePrivate( '_writeFromPUT' )
def _writeFromPUT( self, body ):
headers = {}
......
......@@ -92,13 +92,13 @@ class GeographicAddress(Coordinate, Base):
"""
result = Coordinate.asText(self)
if result is None:
if self.hasData():
street_address, city, zip_code = self._splitCoordinateText(self.getData(''))
else:
if self.isDetailed():
street_address = self.getStreetAddress('')
city = self.getCity('')
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():
return ''
return result
......@@ -109,8 +109,8 @@ class GeographicAddress(Coordinate, Base):
"""Save given data then continue parsing
(deprecated because computed values are stored)
"""
self._setData(coordinate_text)
street_address, city, zip_code = self._splitCoordinateText(coordinate_text)
self._setCoordinateText(coordinate_text)
street_address, zip_code, city = self._splitCoordinateText(coordinate_text)
self.setStreetAddress(street_address)
self.setZipCode(zip_code)
self.setCity(city)
......@@ -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):
"""
result = Coordinate.asText(self)
if result is None:
if self.hasData():
result = '\n'.join(('%s:%s' % (k, v) for k, v in\
self._splitCoordinateText(self.getData())))
else:
tmp = []
if self.isDetailed():
tmp_list = []
for prop in PropertySheet.InternetProtocolAddress._properties:
property_id = prop['id']
getter_name = 'get%s' % convertToUpperCase(property_id)
getter_method = getattr(self, getter_name)
value = getter_method() or ''
tmp.append('%s:%s' % (property_id, value))
result = '\n'.join(tmp)
value = getter_method('')
tmp_list.append('%s:%s' % (property_id, value))
result = '\n'.join(tmp_list)
else:
result = '\n'.join(('%s:%s' % (k, v) for k, v in\
self._splitCoordinateText(self.getCoordinateText())))
return result
security.declareProtected(Permissions.ModifyPortalContent, 'fromText')
......@@ -94,7 +94,7 @@ class InternetProtocolAddress(Base, Coordinate):
"""Save given data then continue parsing
(deprecated because computed values are stored)
"""
self._setData(coordinate_text)
self._setCoordinateText(coordinate_text)
kw_dict = self._splitCoordinateText(coordinate_text)
for name, value in kw_dict.iteritems():
......@@ -116,3 +116,14 @@ broadcast_address:192.168.0.255
dns_server_ip_address:192.168.0.1
gateway_ip_address:192.168.0.1
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):
"""Save given data then continue parsing
(deprecated because computed values are stored)
"""
self._setData(coordinate_text)
self._setCoordinateText(coordinate_text)
method = self._getTypeBasedMethod('fromText')
if method is not None:
return method(text=coordinate_text)
......@@ -327,18 +327,16 @@ class Telephone(Coordinate, Base):
if script is not None:
return script()
if self.hasData():
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
if self.isDetailed():
country = self.getTelephoneCountry('')
area = self.getTelephoneArea('')
city = self.getTelephoneCity('')
number = self.getTelephoneNumber('')
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):
# Define the notation
......@@ -397,6 +395,14 @@ class Telephone(Coordinate, Base):
"""
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):
"""
Returns the notation that will be used by asText method.
......
......@@ -71,9 +71,9 @@ class Url(Coordinate, Base, UrlMixin):
users just enter www.erp5.com or info@erp5.com rather than
http://www.erp5.com or mailto:info@erp5.com
"""
if self.hasData():
return self.getData('')
return self.getUrlString('')
if self.isDetailed():
return self.getUrlString('')
return self.getCoordinateText('')
security.declareProtected(Permissions.ModifyPortalContent, 'fromText')
@deprecated
......@@ -81,7 +81,7 @@ class Url(Coordinate, Base, UrlMixin):
"""
Sets url_string a.k.a. scheme-specific-part of a URL
"""
self._setData(text)
self._setCoordinateText(text)
self.setUrlString(text)
security.declareProtected(Permissions.AccessContentsInformation,
......@@ -97,18 +97,21 @@ class Url(Coordinate, Base, UrlMixin):
def getUrlString(self, default=_marker):
if not self.hasUrlString():
if default is _marker:
return self.getData()
return self.getCoordinateText()
else:
return self.getData(default)
return self.getCoordinateText(default)
else:
if default is _marker:
return self._baseGetUrlString()
else:
return self._baseGetUrlString(default)
security.declareProtected(Permissions.AccessContentsInformation, 'isDetailed')
def isDetailed(self):
return self.hasUrlString()
security.declareProtected(Permissions.UseMailhostServices, 'send')
@deprecated
def send(self, from_url=None, to_url=None, msg=None,
subject=None, attachment_list=None, extra_headers=None):
"""
......
......@@ -52,3 +52,7 @@ class ICoordinate(Interface):
string format for that coordinate
"""
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