From 936c042c6201f720369b9651fbc0282f94e5b0fb Mon Sep 17 00:00:00 2001 From: nad Date: Thu, 23 Jul 2020 20:55:27 +0200 Subject: [PATCH] Creating document stock endpoint --- .../resources/documents/documents.py | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/ereuse_devicehub/resources/documents/documents.py b/ereuse_devicehub/resources/documents/documents.py index e95a9694..3b148362 100644 --- a/ereuse_devicehub/resources/documents/documents.py +++ b/ereuse_devicehub/resources/documents/documents.py @@ -126,6 +126,29 @@ class DevicesDocumentView(DeviceView): return output +class StockDocumentView(DeviceView): + # @cache(datetime.timedelta(minutes=1)) + def find(self, args: dict): + query = self.query(args) + return self.generate_post_csv(query) + + def generate_post_csv(self, query): + """Get device query and put information in csv format.""" + data = StringIO() + cw = csv.writer(data) + first = True + for device in query: + d = DeviceRow(device) + if first: + cw.writerow(d.keys()) + first = False + cw.writerow(d.values()) + output = make_response(data.getvalue()) + output.headers['Content-Disposition'] = 'attachment; filename=export.csv' + output.headers['Content-type'] = 'text/csv' + return output + + class DocumentDef(Resource): __type__ = 'Document' SCHEMA = None @@ -156,6 +179,10 @@ class DocumentDef(Resource): devices_view = DevicesDocumentView.as_view('devicesDocumentView', definition=self, auth=app.auth) + + stock_view = StockDocumentView.as_view('stockDocumentView', 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('/stock/', defaults=d, view_func=stock_view, methods=get)