newmediamonitoring/polls/views/user.py

217 lines
8.5 KiB
Python
Raw Normal View History

2020-09-03 04:03:19 +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
import datetime
2020-09-24 06:51:39 +00:00
from polls.models import Notice, VerifyCode
2020-09-03 04:03:19 +00:00
from polls.utils import generate_code, sent_sms_code
2020-09-03 09:46:04 +00:00
from dashboard.models import Organization, Userprofile
2020-09-03 04:03:19 +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()
user = UserModel.objects.filter(username=phone).first()
if not user:
2020-10-22 13:52:48 +00:00
return JsonResponse({'status': 'error', 'message': '账号不存在,请联系管理员'})
2020-09-23 09:47:05 +00:00
profile = user.userprofile_set.first()
2020-09-22 07:39:33 +00:00
if not profile or profile.status == 0:
2020-10-22 13:52:48 +00:00
return JsonResponse({'status': 'error', 'message': '用户尚未激话'})
2020-10-26 00:58:58 +00:00
if not profile or profile.admin_status == 0:
return JsonResponse({'status': 'error', 'message': '用户已删除,请联系管理员'})
2020-09-03 04:03:19 +00:00
u = authenticate(request, username=phone, password=password)
if u is not None:
login(request, u)
2020-09-03 09:46:04 +00:00
token, created = Token.objects.get_or_create(user=u)
2020-09-03 04:03:19 +00:00
result = dict()
profile = u.userprofile_set.first()
2020-09-04 21:57:48 +00:00
result['phone'] = u.username
2020-09-03 09:46:04 +00:00
result['token'] = token.key
2020-09-03 04:03:19 +00:00
if profile:
result['name'] = profile.name
2020-10-22 13:52:48 +00:00
result['department'] = profile.department
result['post'] = profile.post
2020-09-04 21:57:48 +00:00
result['thumbnail'] = request.build_absolute_uri(
profile.image.url) if profile.image else None
2020-09-03 04:03:19 +00:00
result['organization'] = profile.organization.name
return JsonResponse({'status': 'success', 'message': result})
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-10-22 13:52:48 +00:00
UserModel = get_user_model()
is_exists = UserModel.objects.filter(username=phone).exists()
if not is_exists:
return JsonResponse({'status': 'error', 'message': '账号不存在,请联系管理员'})
2020-09-03 04:03:19 +00:00
exist_code = VerifyCode.objects.filter(
phone=phone, category=category).first()
2020-09-03 09:46:04 +00:00
if exist_code and exist_code.is_in_progress():
2020-09-03 04:03:19 +00:00
return JsonResponse({'status': 'error', 'message': '验证码使用中'})
code = generate_code()
now = datetime.datetime.now()
2020-09-03 09:46:04 +00:00
after_1min = now + datetime.timedelta(minutes=1)
2020-09-03 04:03:19 +00:00
response = sent_sms_code(phone, code)
2020-11-17 00:45:44 +00:00
result = response.code()
2020-10-22 13:52:48 +00:00
print(phone, result)
2020-11-17 00:45:44 +00:00
if result == 0:
2020-09-03 09:46:04 +00:00
VerifyCode.objects.create(
code=code, phone=phone, category=category, timeouted=after_1min)
2020-09-03 04:03:19 +00:00
return JsonResponse({'status': 'success'})
2020-11-17 00:45:44 +00:00
return JsonResponse({'status': 'error', 'message': response.msg()})
2020-09-03 04:03:19 +00:00
@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-09-03 09:46:04 +00:00
category = request.POST.get('category', 0)
if not phone:
return JsonResponse({'status': 'error', 'message': '手机号不正确'})
2020-09-03 04:03:19 +00:00
UserModel = get_user_model()
is_exists = UserModel.objects.filter(username=phone).exists()
2020-10-22 13:52:48 +00:00
if not is_exists:
return JsonResponse({'status': 'error', 'message': '账号不存在,请联系管理员'})
2020-09-03 04:03:19 +00:00
verify_code = VerifyCode.objects.filter(
2020-09-03 09:46:04 +00:00
phone=phone, code=code, category=category).first()
if verify_code and verify_code.is_in_progress():
2020-10-22 13:52:48 +00:00
profile = Userprofile.objects.filter(user__username=phone).first()
return JsonResponse({'status': 'success', 'message': {'id': profile.user_id, 'phone': phone, 'name': profile.name, 'organization': profile.organization.name}})
2020-09-03 04:03:19 +00:00
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')
2020-09-03 09:46:04 +00:00
if not phone:
2020-10-22 13:52:48 +00:00
return JsonResponse({'status': 'error', 'message': '账号不正确'})
department = request.POST.get('department')
if not department:
return JsonResponse({'status': 'error', 'message': '处(科)室不能为空'})
2020-09-03 04:03:19 +00:00
password = request.POST.get('password')
2020-09-03 09:46:04 +00:00
if not password:
return JsonResponse({'status': 'error', 'message': '密码不能为空'})
2020-10-22 13:52:48 +00:00
if len(password) < 6:
return JsonResponse({'status': 'error', 'message': '密码长度至少6位'})
2020-09-22 07:39:33 +00:00
post = request.POST.get('post')
if not post:
return JsonResponse({'status': 'error', 'message': '职务不能为空'})
2020-10-22 13:52:48 +00:00
profile = Userprofile.objects.filter(user__username=phone).first()
if not profile:
return JsonResponse({'status': 'error', 'message': '账号不存在'})
profile.department = department
profile.post = post
2020-10-23 00:10:15 +00:00
profile.status = 1
2020-10-22 13:52:48 +00:00
profile.save()
2020-09-03 04:03:19 +00:00
UserModel = get_user_model()
2020-10-22 13:52:48 +00:00
user = UserModel.objects.get(username=phone)
user.set_password(password)
user.save()
return JsonResponse({'status': 'success', 'message': '激活成功'})
2020-09-03 04:03:19 +00:00
@csrf_exempt
def password_recover_step_one(request):
if request.method == 'GET':
return HttpResponse(status=405)
phone = request.POST.get('phone')
2020-09-03 09:46:04 +00:00
if not phone:
return JsonResponse({'status': 'error', 'message': '手机号不正确'})
2020-09-03 04:03:19 +00:00
code = request.POST.get('code')
2020-10-17 16:00:23 +00:00
print(code)
2020-09-03 09:46:04 +00:00
if not code:
return JsonResponse({'status': 'error', 'message': '验证码不正确'})
2020-09-03 04:03:19 +00:00
verify_code = VerifyCode.objects.filter(
phone=phone, code=code, category=1).first()
2020-09-03 09:46:04 +00:00
if verify_code and verify_code.is_in_progress():
2020-09-03 04:03:19 +00:00
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')
2020-09-03 09:46:04 +00:00
if not phone:
return JsonResponse({'status': 'error', 'message': '手机号不正确'})
2020-09-03 04:03:19 +00:00
password = request.POST.get('password')
2020-09-03 09:46:04 +00:00
if not password:
return JsonResponse({'status': 'error', 'message': '密码不能为空'})
2020-09-03 04:03:19 +00:00
password_confirm = request.POST.get('password_confirm')
2020-09-03 09:46:04 +00:00
if not password_confirm:
return JsonResponse({'status': 'error', 'message': '密码确认不正确'})
2020-09-03 04:03:19 +00:00
if password != password_confirm:
return JsonResponse({'status': 'error', 'message': '两次密码输入不一致'})
2020-09-04 21:57:48 +00:00
print(phone, password, password_confirm)
2020-09-03 04:03:19 +00:00
UserModel = get_user_model()
user = UserModel.objects.filter(username=phone).first()
if not user:
return JsonResponse({'status': 'error', 'message': '用户名不存在'})
user.set_password(password)
2020-09-03 09:46:04 +00:00
user.save()
2020-09-04 21:57:48 +00:00
return JsonResponse({'status': 'success', 'message': '密码修改成功'})
2020-10-22 13:52:48 +00:00
@csrf_exempt
def reset_password(request):
if request.method == 'GET':
return HttpResponse(status=405)
password = request.POST.get('password')
if not password:
2020-10-25 15:59:04 +00:00
return JsonResponse({'status': 'error', 'message': '原密码不能为空'})
new_password = request.POST.get('newPassword')
if not new_password:
2020-10-22 13:52:48 +00:00
return JsonResponse({'status': 'error', 'message': '密码不能为空'})
2020-10-25 15:59:04 +00:00
if len(new_password) < 6:
2020-10-22 13:52:48 +00:00
return JsonResponse({'status': 'error', 'message': '密码长度不能少于6位'})
user_id = request.user.id
UserModel = get_user_model()
user = UserModel.objects.get(id=user_id)
2020-10-25 15:59:04 +00:00
if not user.check_password(password):
return JsonResponse({'status': 'error', 'message': '原密码不正确'})
user.set_password(new_password)
2020-10-22 13:52:48 +00:00
user.save()
return JsonResponse({'status': 'success', 'message': '密码修改成功'})