Пример #1
0
 def post(self, request, *args, **kwargs):
     try:
         password = self.request.data.get("password")
         username = self.request.data.get("username")
         is_active = self.request.data.get("is_active")
         account = self.request.data.get("account")
         telephone = self.request.data.get("telephone")
         gender = self.request.data.get("gender")
         userNo = self.request.data.get("userNo")
         address = self.request.data.get("address")
         detail = self.request.data.get("detail")
         avatar = self.request.data.get("avatar")
         user = User(username=username,
                     password=make_password(password),
                     is_active=is_active)
         user.save()
         userInfo = UserInfo(account=account,
                             telephone=telephone,
                             gender=gender,
                             userNo=userNo,
                             address=address,
                             detail=detail,
                             avatar=avatar)
         userInfo.user = user
         userInfo.save()
     except:
         return Response(data={
             "code": 400,
             "message": "新增用户失败!"
         },
                         status=status.HTTP_400_BAD_REQUEST)
     return Response(data={
         "code": 200,
         "message": "新增用户成功"
     },
                     status=status.HTTP_201_CREATED)
Пример #2
0
    def post(self, request):
        '''登陆/注册?'''
        reType = request.POST.get('type')

        if reType == 'login':
            '''ajax登陆检查'''

            # 获取数据
            username = request.POST.get('username')
            password = request.POST.get('password')
            user = authenticate(username=username, password=password)

            if settings.DEBUG == True:
                print('{username}--登陆'.format(username=username))
                try:
                    print('{username}--认证'.format(username=user.username))
                except:
                    pass

            # 校验数据
            if not all([username, password]):
                return JsonResponse({'check': '2'})

            # 业务处理
            # elif username == 'Louis' and password == '201314abc':
            elif user is not None:
                if user.is_active:
                    # 用户已激活
                    next_url = request.POST.get('next', reverse('main:home'))
                    print(next_url)
                    # 设置session,标记为登陆
                    login(request, user)
                    return JsonResponse({
                        'check': '1',
                        'next': next_url
                    })  # 登陆成功
                else:
                    #用户未激活
                    send_email(user.id, user.email, username)
                    errmsg = '用户{user}未激活,已重新发送邮件'.format(user=username)
                    return JsonResponse({'check': '3', 'errmsg': errmsg})
            else:
                # 用户名或密码错误
                return JsonResponse({'check': '0'})

        elif reType == 'register':
            '''ajax注册处理'''

            # 接收数据
            username = request.POST.get('username')
            password = request.POST.get('password')
            email = request.POST.get('email')

            # 进行校验
            if not all([username, password, email]):
                # 数据不完整
                return JsonResponse({'statusCode': 2})
            if not re.match(
                    r'^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$',
                    email):
                # 邮箱形式不对
                return JsonResponse({'statusCode': 3})

            # 进行业务处理:注册
            # 查找用户名是否重复
            try:
                user = User.objects.get(username=username)
            except User.DoesNotExist:
                user = ""

            try:
                emails = User.objects.get(email=email)
            except User.DoesNotExist:
                emails = ""
            if user:
                return JsonResponse({'statusCode': 0})
            elif emails:
                return JsonResponse({'statusCode': 4})
            elif user == "" and emails == "":
                user = User.objects.create_user(username, email, password)
                user.is_active = 0
                info = UserInfo()
                info.user = user
                info.nickname = username
                user.save()
                info.save()

                #发送激活邮件,包含激活链接:http://nchu-UTP/active/[加盐id]

                #if send_register_active_email(user.id, email, username):   #celery异步处理
                if send_email(user.id, email, username):
                    # 返回应答
                    return JsonResponse({'statusCode': 1})
                else:
                    return JsonResponse({'statusCode': 5})