使用Django auth 登入認(rèn)證模塊
#!/usr/bin/env python
#encoding: utf-8
#author: xiaofangliu
importjson
importuuid
fromdjango.shortcutsimportrender
fromdjango.httpimportJsonResponse
fromdjango.views.decorators.csrfimportcsrf_exempt
fromdjango.contrib.auth.modelsimportUser
fromdjango.contrib.authimportauthenticate, login, logout
frompassport.modelsimportuserInfo
frompassport.modelsimportoperLog
fromappsimportrun_is_admin
#Create your views here.
@csrf_exempt
deflogin(request):
json_data=json.loads(request.body)
username=json_data.get('username','')
password=json_data.get('password','')
users=User.objects.filter(username=username)
ifusers:
user=authenticate(username=username,password=password)
ifuser:
print'user2', user
ifuser.is_active:
#login(request, user)
hash_key=uuid.uuid1()
user_id=User.objects.filter(username=user).values('id')
##print 'user_id', user_id, user_id[0].get('id')
obj=userInfo.objects.create(user_id=user_id[0].get('id'),hashKey=hash_key)
is_admin=run_is_admin(obj.hashKey)
res={'status':True,'msg':'登錄成功。唆阿。','user_id': user.pk,'userhashid': obj.hashKey,'code':'0','username': username,'is_admin': is_admin}
else:
res={'status':False,'msg':'登錄失敗袖订。。','user_id':'','userhashid':'','code':'2'}
else:
res={'status':False,'msg':'登錄失敗。如叼。','user_id':'','userhashid':'','code':'1'}
else:
res={'status':False,'msg':'用戶不存在嘉涌,請(qǐng)注冊(cè)。饼丘。','user_id':'','userhashid':'','code':'1'}
#print 'login', res
operLog.objects.create(user=username,type='login',status=res['status'],desc=res['msg'],code=res['code'])
returnJsonResponse(res)
@csrf_exempt
defsignup(request):
json_data=json.loads(request.body)
username=json_data.get('username','')
password=json_data.get('password','')
email=json_data.get('email','')
res={
'status':True,
'msg':''
? ? }
#print 'json_data', json_data
try:
user=User.objects.get(username=username)
res['code']='1'
res['user_id']=user.id
res['userhashid']=''
res['msg']='用戶已經(jīng)存在趁桃,請(qǐng)直接登入..'
#print 'user1', user
exceptUser.DoesNotExist:
user=User.objects.create_user(username, email, password)
#print 'user2', user
ifuserisnotNone:
user.is_active=True
? ? ? ? ? ? user.save()
#todo 新用戶,并綁定
hash_key=uuid.uuid1()
user_id=User.objects.filter(username=user).values('id')
#print 'user_id', user_id, user_id[0].get('id')
obj=userInfo.objects.create(user_id=user_id[0].get('id'),hashKey=hash_key)
is_admin=run_is_admin(obj.hashKey)
#print 'obj', obj.hashKey
res['username']=username
res['is_admin']=is_admin
res['code']='0'
res['userhashid']=obj.hashKey
res['user_id']=obj.user_id
res['msg']='用戶創(chuàng)建成功..'
else:
res['status']=False
res['code']='2'
res['userhashid']=''
res['msg']='用戶創(chuàng)建失敗..'
print'res1', res
operLog.objects.create(user=username,type='signup',status=res['status'],desc=res['msg'],code=res['code'])
returnJsonResponse(res)
@csrf_exempt
defreset_password(request):
json_data=json.loads(request.body)
username=json_data.get('username','')
first_pwd=json_data.get('firstPwd','')
second_pwd=json_data.get('secondPwd','')
print'json_data', json_data
iffirst_pwd==second_pwd:
print'password'
user=User.objects.filter(username=username)
ifuser:
user=User.objects.get(username=username)
? ? ? ? ? ? user.set_password(second_pwd)
? ? ? ? ? ? user.save()
print'user3', user
hash_key=uuid.uuid1()
user_id=User.objects.filter(username=username).values('id')
#print 'user_id', user_id, user_id[0].get('id')
obj=userInfo.objects.create(user_id=user_id[0].get('id'),hashKey=hash_key)
is_admin=run_is_admin(obj.hashKey)
res={'status':True,'msg':'修改成功肄鸽。卫病。','user_id': obj.user_id,'userhashid': obj.hashKey,'code':'0','username': username,'is_admin': is_admin}
else:
res={'status':False,'msg':'用戶不存在。典徘。','user_id':'','userhashid':'','code':'1'}
else:
res={'status':False,'msg':'修改失敗蟀苛。。','user_id':'','userhashid':'','code':'2'}
operLog.objects.create(user=username,type='reset_password',status=res['status'],desc=res['msg'],code=res['code'])
returnJsonResponse(res)