Author: robin.munn
Date: Mon Mar 21 12:28:54 2005
New Revision: 44

Added:
   trunk/mailman/src/mailman/database/tests/test_attribute.py
Modified:
   trunk/mailman/src/mailman/database/schema.py
Log:
Added new getValue() function to ListStyle class. Pass it an attribute name,
and it will fetch a value (as a string). Also include unit tests to exercise
the function.


Modified: trunk/mailman/src/mailman/database/schema.py
==============================================================================
--- trunk/mailman/src/mailman/database/schema.py        (original)
+++ trunk/mailman/src/mailman/database/schema.py        Mon Mar 21 12:28:54 2005
@@ -211,7 +211,7 @@
 class Attribute(SQLObject):
     _cacheValue     = False
     # Attributes
-    name            = UnicodeCol()
+    name            = UnicodeCol(alternateID=True)
     type            = EnumCol(enumValues=['TEXT',       'NUMBER',
                                           'DATETIME',   'BOOL',
                                           ])
@@ -221,14 +221,15 @@
 class AttributeGroup(SQLObject):
     _cacheValue     = False
     # Attributes
-    collection_name = UnicodeCol()
+    collection_name = UnicodeCol(alternateID=True)
     list_styles     = RelatedJoin('ListStyle')
+    values          = MultipleJoin('AttributeValue', joinColumn='group_id')
 
 
 class AttributeValue(SQLObject):
     _cacheValue     = False
     # Attributes
-    name            = UnicodeCol()
+    name            = UnicodeCol(alternateID=True)
     group           = ForeignKey('AttributeGroup')
     value           = UnicodeCol()
     policy          = EnumCol(enumValues=['LOCKED', 'ADVANCED', 'BASIC'])
@@ -237,8 +238,35 @@
 class ListStyle(SQLObject):
     _cacheValue     = False
     # Attributes
-    name            = UnicodeCol()
+    name            = UnicodeCol(alternateID=True)
     groups          = RelatedJoin('AttributeGroup')
+
+    def getValue(self, attribute_name):
+        """
+        Returns the value corresponding to a given attribute, as a string.
+
+        Returns None if no value can be found. (This will happen, for example,
+        if the attribute name is misspelled).
+
+        Possible ideas for later improvements: convert the string to the
+        appropriate type.
+        """
+        foundValue = None
+        for group in self.groups:
+            for value in group.values:
+                if attribute_name.lower() == value.name.lower():
+                    foundValue = value.value
+                    break
+            if foundValue is not None:
+                break
+        if foundValue is not None:
+            return foundValue
+        else:
+            attr = Attribute.byName(attribute_name)
+            if attr is None:
+                return None
+            else:
+                return attr.default_value
 
 
 # XXX For performance reasons, because these tables are so normalized, we

Added: trunk/mailman/src/mailman/database/tests/test_attribute.py
==============================================================================
--- (empty file)
+++ trunk/mailman/src/mailman/database/tests/test_attribute.py  Mon Mar 21 
12:28:54 2005
@@ -0,0 +1,67 @@
+# Copyright (C) 2005 Barry A. Warsaw
+#
+# This program is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by the Free
+# Software Foundation; either version 2 of the License, or (at your option)
+# any later version.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+# more details.
+#
+# You should have received a copy of the GNU General Public License along with
+# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
+# Place - Suite 330, Boston, MA 02111-1307, USA.
+
+import datetime
+import unittest
+
+from mailman.database import schema
+from mailman.database.tests.base import DatabaseTest
+
+
+
+class TestAttribute(DatabaseTest):
+    def test_one_attribute(self):
+        eq = self.assertEqual
+        attr = schema.Attribute(name='enable_archives',
+                                type='BOOL',
+                                default_value='True')
+        group = schema.AttributeGroup(collection_name='archive_options')
+        value = schema.AttributeValue(name='enable_archives',
+                                      group=group,
+                                      value='False',
+                                      policy='BASIC')
+        style = schema.ListStyle(name='no_archive')
+        style.addAttributeGroup(group)
+        # group.addStyle(style)  # SQLObject automagically does this one
+        fetched_value = style.getValue('enable_archives')
+        eq(value.value, fetched_value)
+
+    def test_default_when_no_value_given(self):
+        eq = self.assertEqual
+        attr = schema.Attribute(name='enable_archives',
+                                type='BOOL',
+                                default_value='True')
+        group = schema.AttributeGroup(collection_name='archive_options')
+        style = schema.ListStyle(name='yes_archive')
+        style.addAttributeGroup(group)
+        fetched_value = style.getValue('enable_archives')
+        eq(fetched_value, attr.default_value)
+
+    def test_default_when_no_value_given_and_no_groups(self):
+        eq = self.assertEqual
+        attr = schema.Attribute(name='enable_archives',
+                                type='BOOL',
+                                default_value='True')
+        style = schema.ListStyle(name='yes_archive')
+        fetched_value = style.getValue('enable_archives')
+        eq(fetched_value, attr.default_value)
+    
+
+
+def test_suite():
+    suite = unittest.TestSuite()
+    suite.addTest(unittest.makeSuite(TestAttribute))
+    return suite
_______________________________________________
Mailman-checkins mailing list
[email protected]
Unsubscribe: 
http://mail.python.org/mailman/options/mailman-checkins/archive%40jab.org

Reply via email to