From e85712704bc0c953e4c00ad557358b7ac624f52a Mon Sep 17 00:00:00 2001 From: xuxiaofei Date: Wed, 28 Oct 2020 23:54:38 +0800 Subject: [PATCH] fix: only link one table --- vislib/views/source.py | 60 ++++++++++++++++++++++-------------------- vislib/views/views.py | 8 ++---- 2 files changed, 33 insertions(+), 35 deletions(-) diff --git a/vislib/views/source.py b/vislib/views/source.py index 77abe85..97464ef 100644 --- a/vislib/views/source.py +++ b/vislib/views/source.py @@ -100,36 +100,38 @@ def sourceTables(request, sourceId): json_data.append(table['fields']) except: - source = SourceDataBase.objects.get(source_id=sourceId) - source = serializers.serialize('json', [source]) - source = json.loads(source)[0]['fields'] - password = source['password'].encode(('utf-8')) - print(password) - host = source['host'] - username = source['username'] - port = source['port'] - password = pc.decrypt(password) - database = source['database'] - print(password) - - db=_mysql.connect( - host=host, - port=int(port), - user=username, - passwd=password, - db=database, - charset='utf8' - ) - db.query('show tables;') - tables = db.store_result().fetch_row(maxrows=0, how=2) - db.close() - json_data = list(tables[0].values()) - for i, table in enumerate(json_data): - json_data[i] = { - 'table': table.decode('utf-8'), + print('no linked tables before') + + source = SourceDataBase.objects.get(source_id=sourceId) + source = serializers.serialize('json', [source]) + source = json.loads(source)[0]['fields'] + password = source['password'].encode(('utf-8')) + host = source['host'] + username = source['username'] + port = source['port'] + password = pc.decrypt(password) + database = source['database'] + + db=_mysql.connect( + host=host, + port=int(port), + user=username, + passwd=password, + db=database + ) + db.query('show tables;') + tables = db.store_result().fetch_row(maxrows=0, how=2) + db.close() + tables = list(tables) + for i, table in enumerate(tables): + tableName = list(table.values())[0].decode('utf-8') + if next((x for x in json_data if x['table'] == tableName), None): + print(tableName + ' linked') + else: + json_data.append({ + 'table': tableName, 'status': 0 - } - + }) return JsonResponse({'code': 20000, 'message': 'success', 'data': json_data }) diff --git a/vislib/views/views.py b/vislib/views/views.py index 157a28b..5ecfb79 100644 --- a/vislib/views/views.py +++ b/vislib/views/views.py @@ -1,21 +1,18 @@ import json +import uuid from django.http import JsonResponse from django.views.decorators.csrf import csrf_exempt from MySQLdb import _mysql from django.core import serializers -from vislib.models import SourceDataBase, SourceDataTable +from vislib.models import SourceDataBase from django.utils import timezone from common.utils.aes import pc -import uuid # Create your views here. def default_datetime(): now = timezone.now() return now -def index(request): - return HttpResponse('hello python and django') - @csrf_exempt def execSql(request): @@ -38,7 +35,6 @@ def execSql(request): user=username, passwd=password, db=database, - charset='utf8' ) db.query(sql) data = db.store_result().fetch_row(maxrows=0, how=2)