Beispiel #1
0
 def create_new_user(self, body):
     user = User(first_name=body['first_name'],
                 last_name=body['last_name'],
                 email=body['email'],
                 username=body['email'])
     user.set_password(body['password'])
     user.save()
Beispiel #2
0
def __authorize_by_code(request):
    post_data = request.body.decode('utf-8')
    post_data = json.loads(post_data)
    print(post_data)
    code = post_data.get('code').strip()
    app_id = post_data.get("appId").strip()
    nick_name = post_data.get("nickName").strip()

    response = {}
    if not code or not app_id:
        response['message'] = "authoreized failed, need entire authorization data. "
        response['code'] = ReturnCode.BROKEN_AUTHORIZED_DATA
        return JsonResponse(data=response, safe=False)

    data = c2s(app_id,code)
    openid = data.get('openid')
    if not openid:
        response = wrap_json_response(code=ReturnCode.FAILED, message="auth failed")
        return JsonResponse(data=response, safe=False)

    request.session['openid'] = openid
    request.session['is_authorized'] = True

    if not User.objects.filter(openid=openid):
        new_user = User(openid=openid, nickname=nick_name)
        new_user.save()

    response = wrap_json_response(code=ReturnCode.SUCCESS, message="auth success")
    return JsonResponse(data=response, safe=False)
    pass
Beispiel #3
0
def add_one():
    # 1
    user = User(open_id='test_open_id', nickname='test_nickname')
    user.save()

    # 2
    User.objects.create(open_id='test_open_id2', nickname='test_nickname2')
Beispiel #4
0
def __authorize_by_code(request):
    post_data = request.body.decode('utf-8')
    post_data = json.loads(post_data)
    code = post_data.get('code').strip()
    app_id = post_data.get('appId').strip()
    nickname = post_data.get('nickname').strip()

    response_data = {}
    if not code or not app_id:
        response_data[
            'message'] = 'authorized failed, need entire authorization data.'
        response_data['code'] = ReturnCode.BROKEN_AUTHORIZED_DATA
        return JsonResponse(data=response_data, safe=False)

    data = c2s(app_id, code)
    openid = data.get('openid')
    print('get openid:', openid)

    if not openid:
        response = CommonResponseMixin.wrap_json_response(
            code=ReturnCode.FAILED, message='auth failed')
        return JsonResponse(data=response, safe=False)
    request.session['open_id'] = openid
    request.session['is_authorized'] = True

    if not User.objects.filter(open_id=openid):
        new_user = User(open_id=openid, nickname=nickname)
        print('new_user:open_id:%s, nickname:%s' % (openid, nickname))
        new_user.save()
    response = CommonResponseMixin.wrap_json_response(code=ReturnCode.SUCCESS,
                                                      message='auth succeess')
    return JsonResponse(data=response, safe=False)
Beispiel #5
0
def get_author(request):
    post_data = request.body.decode('utf-8')
    post_data = json.loads(post_data)
    code = post_data.get('code').strip()
    appid = post_data.get('appid').strip()
    nickName = post_data.get('nickName').strip()
    response = {}
    if not code or not appid:
        response['message'] = 'authorize failed'
        response['code'] = ReturnCode.BROKEN_AUTHORIZED_DATA
        return JsonResponse(data=response, safe=False)
    data = auth.c2s(appid, code)
    openid = data.get('openid')
    if not openid:
        response['message'] = 'authorize failed'
        response['code'] = ReturnCode.BROKEN_AUTHORIZED_DATA
        return JsonResponse(data=response, safe=False)
    request.session['open_id'] = openid
    request.session['is_authorized'] = True
    if not User.objects.filter(openid=openid):
        user = User(openid=openid, nickname=nickName)
        user.save()
    response = CommonResponseMixin.wrap_json_response(code=ReturnCode.SUCCESS,
                                                      message='success auth')
    return JsonResponse(data=response, safe=False)
Beispiel #6
0
 def create_user(self):
     user = User(first_name='first_name',
                 last_name='last_name',
                 email='email',
                 username='******')
     user.set_password('password')
     user.save()
     return user
Beispiel #7
0
 def _create_user(self, email=None):
     email = email if email else self.EMAIL
     user = User(first_name='first_name',
                 last_name='last_name',
                 email=email,
                 username=email)
     user.set_password(self.PASSWORD)
     user.save()
     return user
Beispiel #8
0
def add_one():
    '''
    添加用户,添加用户时openid不能重复
    :return:
    '''
    # 添加用户 法1
    user = User(openid='test_open_id', nickname='test_nickname')
    user.save()
    # 添加用户 法2
    User.objects.create(openid='test_lyq_id2', nickname='test_nickname')
Beispiel #9
0
def __authorize_by_code(request):
    '''
    使用wx.login获取到的临时code发送到微信提供的code2session接口授权
    :param request:
    :return:
    '''
    post_data = request.body.decode(
        'utf-8')  # 接收冲微信小程序传过来的信息(code,appid,nickname)
    post_data = json.loads(post_data)
    code = post_data.get('code').strip()
    appid = post_data.get('appId').strip()
    nickname = post_data.get('nickname').strip()
    print('post 信息:', code)
    print(appid)
    print(nickname)

    response = {}
    if not code or not appid:
        response['message'] = '信息不完整'
        response['code'] = ReturnCode.BROKEN_AUTHORIZED_DATA
        print('信息不完整')
        return JsonResponse(data=response, safe=False)
    data = c2s(appid, code)
    print('data:', data)
    openid = data.get('openid')
    # print('openid:',openid)
    session_key = data.get('session_key')
    print('session_key:', session_key)

    if not openid:
        response = ResponseMixin.json_response(code=ReturnCode.FAILED,
                                               message='auth failed')
        return JsonResponse(data=response, safe=False)

    # 使用session中间件标记了两个数据
    request.session['openid'] = openid  # 获取的opeid
    request.session['is_authorize'] = True  # 是否已经认证,如果已经认证则需要判断用户是否在数据库中
    print('openid:', openid)
    print('nickname:', nickname)
    print(type(nickname))
    openid = json.dumps(openid)
    openid = json.loads(openid)

    print('!!!!!!!!!!', openid)
    # 如果重数据库中查询不到,则说明其不在数据库中,需要将此用户保存下来
    if not User.objects.filter(openid=openid):
        new_user = User(openid=openid, nickname=nickname)  # 新建用户
        print('openid:', openid)
        print('nickname:', nickname)
        new_user.save()  # 保存用户信息

    response = ResponseMixin.json_response(code=ReturnCode.SUCCESS,
                                           message='auth success')
    return JsonResponse(data=response, safe=False)
