Browse Source

fix: error handle

master
xuxiaofei 4 years ago
parent
commit
3ecbec2179
  1. 2
      vislib/views/source.py
  2. 94
      vislib/views/views.py

2
vislib/views/source.py

@ -170,7 +170,7 @@ def sourceLinkedTables(request, sourceId):
json_data.append(table['fields']) json_data.append(table['fields'])
except Exception as e: except Exception as e:
json_data = [] json_data = []
printz(e) print(e)
return JsonResponse({'code': 20000, 'message': 'success', 'data': json_data }) return JsonResponse({'code': 20000, 'message': 'success', 'data': json_data })

94
vislib/views/views.py

@ -1,11 +1,10 @@
import json import json
import uuid
from django.http import JsonResponse from django.http import JsonResponse
from django.core import serializers
from django.utils import timezone
from django.views.decorators.csrf import csrf_exempt from django.views.decorators.csrf import csrf_exempt
from MySQLdb import _mysql from MySQLdb import _mysql
from django.core import serializers
from vislib.models import SourceDataBase from vislib.models import SourceDataBase
from django.utils import timezone
from common.utils.aes import pc from common.utils.aes import pc
# Create your views here. # Create your views here.
@ -16,45 +15,54 @@ def default_datetime():
@csrf_exempt @csrf_exempt
def execSql(request): def execSql(request):
body_unicode = request.body.decode('utf-8') try:
body = json.loads(body_unicode) body_unicode = request.body.decode('utf-8')
sql = body['sql'] body = json.loads(body_unicode)
sourceId = body['source_id'] sql = body['sql']
source = SourceDataBase.objects.get(source_id=sourceId) sourceId = body['source_id']
source = serializers.serialize('json', [source]) source = SourceDataBase.objects.get(source_id=sourceId)
source = json.loads(source)[0]['fields'] source = serializers.serialize('json', [source])
host = source['host'] source = json.loads(source)[0]['fields']
username = source['username'] host = source['host']
port = source['port'] username = source['username']
password = pc.decrypt(source['password']) port = source['port']
database = source['database'] password = pc.decrypt(source['password'])
database = source['database']
db=_mysql.connect( db=_mysql.connect(
host=host, host=host,
port=int(port), port=int(port),
user=username, user=username,
passwd=password, passwd=password,
db=database, db=database,
) )
db.query(sql) db.query(sql)
data = db.store_result().fetch_row(maxrows=0, how=2) data = db.store_result().fetch_row(maxrows=0, how=2)
db.close() db.close()
json_data = [] json_data = []
for index in range(len(data)): for index in range(len(data)):
row = data[index] row = data[index]
json_data.append({}) json_data.append({})
for key in row: for key in row:
if(key.find('.')>0): if(key.find('.')>0):
column = (key.split('.'))[1] column = (key.split('.'))[1]
else: else:
column = key column = key
if isinstance(row[key], bytes): print(row[key], key)
json_data[index][column] = row[key].decode('UTF-8') if isinstance(row[key], bytes):
else: json_data[index][column] = row[key].decode('UTF-8')
json_data[index][column] = row[key] else:
response = { json_data[index][column] = row[key]
'code': 20000, response = {
'message': 'success', 'code': 20000,
'data': json_data 'message': 'success',
} 'data': json_data
}
except Exception as e:
print(e)
response = {
'code': 10000,
'message': 'fail',
'data': e
}
return JsonResponse(response) return JsonResponse(response)

Loading…
Cancel
Save