通过Python/Django管理控制台访问Mailman 3列表成员

y0u0uwnf  于 2022-12-05  发布在  Go
关注(0)|答案(2)|浏览(118)

我尝试直接从Debian Bullseye上的Django管理控制台访问Mailman 3邮件列表的成员,其中Mailman是从deb包(mailman3-full)安装的。我可以这样连接到Django管理控制台(所有3个变体似乎都工作正常):

$ /usr/share/mailman3-web/manage.py shell
$ mailman-web shell
$ mailman-web shell --settings /etc/mailman3/mailman-web.py
Python 3.9.2 (default, Feb 28 2021, 17:03:44)
>>>

但是在Django管理控制台中,一些 Postman 组件似乎丢失了。
我尝试访问列表管理器,如下所述:Docs > Models > The mailing list manager

>>> from mailman.interfaces.listmanager import IListManager
>>> from zope.component import getUtility
>>> list_manager = getUtility(IListManager)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/zope/component/_api.py", line 169, in getUtility
    raise ComponentLookupError(interface, name)
zope.interface.interfaces.ComponentLookupError: (<InterfaceClass mailman.interfaces.listmanager.IListManager>, '')

不知道为什么会发生这种情况。
还尝试使用ListManager实现访问列表:

>>> from mailman.config import config
>>> from mailman.model.listmanager import ListManager
>>> list_manager = ListManager()

>>> list_manager.get('mynews@example.com')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/mailman/database/transaction.py", line 85, in wrapper
    return function(args[0], config.db.store, *args[1:], **kws)
AttributeError: 'NoneType' object has no attribute 'store'

>>> list_manager.get_by_list_id('mynews.example.com')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/mailman/database/transaction.py", line 85, in wrapper
    return function(args[0], config.db.store, *args[1:], **kws)
AttributeError: 'NoneType' object has no attribute 'store'

我在这里做错了什么?如果我还没有了解到这一点,Mailman 3模型文档中的任何示例都不起作用。
任何帮助都不胜感激!

mrzz3bfm

mrzz3bfm1#

只是你用错了shell,你应该用Mailman core shell
它很可能只通过系统中的mailman shell访问。

q9rjltbz

q9rjltbz2#

因此mailman shell工作得很好,我可以交互式地运行它:

from mailman.interfaces.listmanager import IListManager
from zope.component import getUtility
from mailman.testing.documentation import dump_list
from operator import attrgetter

def dump_members(roster):
    all_addresses = list(member.address for member in roster)
    sorted_addresses = sorted(all_addresses, key=attrgetter('email'))
    dump_list(sorted_addresses)

list_manager = getUtility(IListManager)
mlist = list_manager.get('ant@example.com')
dump_members(mlist.members.members)

但如何将其放入可以使用mailman withlist -r listmembers -l ant@example.com运行脚本中呢?

from mailman.testing.documentation import dump_list
from operator import attrgetter

def listmembers(mlist):
    roster = mlist.members.members
    all_addresses = list(member.address for member in roster)
    sorted_addresses = sorted(all_addresses, key=attrgetter('email'))
    dump_list(sorted_addresses)

我应该把这样一个listmembers.py runner放在哪里?我试着把它放在/usr/lib/python3/dist-packages/mailman/runners目录下,但是没有成功:

$ mailman withlist -r listmembers -l ant@example.com
ModuleNotFoundError: No module named 'listmembers'

谢谢你!

相关问题