------------------------------------------------------------
revno: 6767
committer: Barry Warsaw <[email protected]>
branch nick: 3.0
timestamp: Wed 2009-08-12 22:52:53 -0400
message:
  Add command line creation of mailing lists.
added:
  src/mailman/commands/docs/create.txt
modified:
  src/mailman/commands/cli_lists.py


--
lp:mailman
https://code.launchpad.net/~mailman-coders/mailman/3.0

Your team Mailman Checkins is subscribed to branch lp:mailman.
To unsubscribe from this branch go to 
https://code.launchpad.net/~mailman-coders/mailman/3.0/+edit-subscription.
=== modified file 'src/mailman/commands/cli_lists.py'
--- src/mailman/commands/cli_lists.py	2009-08-09 15:25:59 +0000
+++ src/mailman/commands/cli_lists.py	2009-08-13 02:52:53 +0000
@@ -21,15 +21,24 @@
 
 __metaclass__ = type
 __all__ = [
+    'Create',
     'Lists',
     ]
 
 
 from zope.interface import implements
 
+from mailman.Utils import maketext
+from mailman.app.lifecycle import create_list
 from mailman.config import config
-from mailman.i18n import _
+from mailman.constants import system_preferences
+from mailman.core.errors import InvalidEmailAddress
+from mailman.email.message import UserNotification
+from mailman.i18n import _, using_language
 from mailman.interfaces.command import ICLISubCommand
+from mailman.interfaces.domain import (
+    BadDomainSpecificationError, IDomainManager)
+from mailman.interfaces.listmanager import ListAlreadyExistsError
 
 
 
@@ -42,6 +51,7 @@
         """See `ICLISubCommand`."""
         lists_parser = subparser.add_parser(
             'lists', help=_('List all mailing lists'))
+        lists_parser.set_defaults(func=self.process)
         lists_parser.add_argument(
             '-a', '--advertised',
             default=False, action='store_true',
@@ -62,7 +72,6 @@
             default=False, action='store_true',
             help=_(
                 'Show the full mailing list name (i.e. the posting address'))
-        lists_parser.set_defaults(func=self.process)
 
     def process(self, args):
         """See `ICLISubCommand`."""
@@ -73,7 +82,8 @@
             mlist = list_manager.get(fqdn_name)
             if args.advertised and not mlist.advertised:
                 continue
-            if args.domains and mlist.host_name not in args.domains:
+            domains = getattr(args, 'domains', None)
+            if domains and mlist.host_name not in domains:
                 continue
             mailing_lists.append(mlist)
         # Maybe no mailing lists matched.
@@ -100,3 +110,109 @@
                                else _('[no description available]'))
                 print '{0:{2}} - {1:{3}}'.format(
                     name, description, longest, 77 - longest)
