newmediamonitoring/polls/views/user.py

193 lines
7.5 KiB
Python

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
from polls.models import Notice, VerifyCode
from polls.utils import generate_code, sent_sms_code
from dashboard.models import Organization, Userprofile
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:
return JsonResponse({'status': 'error', 'message': '用户名或密码错误'})
profile = user.userprofile_set.first()
if not profile or profile.status == 0:
return JsonResponse({'status': 'error', 'message': '用户在审核中'})
u = authenticate(request, username=phone, password=password)
if u is not None:
login(request, u)
token, created = Token.objects.get_or_create(user=u)
result = dict()
profile = u.userprofile_set.first()
result['phone'] = u.username
result['token'] = token.key
if profile:
result['name'] = profile.name
result['gender'] = profile.sex
result['thumbnail'] = request.build_absolute_uri(
profile.image.url) if profile.image else None
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': '手机号不正确'})
exist_code = VerifyCode.objects.filter(
phone=phone, category=category).first()
if exist_code and exist_code.is_in_progress():
return JsonResponse({'status': 'error', 'message': '验证码使用中'})
code = generate_code()
now = datetime.datetime.now()
after_1min = now + datetime.timedelta(minutes=1)
response = sent_sms_code(phone, code)
result = response.decode('utf8')
print(phone,result)
if "OK" in result:
VerifyCode.objects.create(
code=code, phone=phone, category=category, timeouted=after_1min)
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')
category = request.POST.get('category', 0)
if not phone:
return JsonResponse({'status': 'error', 'message': '手机号不正确'})
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=category).first()
if verify_code and verify_code.is_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')
if not phone:
return JsonResponse({'status': 'error', 'message': '手机号不正确'})
name = request.POST.get('name')
if not name:
return JsonResponse({'status': 'error', 'message': '姓名不能为空'})
password = request.POST.get('password')
if not password:
return JsonResponse({'status': 'error', 'message': '密码不能为空'})
unit = request.POST.get('unit')
if not unit:
return JsonResponse({'status': 'error', 'message': '单位不能为空'})
department = request.POST.get('department')
if not department:
return JsonResponse({'status': 'error', 'message': '处/科室不能为空'})
post = request.POST.get('post')
if not post:
return JsonResponse({'status': 'error', 'message': '职务不能为空'})
UserModel = get_user_model()
is_exists = UserModel.objects.filter(username=phone).exists()
if is_exists:
return JsonResponse({'status': 'error', 'message': '该手机号已经使用'})
u = UserModel.objects.create_user(username=phone, password=password)
profile = Userprofile.objects.create(name=name, user=u, status=0, unit=unit, department=department, post=post)
admins = Userprofile.objects.filter(organization__level__level=1)
for a in admins:
content = '用户%s注册了账号,请审核' % (name,)
Notice.create_reply_notice(a.user.id, content, 'dashboard', 'Userprofile', 'status', profile.id, '1')
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')
if not phone:
return JsonResponse({'status': 'error', 'message': '手机号不正确'})
code = request.POST.get('code')
print(code)
if not code:
return JsonResponse({'status': 'error', 'message': '验证码不正确'})
verify_code = VerifyCode.objects.filter(
phone=phone, code=code, category=1).first()
if verify_code and verify_code.is_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')
if not phone:
return JsonResponse({'status': 'error', 'message': '手机号不正确'})
password = request.POST.get('password')
if not password:
return JsonResponse({'status': 'error', 'message': '密码不能为空'})
password_confirm = request.POST.get('password_confirm')
if not password_confirm:
return JsonResponse({'status': 'error', 'message': '密码确认不正确'})
if password != password_confirm:
return JsonResponse({'status': 'error', 'message': '两次密码输入不一致'})
print(phone, password, password_confirm)
UserModel = get_user_model()
user = UserModel.objects.filter(username=phone).first()
if not user:
return JsonResponse({'status': 'error', 'message': '用户名不存在'})
user.set_password(password)
user.save()
return JsonResponse({'status': 'success', 'message': '密码修改成功'})