newmediamonitoring/polls/views.py

146 lines
5.2 KiB
Python
Raw Normal View History

2020-08-20 14:53:57 +00:00
from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
from django.contrib.auth import get_user_model, authenticate, login
from django.views.decorators.csrf import csrf_exempt
from django_token.models import Token
2020-08-24 14:35:47 +00:00
import datetime
2020-08-20 14:53:57 +00:00
2020-08-24 14:35:47 +00:00
from .models import VerifyCode
2020-08-29 02:23:02 +00:00
#这个地方sent_sms_code原先是send_sms_code报错
from .utils import generate_code, sent_sms_code
2020-08-20 14:53:57 +00:00
2020-08-27 16:26:34 +00:00
2020-08-20 14:53:57 +00:00
def index(request):
username = request.GET.get('username')
password = request.GET.get('password')
print(username, password)
return JsonResponse({'status': 'error', 'message': '用户名或密码错误'})
def status_500(request):
return HttpResponse(status=500)
def status_401(request):
return HttpResponse(status=401)
@csrf_exempt
def polls_login(request):
if request.method == 'POST':
phone = request.POST.get('phone')
password = request.POST.get('password')
UserModel = get_user_model()
2020-08-27 16:26:34 +00:00
user = UserModel.objects.filter(username=phone).first()
2020-08-20 14:53:57 +00:00
if not user:
return JsonResponse({'status': 'error', 'message': '用户名或密码错误'})
2020-08-27 16:26:34 +00:00
u = authenticate(request, username=phone, password=password)
2020-08-20 14:53:57 +00:00
if u is not None:
login(request, u)
token = Token.objects.get_or_create(user=u)
2020-08-20 14:53:57 +00:00
result = dict()
profile = u.userprofile_set.first()
result['phone'] = u.first_name
result['token'] = token
if profile:
result['name'] = profile.name
result['gender'] = profile.sex
result['thumbnail'] = profile.image.path
result['organization'] = profile.organization.name
return JsonResponse({'status': 'success', 'message': result})
2020-08-20 14:53:57 +00:00
else:
return JsonResponse({'status': 'error', 'message': '用户名或密码错误'})
else:
return HttpResponse(status=405)
@csrf_exempt
def send_code(request):
if request.method == 'GET':
return HttpResponse(status=405)
phone = request.POST.get('phone')
category = request.POST.get('category', 0)
if not phone:
return JsonResponse({'status': 'error', 'message': '手机号不正确'})
2020-08-27 16:26:34 +00:00
exist_code = VerifyCode.objects.filter(
phone=phone, category=category).first()
if exist_code and exist_code.in_progress():
return JsonResponse({'status': 'error', 'message': '验证码使用中'})
code = generate_code()
now = datetime.datetime.now()
after_10mins = now + datetime.timedelta(minutes=10)
2020-08-29 02:23:02 +00:00
response = sent_sms_code(phone, code)
2020-08-24 14:35:47 +00:00
result = response.decode('utf8')
if "OK" in result:
VerifyCode.objects.create(code=code, phone=phone, category=category)
return JsonResponse({'status': 'success'})
return JsonResponse({'status': 'error', 'message': '验证码发送失败'})
@csrf_exempt
def register_step_one(request):
if request.method == 'GET':
return HttpResponse(status=405)
phone = request.POST.get('phone')
code = request.POST.get('code')
2020-08-27 16:26:34 +00:00
UserModel = get_user_model()
is_exists = UserModel.objects.filter(username=phone).exists()
if is_exists:
return JsonResponse({'status': 'error', 'message': '该手机号已经使用'})
verify_code = VerifyCode.objects.filter(
phone=phone, code=code, category=0).first()
if verify_code and verify_code.in_progress():
return JsonResponse({'status': 'success', 'message': {phone: phone}})
else:
return JsonResponse({'status': 'error', 'message': '验证码超时,请重发'})
@csrf_exempt
def register_step_two(request):
if request.method == 'GET':
return HttpResponse(status=405)
phone = request.POST.get('phone')
name = request.POST.get('name')
gender = request.POST.get('gender')
2020-08-27 16:26:34 +00:00
password = request.POST.get('password')
organization = request.POST.get('organization')
2020-08-27 16:26:34 +00:00
UserModel = get_user_model()
UserModel.objects.create_user(username=phone, password=password)
return JsonResponse({'status': 'success', 'message': '注册成功'})
@csrf_exempt
def password_recover_step_one(request):
if request.method == 'GET':
return HttpResponse(status=405)
phone = request.POST.get('phone')
code = request.POST.get('code')
2020-08-27 16:26:34 +00:00
verify_code = VerifyCode.objects.filter(
phone=phone, code=code, category=1).first()
if verify_code and verify_code.in_progress():
return JsonResponse({'status': 'success', 'message': {phone: phone}})
else:
return JsonResponse({'status': 'error', 'message': '验证码超时,请重发'})
@csrf_exempt
def password_recover_step_two(request):
if request.method == 'GET':
return HttpResponse(status=405)
phone = request.POST.get('phone')
password = request.POST.get('password')
password_confirm = request.POST.get('password_confirm')
2020-08-27 16:26:34 +00:00
if password != password_confirm:
return JsonResponse({'status': 'error', 'message': '两次密码输入不一致'})
UserModel = get_user_model()
user = UserModel.objects.filter(username=phone).first()
if not user:
return JsonResponse({'status': 'error', 'message': '用户名不存在'})
user.set_password(password)
2020-08-29 02:23:02 +00:00
return JsonResponse({'status': 'success', 'message': {phone: phone}})