From b524ec739886ec9f21e2717936cbdf75af2a64b0 Mon Sep 17 00:00:00 2001 From: nad Date: Tue, 28 Jul 2020 16:14:56 +0200 Subject: [PATCH 1/8] Creating export lots test --- tests/test_documents.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/test_documents.py b/tests/test_documents.py index 0b40bf23..2917a536 100644 --- a/tests/test_documents.py +++ b/tests/test_documents.py @@ -9,6 +9,7 @@ from pathlib import Path from ereuse_devicehub.client import Client, UserClient from ereuse_devicehub.resources.action.models import Snapshot from ereuse_devicehub.resources.documents import documents +from ereuse_devicehub.resources.lot.models import Lot from tests.conftest import file @@ -223,3 +224,21 @@ def test_export_multiple_different_devices(user: UserClient): del row[8] assert fixture_csv == export_csv + + +@pytest.mark.mvp +def test_get_document_lots(user: UserClient): + """Tests submitting and retreiving all lots.""" + + l, _ = user.post({'name': 'Lot1', 'description': 'comments,lot1,testcomment,'}, res=Lot) + l, _ = user.post({'name': 'Lot2', 'description': 'comments,lot2,testcomment,'}, res=Lot) + l, _ = user.post({'name': 'Lot3', 'description': 'comments,lot3,testcomment,'}, res=Lot) + + csv_str, _ = user.get(res=documents.DocumentDef.t, + item='lots/', + accept='text/csv') + f = StringIO(csv_str) + obj_csv = csv.reader(f, f) + export_csv = list(obj_csv) + assert len(export_csv) == 4 + assert export_csv[0] == ['Id', 'Name', 'Registered in', 'Description'] From c0fcfcfdbf813b9aaca242b5e6f81338b7b83e66 Mon Sep 17 00:00:00 2001 From: nad Date: Tue, 28 Jul 2020 16:16:17 +0200 Subject: [PATCH 2/8] Creating 'documents/lots/' endpoint --- .../resources/documents/documents.py | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/ereuse_devicehub/resources/documents/documents.py b/ereuse_devicehub/resources/documents/documents.py index e95a9694..aa61dbb0 100644 --- a/ereuse_devicehub/resources/documents/documents.py +++ b/ereuse_devicehub/resources/documents/documents.py @@ -2,6 +2,7 @@ import csv import datetime import enum import uuid +from collections import OrderedDict from io import StringIO from typing import Callable, Iterable, Tuple @@ -19,6 +20,8 @@ from ereuse_devicehub.resources.action import models as evs from ereuse_devicehub.resources.device import models as devs from ereuse_devicehub.resources.device.views import DeviceView from ereuse_devicehub.resources.documents.device_row import DeviceRow +from ereuse_devicehub.resources.lot import LotView +from ereuse_devicehub.resources.lot.models import Lot class Format(enum.Enum): @@ -126,6 +129,41 @@ class DevicesDocumentView(DeviceView): return output +class LotsDocumentView(LotView): + def find(self, args: dict): + return self.generate_lots_csv(Lot.query) + + def generate_lots_csv(self, query): + """Get device query and put information in csv format.""" + data = StringIO() + cw = csv.writer(data) + first = True + for lot in query: + l = LotRow(lot) + if first: + cw.writerow(l.keys()) + first = False + cw.writerow(l.values()) + output = make_response(data.getvalue()) + output.headers['Content-Disposition'] = 'attachment; filename=lots-info.csv' + output.headers['Content-type'] = 'text/csv' + return output + + +class LotRow(OrderedDict): + def __init__(self, lot: Lot) -> None: + super().__init__() + self.lot = lot + # General information about device + self['Id'] = lot.id.hex + self['Name'] = lot.name + self['Registered in'] = format(lot.created, '%c') + try: + self['Description'] = lot.description + except: + self['Description'] = '' + + class DocumentDef(Resource): __type__ = 'Document' SCHEMA = None @@ -156,6 +194,8 @@ class DocumentDef(Resource): devices_view = DevicesDocumentView.as_view('devicesDocumentView', definition=self, auth=app.auth) + lots_view = LotsDocumentView.as_view('lotsDocumentView', definition=self) if self.AUTH: devices_view = app.auth.requires_auth(devices_view) self.add_url_rule('/devices/', defaults=d, view_func=devices_view, methods=get) + self.add_url_rule('/lots/', defaults=d, view_func=lots_view, methods=get) From a2303498cd9d29768df7be8b5e42a464c56a3c02 Mon Sep 17 00:00:00 2001 From: nad Date: Tue, 28 Jul 2020 16:22:17 +0200 Subject: [PATCH 3/8] Minors comments changes and new test_get_all_lots was added --- ereuse_devicehub/resources/documents/documents.py | 4 ++-- tests/test_lot.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/ereuse_devicehub/resources/documents/documents.py b/ereuse_devicehub/resources/documents/documents.py index aa61dbb0..57b8844c 100644 --- a/ereuse_devicehub/resources/documents/documents.py +++ b/ereuse_devicehub/resources/documents/documents.py @@ -134,7 +134,7 @@ class LotsDocumentView(LotView): return self.generate_lots_csv(Lot.query) def generate_lots_csv(self, query): - """Get device query and put information in csv format.""" + """Get lot query and put information in csv format.""" data = StringIO() cw = csv.writer(data) first = True @@ -154,7 +154,7 @@ class LotRow(OrderedDict): def __init__(self, lot: Lot) -> None: super().__init__() self.lot = lot - # General information about device + # General information about lot self['Id'] = lot.id.hex self['Name'] = lot.name self['Registered in'] = format(lot.created, '%c') diff --git a/tests/test_lot.py b/tests/test_lot.py index c1473496..50f01641 100644 --- a/tests/test_lot.py +++ b/tests/test_lot.py @@ -381,3 +381,17 @@ def test_lot_post_add_remove_device_view(app: Devicehub, user: UserClient): query=[('id', device_id)], status=200) assert not len(lot['devices']) + + +@pytest.mark.mvp +def test_get_all_lots(user: UserClient): + """Tests submitting and retreiving all lots.""" + l, _ = user.post({'name': 'Lot1', 'description': 'comments,lot1,testcomment,'}, res=Lot) + l, _ = user.post({'name': 'Lot2', 'description': 'comments,lot2,testcomment,'}, res=Lot) + l, _ = user.post({'name': 'Lot3', 'description': 'comments3,lot3,testcomment,'}, res=Lot) + + l, _ = user.get(res=Lot) + assert l['items'][0]['name'] == 'Lot1' + assert l['items'][0]['description'] == 'comments,lot1,testcomment,' + assert l['items'][2]['name'] == 'Lot3' + assert l['items'][2]['description'] == 'comments,lot3,testcomment,' From 6017476822c8f08be23fb24763aa48aeacfa4471 Mon Sep 17 00:00:00 2001 From: nad Date: Mon, 3 Aug 2020 18:25:55 +0200 Subject: [PATCH 4/8] Adding query to Lot views --- ereuse_devicehub/resources/documents/documents.py | 3 ++- ereuse_devicehub/resources/lot/views.py | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/ereuse_devicehub/resources/documents/documents.py b/ereuse_devicehub/resources/documents/documents.py index 57b8844c..d95e13f9 100644 --- a/ereuse_devicehub/resources/documents/documents.py +++ b/ereuse_devicehub/resources/documents/documents.py @@ -131,7 +131,8 @@ class DevicesDocumentView(DeviceView): class LotsDocumentView(LotView): def find(self, args: dict): - return self.generate_lots_csv(Lot.query) + query = self.query(args) + return self.generate_lots_csv(query) def generate_lots_csv(self, query): """Get lot query and put information in csv format.""" diff --git a/ereuse_devicehub/resources/lot/views.py b/ereuse_devicehub/resources/lot/views.py index 3590ba49..1092e3ea 100644 --- a/ereuse_devicehub/resources/lot/views.py +++ b/ereuse_devicehub/resources/lot/views.py @@ -94,6 +94,10 @@ class LotView(View): ) return jsonify(ret) + def query(self, args): + query = Lot.query.distinct() + return query + def delete(self, id): lot = Lot.query.filter_by(id=id).one() lot.delete() From eb859ce6c2f97291e072d06b2fad76f6518931ca Mon Sep 17 00:00:00 2001 From: Jordi Nadeu Date: Wed, 5 Aug 2020 11:54:43 +0200 Subject: [PATCH 5/8] Fixing test_get_all_lots --- tests/test_lot.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_lot.py b/tests/test_lot.py index 50f01641..b2beb27f 100644 --- a/tests/test_lot.py +++ b/tests/test_lot.py @@ -386,12 +386,12 @@ def test_lot_post_add_remove_device_view(app: Devicehub, user: UserClient): @pytest.mark.mvp def test_get_all_lots(user: UserClient): """Tests submitting and retreiving all lots.""" - l, _ = user.post({'name': 'Lot1', 'description': 'comments,lot1,testcomment,'}, res=Lot) - l, _ = user.post({'name': 'Lot2', 'description': 'comments,lot2,testcomment,'}, res=Lot) + l, _ = user.post({'name': 'Lot1', 'description': 'comments1,lot1,testcomment,'}, res=Lot) + l, _ = user.post({'name': 'Lot2', 'description': 'comments2,lot2,testcomment,'}, res=Lot) l, _ = user.post({'name': 'Lot3', 'description': 'comments3,lot3,testcomment,'}, res=Lot) l, _ = user.get(res=Lot) assert l['items'][0]['name'] == 'Lot1' - assert l['items'][0]['description'] == 'comments,lot1,testcomment,' + assert l['items'][0]['description'] == 'comments1,lot1,testcomment,' assert l['items'][2]['name'] == 'Lot3' - assert l['items'][2]['description'] == 'comments,lot3,testcomment,' + assert l['items'][2]['description'] == 'comments3,lot3,testcomment,' From 6087044a5ece84ecc4d050c52303dfc6f2997fae Mon Sep 17 00:00:00 2001 From: nad Date: Tue, 18 Aug 2020 19:19:18 +0200 Subject: [PATCH 6/8] Fixing test_get_multiple_lots and test_api_docs --- ereuse_devicehub/resources/documents/documents.py | 1 + tests/test_basic.py | 1 + tests/test_lot.py | 9 +++------ 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/ereuse_devicehub/resources/documents/documents.py b/ereuse_devicehub/resources/documents/documents.py index ab188c6a..e9ba2cfc 100644 --- a/ereuse_devicehub/resources/documents/documents.py +++ b/ereuse_devicehub/resources/documents/documents.py @@ -129,6 +129,7 @@ class DevicesDocumentView(DeviceView): output.headers['Content-type'] = 'text/csv' return output + class LotsDocumentView(LotView): def find(self, args: dict): query = self.query(args) diff --git a/tests/test_basic.py b/tests/test_basic.py index eab9d4ef..dc0ce248 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -50,6 +50,7 @@ def test_api_docs(client: Client): '/diy-and-gardenings/{id}/merge/', '/documents/devices/', '/documents/erasures/', + '/documents/lots/', '/documents/static/{filename}', '/documents/stock/', '/drills/{id}/merge/', diff --git a/tests/test_lot.py b/tests/test_lot.py index a378736a..9c04b046 100644 --- a/tests/test_lot.py +++ b/tests/test_lot.py @@ -384,14 +384,11 @@ def test_lot_post_add_remove_device_view(app: Devicehub, user: UserClient): @pytest.mark.mvp -def test_get_all_lots(user: UserClient): - """Tests submitting and retreiving all lots.""" +def test_get_multiple_lots(user: UserClient): + """Tests submitting and retreiving multiple lots.""" l, _ = user.post({'name': 'Lot1', 'description': 'comments1,lot1,testcomment,'}, res=Lot) l, _ = user.post({'name': 'Lot2', 'description': 'comments2,lot2,testcomment,'}, res=Lot) l, _ = user.post({'name': 'Lot3', 'description': 'comments3,lot3,testcomment,'}, res=Lot) l, _ = user.get(res=Lot) - assert l['items'][0]['name'] == 'Lot1' - assert l['items'][0]['description'] == 'comments1,lot1,testcomment,' - assert l['items'][2]['name'] == 'Lot3' - assert l['items'][2]['description'] == 'comments3,lot3,testcomment,' + assert len(l) == 3 From b61398ee592f56fbdeaafa9ed1de74756810b27d Mon Sep 17 00:00:00 2001 From: nad Date: Tue, 18 Aug 2020 21:02:47 +0200 Subject: [PATCH 7/8] Adding two users on test_get_document_lots to check lot permissions --- tests/test_documents.py | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/tests/test_documents.py b/tests/test_documents.py index ffb22a75..15741775 100644 --- a/tests/test_documents.py +++ b/tests/test_documents.py @@ -263,18 +263,35 @@ def test_report_devices_stock_control(user: UserClient, user2: UserClient): @pytest.mark.mvp -def test_get_document_lots(user: UserClient): +def test_get_document_lots(user: UserClient, user2: UserClient): """Tests submitting and retreiving all lots.""" - l, _ = user.post({'name': 'Lot1', 'description': 'comments,lot1,testcomment,'}, res=Lot) - l, _ = user.post({'name': 'Lot2', 'description': 'comments,lot2,testcomment,'}, res=Lot) - l, _ = user.post({'name': 'Lot3', 'description': 'comments,lot3,testcomment,'}, res=Lot) + l, _ = user.post({'name': 'Lot1', 'description': 'comments,lot1,testcomment-lot1,'}, res=Lot) + l, _ = user.post({'name': 'Lot2', 'description': 'comments,lot2,testcomment-lot2,'}, res=Lot) + l, _ = user2.post({'name': 'Lot3-User2', 'description': 'comments,lot3,testcomment-lot3,'}, res=Lot) csv_str, _ = user.get(res=documents.DocumentDef.t, item='lots/', accept='text/csv') + + csv2_str, _ = user2.get(res=documents.DocumentDef.t, + item='lots/', + accept='text/csv') + f = StringIO(csv_str) obj_csv = csv.reader(f, f) export_csv = list(obj_csv) - assert len(export_csv) == 4 - assert export_csv[0] == ['Id', 'Name', 'Registered in', 'Description'] + + f = StringIO(csv2_str) + obj2_csv = csv.reader(f, f) + export2_csv = list(obj2_csv) + + assert len(export_csv) == 3 + assert len(export2_csv) == 2 + + assert export_csv[0] == export2_csv[0] == ['Id', 'Name', 'Registered in', 'Description'] + + assert export_csv[1][1] == 'Lot1' or 'Lot2' + assert export_csv[1][3] == 'comments,lot1,testcomment-lot1,' or 'comments,lot2,testcomment-lot2,' + assert export2_csv[1][1] == 'Lot3-User2' + assert export2_csv[1][3] == 'comments,lot3,testcomment-lot3,' From 306bad966b4ff7d111162d1717d00d95d71f3098 Mon Sep 17 00:00:00 2001 From: nad Date: Tue, 18 Aug 2020 21:02:54 +0200 Subject: [PATCH 8/8] Adding lot permissions to LotsDocumentView --- ereuse_devicehub/resources/documents/documents.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ereuse_devicehub/resources/documents/documents.py b/ereuse_devicehub/resources/documents/documents.py index bb432019..53bd8d82 100644 --- a/ereuse_devicehub/resources/documents/documents.py +++ b/ereuse_devicehub/resources/documents/documents.py @@ -133,7 +133,7 @@ class DevicesDocumentView(DeviceView): class LotsDocumentView(LotView): def find(self, args: dict): - query = self.query(args) + query = (x for x in self.query(args) if x.owner_id == g.user.id) return self.generate_lots_csv(query) def generate_lots_csv(self, query):