from polls.decorators import polls_login_required 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 django.conf import settings import shutil import uuid import os from pathlib import Path 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') platform = request.POST.get('platform', 'android') 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': '用户尚未激话'}) if not profile or profile.admin_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 result['last_login'] = u.last_login if profile: result['name'] = profile.name result['department'] = profile.department result['post'] = profile.post result['thumbnail'] = request.build_absolute_uri( profile.image.url) if profile.image else None result['organization'] = profile.organization.name profile.platform = platform profile.save() 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': '手机号不正确'}) UserModel = get_user_model() is_exists = UserModel.objects.filter(username=phone).exists() if not is_exists: 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.code() print(phone, result) if result == 0: VerifyCode.objects.create( code=code, phone=phone, category=category, timeouted=after_1min) return JsonResponse({'status': 'success'}) return JsonResponse({'status': 'error', 'message': response.msg()}) @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 not 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(): profile = Userprofile.objects.filter(user__username=phone).first() if profile.status == 1: return JsonResponse({'status': 'error', 'message': '账号已激活,不需要重复激活'}) return JsonResponse({'status': 'success', 'message': {'id': profile.user_id, 'phone': phone, 'name': profile.name, 'organization': profile.organization.name}}) 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': '账号不正确'}) department = request.POST.get('department') if not department: return JsonResponse({'status': 'error', 'message': '处(科)室不能为空'}) password = request.POST.get('password') if not password: return JsonResponse({'status': 'error', 'message': '密码不能为空'}) if len(password) < 6: return JsonResponse({'status': 'error', 'message': '密码长度至少6位'}) post = request.POST.get('post') if not post: return JsonResponse({'status': 'error', 'message': '职务不能为空'}) profile = Userprofile.objects.filter(user__username=phone).first() if not profile: return JsonResponse({'status': 'error', 'message': '账号不存在'}) profile.department = department profile.post = post profile.status = 1 profile.save() UserModel = get_user_model() user = UserModel.objects.get(username=phone) user.set_password(password) user.save() 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': '密码修改成功'}) @csrf_exempt def reset_password(request): if request.method == 'GET': return HttpResponse(status=405) password = request.POST.get('password') if not password: return JsonResponse({'status': 'error', 'message': '原密码不能为空'}) new_password = request.POST.get('newPassword') if not new_password: return JsonResponse({'status': 'error', 'message': '密码不能为空'}) if len(new_password) < 6: return JsonResponse({'status': 'error', 'message': '密码长度不能少于6位'}) user_id = request.user.id UserModel = get_user_model() user = UserModel.objects.get(id=user_id) if not user.check_password(password): return JsonResponse({'status': 'error', 'message': '原密码不正确'}) user.set_password(new_password) user.save() return JsonResponse({'status': 'success', 'message': '密码修改成功'}) @csrf_exempt @polls_login_required def upload_profile(request): u = request.user source = request.FILES['photo'] ext = source.name.split('.')[-1] if ext not in ['jpg', 'png', 'jpeg', 'gif']: return JsonResponse({'status': 'error', 'message': '上传的文件不是图片'}) filename = "%s.%s" % (uuid.uuid4(), ext) foldpath = os.path.join(settings.MEDIA_ROOT, 'profile') Path(foldpath).mkdir(parents=True, exist_ok=True) filepath = os.path.join(foldpath, filename) with open(filepath, 'wb') as dest: shutil.copyfileobj(source, dest) profile = Userprofile.objects.filter(user=u).first() profile.image = '/profile/%s' % filename profile.save() return JsonResponse({'status': 'success', 'message': request.build_absolute_uri('/media/profile/%s' % filename)})