Author: robin.munn
Date: Tue Mar 22 12:24:35 2005
New Revision: 59

Modified:
   trunk/mailman/src/mailman/database/schema.py
   trunk/mailman/src/mailman/database/tests/test_attribute.py
   trunk/mailman/src/mailman/database/tests/test_mlist.py
   trunk/mailman/src/mailman/database/tests/test_preferences.py
   trunk/mailman/src/mailman/mlist.py
Log:
Several changes:
  * Added a required "name" field to the schema.Domain class.
  * Updated several tests to know about the new "name" field in Domain.
  * Commented out the old mlist.MailList class.
  * Created a new mlist.MailingList class to be the "main" mailing list
    class, where most of the business logic will live. It can create a
    new schema.MailingList object from scratch along with all required
    helper tables.
  * Added __getattr__ and __setattr__ magic to mlist.MailingList class
    so that mailing lists know how to get and set their own attributes,
    making the database magic (AttributeValue, AttributeGroup, etc.)
    happen automagically behind the scenes.
  * Wrote tests to exercise mailing list creation and the __getattr__
    and __setattr__ magic.


Modified: trunk/mailman/src/mailman/database/schema.py
==============================================================================
--- trunk/mailman/src/mailman/database/schema.py        (original)
+++ trunk/mailman/src/mailman/database/schema.py        Tue Mar 22 12:24:35 2005
@@ -100,7 +100,11 @@
 
         # If nothing found in overrides, check style
         if result is None:
-            result = self.list_style.get_value(attribute_name)
+            style = self.list_style
+            if style is None:
+                result = Attribute.byName(attribute_name).default_value
+            else:
+                result = self.list_style.get_value(attribute_name)
 
         return result
 
@@ -120,11 +124,46 @@
         # We're done!
         return result
 
+    def is_locked(self, attribute_name):
+        """
+        Return True if attribute_name is locked, i.e., we're not allowed to
+        change it.
+
+        There are two places where at attribute can be locked: at the site-wide
+        level, and at the domain level.
+        """
+        defaultgroup = AttributeGroup.byCollection_name('default')
+        attrs = list(AttributeValue.select(
+            AND(AttributeValue.q.groupID == defaultgroup.id,
+                AttributeValue.q.name == attribute_name)))
+        if len(attrs) > 0:
+            if attrs[0].policy == 'LOCKED':
+                return True
+        domaingroup = AttributeGroup.byCollection_name(self.list_domain())
+        attrs = list(AttributeValue.select(
+            AND(AttributeValue.q.groupID == domaingroup.id,
+                AttributeValue.q.name == attribute_name)))
+        if len(attrs) > 0:
+            if attrs[0].policy == 'LOCKED':
+                return True
+        return False
+
+    def list_domain(self):
+        """
+        Return the domain part of the list name.
+        """
+        if self.name.find('@') != -1:
+            listname, listdomain = self.name.split('@', 1)
+        else:
+            listname, listdomain = self.name, ''
+        return listdomain
+
 
 
 class Domain(SQLObject):
     _cacheValue = False
     # Attributes
+    name        = UnicodeCol(alternateID=True)
     mailhost    = UnicodeCol()
     webhost     = UnicodeCol(default=None)
     newshost    = UnicodeCol(default=None)
@@ -373,7 +412,7 @@
 
         # still nothing found, lookup "glabal" default for attribute
         if result is None:
-            attr = Attribute.byName(attribute_name)
+            attr = Attribute.byName(attribute_name)  # XXX Need test: what 
happens if attribute doesn't exist? I believe it raises SQLObjectNotFound.
             if attr is None:
                 result = None
             else:

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  Tue Mar 22 
12:24:35 2005
@@ -61,7 +61,9 @@
         members = Roster()
         # Create a mailing list domain
         domain_admins = Roster()
-        domain = Domain(mailhost='localhost', admins=domain_admins)
+        domain = Domain(name='localhost',
+                        mailhost='localhost',
+                        admins=domain_admins)
         # Create the required message collections
         archives = MessageCollection()
         owner_archives = MessageCollection()
@@ -107,7 +109,9 @@
         members = Roster()
         # Create a mailing list domain
         domain_admins = Roster()
-        domain = Domain(mailhost='localhost', admins=domain_admins)
+        domain = Domain(name='localhost',
+                        mailhost='localhost',
+                        admins=domain_admins)
         # Create the required message collections
         archives = MessageCollection()
         owner_archives = MessageCollection()
