Commits

Antoine Nguyen committed e7c0278

Simple object cache and minor improvements.

Comments (0)

Files changed (2)

+.pyc
+.egg-info

redminelib/client.py

         self.url = url
         self.apikey = apikey
         self.output_type = output_type
-
-        self.__objects_cache = {
-            "projects": {}
-        }
+        self.__objects_cache = {}
 
     def __request(self, uri, payload=None, **options):
         uri += ".%s" % self.output_type
             return ret
         return resp
 
-    def _get_list(self, objtype, **options):
+    def __set_custom_fields(self, obj):
+        csfields = {}
+        for cs in obj['custom_fields']:
+            csfields[cs['name']] = cs
+        obj['custom_fields'] = csfields
+
+    def __save_to_cache(self, objtype, obj):
+        if not objtype in self.__objects_cache:
+            self.__objects_cache[objtype] = {}
+        self.__objects_cache[objtype][obj["id"]] = obj
+
+    def _get_list(self, objtype, rooturi=None, **options):
         """Retrieve the full list of a given object type.
 
         Use this function with caution!
         """
         ret = []
-        resp = self.__request(objtype, **options)
+        url = "%s%s" % (rooturi if rooturi is not None else "", objtype)
+        resp = self.__request(url, **options)
         total = int(resp['total_count'])
-        limit = int(resp['limit'])
+        limit = int(resp['limit']) if 'limit' in resp else total
         offset = 0
-        #print "%s to retrieve: %s" % (objtype, total)
         while True:
             for obj in resp[objtype]:
-                self.__objects_cache[objtype][obj["identifier"]] = obj
+                if 'custom_fields' in obj:
+                    self.__set_custom_fields(obj)
+                self.__save_to_cache(objtype, obj)
                 ret.append(obj)
             offset += limit
             if offset >= total:
                 break
-            resp = self.__request(objtype, offset=offset, **options)
+            resp = self.__request(url, offset=offset, **options)
         return ret
 
     def users(self, **options):
         return self._get_list('time_entries', **options)
 
     def versions(self, project_id):
-        return self.__request("projects/%d/versions" % int(project_id))
+        return self._get_list("versions", "projects/%d/" % int(project_id))