From cdf0e83b6697d2ff75ba30b567ead5bcfea0fa25 Mon Sep 17 00:00:00 2001 From: SerenaFeng Date: Mon, 6 Jun 2016 11:01:13 +0800 Subject: refactor int query parameter process in testAPI add method GenericResultHandler.get_int() refactor GenericResultHandler.set_query() & ResultsCLHandler.get() JIRA: FUNCTEST-294 Change-Id: I44636983af7784a5b2863427abc0895e89cfa83c Signed-off-by: SerenaFeng --- .../opnfv_testapi/resources/result_handlers.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'utils/test/result_collection_api') diff --git a/utils/test/result_collection_api/opnfv_testapi/resources/result_handlers.py b/utils/test/result_collection_api/opnfv_testapi/resources/result_handlers.py index 0a4c0db7b..44834fd63 100644 --- a/utils/test/result_collection_api/opnfv_testapi/resources/result_handlers.py +++ b/utils/test/result_collection_api/opnfv_testapi/resources/result_handlers.py @@ -25,6 +25,13 @@ class GenericResultHandler(GenericApiHandler): self.table = self.db_results self.table_cls = TestResult + def get_int(self, key, value): + try: + value = int(value) + except: + raise HTTPError(HTTP_BAD_REQUEST, '{} must be int', key) + return value + def set_query(self): query = dict() for k in self.request.query_arguments.keys(): @@ -32,10 +39,7 @@ class GenericResultHandler(GenericApiHandler): if k == 'project' or k == 'pod' or k == 'case': query[k + '_name'] = v elif k == 'period': - try: - v = int(v) - except: - raise HTTPError(HTTP_BAD_REQUEST, 'period must be int') + v = self.get_int(k, v) if v > 0: period = datetime.now() - timedelta(days=v) obj = {"$gte": str(period)} @@ -119,10 +123,7 @@ class ResultsCLHandler(GenericResultHandler): """ last = self.get_query_argument('last', 0) if last is not None: - try: - last = int(last) - except: - raise HTTPError(HTTP_BAD_REQUEST, 'last must be int') + last = self.get_int('last', last) self._list(self.set_query(), sort=[{'start_date', -1}], last=last) -- cgit 1.2.3-korg