From 294afba2ac98990465022000a231c299ca37dd2b Mon Sep 17 00:00:00 2001 From: David Snopek Date: Wed, 31 Jul 2013 18:17:58 +0100 Subject: [PATCH] Added a quick way to get a list of all collections. --- AnkiServer/apps/rest_app.py | 7 +++++++ tests/test_rest_app.py | 9 +++++++++ 2 files changed, 16 insertions(+) diff --git a/AnkiServer/apps/rest_app.py b/AnkiServer/apps/rest_app.py index a4fca4d..58d25bb 100644 --- a/AnkiServer/apps/rest_app.py +++ b/AnkiServer/apps/rest_app.py @@ -120,6 +120,10 @@ class RestApp(object): if hasReturnValue: return ret + def list_collections(self): + """Returns an array of valid collection names in our self.data_path.""" + return [x for x in os.listdir(self.data_root) if os.path.exists(os.path.join(self.data_root, x, 'collection.anki2'))] + def _checkRequest(self, req): """Raises an exception if the request isn't allowed or valid for some reason.""" if self.allowed_hosts != '*': @@ -234,6 +238,9 @@ class RestApp(object): # make sure the request is valid self._checkRequest(req) + if req.path == '/list_collections': + return Response(json.dumps(self.list_collections()), content_type='application/json') + # parse the path type, name, ids = self._parsePath(req.path) diff --git a/tests/test_rest_app.py b/tests/test_rest_app.py index 15aa53a..5033512 100644 --- a/tests/test_rest_app.py +++ b/tests/test_rest_app.py @@ -33,6 +33,15 @@ class RestAppTest(unittest.TestCase): self.collection_manager = None self.rest_app = None shutil.rmtree(self.temp_dir) + + def test_list_collections(self): + os.mkdir(os.path.join(self.temp_dir, 'test1')) + os.mkdir(os.path.join(self.temp_dir, 'test2')) + + with open(os.path.join(self.temp_dir, 'test1', 'collection.anki2'), 'wt') as fd: + fd.write('Testing!') + + self.assertEqual(self.rest_app.list_collections(), ['test1']) def test_parsePath(self): tests = [