[Erp5-report] r13463 - /erp5/trunk/products/ERP5/Document/Url.py

nobody at svn.erp5.org nobody at svn.erp5.org
Sat Mar 17 11:03:52 CET 2007


Author: jp
Date: Sat Mar 17 11:03:49 2007
New Revision: 13463

URL: http://svn.erp5.org?rev=13463&view=rev
Log:
Implemented MixIn which supports URL encoding and decoding.

Modified:
    erp5/trunk/products/ERP5/Document/Url.py

Modified: erp5/trunk/products/ERP5/Document/Url.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5/Document/Url.py?rev=13463&r1=13462&r2=13463&view=diff
==============================================================================
--- erp5/trunk/products/ERP5/Document/Url.py (original)
+++ erp5/trunk/products/ERP5/Document/Url.py Sat Mar 17 11:03:49 2007
@@ -36,8 +36,47 @@
 from mimetools import choose_boundary
 from mimetypes import guess_type
 
-
-class Url(Coordinate, Base):
+class UrlMixIn:
+
+  # Declarative security
+  security = ClassSecurityInfo()
+  security.declareObjectProtected(Permissions.AccessContentsInformation)
+
+  no_host_protocol_list = ['mailto', 'news', ]
+  default_protocol_dict = { 'Email' : 'mailto:',
+                          }
+
+  security.declareProtected(Permissions.AccessContentsInformation,
+                            'asURL')
+  def asURL(self):
+    """
+    Returns a text representation of the Url
+    """
+    protocol = self.getUrlProtocol()
+    if not protocol:
+      # A quick fix for all objects which did not
+      # define protocol such as email addresses
+      ptype = self.getPortalType()
+      if UrlMixIn.default_protocol_dict.has_key():
+        protocol = UrlMixIn.default_protocol_dict[ptype]
+      else:
+        protocol = 'http'
+    url_string = self.getUrlString()
+    if protocol in UrlMixIn.no_host_protocol_list or url_string.startswith('//'):
+      return '%s:%s' % (protocol, url_string)
+    return '%s://%s' % (protocol, url_string)
+
+  security.declareProtected(Permissions.ModifyPortalContent, 'fromText')
+  def fromURL(self, url):
+    """
+    Analyses a URL and splits it into two parts.
+    """
+    protocol, url_string = url.split(':')
+    if url_string.startswith('//'): url_string = url_string[2:]
+    self._setUrlProtocol(protocol)
+    self.setUrlString(url_string)
+
+class Url(Coordinate, Base, UrlMixIn):
   """
   A Url is allows to represent in a standard way coordinates
   such as web sites, emails, ftp sites, etc.
@@ -54,7 +93,7 @@
   security.declareObjectProtected(Permissions.AccessContentsInformation)
 
   # Default Properties
-  property_sheets = ( PropertySheet.Base
+  property_sheets = (   PropertySheet.Base
                       , PropertySheet.SimpleItem
                       , PropertySheet.Url
                       )
@@ -65,14 +104,14 @@
     """
     Returns a text representation of the Url
     """
-    return self.url_string
+    return self.asURL()
 
   security.declareProtected(Permissions.ModifyPortalContent, 'fromText')
   def fromText(self, text):
     """
     set the Url from its text representation
     """
-    self.url_string = text
+    self.fromURL(text)
 
   security.declareProtected(Permissions.AccessContentsInformation,
                             'standardTextFormat')
@@ -80,7 +119,7 @@
     """
     Returns the standard text formats for urls
     """
-    return ("http://www.erp5.org","mailto:info@erp5.org")
+    return ("http://www.erp5.org", "mailto:info at erp5.org")
 
   security.declareProtected(Permissions.UseMailhostServices, 'send')
   def send(self, from_url=None, to_url=None, msg=None, subject=None,  attachment_list=None):




More information about the Erp5-report mailing list