+
+
+
+class Create:
+    """The `create` subcommand."""
+
+    implements(ICLISubCommand)
+
+    def add(self, parser, subparser):
+        """See `ICLISubCommand`."""
+        self.parser = parser
+        create_parser = subparser.add_parser(
+            'create', help=_('Create a mailing list'))
+        create_parser.set_defaults(func=self.process)
+        create_parser.add_argument(
+            '--language',
+            type='unicode', metavar='CODE', help=_("""\
+            Set the list's preferred language to CODE, which must be a
+            registered two letter language code."""))
+        create_parser.add_argument(
+            '-o', '--owner',
+            type='unicode', action='append', default=[],
+            dest='owners', metavar='OWNER', help=_("""\
+            Specify a listowner email address.  If the address is not
+            currently registered with Mailman, the address is registered and
+            linked to a user.  Mailman will send a confirmation message to the
+            address, but it will also send a list creation notice to the
+            address.  More than one owner can be specified."""))
+        create_parser.add_argument(
+            '-n', '--notify',
+            default=False, action='store_true',
+            help=_("""\
+            Notify the list owner by email that their mailing list has been
+            created."""))
+        create_parser.add_argument(
+            '-q', '--quiet',
+            default=False, action='store_true',
+            help=_('Print less output.'))
+        create_parser.add_argument(
+            '-d', '--domain',
+            default=False, action='store_true',
+            help=_("""\
+            Register the mailing list's domain if not yet registered."""))
+        # Required positional argument.
+        create_parser.add_argument(
+            'listname', metavar='LISTNAME', nargs=1,
+            help=_("""\
+            The 'fully qualified list name', i.e. the posting address of the
+            mailing list.  It must be a valid email address and the domain
+            must be registered with Mailman.  List names are forced to lower
+            case."""))
+
+    def process(self, args):
+        """See `ICLISubCommand`."""
+        language_code = (args.language
+                         if args.language is not None
+                         else system_preferences.preferred_language.code)
+        # Make sure that the selected language code is known.
+        if language_code not in config.languages.codes:
+            self.parser.error(_('Invalid language code: $language_code'))
+            return
+        assert len(args.listname) == 1, (
+            'Unexpected positional arguments: %s' % args.listname)
+        # Check to see if the domain exists or not.
+        fqdn_listname = args.listname[0]
+        listname, at, domain = fqdn_listname.partition('@')
+        domain_mgr = IDomainManager(config)
+        if domain_mgr.get(domain) is None and args.domain:
+            domain_mgr.add(domain)
+        try:
+            mlist = create_list(fqdn_listname, args.owners)
+        except InvalidEmailAddress:
+            self.parser.error(_('Illegal list name: $fqdn_listname'))
+            return
+        except ListAlreadyExistsError:
+            self.parser.error(_('List already exists: $fqdn_listname'))
+            return
+        except BadDomainSpecificationError, domain:
+            self.parser.error(_('Undefined domain: $domain'))
+            return
+        # Find the language associated with the code, then set the mailing
+        # list's preferred language to that.  The changes then must be
+        # committed to the database.
+        mlist.preferred_language = config.languages[language_code]
+        config.db.commit()
+        # Do the notification.
+        if not args.quiet:
+            print _('Created mailing list: $mlist.fqdn_listname')
+        if args.notify:
+            d = dict(
+                listname        = mlist.fqdn_listname,
+                admin_url       = mlist.script_url('admin'),
+                listinfo_url    = mlist.script_url('listinfo'),
+                requestaddr     = mlist.request_address,
+                siteowner       = mlist.no_reply_address,
+                )
+            text = maketext('newlist.txt', d, mlist=mlist)
+            # Set the I18N language to the list's preferred language so the
+            # header will match the template language.  Stashing and restoring
+            # the old translation context is just (healthy? :) paranoia.
+            with using_language(mlist.preferred_language.code):
+                msg = UserNotification(
+                    owner_mail, mlist.no_reply_address,
+                    _('Your new mailing list: $fqdn_listname'),
+                    text, mlist.preferred_language)
+                msg.send(mlist)

=== added file 'src/mailman/commands/docs/create.txt'
--- src/mailman/commands/docs/create.txt	1970-01-01 00:00:00 +0000
+++ src/mailman/commands/docs/create.txt	2009-08-13 02:52:53 +0000
@@ -0,0 +1,45 @@
+==========================
+Command line list creation
+==========================
+
+A system administrator can create mailing lists from the command line.
+
+    >>> class FakeArgs:
+    ...     language = None
+    ...     owners = []
+    ...     quiet = False
+    ...     domain = None
+    ...     listname = None
+    ...     notify = False
+    >>> args = FakeArgs()
+
+You cannot create a mailing list in an unknown domain...
+
+    >>> from mailman.commands.cli_lists import Create
+    >>> command = Create()
+
+    >>> class FakeParser:
+    ...     def error(self, message):
+    ...         print message
+    >>> command.parser = FakeParser()
+
+    >>> args.listname = ['[email protected]']
+    >>> command.process(args)
+    Undefined domain: example.xx
+
+...although you can tell Mailman to auto-register the domain.  In that case,
+the mailing list and domain will be created.
+
+    >>> args.domain = True
+    >>> command.process(args)
+    Created mailing list: [email protected]
+
+Now both the domain and the mailing list exist in the database.
+
+    >>> config.db.list_manager.get('[email protected]')
+    <mailing list "[email protected]" at ...>
+
+    >>> from mailman.interfaces.domain import IDomainManager
+    >>> IDomainManager(config).get('example.xx')
+    <Domain example.xx, base_url: http://example.xx,
+            contact_address: [email protected]>

_______________________________________________
Mailman-checkins mailing list
[email protected]
Unsubscribe: 
http://mail.python.org/mailman/options/mailman-checkins/archive%40jab.org

Reply via email to