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
disable=
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():
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)

1
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()

35
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 = []

46
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'})

4
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')

Loading…
Cancel
Save