r13609 - in Products.Archetypes/trunk: . Products/Archetypes Products/Archetypes/tests

"Hanno Schlichting" <[email protected]> Fri, 06 May 2011 18:10:54 +0000
Newsgroups gmane.comp.web.zope.plone.archetypes.cvs
Message-ID <[email protected]>
Author: hannosch
Date: Fri May  6 18:10:53 2011
New Revision: 13609

Modified:
   Products.Archetypes/trunk/CHANGES.txt
   Products.Archetypes/trunk/Products/Archetypes/BaseUnit.py
   Products.Archetypes/trunk/Products/Archetypes/Field.py
   Products.Archetypes/trunk/Products/Archetypes/tests/test_filename.py
Log:
Clarify default value of `BaseUnit.filename` to be `None` instead of switching between `None` and an empty string in every request.

Avoid flagging base units as changed, if their values didn't actually change.


Modified: Products.Archetypes/trunk/CHANGES.txt
==============================================================================
--- Products.Archetypes/trunk/CHANGES.txt	(original)
+++ Products.Archetypes/trunk/CHANGES.txt	Fri May  6 18:10:53 2011
@@ -4,6 +4,13 @@
 1.7.5 - unreleased
 ------------------
 
+- Avoid flagging base units as changed, if their values didn't actually change.
+  [hannosch]
+
+- Clarify default value of `BaseUnit.filename` to be `None` instead of
+  switching between `None` and an empty string in every request.
+  [hannosch]
+
 - Rights field has text/plain format only. (If no allowable_format specified
   for secondary fields, we get unexpected behaviour) This refs
   http://dev.plone.org/plone/ticket/9345.

Modified: Products.Archetypes/trunk/Products/Archetypes/BaseUnit.py
==============================================================================
--- Products.Archetypes/trunk/Products/Archetypes/BaseUnit.py	(original)
+++ Products.Archetypes/trunk/Products/Archetypes/BaseUnit.py	Fri May  6 18:10:53 2011
@@ -1,5 +1,4 @@
 import os.path
-from types import StringType
 from zope.interface import implements
 
 from Products.Archetypes.interfaces import IBaseUnit
@@ -22,6 +21,12 @@
     implements(IBaseUnit, IWriteLock)
 
     isUnit = 1
+    mimetype = None
+    binary = None
+    original_encoding = None
+    raw = None
+    size = None
+    filename = None
 
     security = ClassSecurityInfo()
 
@@ -46,8 +51,10 @@
         adapter = getToolByName(context, 'mimetypes_registry')
         data, filename, mimetype = adapter(data, **kw)
 
-        self.mimetype = str(mimetype)
-        self.binary = mimetype.binary
+        if self.mimetype != str(mimetype):
+            self.mimetype = str(mimetype)
+        if self.binary != mimetype.binary:
+            self.binary = mimetype.binary
         if not self.isBinary():
             if encoding is None:
                 try:
@@ -56,11 +63,14 @@
                     # adapter is not defined, we are in object creation
                     import site
                     encoding = site.encoding
-            self.original_encoding = encoding
+            if self.original_encoding != encoding:
+                self.original_encoding = encoding
         else:
-            self.original_encoding = None
-        self.raw  = data
-        self.size = len(data)
+            if self.original_encoding != encoding:
+                self.original_encoding = None
+        if type(self.raw) != type(data) or self.raw != data:
+            self.raw = data
+            self.size = len(data)
         # taking care of stupid IE
         self.setFilename(filename)
         self._cacheExpire()
@@ -185,8 +195,10 @@
         if not result:
             raise ValueError('Unknown mime type %s' % value)
         mimetype = result[0]
-        self.mimetype = str(mimetype)
-        self.binary = mimetype.binary
+        if self.mimetype != str(mimetype):
+            self.mimetype = str(mimetype)
+        if self.binary != mimetype.binary:
+            self.binary = mimetype.binary
         self._cacheExpire()
 
     def getFilename(self):
@@ -197,11 +209,14 @@
     def setFilename(self, filename):
         """Set the file name.
         """
-        if type(filename) is StringType:
+        if isinstance(filename, str):
             filename = os.path.basename(filename)
-            self.filename = filename.split("\\")[-1]
+            filename = filename.split("\\")[-1]
+            if self.filename != filename:
+                self.filename = filename
         else:
-            self.filename = filename
+            if self.filename != filename:
+                self.filename = filename
         self._cacheExpire()
 
     def _cacheExpire(self):

Modified: Products.Archetypes/trunk/Products/Archetypes/Field.py
==============================================================================
--- Products.Archetypes/trunk/Products/Archetypes/Field.py	(original)
+++ Products.Archetypes/trunk/Products/Archetypes/Field.py	Fri May  6 18:10:53 2011
@@ -1281,7 +1281,7 @@
         if file is None:
             file = self._make_file(self.getName(), title='',
                                    file='', instance=instance)
-        filename = kwargs.get('filename') or ''
+        filename = kwargs.get('filename', None)
         body = None
         if IBaseUnit.providedBy(value):
             mimetype = value.getContentType() or mimetype
@@ -1334,10 +1334,11 @@
         if isinstance(value, Pdata):
             # TODO Should be fixed eventually
             value = str(value)
-        filename = filename[max(filename.rfind('/'),
-                                filename.rfind('\\'),
-                                filename.rfind(':'),
-                                )+1:]
+        if isinstance(filename, basestring):
+            filename = filename[max(filename.rfind('/'),
+                                    filename.rfind('\\'),
+                                    filename.rfind(':'),
+                                    )+1:]
 
         if mimetype is None or mimetype == 'text/x-unknown-content-type':
             if body is None:

Modified: Products.Archetypes/trunk/Products/Archetypes/tests/test_filename.py
==============================================================================
--- Products.Archetypes/trunk/Products/Archetypes/tests/test_filename.py	(original)
+++ Products.Archetypes/trunk/Products/Archetypes/tests/test_filename.py	Fri May  6 18:10:53 2011
@@ -48,7 +48,7 @@
     def test_textfieldwithfilename(self):
         obj = self._dummy
         field = obj.getField('atextfield')
-        self.assertEqual(field.getFilename(obj), '')
+        self.assertEqual(field.getFilename(obj), None)
         self.assertEqual(field.getRaw(obj), default_text)
         obj.setAtextfield('Bla', filename='name.rst')
         self.assertEqual(field.getFilename(obj), 'name.rst')
@@ -66,7 +66,7 @@
         field = obj.getField('atextfield')
         obj.setAtextfield('Bli')
         self.assertEqual(str(field.getRaw(obj)), 'Bli')
-        self.assertEqual(field.getFilename(obj), '')
+        self.assertEqual(field.getFilename(obj), None)
 
     def test_textfielduploadwithoutfilename(self):
         obj = self._dummy

------------------------------------------------------------------------------
WhatsUp Gold - Download Free Network Management Software
The most intuitive, comprehensive, and cost-effective network 
management toolset available today.  Delivers lowest initial 
acquisition cost and overall TCO of any competing solution.
http://p.sf.net/sfu/whatsupgold-sd