Browse Source

fix: code lint

master
xuxiaofei 4 years ago
parent
commit
70ff8e033c
  1. 1
      .pylintrc
  2. 8
      .vscode/settings.json
  3. 76
      common/utils/aes.py
  4. 1
      vislib/views/dashboard.py
  5. 35
      vislib/views/source.py
  6. 46
      vislib/views/user.py
  7. 4
      vislib/views/views.py

1
.pylintrc

@ -3,3 +3,4 @@
ignore=migrations ignore=migrations
disable= disable=
C0114, # missing-module-docstring C0114, # missing-module-docstring
indent-string=' '

8
.vscode/settings.json

@ -1,3 +1,7 @@
{ {
"python.linting.pylintEnabled": true "python.linting.pylintEnabled": true,
} "python.linting.pylintArgs": [
"--disable=C0103",
"--disable=C0111"
]
}

76
common/utils/aes.py

@ -10,48 +10,48 @@ from django.conf import settings
class prpcrypt(): class prpcrypt():
def __init__(self,key): def __init__(self,key):
self.key = key self.key = key
self.mode = AES.MODE_CBC self.mode = AES.MODE_CBC
#加密函数,如果text不足16位就用空格补足为16位, #加密函数,如果text不足16位就用空格补足为16位,
#如果大于16当时不是16的倍数,那就补足为16的倍数。 #如果大于16当时不是16的倍数,那就补足为16的倍数。
def encrypt(self,text): def encrypt(self,text):
cryptor = AES.new(self.key,self.mode, b'0000000000000000') cryptor = AES.new(self.key,self.mode, b'0000000000000000')
#这里密钥key 长度必须为16(AES-128), #这里密钥key 长度必须为16(AES-128),
#24(AES-192),或者32 (AES-256)Bytes 长度 #24(AES-192),或者32 (AES-256)Bytes 长度
#目前AES-128 足够目前使用 #目前AES-128 足够目前使用
# nonce = cryptor.nonce # nonce = cryptor.nonce
length = 32 length = 32
count = len(text) count = len(text)
if count < length: if count < length:
add = (length-count) add = (length-count)
#\0 backspace #\0 backspace
text = text + ('\0' * add) text = text + ('\0' * add)
elif count > length: elif count > length:
add = (length-(count % length)) add = (length-(count % length))
text = text + ('\0' * add) text = text + ('\0' * add)
print(text) print(text)
self.ciphertext = cryptor.encrypt(text.encode("utf8")) self.ciphertext = cryptor.encrypt(text.encode("utf8"))
#因为AES加密时候得到的字符串不一定是ascii字符集的,输出到终端或者保存时候可能存在问题 #因为AES加密时候得到的字符串不一定是ascii字符集的,输出到终端或者保存时候可能存在问题
#所以这里统一把加密后的字符串转化为16进制字符串 #所以这里统一把加密后的字符串转化为16进制字符串
return b2a_hex(self.ciphertext) return b2a_hex(self.ciphertext)
#解密后,去掉补足的空格用strip() 去掉 #解密后,去掉补足的空格用strip() 去掉
def decrypt(self,text): def decrypt(self,text):
cryptor = AES.new(self.key,self.mode,b'0000000000000000') cryptor = AES.new(self.key,self.mode,b'0000000000000000')
plain_text = cryptor.decrypt(a2b_hex(text)).decode("utf8") plain_text = cryptor.decrypt(a2b_hex(text)).decode("utf8")
return plain_text.rstrip('\0') return plain_text.rstrip('\0')
pc = prpcrypt(settings.CRYPT_KEY.encode('utf8')) #初始化密钥 pc = prpcrypt(settings.CRYPT_KEY.encode('utf8')) #初始化密钥
if __name__ == '__main__': if __name__ == '__main__':
# for test # for test
print(settings.CRYPT_KEY) print(settings.CRYPT_KEY)
pc = prpcrypt(settings.CRYPT_KEY.encode('utf8')) #初始化密钥 pc = prpcrypt(settings.CRYPT_KEY.encode('utf8')) #初始化密钥
import sys import sys
str = sys.argv[1] str = sys.argv[1]
e = pc.encrypt(str) #加密 e = pc.encrypt(str) #加密
print("加密:",e) print("加密:",e)
d = pc.decrypt(e) #解密 d = pc.decrypt(e) #解密
print("解密:",d) print("解密:",d)

1
vislib/views/dashboard.py

@ -17,7 +17,6 @@ def createDashboard(request):
body = json.loads(body_unicode) body = json.loads(body_unicode)
name = body['name'] name = body['name']
desc = body.get('desc', '') desc = body.get('desc', '')
content = body.get('content', '')
creator = request.user creator = request.user
dashboard_id = uuid.uuid4() dashboard_id = uuid.uuid4()

35
vislib/views/source.py

@ -1,17 +1,17 @@
import json import json
from django.shortcuts import render import uuid
from django.http import JsonResponse from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt from django.views.decorators.csrf import csrf_exempt
from django.core import serializers from django.core import serializers
from vislib.models import SourceDataBase, SourceDataTable
from django.utils import timezone from django.utils import timezone
from common.utils.aes import pc
from MySQLdb import _mysql from MySQLdb import _mysql
import uuid from py_vislib.vislib.models import SourceDataBase, SourceDataTable
from py_vislib.common.utils.aes import pc
def default_datetime(): def default_datetime():
now = timezone.now() now = timezone.now()
return now return now
@csrf_exempt @csrf_exempt
def createSource(request): def createSource(request):
@ -58,7 +58,7 @@ def updateSource(request):
source.port = body.get('port', 3306) source.port = body.get('port', 3306)
source.username = body.get('username') source.username = body.get('username')
if body.get('password'): if body.get('password'):
source.password = pc.encrypt(body.get('password')) source.password = pc.encrypt(body.get('password')).decode('utf-8')
else: else:
source = serializers.serialize('json', [source]) source = serializers.serialize('json', [source])
source = json.loads(source)[0]['fields'] source = json.loads(source)[0]['fields']
@ -71,11 +71,11 @@ def updateSource(request):
@csrf_exempt @csrf_exempt
def sourceList(request): def sourceList(request):
sourceList = SourceDataBase.objects.filter(creator=request.user) sources = SourceDataBase.objects.filter(creator=request.user)
sourceList = serializers.serialize('json', sourceList) sources = serializers.serialize('json', sources)
sourceList = json.loads(sourceList) sources = json.loads(sources)
sourceArr = [] sourceArr = []
for source in sourceList: for source in sources:
source['fields']['source_id'] = source['pk'] source['fields']['source_id'] = source['pk']
source['fields']['password'] = None source['fields']['password'] = None
sourceArr.append(source['fields']) sourceArr.append(source['fields'])
@ -83,11 +83,11 @@ def sourceList(request):
@csrf_exempt @csrf_exempt
def sourceDetail(request, sourceId): def sourceDetail(request, sourceId):
sourceDetail = SourceDataBase.objects.get(source_id=sourceId) sourceItem = SourceDataBase.objects.get(source_id=sourceId)
sourceDetail = serializers.serialize('json', [sourceDetail]) sourceItem = serializers.serialize('json', [sourceItem])
sourceDetail = json.loads(sourceDetail)[0] sourceItem = json.loads(sourceItem)[0]
return JsonResponse({'code': 20000, 'message': 'success', 'data':sourceDetail['fields'] }) return JsonResponse({'code': 20000, 'message': 'success', 'data':sourceItem['fields'] })
@csrf_exempt @csrf_exempt
def sourceTables(request, sourceId): def sourceTables(request, sourceId):
@ -99,7 +99,7 @@ def sourceTables(request, sourceId):
for table in tables: for table in tables:
json_data.append(table['fields']) json_data.append(table['fields'])
except: finally:
source = SourceDataBase.objects.get(source_id=sourceId) source = SourceDataBase.objects.get(source_id=sourceId)
source = serializers.serialize('json', [source]) source = serializers.serialize('json', [source])
source = json.loads(source)[0]['fields'] source = json.loads(source)[0]['fields']
@ -165,8 +165,7 @@ def sourceLinkedTables(request, sourceId):
json_data = [] json_data = []
for table in tables: for table in tables:
json_data.append(table['fields']) json_data.append(table['fields'])
finally:
except:
json_data = [] json_data = []

46
vislib/views/user.py

@ -9,35 +9,35 @@ from django.contrib.auth.models import User
@csrf_exempt @csrf_exempt
def userInfo(request): def userInfo(request):
if request.user.is_authenticated: if request.user.is_authenticated:
username = request.user.get_username() username = request.user.get_username()
return JsonResponse({'code': 20000, 'data': {'username': username}}) return JsonResponse({'code': 20000, 'data': {'username': username}})
else: else:
return JsonResponse({'code': 40000, 'message': 'Please login'}) return JsonResponse({'code': 40000, 'message': 'Please login'})
@csrf_exempt @csrf_exempt
def userSignup(request): def userSignup(request):
body = json.loads(request.body) body = json.loads(request.body)
if User.objects.filter(username=body['userName']).exists(): if User.objects.filter(username=body['userName']).exists():
return JsonResponse({'code': 10000, 'message': 'User Name ' + body['userName'] + ' is Already Tabken.'}) return JsonResponse({'code': 10000, 'message': 'User Name ' + body['userName'] + ' is Already Tabken.'})
if User.objects.filter(email=body['email']).exists(): if User.objects.filter(email=body['email']).exists():
return JsonResponse({'code': 10000, 'message': 'Email ' + body['emaul'] + ' is Registered.'}) return JsonResponse({'code': 10000, 'message': 'Email ' + body['emaul'] + ' is Registered.'})
user = User.objects.create_user(body['userName'], body['email'], body['password']) user = User.objects.create_user(body['userName'], body['email'], body['password'])
user.first_name=body['userName'] user.first_name=body['userName']
user.save() user.save()
return JsonResponse({'code': 20000, 'message': 'success'}) return JsonResponse({'code': 20000, 'message': 'success'})
@csrf_exempt @csrf_exempt
def userLogin(request): def userLogin(request):
body = json.loads(request.body) body = json.loads(request.body)
user = authenticate(request, username=body['userName'], password=body['password']) user = authenticate(request, username=body['userName'], password=body['password'])
if user is not None: if user is not None:
login(request, user) login(request, user)
return JsonResponse({'code': 20000, 'message': 'success'}) return JsonResponse({'code': 20000, 'message': 'success'})
else: else:
return JsonResponse({'code': 10000, 'message': 'Name or Password Not Correct, Please Try Again.'}) return JsonResponse({'code': 10000, 'message': 'Name or Password Not Correct, Please Try Again.'})
@csrf_exempt @csrf_exempt
def userLogout(request): def userLogout(request):
logout(request) logout(request)
return JsonResponse({'code': 20000, 'message': 'success'}) return JsonResponse({'code': 20000, 'message': 'success'})

4
vislib/views/views.py

@ -10,8 +10,8 @@ import uuid
# Create your views here. # Create your views here.
def default_datetime(): def default_datetime():
now = timezone.now() now = timezone.now()
return now return now
def index(request): def index(request):
return HttpResponse('hello python and django') return HttpResponse('hello python and django')

Loading…
Cancel
Save