From 70ff8e033c19fafb54cbb8b3bc3a9e81eac2da82 Mon Sep 17 00:00:00 2001 From: xuxiaofei Date: Sat, 19 Sep 2020 23:40:40 +0800 Subject: [PATCH] fix: code lint --- .pylintrc | 1 + .vscode/settings.json | 8 +++-- common/utils/aes.py | 76 +++++++++++++++++++-------------------- vislib/views/dashboard.py | 1 - vislib/views/source.py | 35 +++++++++--------- vislib/views/user.py | 46 ++++++++++++------------ vislib/views/views.py | 4 +-- 7 files changed, 87 insertions(+), 84 deletions(-) diff --git a/.pylintrc b/.pylintrc index 774ff4b..21a2bbd 100644 --- a/.pylintrc +++ b/.pylintrc @@ -3,3 +3,4 @@ ignore=migrations disable= C0114, # missing-module-docstring +indent-string=' ' diff --git a/.vscode/settings.json b/.vscode/settings.json index 72513b9..e762d65 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,7 @@ { - "python.linting.pylintEnabled": true -} \ No newline at end of file + "python.linting.pylintEnabled": true, + "python.linting.pylintArgs": [ + "--disable=C0103", + "--disable=C0111" + ] +} diff --git a/common/utils/aes.py b/common/utils/aes.py index 7b06d29..e4d5537 100644 --- a/common/utils/aes.py +++ b/common/utils/aes.py @@ -10,48 +10,48 @@ from django.conf import settings class prpcrypt(): - def __init__(self,key): - self.key = key - self.mode = AES.MODE_CBC + def __init__(self,key): + self.key = key + self.mode = AES.MODE_CBC - #加密函数,如果text不足16位就用空格补足为16位, - #如果大于16当时不是16的倍数,那就补足为16的倍数。 - def encrypt(self,text): - cryptor = AES.new(self.key,self.mode, b'0000000000000000') - #这里密钥key 长度必须为16(AES-128), - #24(AES-192),或者32 (AES-256)Bytes 长度 - #目前AES-128 足够目前使用 - # nonce = cryptor.nonce - length = 32 - count = len(text) - if count < length: - add = (length-count) - #\0 backspace - text = text + ('\0' * add) - elif count > length: - add = (length-(count % length)) - text = text + ('\0' * add) - print(text) - self.ciphertext = cryptor.encrypt(text.encode("utf8")) - #因为AES加密时候得到的字符串不一定是ascii字符集的,输出到终端或者保存时候可能存在问题 - #所以这里统一把加密后的字符串转化为16进制字符串 - return b2a_hex(self.ciphertext) + #加密函数,如果text不足16位就用空格补足为16位, + #如果大于16当时不是16的倍数,那就补足为16的倍数。 + def encrypt(self,text): + cryptor = AES.new(self.key,self.mode, b'0000000000000000') + #这里密钥key 长度必须为16(AES-128), + #24(AES-192),或者32 (AES-256)Bytes 长度 + #目前AES-128 足够目前使用 + # nonce = cryptor.nonce + length = 32 + count = len(text) + if count < length: + add = (length-count) + #\0 backspace + text = text + ('\0' * add) + elif count > length: + add = (length-(count % length)) + text = text + ('\0' * add) + print(text) + self.ciphertext = cryptor.encrypt(text.encode("utf8")) + #因为AES加密时候得到的字符串不一定是ascii字符集的,输出到终端或者保存时候可能存在问题 + #所以这里统一把加密后的字符串转化为16进制字符串 + return b2a_hex(self.ciphertext) - #解密后,去掉补足的空格用strip() 去掉 - def decrypt(self,text): - cryptor = AES.new(self.key,self.mode,b'0000000000000000') - plain_text = cryptor.decrypt(a2b_hex(text)).decode("utf8") - return plain_text.rstrip('\0') + #解密后,去掉补足的空格用strip() 去掉 + def decrypt(self,text): + cryptor = AES.new(self.key,self.mode,b'0000000000000000') + plain_text = cryptor.decrypt(a2b_hex(text)).decode("utf8") + return plain_text.rstrip('\0') pc = prpcrypt(settings.CRYPT_KEY.encode('utf8')) #初始化密钥 if __name__ == '__main__': # for test - print(settings.CRYPT_KEY) - pc = prpcrypt(settings.CRYPT_KEY.encode('utf8')) #初始化密钥 - import sys - str = sys.argv[1] - e = pc.encrypt(str) #加密 - print("加密:",e) - d = pc.decrypt(e) #解密 - print("解密:",d) + print(settings.CRYPT_KEY) + pc = prpcrypt(settings.CRYPT_KEY.encode('utf8')) #初始化密钥 + import sys + str = sys.argv[1] + e = pc.encrypt(str) #加密 + print("加密:",e) + d = pc.decrypt(e) #解密 + print("解密:",d) diff --git a/vislib/views/dashboard.py b/vislib/views/dashboard.py index 7769624..ff48990 100644 --- a/vislib/views/dashboard.py +++ b/vislib/views/dashboard.py @@ -17,7 +17,6 @@ def createDashboard(request): body = json.loads(body_unicode) name = body['name'] desc = body.get('desc', '') - content = body.get('content', '') creator = request.user dashboard_id = uuid.uuid4() diff --git a/vislib/views/source.py b/vislib/views/source.py index 2537263..3b0389f 100644 --- a/vislib/views/source.py +++ b/vislib/views/source.py @@ -1,17 +1,17 @@ import json -from django.shortcuts import render +import uuid from django.http import JsonResponse from django.views.decorators.csrf import csrf_exempt from django.core import serializers -from vislib.models import SourceDataBase, SourceDataTable from django.utils import timezone -from common.utils.aes import pc 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(): - now = timezone.now() - return now + now = timezone.now() + return now @csrf_exempt def createSource(request): @@ -58,7 +58,7 @@ def updateSource(request): source.port = body.get('port', 3306) source.username = body.get('username') if body.get('password'): - source.password = pc.encrypt(body.get('password')) + source.password = pc.encrypt(body.get('password')).decode('utf-8') else: source = serializers.serialize('json', [source]) source = json.loads(source)[0]['fields'] @@ -71,11 +71,11 @@ def updateSource(request): @csrf_exempt def sourceList(request): - sourceList = SourceDataBase.objects.filter(creator=request.user) - sourceList = serializers.serialize('json', sourceList) - sourceList = json.loads(sourceList) + sources = SourceDataBase.objects.filter(creator=request.user) + sources = serializers.serialize('json', sources) + sources = json.loads(sources) sourceArr = [] - for source in sourceList: + for source in sources: source['fields']['source_id'] = source['pk'] source['fields']['password'] = None sourceArr.append(source['fields']) @@ -83,11 +83,11 @@ def sourceList(request): @csrf_exempt def sourceDetail(request, sourceId): - sourceDetail = SourceDataBase.objects.get(source_id=sourceId) - sourceDetail = serializers.serialize('json', [sourceDetail]) - sourceDetail = json.loads(sourceDetail)[0] + sourceItem = SourceDataBase.objects.get(source_id=sourceId) + sourceItem = serializers.serialize('json', [sourceItem]) + 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 def sourceTables(request, sourceId): @@ -99,7 +99,7 @@ def sourceTables(request, sourceId): for table in tables: json_data.append(table['fields']) - except: + finally: source = SourceDataBase.objects.get(source_id=sourceId) source = serializers.serialize('json', [source]) source = json.loads(source)[0]['fields'] @@ -165,8 +165,7 @@ def sourceLinkedTables(request, sourceId): json_data = [] for table in tables: json_data.append(table['fields']) - - except: + finally: json_data = [] diff --git a/vislib/views/user.py b/vislib/views/user.py index e45b34b..b39f942 100644 --- a/vislib/views/user.py +++ b/vislib/views/user.py @@ -9,35 +9,35 @@ from django.contrib.auth.models import User @csrf_exempt def userInfo(request): - if request.user.is_authenticated: - username = request.user.get_username() - return JsonResponse({'code': 20000, 'data': {'username': username}}) - else: - return JsonResponse({'code': 40000, 'message': 'Please login'}) + if request.user.is_authenticated: + username = request.user.get_username() + return JsonResponse({'code': 20000, 'data': {'username': username}}) + else: + return JsonResponse({'code': 40000, 'message': 'Please login'}) @csrf_exempt def userSignup(request): - body = json.loads(request.body) - if User.objects.filter(username=body['userName']).exists(): - return JsonResponse({'code': 10000, 'message': 'User Name ' + body['userName'] + ' is Already Tabken.'}) - if User.objects.filter(email=body['email']).exists(): - return JsonResponse({'code': 10000, 'message': 'Email ' + body['emaul'] + ' is Registered.'}) - user = User.objects.create_user(body['userName'], body['email'], body['password']) - user.first_name=body['userName'] - user.save() - return JsonResponse({'code': 20000, 'message': 'success'}) + body = json.loads(request.body) + if User.objects.filter(username=body['userName']).exists(): + return JsonResponse({'code': 10000, 'message': 'User Name ' + body['userName'] + ' is Already Tabken.'}) + if User.objects.filter(email=body['email']).exists(): + return JsonResponse({'code': 10000, 'message': 'Email ' + body['emaul'] + ' is Registered.'}) + user = User.objects.create_user(body['userName'], body['email'], body['password']) + user.first_name=body['userName'] + user.save() + return JsonResponse({'code': 20000, 'message': 'success'}) @csrf_exempt def userLogin(request): - body = json.loads(request.body) - user = authenticate(request, username=body['userName'], password=body['password']) - if user is not None: - login(request, user) - return JsonResponse({'code': 20000, 'message': 'success'}) - else: - return JsonResponse({'code': 10000, 'message': 'Name or Password Not Correct, Please Try Again.'}) + body = json.loads(request.body) + user = authenticate(request, username=body['userName'], password=body['password']) + if user is not None: + login(request, user) + return JsonResponse({'code': 20000, 'message': 'success'}) + else: + return JsonResponse({'code': 10000, 'message': 'Name or Password Not Correct, Please Try Again.'}) @csrf_exempt def userLogout(request): - logout(request) - return JsonResponse({'code': 20000, 'message': 'success'}) + logout(request) + return JsonResponse({'code': 20000, 'message': 'success'}) diff --git a/vislib/views/views.py b/vislib/views/views.py index d0de850..157a28b 100644 --- a/vislib/views/views.py +++ b/vislib/views/views.py @@ -10,8 +10,8 @@ import uuid # Create your views here. def default_datetime(): - now = timezone.now() - return now + now = timezone.now() + return now def index(request): return HttpResponse('hello python and django')