vue可视化报表平台后台代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

177 rivejä
5.1 KiB

5 vuotta sitten
import json
4 vuotta sitten
import uuid
5 vuotta sitten
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from django.core import serializers
from django.utils import timezone
from MySQLdb import _mysql
4 vuotta sitten
from vislib.models import SourceDataBase, SourceDataTable
from common.utils.aes import pc
4 vuotta sitten
5 vuotta sitten
def default_datetime():
4 vuotta sitten
now = timezone.now()
return now
5 vuotta sitten
@csrf_exempt
def createSource(request):
body_unicode = request.body.decode('utf-8')
body = json.loads(body_unicode)
host = body['host']
port = body.get('port', 3306)
username = body.get('username')
password = pc.encrypt(body.get('password')).decode('utf-8')
database = body.get('database')
base_alias = body.get('base_alias')
creator = request.user
source_id = uuid.uuid4()
SourceDataBase.objects.create(
source_id=source_id,
host=host,
port=port,
username=username,
password=password,
database=database,
base_alias=base_alias,
creator=creator,
is_private=True,
status=1,
updated_at=default_datetime()
)
return JsonResponse({'code': 20000, 'message': 'success', 'data': {'id': source_id}})
@csrf_exempt
def deleteSource(request):
body_unicode = request.body.decode('utf-8')
body = json.loads(body_unicode)
source = SourceDataBase.objects.get(source_id=body['source_id'])
source.delete()
return JsonResponse({'code': 20000, 'message': 'success'})
@csrf_exempt
def updateSource(request):
body_unicode = request.body.decode('utf-8')
body = json.loads(body_unicode)
source = SourceDataBase.objects.get(source_id=body['source_id'])
source.host = body['host']
source.port = body.get('port', 3306)
source.username = body.get('username')
if body.get('password'):
4 vuotta sitten
source.password = pc.encrypt(body.get('password')).decode('utf-8')
5 vuotta sitten
else:
source = serializers.serialize('json', [source])
source = json.loads(source)[0]['fields']
source.password = source['password']
source.database = body.get('database')
source.base_alias = body.get('base_alias')
source.save()
return JsonResponse({'code': 20000, 'message': 'success'})
@csrf_exempt
def sourceList(request):
4 vuotta sitten
sources = SourceDataBase.objects.filter(creator=request.user)
sources = serializers.serialize('json', sources)
sources = json.loads(sources)
5 vuotta sitten
sourceArr = []
4 vuotta sitten
for source in sources:
5 vuotta sitten
source['fields']['source_id'] = source['pk']
source['fields']['password'] = None
sourceArr.append(source['fields'])
return JsonResponse({'code': 20000, 'data': sourceArr})
@csrf_exempt
def sourceDetail(request, sourceId):
4 vuotta sitten
sourceItem = SourceDataBase.objects.get(source_id=sourceId)
sourceItem = serializers.serialize('json', [sourceItem])
sourceItem = json.loads(sourceItem)[0]
5 vuotta sitten
4 vuotta sitten
return JsonResponse({'code': 20000, 'message': 'success', 'data':sourceItem['fields'] })
5 vuotta sitten
@csrf_exempt
def sourceTables(request, sourceId):
json_data = []
5 vuotta sitten
try:
4 vuotta sitten
tables = SourceDataTable.objects.filter(database=sourceId)
tables = serializers.serialize('json', tables)
5 vuotta sitten
tables = json.loads(tables)
for table in tables:
json_data.append(table['fields'])
4 vuotta sitten
except Exception as e:
print('no linked tables before', e)
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,
5 vuotta sitten
'status': 0
})
5 vuotta sitten
return JsonResponse({'code': 20000, 'message': 'success', 'data': json_data })
@csrf_exempt
def sourceTableSave(request):
body_unicode = request.body.decode('utf-8')
body = json.loads(body_unicode)
print(body)
source_id = body['source_id']
SourceDataTable.objects.filter(database=source_id).delete()
source = SourceDataBase.objects.get(source_id=source_id)
for table in body['tables']:
tableConfig = SourceDataTable.objects.create(
id=uuid.uuid4(),
database=source,
table=table['table'],
table_alias=table['table_alias'],
creator=request.user,
status=table['status'],
updated_at=default_datetime()
)
tableConfig.save()
return JsonResponse({'code': 20000, 'message': 'success' })
@csrf_exempt
def sourceLinkedTables(request, sourceId):
try:
4 vuotta sitten
tables = SourceDataTable.objects.filter(database=sourceId)
tables = serializers.serialize('json', tables)
5 vuotta sitten
tables = json.loads(tables)
json_data = []
for table in tables:
json_data.append(table['fields'])
4 vuotta sitten
except Exception as e:
5 vuotta sitten
json_data = []
4 vuotta sitten
print(e)
5 vuotta sitten
return JsonResponse({'code': 20000, 'message': 'success', 'data': json_data })