Commit 8b9c3a90 authored by Barry Warsaw's avatar Barry Warsaw

Members and subscriptions.

parent 4ef8888b
......@@ -37,6 +37,16 @@ from urlparse import urljoin
from mailman.client import __version__
def _member_key(member_dict):
"""Return the keys for sorting a member.
:param member_dict: The JSON dictionary for a member.
:return: 2-tuple of (fqdn_listname, address)
"""
return (member_dict['fqdn_listname'], member_dict['address'])
class _Connection:
"""A connection to the REST client."""
......@@ -148,6 +158,15 @@ class Client:
for entry in sorted(content['entries'],
key=itemgetter('url_host'))]
@property
def members(self):
response, content = self._connection.call('members')
if 'entries' not in content:
return []
return [_Member(self._connection, entry['self_link'])
for entry in sorted(content['entries'],
key=_member_key)]
def create_domain(self, email_host, base_url=None,
description=None, contact_address=None):
data = dict(email_host=email_host)
......@@ -252,3 +271,46 @@ class _List:
def real_name(self):
self._get_info()
return self._info['real_name']
def subscribe(self, address, real_name=None):
"""Subscribe an email address to a mailing list.
:param address: Email address to subscribe to the list.
:type address: str
:param real_name: The real name of the new member.
:type real_name: str
"""
data = dict(
fqdn_listname=self.fqdn_listname,
address=address,
real_name=real_name,
)
response, content = self._connection.call('members', data)
return _Member(self._connection, response['location'])
class _Member:
def __init__(self, connection, url):
self._connection = connection
self._url = url
self._info = None
def __repr__(self):
return '<Member "{0}" on "{1}">'.format(
self.address, self.fqdn_listname)
def _get_info(self):
if self._info is None:
response, content = self._connection.call(self._url)
self._info = content
@property
def fqdn_listname(self):
self._get_info()
return self._info['fqdn_listname']
@property
def address(self):
self._get_info()
return self._info['address']
......@@ -111,10 +111,49 @@ And you can print all the known mailing lists.
<List "test-three@example.net">
>>> example.create_list('test-three')
<List "test-three@example.com">
>>> for mlist in client.lists:
... print mlist
<List "test-one@example.com">
<List "test-three@example.com">
<List "test-three@example.net">
<List "test-two@example.com">
Membership
==========
Email addresses can subscribe to existing mailing lists, becoming members of
that list. The address is a unique id for a specific user in the system, and
a member is a user that is subscribed to a mailing list. Email addresses need
not be pre-registered, though the auto-registered user will be unique for each
email address.
The system starts out with no members.
>>> client.members
[]
New members can be easily added; users are automatically registered.
::
>>> test_two = client.get_list('test-two@example.com')
>>> test_one.subscribe('anna@example.com', 'Anna')
<Member "anna@example.com" on "test-one@example.com">
>>> test_one.subscribe('bill@example.com', 'Bill')
<Member "bill@example.com" on "test-one@example.com">
>>> test_two.subscribe('anna@example.com')
<Member "anna@example.com" on "test-two@example.com">
>>> test_two.subscribe('cris@example.com', 'Cris')
<Member "cris@example.com" on "test-two@example.com">
We can retrieve all known memberships. These are sorted first by mailing list
name, then by email address.
>>> for member in client.members:
... print member
<Member "anna@example.com" on "test-one@example.com">
<Member "bill@example.com" on "test-one@example.com">
<Member "anna@example.com" on "test-two@example.com">
<Member "cris@example.com" on "test-two@example.com">
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment