Commit 48cf8cf9 authored by Florian Fuchs's avatar Florian Fuchs

- Added handling of MM3's user API resource.

- Added user addresses lookup.
parent 459b66b6
......@@ -47,7 +47,6 @@ def _member_key(member_dict):
return (member_dict['fqdn_listname'], member_dict['address'])
class _Connection:
"""A connection to the REST client."""
......@@ -166,6 +165,15 @@ class Client:
for entry in sorted(content['entries'],
key=_member_key)]
@property
def users(self):
response, content = self._connection.call('users')
if 'entries' not in content:
return []
return [_User(self._connection, entry['self_link'])
for entry in sorted(content['entries'],
key=itemgetter('real_name'))]
def create_domain(self, mail_host, base_url=None,
description=None, contact_address=None):
data = dict(mail_host=mail_host)
......@@ -195,6 +203,11 @@ class Client:
return domain
return []
def get_user(self, address):
response, content = self._connection.call(
'users/{0}'.format(address))
return _User(self._connection, content['self_link'])
def get_list(self, fqdn_listname):
response, content = self._connection.call(
'lists/{0}'.format(fqdn_listname))
......@@ -204,7 +217,6 @@ class Client:
response, content = self._connection.call(
'lists/{0}'.format(fqdn_listname), None, 'DELETE')
class _Domain:
def __init__(self, connection, url):
......@@ -416,12 +428,61 @@ class _Member:
self._connection.call(self.self_link, method='DELETE')
READ_ONLY_ATTRS = ('bounces_address', 'created_at', 'digest_last_sent_at',
'fqdn_listname', 'http_etag', 'mail_host', 'join_address',
'last_post_at', 'leave_address', 'list_id', 'list_name',
'next_digest_number', 'no_reply_address', 'owner_address',
'post_id', 'posting_address', 'request_address', 'scheme',
'volume', 'web_host',)
class _User:
def __init__(self, connection, url):
self._connection = connection
self._url = url
self._info = None
self._addresses = None
def __repr__(self):
return '<User "{0}" ({1})>'.format(
self.real_name, self.user_id)
def _get_info(self):
if self._info is None:
response, content = self._connection.call(self._url)
self._info = content
def _get_addresses(self):
if self._addresses is None:
response, content = self._connection.call('users/{0}/addresses'.format(self.user_id))
if 'entries' not in content:
self._addresses = []
self._addresses = content['entries']
@property
def addresses(self):
self._get_addresses()
return self._addresses
@property
def real_name(self):
self._get_info()
return self._info['real_name']
@property
def user_id(self):
self._get_info()
return self._info['user_id']
@property
def created_on(self):
self._get_info()
return self._info['created_on']
@property
def self_link(self):
self._get_info()
return self._info['self_link']
LIST_READ_ONLY_ATTRS = ('bounces_address', 'created_at', 'digest_last_sent_at',
'fqdn_listname', 'http_etag', 'mail_host', 'join_address',
'last_post_at', 'leave_address', 'list_id', 'list_name',
'next_digest_number', 'no_reply_address', 'owner_address',
'post_id', 'posting_address', 'request_address', 'scheme',
'volume', 'web_host',)
class _Settings():
......@@ -455,7 +516,7 @@ class _Settings():
def save(self):
data = {}
for attribute, value in self._info.items():
if attribute not in READ_ONLY_ATTRS:
if attribute not in LIST_READ_ONLY_ATTRS:
data[attribute] = value
response, content = self._connection.call(self._url, data, 'PATCH')
......
......@@ -248,6 +248,30 @@ If you try to unsubscribe an address which is not a member address `ValueError`
ValueError: nomember@example.com is not a member address of test-one@example.com
Users
=====
Users are people with one or more list memberhips. To get a list of all users, access the clients user property.
>>> for user in client.users:
... print user
<User "Anna" (...)>
<User "Bill" (...)>
<User "Cris" (...)>
A single user can be retrieved using their email address.
>>> cris = client.get_user('cris@example.com')
>>> print cris.real_name
Cris
Every user has a list of one or more addresses.
>>> for address in cris.addresses:
... print address['email']
cris@example.com
List Settings
=============
......
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