Author: barry.warsaw
Date: Mon Mar 21 16:06:37 2005
New Revision: 51
Added:
trunk/mailman/src/mailman/database/tests/test_preferences.py
Modified:
trunk/mailman/src/mailman/database/schema.py
Log:
Added test_preference to test the new preference related tables.
MailingList.name is now alternateID. Uncomment EmailAddress.priority. Also
added a get_preference() convenience method which will find the preference
value for the address, with an optional mailing list to find a subscription's
preference.
Redesigned the preferences related tables so that they work similar to the
attribute related tables. This allows us to add new preferences without
having to change the schema.
On ListMemberPreferences, changed preferences to group.
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:06:37 2005
@@ -19,6 +19,7 @@
import sys
import logging
import datetime
+import operator
from sqlobject import *
from sqlobject.col import *
@@ -31,7 +32,7 @@
class MailingList(SQLObject):
_cacheValue = False
# Attributes
- name = UnicodeCol()
+ name = UnicodeCol(alternateID=True)
description = ForeignKey('CachedLanguageText', default=None)
owners = ForeignKey('Roster')
moderators = ForeignKey('Roster')
@@ -81,14 +82,40 @@
_inheritable = False
# Attributes
address = UnicodeCol()
- #priority = IntCol(default=None)
+ priority = IntCol(default=0)
#member_id
- #preference_id
label = UnicodeCol(default=None)
realname = UnicodeCol(default=None)
registration_date = DateTimeCol(default=datetime.datetime.now)
verification_date = DateTimeCol(default=None)
+ def get_preference(self, preference, listname=None):
+ if not listname:
+ results = ListMemberPreferences.select(
+ AND(ListMemberPreferences.q.addressID == self.id,
+ ListMemberPreferences.q.mailing_listID == None))
+ else:
+ mlist = MailingList.byName(listname)
+ results = ListMemberPreferences.select(
+ AND(ListMemberPreferences.q.addressID == self.id,
+ OR(ListMemberPreferences.q.mailing_listID == None,
+ ListMemberPreferences.q.mailing_listID == mlist.id)))
+ # Sort the rows so that NULLs are last, meaning that we want
+ # subscription specific preferences to override address specific
+ # preferences. We can't trust SQL to do this for us, since that's
+ # dialect dependent behavior.
+ preflist = list(results)
+ preflist.sort(key=operator.attrgetter('mailing_list'), reverse=True)
+ for lmpref in preflist:
+ prefval = PreferenceValue.select(
+ AND(PreferenceValue.q.groupID == lmpref.group.id,
+ PreferenceValue.q.name == preference))
+ if not prefval:
+ continue
+ return prefval[0].value
+ # This address never set this preference, so get preference's default.
+ return Preference.byName(preference).default_value
+
class RosterReference(RosterEntry):
_cacheValue = False
@@ -105,33 +132,36 @@
-# XXX Can we just make this an AttributeGroup?
class Preference(SQLObject):
_cacheValue = False
# Attributes
- delivery_mode = EnumCol(enumValues=
- ['PlainDigest', 'MIMEDigest', 'Normal'],
- default='Normal')
- status = EnumCol(enumValues=
- ['Enabled', 'DisabledByUser',
- 'DisabledByAdmin', 'DisabledByBounce'],
- default='Enabled')
- vacation_start = DateTimeCol(default=None)
- vacation_end = DateTimeCol(default=None)
- vacation_catchup = BoolCol(default=None)
- receive_own_posts = BoolCol(default=None)
- acknowledge_posts = BoolCol(default=None)
- conceal_subscription = BoolCol(default=None)
- receive_list_copy = BoolCol(default=None)
- preferred_language = UnicodeCol(default=None)
+ name = UnicodeCol(alternateID=True)
+ type = EnumCol(enumValues=['TEXT', 'NUMBER',
+ 'DATETIME', 'BOOL',
+ ])
+ default_value = UnicodeCol()
+
+
+class PreferenceValue(SQLObject):
+ _cacheValue = False
+ # Attributes
+ name = UnicodeCol(alternateID=True)
+ group = ForeignKey('PreferenceGroup')
+ value = UnicodeCol()
+
+
+class PreferenceGroup(SQLObject):
+ _cacheValue = False
+ # Attributes
+ preferences = MultipleJoin('PreferenceValue', joinColumn='group_id')
class ListMemberPreferences(SQLObject):
_cacheValue = False
# Attributes
- mailing_list = ForeignKey('MailingList')
+ mailing_list = ForeignKey('MailingList', default=None)
address = ForeignKey('EmailAddress')
- preferences = ForeignKey('Preference')
+ group = ForeignKey('PreferenceGroup')
Added: trunk/mailman/src/mailman/database/tests/test_preferences.py
==============================================================================
--- (empty file)
+++ trunk/mailman/src/mailman/database/tests/test_preferences.py Mon Mar
21 16:06:37 2005
@@ -0,0 +1,47 @@
+# 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 unittest
+
+from mailman.database import schema
+from mailman.database.tests.base import DatabaseTest
+
+
+
+class TestPreferences(DatabaseTest):
+ def setUp(self):
+ DatabaseTest.setUp(self)
+ self._pref1 = schema.Preference(name='status', type='INT',
default_value='0')
+ self._pref2 = schema.Preference(name='conceal', type='BOOL',
default_value='False')
+
+ def test_preferences_for_email_address(self):
+ eq = self.assertEqual
+ roster = schema.Roster()
+ addr = schema.EmailAddress(address='[EMAIL PROTECTED]',
member_of=roster)
+ prefgroup = schema.PreferenceGroup()
+ pref1 = schema.PreferenceValue(name='status', group=prefgroup,
value='0')
+ pref2 = schema.PreferenceValue(name='conceal', group=prefgroup,
value='True')
+ memberprefs = schema.ListMemberPreferences(
+ address=addr, group=prefgroup)
+ status = memberprefs.address.get_preference('status')
+ eq(status, '0')
+
+
+
+def test_suite():
+ suite = unittest.TestSuite()
+ suite.addTest(unittest.makeSuite(TestPreferences))
+ return suite
_______________________________________________
Mailman-checkins mailing list
[email protected]
Unsubscribe:
http://mail.python.org/mailman/options/mailman-checkins/archive%40jab.org