Author: barry.warsaw
Date: Tue Mar 22 17:42:21 2005
New Revision: 64

Modified:
   trunk/mailman/src/mailman/database/schema.py
   trunk/mailman/src/mailman/mlist.py
   trunk/mailman/src/mailman/tests/test_app_mlist.py
Log:
Changed the semantics of MailingList.__init__(), .create(), and .open(), and
updated the test case.  Now the semantics are:

__init__() fetches the mailing list, which must exist already.  Throws an
exception if the list doesn't yet exist.

create(): creates the mailing list but throws an exception if it already
exists.

open(): Like init, but as a class method.

In the schema, make mailhost not required.



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 17:42:21 2005
@@ -136,6 +136,9 @@
                 result[attr.name] = attr.value
         return result
 
+    # XXX This makes it seem like the test is whether the mailing list is
+    # locked instead of what it really tests (whether the attribute is
+    # locked).  This should be renamed or moved.
     def is_locked(self, attribute_name):
         """Return True if attribute_name is locked, i.e., we're not allowed to
         change it.
@@ -168,12 +171,14 @@
 
 class Domain(SQLObject):
     _cacheValue = False
+    # A name for the domain which is the part after the @ of the listid.  This
+    # is not tied to a particular host name.
+    name        = UnicodeCol(alternateID=True)
     # These values contain various host names for the domain.  The only
     # required value is the mailhost, which is the host name part of the email
     # address.  E.g. in [EMAIL PROTECTED], it's 'example.com'.  The other
     # host names are required only if those interfaces are enabled.
-    name        = UnicodeCol(alternateID=True)
-    mailhost    = UnicodeCol()
+    mailhost    = UnicodeCol(default=None)
     webhost     = UnicodeCol(default=None)
     newshost    = UnicodeCol(default=None)
     imaphost    = UnicodeCol(default=None)
@@ -493,6 +498,7 @@
     # or "Digestable".  Thus only the attributes that correspond to the
     # partial style are contained in this group, and groups are composable
     # into list styles.
+    # XXX rename me!
     collection_name = UnicodeCol(alternateID=True)
     list_styles     = RelatedJoin('ListStyle')
     values          = MultipleJoin('AttributeValue', joinColumn='group_id')

Modified: trunk/mailman/src/mailman/mlist.py
==============================================================================
--- trunk/mailman/src/mailman/mlist.py  (original)
+++ trunk/mailman/src/mailman/mlist.py  Tue Mar 22 17:42:21 2005
@@ -18,7 +18,8 @@
 import logging
 
 ##from mailman import categories
-##from mailman import errors
+from mailman import errors
+from mailman.database import schema
 ##from mailman import utils
 ##from mailman.config import configuration
 ##from mailman.dbmgr import dbmgr
@@ -27,81 +28,52 @@
 
 
 
-''' Old version:
-class MailList(object):
-    @staticmethod
-    def open(listname, domain):
-        """An alternative interface to construct MailList objects."""
-        listid = '[EMAIL PROTECTED]' % (listname, domain)
-        return MailList(listid)
-
-    @staticmethod
-    def create(listname, domain, style='default'):
-        listname = listname.lower()
-        # Verify that the listname is valid by checking to make sure that the
-        # posting address is valid.
-        mailhost, webhost = configuration.domains.bydomain[domain]
-        postaddr = '[EMAIL PROTECTED]' % (listname, mailhost)
-        if not utils.is_valid_address(postaddr):
-            raise errors.MailingListError(
-                'Bad list posting address: %s' % postaddr)
-        listid = listname + '@' + domain
-        utcnow = dbmgr.lists.create_list(listid)
-        log.info('List %s created at %s UTC', listid, utcnow)
-        mlist = MailList(listid)
-        # Basic variable initialization
-        for name, (prop, cat) in categories.allvars.items():
-            setattr(mlist, name, prop.default())
-        # Populate it with attributes only the create() function knows.
-        mlist.listid = listid
-        mlist.listname = listname
-        mlist.mailhost = mailhost
-        mlist.webhost = webhost
-        mlist.created = utcnow
-        # Apply style initializations.
-        stylemod = 'mailman.styles.' + style
-        __import__(stylemod)
-        mod = sys.modules[stylemod]
-        mod.initialize(mlist)
-        return mlist
-
+class MailingList(object):
     def __init__(self, listid):
-        if not dbmgr.lists.list_exists(listid):
-            raise errors.NoListError(listid)
-        self._listid = listid
-
-    def __getattr__(self, attr):
-        return dbmgr.lists.get(self._listid, attr)
-
-    def __setattr__(self, attr, val):
-        if attr.startswith('_'):
-            super(MailList, self).__setattr__(attr, val)
-            return
-        dbmgr.lists.set(self._listid, attr, val)
-
-    def delete(self):
-        dbmgr.lists.delete_list(self._listid)
-'''
-
-from mailman.database import schema
+        """Open an existing mailing list.
 
