From 4acd03e714d9c5227f1b14c80aa1f515757b4eff Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 21 Dec 2021 09:55:06 +0100 Subject: [PATCH 1/2] fixing ereuse pdf --- .../resources/documents/documents.py | 41 +++++++++++-------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/ereuse_devicehub/resources/documents/documents.py b/ereuse_devicehub/resources/documents/documents.py index 83d8ebf5..b894e3a1 100644 --- a/ereuse_devicehub/resources/documents/documents.py +++ b/ereuse_devicehub/resources/documents/documents.py @@ -61,23 +61,29 @@ class DocumentView(DeviceView): args = self.QUERY_PARSER.parse(self.find_args, flask.request, locations=('querystring',)) - if id: - # todo we assume we can pass both device id and action id - # for certificates... how is it going to end up being? - try: - id = uuid.UUID(id) - except ValueError: - try: - id = int(id) - except ValueError: - raise teal.marshmallow.ValidationError('Document must be an ID or UUID.') - else: - query = devs.Device.query.filter_by(id=id) - else: - query = evs.Action.query.filter_by(id=id) - else: - flask.current_app.auth.requires_auth(lambda: None)() # todo not nice - query = self.query(args) + ids = [] + if 'filter' in request.args: + filters = json.loads(request.args.get('filter', {})) + ids = filters.get('ids', []) + query = devs.Device.query.filter(Device.id.in_(ids)) + + # if id: + # # todo we assume we can pass both device id and action id + # # for certificates... how is it going to end up being? + # try: + # id = uuid.UUID(id) + # except ValueError: + # try: + # id = int(id) + # except ValueError: + # raise teal.marshmallow.ValidationError('Document must be an ID or UUID.') + # else: + # query = devs.Device.query.filter_by(id=id) + # else: + # query = evs.Action.query.filter_by(id=id) + # else: + # flask.current_app.auth.requires_auth(lambda: None)() # todo not nice + # query = self.query(args) type = urlutils.URL(flask.request.url).path_parts[-2] if type == 'erasures': @@ -93,6 +99,7 @@ class DocumentView(DeviceView): @staticmethod def erasure(query: db.Query): + # import pdb; pdb.set_trace() def erasures(): for model in query: if isinstance(model, devs.Computer): From 47dcb5efe86e7471b40e20ec890e62db8716c2f1 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 21 Dec 2021 10:32:04 +0100 Subject: [PATCH 2/2] fixing tests --- .../resources/documents/documents.py | 22 +++++++++++++++++-- tests/test_documents.py | 8 +++---- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/ereuse_devicehub/resources/documents/documents.py b/ereuse_devicehub/resources/documents/documents.py index b894e3a1..1a1584e8 100644 --- a/ereuse_devicehub/resources/documents/documents.py +++ b/ereuse_devicehub/resources/documents/documents.py @@ -65,7 +65,26 @@ class DocumentView(DeviceView): if 'filter' in request.args: filters = json.loads(request.args.get('filter', {})) ids = filters.get('ids', []) - query = devs.Device.query.filter(Device.id.in_(ids)) + + if not ids and not id: + msg = 'Document must be an ID or UUID.' + raise teal.marshmallow.ValidationError(msg) + + if id: + try: + id = uuid.UUID(id) + except ValueError: + try: + ids.append(int(id)) + except ValueError: + msg = 'Document must be an ID or UUID.' + raise teal.marshmallow.ValidationError(msg) + else: + query = devs.Device.query.filter(Device.id.in_(ids)) + else: + query = evs.Action.query.filter_by(id=id) + else: + query = devs.Device.query.filter(Device.id.in_(ids)) # if id: # # todo we assume we can pass both device id and action id @@ -99,7 +118,6 @@ class DocumentView(DeviceView): @staticmethod def erasure(query: db.Query): - # import pdb; pdb.set_trace() def erasures(): for model in query: if isinstance(model, devs.Computer): diff --git a/tests/test_documents.py b/tests/test_documents.py index bf11bb4a..0db7d716 100644 --- a/tests/test_documents.py +++ b/tests/test_documents.py @@ -70,7 +70,7 @@ def test_erasure_certificate_private_query(user: UserClient): doc, response = user.get(res=documents.DocumentDef.t, item='erasures/', query=[ - ('filter', {'id': [snapshot['device']['id']]})], + ('filter', {'ids': [snapshot['device']['id']]})], accept=ANY) assert 'html' in response.content_type assert '