diff options
author | Victor Shyba <victor1984@riseup.net> | 2017-07-13 03:33:48 -0300 |
---|---|---|
committer | drebs <drebs@leap.se> | 2017-07-21 10:58:47 -0300 |
commit | 7690dc61d1186b4c1b350566f0e6e1600165bf33 (patch) | |
tree | 3dbacd6f3489b8aa99f6d19bcb077ea2489a4016 | |
parent | f4ada0de252a44c21125811d19ab0d4d341381a3 (diff) |
[feature] LIST blobs - filter by flags
filter_flag parameter can now be used to filter listing blobs by a
specific flags. Eg: I can ask for blobs on incoming namespace flagged as pending.
-- Resolves: #8913
-rw-r--r-- | src/leap/soledad/server/_blobs.py | 19 | ||||
-rw-r--r-- | testing/tests/server/test_blobs_server.py | 32 |
2 files changed, 49 insertions, 2 deletions
diff --git a/src/leap/soledad/server/_blobs.py b/src/leap/soledad/server/_blobs.py index 5abb0b02..6f298ba7 100644 --- a/src/leap/soledad/server/_blobs.py +++ b/src/leap/soledad/server/_blobs.py @@ -148,7 +148,8 @@ class FilesystemBlobsBackend(object): count += len(filenames) return json.dumps({"count": count}) - def list_blobs(self, user, request, namespace='', order_by=None): + def list_blobs(self, user, request, namespace='', order_by=None, + filter_flag=False): blob_ids = [] base_path = self._get_path(user, custom_preffix=namespace) for root, dirs, filenames in os.walk(base_path): @@ -160,9 +161,21 @@ class FilesystemBlobsBackend(object): blob_ids.sort(key=lambda x: os.path.getmtime(x), reverse=True) elif order_by: raise Exception("Unsupported order_by parameter: %s" % order_by) + if filter_flag: + blob_ids = list(self._filter_flag(blob_ids, filter_flag)) blob_ids = [os.path.basename(path) for path in blob_ids] return json.dumps(blob_ids) + def _filter_flag(self, blob_paths, flag): + for blob_path in blob_paths: + flag_path = blob_path + '.flags' + if not os.path.isfile(flag_path): + continue + with open(flag_path, 'r') as flags_file: + blob_flags = json.loads(flags_file.read()) + if flag in blob_flags: + yield blob_path + def get_total_storage(self, user): return self._get_disk_usage(self._get_path(user)) @@ -238,8 +251,10 @@ class BlobsResource(resource.Resource): return self._handler.count(user, request, namespace) elif not blob_id: order = request.args.get('order_by', [None])[0] + filter_flag = request.args.get('filter_flag', [False])[0] return self._handler.list_blobs(user, request, namespace, - order_by=order) + order_by=order, + filter_flag=filter_flag) if 'only_flags' in request.args: return self._handler.get_flags(user, blob_id, request, namespace) self._handler.add_tag_header(user, blob_id, request) diff --git a/testing/tests/server/test_blobs_server.py b/testing/tests/server/test_blobs_server.py index 249d76bc..869ef9a0 100644 --- a/testing/tests/server/test_blobs_server.py +++ b/testing/tests/server/test_blobs_server.py @@ -70,6 +70,38 @@ class BlobServerTestCase(unittest.TestCase): @defer.inlineCallbacks @pytest.mark.usefixtures("method_tmpdir") + def test_list_filter_flag(self): + manager = BlobManager('', self.uri, self.secret, + self.secret, 'user') + fd = BytesIO("flag me") + yield manager._encrypt_and_upload('blob_id', fd) + yield manager.set_flags('blob_id', [Flags.PROCESSING]) + blobs_list = yield manager.remote_list(filter_flag=Flags.PENDING) + self.assertEquals([], blobs_list) + blobs_list = yield manager.remote_list(filter_flag=Flags.PROCESSING) + self.assertEquals(['blob_id'], blobs_list) + + @defer.inlineCallbacks + @pytest.mark.usefixtures("method_tmpdir") + def test_list_filter_flag_order_by_date(self): + manager = BlobManager('', self.uri, self.secret, + self.secret, 'user') + yield manager._encrypt_and_upload('blob_id1', BytesIO("x")) + yield manager._encrypt_and_upload('blob_id2', BytesIO("x")) + yield manager._encrypt_and_upload('blob_id3', BytesIO("x")) + yield manager.set_flags('blob_id1', [Flags.PROCESSING]) + yield manager.set_flags('blob_id2', [Flags.PROCESSING]) + yield manager.set_flags('blob_id3', [Flags.PROCESSING]) + blobs_list = yield manager.remote_list(filter_flag=Flags.PROCESSING, + order_by='+date') + expected_list = ['blob_id1', 'blob_id2', 'blob_id3'] + self.assertEquals(expected_list, blobs_list) + blobs_list = yield manager.remote_list(filter_flag=Flags.PROCESSING, + order_by='-date') + self.assertEquals(list(reversed(expected_list)), blobs_list) + + @defer.inlineCallbacks + @pytest.mark.usefixtures("method_tmpdir") def test_cant_set_invalid_flags(self): manager = BlobManager('', self.uri, self.secret, self.secret, 'user') |