From 3ecbec2179db247040d141d158dc9731a728f9f0 Mon Sep 17 00:00:00 2001 From: xuxiaofei Date: Sat, 31 Oct 2020 10:03:58 +0800 Subject: [PATCH] fix: error handle --- vislib/views/source.py | 2 +- vislib/views/views.py | 94 +++++++++++++++++++++++------------------- 2 files changed, 52 insertions(+), 44 deletions(-) diff --git a/vislib/views/source.py b/vislib/views/source.py index c047514..6d29550 100644 --- a/vislib/views/source.py +++ b/vislib/views/source.py @@ -170,7 +170,7 @@ def sourceLinkedTables(request, sourceId): json_data.append(table['fields']) except Exception as e: json_data = [] - printz(e) + print(e) return JsonResponse({'code': 20000, 'message': 'success', 'data': json_data }) diff --git a/vislib/views/views.py b/vislib/views/views.py index 5ecfb79..8ae5f79 100644 --- a/vislib/views/views.py +++ b/vislib/views/views.py @@ -1,11 +1,10 @@ import json -import uuid from django.http import JsonResponse +from django.core import serializers +from django.utils import timezone from django.views.decorators.csrf import csrf_exempt from MySQLdb import _mysql -from django.core import serializers from vislib.models import SourceDataBase -from django.utils import timezone from common.utils.aes import pc # Create your views here. @@ -16,45 +15,54 @@ def default_datetime(): @csrf_exempt def execSql(request): - body_unicode = request.body.decode('utf-8') - body = json.loads(body_unicode) - sql = body['sql'] - sourceId = body['source_id'] - source = SourceDataBase.objects.get(source_id=sourceId) - source = serializers.serialize('json', [source]) - source = json.loads(source)[0]['fields'] - host = source['host'] - username = source['username'] - port = source['port'] - password = pc.decrypt(source['password']) - database = source['database'] + try: + body_unicode = request.body.decode('utf-8') + body = json.loads(body_unicode) + sql = body['sql'] + sourceId = body['source_id'] + source = SourceDataBase.objects.get(source_id=sourceId) + source = serializers.serialize('json', [source]) + source = json.loads(source)[0]['fields'] + host = source['host'] + username = source['username'] + port = source['port'] + password = pc.decrypt(source['password']) + database = source['database'] - db=_mysql.connect( - host=host, - port=int(port), - user=username, - passwd=password, - db=database, - ) - db.query(sql) - data = db.store_result().fetch_row(maxrows=0, how=2) - db.close() - json_data = [] - for index in range(len(data)): - row = data[index] - json_data.append({}) - for key in row: - if(key.find('.')>0): - column = (key.split('.'))[1] - else: - column = key - if isinstance(row[key], bytes): - json_data[index][column] = row[key].decode('UTF-8') - else: - json_data[index][column] = row[key] - response = { - 'code': 20000, - 'message': 'success', - 'data': json_data - } + db=_mysql.connect( + host=host, + port=int(port), + user=username, + passwd=password, + db=database, + ) + db.query(sql) + data = db.store_result().fetch_row(maxrows=0, how=2) + db.close() + json_data = [] + for index in range(len(data)): + row = data[index] + json_data.append({}) + for key in row: + if(key.find('.')>0): + column = (key.split('.'))[1] + else: + column = key + print(row[key], key) + if isinstance(row[key], bytes): + json_data[index][column] = row[key].decode('UTF-8') + else: + json_data[index][column] = row[key] + response = { + 'code': 20000, + 'message': 'success', + 'data': json_data + } + except Exception as e: + print(e) + response = { + 'code': 10000, + 'message': 'fail', + 'data': e + } return JsonResponse(response)