Peter Sanchez avatar Peter Sanchez committed da3d816

Added ZoneHostList to paginate host entries. Needed for zones with more than 300 host's. #2

Comments (0)

Files changed (2)

zerigodns/__init__.py

 ''' Copyright 2009 Peter Sanchez <petersanchez@gmail.com>.  
     See BSD-LICENSE for license information.
 '''
-from api import ZerigoError, ZerigoNotFound, NSZone, NSHost, \
-                NSZoneTemplate, NSHostTemplate
-__version__ = '0.3'
+from api import ZerigoError, ZerigoNotFound, NSZone, ZoneHostList, \
+                NSHost, NSZoneTemplate, NSHostTemplate
+__version__ = '0.4'
         req_headers.update(headers)
         if method in ('POST', 'PUT'):
             req_headers['Content-Type'] = 'application/xml'
-            # XXX This is a hack. For some reason letting the Python 
-            # HTTPConnection.request() method set the Content-Length 
-            # header (default) was setting the length 2 bytes short.
-            # Manually set the length for now until we can look into it
-            #req_headers['Content-Length'] = (len(request) + 2)
         
         if self.is_debug:
             print 'DEBUG (method): ', method
         return (not self.has_errors())
 
 
+class ZoneHostList(list):
+    ''' Wrapper around list to paginate through a Zerigo zone's 
+        host list.
+    '''
+    _paginate_by = 100
+
+    def __init__(self, zone, *args, **kwargs):
+        self._zone = zone
+        self._host_paginator = NSHost(
+            zone.user,
+            zone.key,
+            is_secure=zone.is_secure,
+            is_debug=zone.is_debug,
+        )
+        super(ZoneHostList, self).__init__(*args, **kwargs)
+        if self.cur_count == 0 and self.host_count > 0:
+            self.load()
+
+    @property
+    def host_count(self):
+        return self._zone.hosts_count
+
+    @property
+    def cur_count(self):
+        return len(self)
+
+    @property
+    def paginate_by(self):
+        if self._paginate_by <= 0:
+            self._paginate_by = 100
+        elif self._paginate_by > 1000:
+            self._paginate_by = 1000
+        return self._paginate_by
+
+    @property
+    def cur_page(self):
+        return (self.cur_count / self.paginate_by) + 1
+
+    @property
+    def next_page(self):
+        if self.cur_count < self.host_count:
+            if self.cur_count == 0:
+                return self.cur_page
+            return self.cur_page + 1
+        return -1
+
+    @property
+    def num_pages(self):
+        return (self.host_count / self.paginate_by) + 1
+
+    @property
+    def all_pages(self):
+        return [(x + 1) for x in range(self.num_pages)]
+
+    def load(self):
+        if self.next_page > 0:
+            hosts = self._host_paginator.find_all(
+                self._zone, {
+                    'page': self.next_page,
+                    'per_page': self.paginate_by,
+                },
+            )
+            self.extend(hosts)
+            return hosts
+        return []
+
+    def load_all(self):
+        host_tmp = self.load()
+        while host_tmp:
+            host_tmp = self.load()
+        return True
+
+
 class NSZone(ZerigoDNS):
     ''' Class to manage zone entries 
     '''
     def __setattr__(self, name, val):
         if name == 'hosts' and isinstance(val, (list, tuple)):
             # Add hosts as NSHost objects
-            self.vals[name] = [
-                NSHost(
+            self.vals[name] = ZoneHostList(
+                self,
+                [NSHost(
                     self.user,
                     self.key,
                     data=zdata['host'],
                     is_secure=self.is_secure,
                     is_debug=self.is_debug,
-                ) for zdata in val
-            ]
+                ) for zdata in val],
+            )
         else:
             super(NSZone, self).__setattr__(name, val)
+
+    def load(self, data, reset=True):
+        super(NSZone, self).load(data, reset=reset)
+        host_count = getattr(self, 'host_count', 0)
+        if 'hosts' not in self.vals and host_count > 0:
+            setattr(self, 'hosts', ZoneHostList(self))
     
     def to_xml(self):
         return self._to_xml({'zone': self.get_xml_data()})
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.