Example #1
0
 def create(self, request, *args, **kwargs):
     username = request.data.get("username", "")
     password = request.data.get("password", "")
     checkpass = request.data.get("checkpass", "")
     email = request.data.get("email", "")
     captcha_code = request.data.get("captcha_code", "")
     hashkey = request.data.get("hashkey", "")
     get_setting_info = get_setting_config()
     if not username:
         return JsonResponse({"code": 400, "msg": "用户名不能为空"})
     if UserProfile.objects.filter(username=username).count():
         return JsonResponse({"code": 400, "msg": "该用户已被注册"})
     if not email:
         return JsonResponse({"code": 400, "msg": "邮箱不能为空"})
     if UserProfile.objects.filter(email=email, has_active=True).count():
         return JsonResponse({"code": 400, "msg": "该邮箱已被注册"})
     if not captcha_code:
         return JsonResponse({"code": 400, "msg": "验证码不能为空"})
     if not judge_captcha(captcha_code, hashkey):
         return JsonResponse({"code": 400, "msg": "验证码错误"})
     if password != checkpass:
         return JsonResponse({"code": 400, "msg": "两次密码输入不一致"})
     code = generate_code(6)
     keys = red_user_cache.keys()
     for single_key in keys:
         try:
             single_user_info = red_user_cache.get(single_key)
             redis_username, redis_password, redis_email = json.loads(
                 single_user_info)
             if username == redis_username:
                 return JsonResponse({"code": 400, "msg": "该用户已被注册"})
             if redis_email == email:
                 return JsonResponse({"code": 400, "msg": "该邮箱已被注册"})
         except Exception as e:
             return JsonResponse({"code": 400, "msg": "用户注册失败"})
     if get_setting_info['cancel_validation'] == False:
         user = UserProfile(username=username, email=email)
         user.set_password(password)
         user.has_active = True
         user.greenhand = True
         user.save()
         return JsonResponse({"code": 200, "msg": "注册成功"})
     try:
         send_activate_email(receiver_email=email,
                             code=code,
                             request=request)
     except smtplib.SMTPDataError as e:
         return JsonResponse({"code": 400, "msg": "邮件发送失败,请减缓发送频率"})
     except Exception as e:
         return JsonResponse({"code": 400, "msg": "邮件发送失败"})
     try:
         user_info = [username, password, email]
         red_user_cache.set(code, json.dumps(user_info), ex=300)
     except Exception as e:
         return JsonResponse({"code": 400, "msg": "注册失败"})
     return JsonResponse({"code": 200, "msg": "注册用户成功,请到邮箱激活您的账号"})
Example #2
0
 def get(self, request):
     '''
     验证链接是否有效
     '''
     code = request.GET.get("code", "")
     try:
         user_info = red_user_cache.get(code)
         redis_username, redis_password, redis_email = json.loads(user_info)
         user = UserProfile(username=redis_username, email=redis_email)
         user.set_password(redis_password)
         user.has_active = True
         user.greenhand = True
         user.save()
         red_user_cache.delete(code)
     except Exception as e:
         return JsonResponse({"code": 400, "msg": "链接不存在或已失效"})
     return JsonResponse({"code": 200, "msg": "ok"})