-class MailingList(object):
-    def create(self, listid):
-        """
-        Create a new mailing list object and store it in the database.
+        If the list doesn't yet exist, a NoListError is raised.
         """
-        if listid.find('@') == -1:
-            listname, listdomain = listid, 'localhost'  # XXX Or default 
domain from configuration
+        try:
+            self._mlist = schema.MailingList.byName(listid)
+        except schema.SQLObjectNotFound:
+            raise errors.NoListError(listid)
+
+    @classmethod
+    def open(cls, listid):
+        return cls(listid)
+
+    @classmethod
+    def create(cls, listid):
+        """Create a new mailing list object and store it in the database.
+
+        Raise ListExistsError if the mailing list already exists.  Raise
+        MailingListError if the listid is invalid (e.g. it does not have an @
+        sign).
+        """
+        # First, validate the listid
+        parts = listid.split('@', 1)
+        if len(parts) <> 2:
+            raise errors.MailingListError('Bad listid: %s' % listid)
+        listname, domain = parts
+        # Make sure the list doesn't already exist
+        try:
+            schema.MailingList.byName(listid)
+        except schema.SQLObjectNotFound:
+            # The mailing list doesn't yet exist
+            pass
         else:
-            listname, listdomain = listid.split('@', 1)
-        listid = '@'.join([listname, listdomain])
+            raise errors.ListExistsError('List already exists: %s' % listid)
+        # Find the domain object if it exists, and if not, create it
         try:
-            domainobj = schema.Domain.byName(listdomain)
+            domainobj = schema.Domain.byName(domain)
         except schema.SQLObjectNotFound:
-            domainobj = schema.Domain(name=listdomain,
-                                      mailhost='mail.%s' % listdomain,  # XXX 
Get this from config
+            domainobj = schema.Domain(name=domain,
                                       admins=schema.Roster())
-        self._mlist = schema.MailingList(
+        # Create all the required associated rows for this mailing list, then
+        # create an application level mailing list object that has the listid
+        # we just created.
+        mlist = schema.MailingList(
             name=listid,
             owners=schema.Roster(),
             moderators=schema.Roster(),
@@ -109,51 +81,40 @@
             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)
+            )
+        return cls.open(listid)
 
     def __getattr__(self, attrname):
-        """
-        Automagically fetch the appropriate attribute from the database.
-        """
+        """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
+            raise AttributeError(attrname)
         return result
 
     def __setattr__(self, attrname, attrvalue):
-        """
-        Automagically set the attribute in the database, creating the override
-        group if necessary.
+        """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
+            raise AttributeError('Read only attribute: %s' % attrname)
+        # Values for the mailing list always go in an overrides group.  Get
+        # the overrides if they exist, otherwise create an overrides group.
         try:
-            overrides = 
schema.AttributeGroup.byCollection_name(self._mlist.name)
+            overrides = schema.AttributeGroup.byCollection_name(
+                self._mlist.name)
         except schema.SQLObjectNotFound:
             overrides = schema.AttributeGroup(collection_name=self._mlist.name)
+        # From the overrides, get the attribute for the attribute it it
+        # exists.  If the attribute value exists, we'll change the value,
+        # otherwise we'll create a row with the value in it.
         fetched_values = list(schema.AttributeValue.select(
             (schema.AttributeValue.q.name == attrname) &
             (schema.AttributeValue.q.groupID == overrides.id)))
-        if len(fetched_values) == 0:
+        if not fetched_values:
+            # Attribute value does not yet exist
             value = schema.AttributeValue(name=attrname,
                                           group=overrides,
                                           value=attrvalue,
@@ -161,4 +122,3 @@
         else:
             value = fetched_values[0]
             value.set(value=attrvalue)
-

Modified: trunk/mailman/src/mailman/tests/test_app_mlist.py
==============================================================================
--- trunk/mailman/src/mailman/tests/test_app_mlist.py   (original)
+++ trunk/mailman/src/mailman/tests/test_app_mlist.py   Tue Mar 22 17:42:21 2005
@@ -30,7 +30,7 @@
         defaultsgroup = AttributeGroup(collection_name='default')
         # Domain-wide defaults
         domaingroup = AttributeGroup(collection_name='localhost')
-        mlist = MailingList('[EMAIL PROTECTED]')
+        mlist = MailingList.create('[EMAIL PROTECTED]')
         attr = Attribute(name='max_hold',
                          type='NUMBER',
                          default_value='10')
_______________________________________________
Mailman-checkins mailing list
[email protected]
Unsubscribe: 
http://mail.python.org/mailman/options/mailman-checkins/archive%40jab.org

Reply via email to