Author: robin.munn
Date: Mon Mar 21 16:51:03 2005
New Revision: 53
Modified:
trunk/mailman/src/mailman/database/schema.py
trunk/mailman/src/mailman/database/tests/test_attribute.py
Log:
Added test and logic to lookup attribute values for mailing lists
directly (we can already look them up on mailing list styles).
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 16:51:03 2005
@@ -47,6 +47,43 @@
list_style = ForeignKey('ListStyle', default=None)
overrides = ForeignKey('AttributeGroup', default=None)
+ def get_value(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).
+
+ Lookup on attribute_name is case-insensitive.
+
+ Possible ideas for later improvements: convert the string to the
+ appropriate type.
+ """
+ result = None
+ if self.overrides is not None:
+ for value in self.overrides.values:
+ if attribute_name.lower() == value.name.lower():
+ result = value.value
+ break
+
+ # If nothing found in overrides, check style
+ if result is None:
+ result = self.list_style.get_value(attribute_name)
+
+ return result
+
+ def get_attributes(self):
+ """
+ Returns all attributes and their values, as a dict.
+ """
+ result = self.list_style.get_attributes()
+ # Then set overrides
+ if self.overrides is not None:
+ for attr in self.overrides.values:
+ result[attr.name] = attr.value
+ # We're done!
+ return result
+
class Domain(SQLObject):
Modified: trunk/mailman/src/mailman/database/tests/test_attribute.py
==============================================================================
--- trunk/mailman/src/mailman/database/tests/test_attribute.py (original)
+++ trunk/mailman/src/mailman/database/tests/test_attribute.py Mon Mar 21
16:51:03 2005
@@ -23,7 +23,7 @@
class TestAttribute(DatabaseTest):
- def test_one_attribute(self):
+ def test_fetch_attribute_from_list_style(self):
eq = self.assertEqual
attr = schema.Attribute(name='enable_archives',
type='BOOL',
@@ -37,7 +37,49 @@
style.addAttributeGroup(group)
# group.addStyle(style) # SQLObject automagically does this one
fetched_value = style.get_value('enable_archives')
- eq(value.value, fetched_value)
+ eq(fetched_value, value.value)
+ value.set(value='True')
+ fetched_value = style.get_value('enable_archives')
+ eq(fetched_value, value.value)
+
+ def test_fetch_attribute_from_mailing_list(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
+ # Need to add a MailingList -- paste code from test_roster.py here
+ owners = schema.Roster()
+ moderators = schema.Roster()
+ members = schema.Roster()
+ # Create a mailing list domain
+ domain_admins = schema.Roster()
+ domain = schema.Domain(mailhost='localhost', admins=domain_admins)
+ # Create the required message collections
+ archives = schema.MessageCollection()
+ owner_archives = schema.MessageCollection()
+ # Finally! Create the mailing list.
+ mlist = schema.MailingList(name='test',
+ owners=owners,
+ moderators=moderators,
+ members=members,
+ domain=domain,
+ archives=archives,
+ owner_archives=owner_archives,
+ list_style=style,
+ )
+ fetched_value = mlist.get_value('enable_archives')
+ eq(fetched_value, value.value)
+ value.set(value='True')
+ fetched_value = mlist.get_value('enable_archives')
+ eq(fetched_value, value.value)
def test_default_when_no_value_given(self):
eq = self.assertEqual
@@ -59,7 +101,7 @@
fetched_value = style.get_value('enable_archives')
eq(fetched_value, attr.default_value)
- def test_get_all_attributes(self):
+ def test_fetch_all_attributes_from_list_style(self):
eq = self.assertEqual
attr1 = schema.Attribute(name='enable_archives',
type='BOOL',
@@ -77,6 +119,48 @@
style.addAttributeGroup(group)
# group.addStyle(style) # SQLObject automagically does this one
fetched_attrs = style.get_attributes()
+ eq(len(fetched_attrs), 2)
+ eq(fetched_attrs[attr1.name], value1.value)
+ eq(fetched_attrs[attr2.name], attr2.default_value)
+
+ def test_fetch_all_attributes_from_mailing_list(self):
+ eq = self.assertEqual
+ attr1 = schema.Attribute(name='enable_archives',
+ type='BOOL',
+ default_value='True')
+ attr2 = schema.Attribute(name='max_hold',
+ type='NUMBER',
+ default_value='10')
+ group = schema.AttributeGroup(collection_name='archive_options')
+ value1 = schema.AttributeValue(name='enable_archives',
+ group=group,
+ value='False',
+ policy='BASIC')
+ # No value2 -- we want to get its default value
+ style = schema.ListStyle(name='no_archive')
+ style.addAttributeGroup(group)
+ # group.addStyle(style) # SQLObject automagically does this one
+ # Need to add a MailingList -- paste code from test_roster.py here
+ owners = schema.Roster()
+ moderators = schema.Roster()
+ members = schema.Roster()
+ # Create a mailing list domain
+ domain_admins = schema.Roster()
+ domain = schema.Domain(mailhost='localhost', admins=domain_admins)
+ # Create the required message collections
+ archives = schema.MessageCollection()
+ owner_archives = schema.MessageCollection()
+ # Finally! Create the mailing list.
+ mlist = schema.MailingList(name='test',
+ owners=owners,
+ moderators=moderators,
+ members=members,
+ domain=domain,
+ archives=archives,
+ owner_archives=owner_archives,
+ list_style=style,
+ )
+ fetched_attrs = mlist.get_attributes()
eq(len(fetched_attrs), 2)
eq(fetched_attrs[attr1.name], value1.value)
eq(fetched_attrs[attr2.name], attr2.default_value)
_______________________________________________
Mailman-checkins mailing list
[email protected]
Unsubscribe:
http://mail.python.org/mailman/options/mailman-checkins/archive%40jab.org