Beispiel #10
0
    def post(self, request, *args, **kwargs):
        username = request.data['username']
        email = request.data['email']
        password = request.data['password']
        allow_send_email = request.data['allow_send_email']

        user = User(username=username,
                    email=email,
                    password=password,
                    allow_send_email=allow_send_email)
        user.save()

        return Response(status=status.HTTP_200_OK)
Beispiel #11
0
    def create_users_and_posts(self):
        user1 = User(first_name='first_name',
                     last_name='last_name',
                     email='email1',
                     username='******')
        user1.set_password('password')
        user1.save()

        user2 = User(first_name='first_name',
                     last_name='last_name',
                     email='email2',
                     username='******')
        user2.set_password('password')
        user2.save()

        post_us1_ac = Post.objects.create(name='name1',
                                          text='text',
                                          author=user1,
                                          status=Post.ACTIVE)
        post_us1_pd = Post.objects.create(name='name2',
                                          text='text',
                                          author=user1,
                                          status=Post.PENDING)
        post_us1_dl = Post.objects.create(name='name3',
                                          text='text',
                                          author=user1,
                                          status=Post.DELETED)

        post_us2_ac = Post.objects.create(name='name4',
                                          text='text',
                                          author=user2,
                                          status=Post.ACTIVE)
        post_us2_pd = Post.objects.create(name='name5',
                                          text='text',
                                          author=user2,
                                          status=Post.PENDING)
        post_us2_dl = Post.objects.create(name='name6',
                                          text='text',
                                          author=user2,
                                          status=Post.DELETED)
        return (user1, user2,
                post_us1_ac, post_us1_pd, post_us1_dl,
                post_us2_ac, post_us2_pd, post_us2_dl)
Beispiel #12
0
def __authorize_by_code(request):
    """
    使用wx.login()得到的临时code获得微信提供的code2session接口授权
    :param request:
    :return:
    """
    post_data = request.body.decode('utf-8')
    post_data = json.loads(post_data)
    code = post_data.get('code')
    app_id = post_data.get('appId')
    nickname = post_data.get('nickname')

    response = {}
    if not code or not app_id:
        response['message'] = '参数不完整'
        response['code'] = ReturnCode.BROKEN_AUTHORIZED_DATA
        return JsonResponse(data=response, safe=False)
    data = code2session(appid=app_id, code=code)
    openid = data.get('openid')
    print('openid is ' + openid)

    if not openid:
        response = wrap_json_response(code=ReturnCode.FAILED,
                                      message='auth failed!')
        return JsonResponse(data=response, safe=False)

    # openid存在
    request.session['openid'] = openid
    request.session['is_authorized'] = True  # 是否已经认证

    # 如果成功认证,判断该用户是否在数据库中
    if not User.objects.filter(open_id=openid):  # 如果不在,将用户保存到数据库
        new_user = User(open_id=openid, nickname=nickname)
        new_user.save()
        response = wrap_json_response(code=ReturnCode.SUCCESS,
                                      message='auth success')
        print('auth is not in tables')
        return JsonResponse(data=response, safe=False)

    print('auth is in tables')
    response = wrap_json_response(code=ReturnCode.SUCCESS,
                                  message='auth success')
    return JsonResponse(data=response, safe=False)
def __authorize_by_code(request):
    """
    使用wx.login拿到的临时code到微信提供的code2session接口授权
    """
    post_data = request.body.decode('utf-8')
    post_data = json.loads(post_data)
    code = post_data.get('code').strip()
    app_id = post_data.get('appId').strip()
    nickname = post_data.get('nickname').strip()

    response = {}
    if not code or not app_id:
        response['message'] = '认证不完整,需要完整数据'
        response['code'] = ReturnCode.BROKEN_AUTHORIZED_DATA
        return JsonResponse(data=response, safe=False)

    # 将app_id和code拼凑成url,去微信接口服务做认证,返回值是认证结果
    data = c2s(app_id, code)
    openid = data.get('openid')
    print('get openid:',openid)
    if not openid:
        # 如果openid不存在,返回认证失败的接口信息
        response = CommonResponseMixin.wrap_json_response(code=ReturnCode.SUCCESS, message='auth failed')
        return JsonResponse(data=response, safe=False)

    # open_id存在就做进一步开发,利用session中间件标记两个数据
    request.session['open_id'] = openid
    request.session['is_authorized'] = True

    if not User.objects.filter(open_id=openid):
        # 如果数据库中查不到这个open_id就保存
        new_user = User(open_id=openid, nickname=nickname)
        print('new user: open_id: %s, nickname: %s' %(openid, nickname))
        new_user.save()

    response = CommonResponseMixin.wrap_json_response(code=ReturnCode.SUCCESS, message='auth success')
    return JsonResponse(data=response, safe=False)