thread merge
[mailman.git] / mailman / database / listmanager.py
blobf37b7734ef60d933e5cb2da749b5034a18457af5
1 # Copyright (C) 2007-2008 by the Free Software Foundation, Inc.
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
16 # USA.
18 """A mailing list manager."""
20 import datetime
22 from zope.interface import implements
24 from mailman import Errors
25 from mailman.Utils import split_listname, fqdn_listname
26 from mailman.configuration import config
27 from mailman.database.mailinglist import MailingList
28 from mailman.interfaces import IListManager, ListAlreadyExistsError
32 class ListManager(object):
33 """An implementation of the `IListManager` interface."""
35 implements(IListManager)
37 def create(self, fqdn_listname):
38 """See `IListManager`."""
39 listname, hostname = split_listname(fqdn_listname)
40 mlist = config.db.store.find(
41 MailingList,
42 MailingList.list_name == listname,
43 MailingList.host_name == hostname).one()
44 if mlist:
45 raise ListAlreadyExistsError(fqdn_listname)
46 mlist = MailingList(fqdn_listname)
47 mlist.created_at = datetime.datetime.now()
48 config.db.store.add(mlist)
49 return mlist
51 def get(self, fqdn_listname):
52 """See `IListManager`."""
53 listname, hostname = split_listname(fqdn_listname)
54 mlist = config.db.store.find(MailingList,
55 list_name=listname,
56 host_name=hostname).one()
57 if mlist is not None:
58 # XXX Fixme
59 mlist._restore()
60 return mlist
62 def delete(self, mlist):
63 """See `IListManager`."""
64 config.db.store.remove(mlist)
66 @property
67 def mailing_lists(self):
68 """See `IListManager`."""
69 for fqdn_listname in self.names:
70 yield self.get(fqdn_listname)
72 @property
73 def names(self):
74 """See `IListManager`."""
75 for mlist in config.db.store.find(MailingList):
76 yield fqdn_listname(mlist.list_name, mlist.host_name)