From 3c12d1f75cf0bda954cf687da5abe5893df6f78d Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 21 Dec 2020 13:40:07 +0100 Subject: [PATCH] add endpoint --- .../resources/documents/documents.py | 25 +++++++++++++++---- tests/test_documents.py | 3 +++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/ereuse_devicehub/resources/documents/documents.py b/ereuse_devicehub/resources/documents/documents.py index 822adbf6..fb5572bd 100644 --- a/ereuse_devicehub/resources/documents/documents.py +++ b/ereuse_devicehub/resources/documents/documents.py @@ -11,20 +11,19 @@ import flask import flask_weasyprint import teal.marshmallow from boltons import urlutils -from flask import make_response, g +from flask import make_response, g, request +from flask.json import jsonify from teal.cache import cache -from teal.resource import Resource +from teal.resource import Resource, View from ereuse_devicehub.db import db 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, StockRow -from ereuse_devicehub.resources.documents.device_row import DeviceRow from ereuse_devicehub.resources.lot import LotView from ereuse_devicehub.resources.lot.models import Lot -from ereuse_devicehub.resources.hash_reports import insert_hash - +from ereuse_devicehub.resources.hash_reports import insert_hash, ReportHash class Format(enum.Enum): @@ -193,6 +192,19 @@ class StockDocumentView(DeviceView): return output +class CheckView(View): + model = ReportHash + + def get(self): + qry = dict(request.values) + hash3 = qry['hash'] + + result = False + if ReportHash.query.filter_by(hash3=hash3).count(): + result = True + return jsonify(result) + + class DocumentDef(Resource): __type__ = 'Document' SCHEMA = None @@ -241,3 +253,6 @@ class DocumentDef(Resource): stock_view = StockDocumentView.as_view('stockDocumentView', definition=self, auth=app.auth) stock_view = app.auth.requires_auth(stock_view) self.add_url_rule('/stock/', defaults=d, view_func=stock_view, methods=get) + + check_view = CheckView.as_view('CheckView', definition=self, auth=app.auth) + self.add_url_rule('/check/', defaults={}, view_func=check_view, methods=get) diff --git a/tests/test_documents.py b/tests/test_documents.py index 162ed5a4..895aaf30 100644 --- a/tests/test_documents.py +++ b/tests/test_documents.py @@ -144,6 +144,9 @@ def test_check_insert_hash(app: Devicehub, user: UserClient): query=[('filter', {'type': ['Computer']})]) hash3 = hashlib.sha3_256(csv_str.encode('utf-8')).hexdigest() assert ReportHash.query.filter_by(hash3=hash3).count() == 1 + result, status = user.get(res=documents.DocumentDef.t, item='check/', query=[('hash', hash3)]) + assert status.status_code == 200 + assert result == True @pytest.mark.mvp