@@ -201,7 +205,9 @@
         members = Roster()
         # Create a mailing list domain
         domain_admins = Roster()
-        domain = Domain(mailhost='localhost', admins=domain_admins)
+        domain = Domain(name='localhost',
+                        mailhost='localhost',
+                        admins=domain_admins)
         # Create the required message collections
         archives = MessageCollection()
         owner_archives = MessageCollection()

Modified: trunk/mailman/src/mailman/database/tests/test_mlist.py
==============================================================================
--- trunk/mailman/src/mailman/database/tests/test_mlist.py      (original)
+++ trunk/mailman/src/mailman/database/tests/test_mlist.py      Tue Mar 22 
12:24:35 2005
@@ -30,12 +30,14 @@
         members = Roster()
         # Create a mailing list domain
         domain_admins = Roster()
-        domain = Domain(mailhost='localhost', admins=domain_admins)
+        domain = Domain(name='localhost',
+                        mailhost='localhost',
+                        admins=domain_admins)
         # Create the required message collection
         archives = MessageCollection()
         owner_archives = MessageCollection()
         # Finally!  Create the mailing list.
-        mlist = MailingList(name='test',
+        mlist = MailingList(name='[EMAIL PROTECTED]',
                             owners=owners,
                             moderators=moderators,
                             members=members,
@@ -43,7 +45,7 @@
                             archives=archives,
                             owner_archives=owner_archives,
                             )
-        eq(mlist.name, 'test')
+        eq(mlist.name, '[EMAIL PROTECTED]')
         self.failUnless(mlist.creation_date <= datetime.datetime.now())
         eq(mlist.domain.mailhost, 'localhost')
         eq(mlist.domain.webhost, None)
@@ -51,6 +53,62 @@
         eq(list(mlist.members.entries), [])
         eq(list(mlist.archives.messages), [])
         self.assertNotEqual(mlist.owners, mlist.members)
+
+    def test_policy(self):
+        eq = self.assertEqual
+        self.test_create_list()
+        mlist = MailingList.byName('[EMAIL PROTECTED]')
+        attr = Attribute(name='max_hold',
+                         type='NUMBER',
+                         default_value='10')
+        defaultsgroup = AttributeGroup(collection_name='default')  # Site-wide 
defaults
+        domaingroup = AttributeGroup(collection_name='localhost')  # 
Domain-wide defaults
+        # First, what happens when no AttributeValue entries exist?
+        eq(mlist.is_locked('max_hold'), False)
+        # Locked at domain level but not site-wide
+        domainvalue = AttributeValue(name='max_hold',
+                                     group=domaingroup,
+                                     value='50',
+                                     policy='LOCKED')
+        eq(mlist.is_locked('max_hold'), True)
+        # Locked at both domain level *and* site-wide level
+        defaultsvalue = AttributeValue(name='max_hold',
+                                     group=defaultsgroup,
+                                     value='50',
+                                     policy='LOCKED')
+        eq(mlist.is_locked('max_hold'), True)
+        # Locked at site-wide level but not domain level
+        domainvalue.set(policy='BASIC')
+        eq(mlist.is_locked('max_hold'), True)
+        # Explicitly unlocked at both site-wide and domain levels
+        defaultsvalue.set(policy='BASIC')
+        eq(mlist.is_locked('max_hold'), False)
+
+
+    def test_attribute_setting(self):
+        eq = self.assertEqual
+        defaultsgroup = AttributeGroup(collection_name='default')  # Site-wide 
defaults
+        domaingroup = AttributeGroup(collection_name='localhost')  # 
Domain-wide defaults
+        from mailman.mlist import MailingList
+        mlist = MailingList('[EMAIL PROTECTED]')
+        attr = Attribute(name='max_hold',
+                         type='NUMBER',
+                         default_value='10')
+        eq(mlist.max_hold, '10')
+        mlist.max_hold = '50'
+        eq(mlist.max_hold, '50')
+        # Check the database table also
+        values = list(AttributeValue.select(
+            (AttributeValue.q.name == 'max_hold') & \
+            (AttributeValue.q.groupID == 
AttributeGroup.byCollection_name(mlist._mlist.name).id)))
+        # Note: The mlist._mlist.name above was originally mlist.name, because
+        # I had forgotten that I was using the MailingList object from mlist.py
+        # (I thought it was the one from schema.py). This needs to be fixed:
+        # the schema object and the "main" object should not have such similar
+        # and confusing names. Think about this one...
+        eq(len(values), 1)
+        eq(values[0].value, '50')
+
 
 
 

Modified: trunk/mailman/src/mailman/database/tests/test_preferences.py
==============================================================================
--- trunk/mailman/src/mailman/database/tests/test_preferences.py        
(original)
+++ trunk/mailman/src/mailman/database/tests/test_preferences.py        Tue Mar 
22 12:24:35 2005
@@ -53,7 +53,9 @@
         members = Roster()
         # Create a mailing list domain
         domain_admins = Roster()
-        domain = Domain(mailhost='localhost', admins=domain_admins)
+        domain = Domain(name='localhost',
+                        mailhost='localhost',
+                        admins=domain_admins)
         # Create the required message collection
         archives = MessageCollection()
         owner_archives = MessageCollection()

Modified: trunk/mailman/src/mailman/mlist.py
==============================================================================
--- trunk/mailman/src/mailman/mlist.py  (original)
+++ trunk/mailman/src/mailman/mlist.py  Tue Mar 22 12:24:35 2005
@@ -27,6 +27,7 @@
 
 
 
+''' Old version:
 class MailList(object):
     @staticmethod
     def open(listname, domain):
@@ -80,3 +81,84 @@
 
     def delete(self):
         dbmgr.lists.delete_list(self._listid)
+'''
+
+from mailman.database import schema
+
+class MailingList(object):
+    def create(self, listid):
+        """
+        Create a new mailing list object and store it in the database.
+        """
+        if listid.find('@') == -1:
+            listname, listdomain = listid, 'localhost'  # XXX Or default 
domain from configuration
+        else:
+            listname, listdomain = listid.split('@', 1)
+        listid = '@'.join([listname, listdomain])
+        try:
+            domainobj = schema.Domain.byName(listdomain)
+        except schema.SQLObjectNotFound:
+            domainobj = schema.Domain(name=listdomain,
+                                      mailhost='mail.%s' % listdomain,  # XXX 
Get this from config
+                                      admins=schema.Roster())
+        self._mlist = schema.MailingList(
+            name=listid,
+            owners=schema.Roster(),
+            moderators=schema.Roster(),
+            members=schema.Roster(),
+            domain=domainobj,
+            archives=schema.MessageCollection(),
+            owner_archives=schema.MessageCollection(),
+        )
+
+
+    def open(self, listid):
+        """
+        Retrieve an existing mailing list object from the database.
+
+        Raises SQLObjectNotFound if no such mailing list exists.
+        """
+        self._mlist = schema.MailingList.byName(listid)
+
+
+    def __init__(self, listid):
+        try:
+            self.open(listid)
+        except schema.SQLObjectNotFound:
+            self.create(listid)
+
+    def __getattr__(self, attrname):
+        """
+        Automagically fetch the appropriate attribute from the database.
+        """
+        try:
+            result = self._mlist.get_value(attrname)
+        except schema.SQLObjectNotFound:
+            raise AttributeError, '%s is not a valid MailingList attribute' % 
attrname
+        return result
+
+    def __setattr__(self, attrname, attrvalue):
+        """
+        Automagically set the attribute in the database, creating the override
+        group if necessary.
+        """
+        if attrname.startswith('_'):
+            return super(MailingList, self).__setattr__(attrname, attrvalue)
+        if self._mlist.is_locked(attrname):
+            raise AttributeError, '%s is locked and therefore read-only' % 
attrname
+        try:
+            overrides = 
schema.AttributeGroup.byCollection_name(self._mlist.name)
+        except schema.SQLObjectNotFound:
+            overrides = schema.AttributeGroup(collection_name=self._mlist.name)
+        fetched_values = list(schema.AttributeValue.select(
+            (schema.AttributeValue.q.name == attrname) &
+            (schema.AttributeValue.q.groupID == overrides.id)))
+        if len(fetched_values) == 0:
+            value = schema.AttributeValue(name=attrname,
+                                          group=overrides,
+                                          value=attrvalue,
+                                          policy='BASIC')
+        else:
+            value = fetched_values[0]
+            value.set(value=attrvalue)
+
_______________________________________________
Mailman-checkins mailing list
[email protected]
Unsubscribe: 
http://mail.python.org/mailman/options/mailman-checkins/archive%40jab.org

Reply via email to