------------------------------------------------------------
revno: 42
committer: Florian Fuchs <[email protected]>
branch nick: mailman.client
timestamp: Thu 2013-03-21 14:17:54 -0700
message:
  added user pagination
modified:
  src/mailmanclient/_client.py
  src/mailmanclient/docs/using.txt


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

Your team Mailman Coders is subscribed to branch lp:mailman.client.
To unsubscribe from this branch go to 
https://code.launchpad.net/~mailman-coders/mailman.client/trunk/+edit-subscription
=== modified file 'src/mailmanclient/_client.py'
--- src/mailmanclient/_client.py	2013-03-19 21:18:04 +0000
+++ src/mailmanclient/_client.py	2013-03-21 21:17:54 +0000
@@ -39,6 +39,9 @@
 from mailmanclient import __version__
 
 
+DEFAULT_PAGE_ITEM_COUNT = 50
+
+
 def _member_key(member_dict):
     """Return the keys for sorting a member.
 
@@ -189,6 +192,9 @@
                 for entry in sorted(content['entries'],
                                     key=itemgetter('self_link'))]
 
+    def get_user_page(self, count=50, page=1):
+        return _Page(self._connection, 'users', _User, count, page)
+
     def create_domain(self, mail_host, base_url=None,
                       description=None, contact_address=None):
         data = dict(mail_host=mail_host)
@@ -765,7 +771,7 @@
 
     def __iter__(self):
         for preference in self._preferences:
-            yield _Preference(self._connection, preference)
+            yield _Preferences(self._connection, preference)
 
 
 LIST_READ_ONLY_ATTRS = ('bounces_address', 'created_at', 'digest_last_sent_at',
@@ -817,3 +823,53 @@
             if attribute not in LIST_READ_ONLY_ATTRS:
                 data[attribute] = value
         response, content = self._connection.call(self._url, data, 'PATCH')
+
+
+class _Page:
+    def __init__(self, connection, path, model, count=DEFAULT_PAGE_ITEM_COUNT,
+                 page=1):
+        self._connection = connection
+        self._path = path
+        self._count = count
+        self._page = page
+        self._model = model
+        self._entries = []
+        self._create_page()
+
+    def __getitem__(self, key):
+        return self._entries[key]
+
+    def __iter__(self):
+        for entry in self._entries:
+            yield entry
+
+    def __repr__(self):
+        return '<Page {0} ({1})'.format(self._page, self._model)
+
+    def _create_page(self):
+        self._entries = []
+        # create url
+        path = '{0}?count={1}&page={2}'.format(self._path, self._count,
+                                               self._page)
+        response, content = self._connection.call(path)
+        if 'entries' in content:
+            for entry in content['entries']:
+                self._entries.append(self._model(self._connection,
+                                     entry['self_link']))
+
+    @property
+    def nr(self):
+        return self._page
+
+    @property
+    def next(self):
+        self._page += 1
+        self._create_page()
+        return self
+
+    @property
+    def previous(self):
+        if self._count > 0:
+            self._page -= 1
+            self._create_page()
+            return self

=== modified file 'src/mailmanclient/docs/using.txt'
--- src/mailmanclient/docs/using.txt	2013-03-19 21:18:04 +0000
+++ src/mailmanclient/docs/using.txt	2013-03-21 21:17:54 +0000
@@ -43,13 +43,14 @@
 It's easy to create a new domain; when you do, a proxy object for that domain
 is returned.
 
-    >>> example_dot_com = client.create_domain('example.com')
+    >>> example_dot_com = client.create_domain('example.com',
+    ...     contact_address='[email protected]')
     >>> example_dot_com
     <Domain "example.com">
     >>> print example_dot_com.base_url
     http://example.com
     >>> print example_dot_com.contact_address
-    [email protected]
+    [email protected]
     >>> print example_dot_com.description
     None
     >>> print example_dot_com.mail_host
@@ -269,6 +270,36 @@
     <User "..." (...)>
     <User "..." (...)>
 
+The users can also be paginated:
+
+    >>> page = client.get_user_page(count=2, page=1)
+    >>> page.nr
+    1
+
+    >>> for user in page:
+    ...     print user
+    <User "Anna" (...)>
+    <User "Bill" (...)>
+
+You can get the next or previous pages without calling ``get_userpage`` again.
+
+    >>> page = page.next
+    >>> page.nr
+    2
+
+    >>> for user in page:
+    ...     print user
+    <User "Cris" (...)>
+
+    >>> page = page.previous
+    >>> page.nr
+    1
+
+    >>> for user in page:
+    ...     print user
+    <User "Anna" (...)>
+    <User "Bill" (...)>
+
 A single user can be retrieved using their email address.
 
     >>> cris = client.get_user('[email protected]')
@@ -345,7 +376,7 @@
 User Subscriptions
 ------------------
 
-A Users subscriptions can be access through their ``subscriptions`` property.
+A User's subscriptions can be access through their ``subscriptions`` property.
 
     >>> bill = client.get_user('[email protected]')
     >>> for subscription in bill.subscriptions:

_______________________________________________
Mailman-coders mailing list
[email protected]
http://mail.python.org/mailman/listinfo/mailman-coders

Reply via email to