Ejemplo n.º 1
0
def ajax_1(request):
    """
    ajax_type='1': 第一次种树填入树名字时候保存到数据库
    :param request:
    :return:
    """
    response = HttpResponse()
    response['Content-Type'] = 'text/javascript'
    user_id = request.POST.get('openid', '')
    user_name = request.POST.get('nickname', '')
    tree_name = request.POST.get('tree_name', '')
    if user_id and user_name and tree_name:
        try:
            user = User.objects.get(openid=user_id)
            user.is_plant = True
            user.nickname = user_name
            user.tree_name = tree_name
            user.friends.add(user)
            friend = user.friends.exclude(openid=user_id)[0]
            # friend = User.objects.get(openid=friend_id.openid)
            friend.count = friend.count + 3000
            friend.save()
            user.save()
            msg = Tree(owner=friend, tree_name=friend.tree_name, type=4, action_time=time.time(),
                       read=False, source_id=user_id, content='成功添加好友'+user_name)
            msg.save()
            tree = Tree(owner=user, tree_name=user.tree_name, type=7, action_time=time.time(),
                        source_id=user_id, content='种下了自己的幸福树')
            tree.save()
            ret = '4'
        except ObjectDoesNotExist:
            user = User(openid=user_id, nickname=user_name, time_stamp=time.time(), tree_name=tree_name)
            user.save()
            user.friends.add(user)
            user.save()
            tree = Tree(owner=user, tree_name=tree_name, type=7, action_time=time.time(), read=True, source_id=user_id,
                        content='创建了祝福树')
            tree.save()
            ret = '3'
    else:
        ret = '2'
    response.write(ret)
    return response
Ejemplo n.º 2
0
def ajax_1(request):
    """
    ajax_type='1': 第一次种树填入树名字时候保存到数据库
    :param request:
    :return:
    """
    response = HttpResponse()
    response['Content-Type'] = 'text/javascript'
    user_id = request.POST.get('openid', '')
    user_name = request.POST.get('nickname', '')
    tree_name = request.POST.get('tree_name', '')
    if user_id and user_name and tree_name:
        try:
            user = User.objects.get(openid=user_id)
            user.is_plant = True
            user.nickname = user_name
            user.tree_name = tree_name
            user.friends.add(user)
            friend = user.friends.exclude(openid=user_id)[0]
            # friend = User.objects.get(openid=friend_id.openid)
            friend.count = friend.count + 3000
            friend.save()
            user.save()
            msg = Tree(owner=friend,
                       tree_name=friend.tree_name,
                       type=4,
                       action_time=time.time(),
                       read=False,
                       source_id=user_id,
                       content='成功添加好友' + user_name)
            msg.save()
            tree = Tree(owner=user,
                        tree_name=user.tree_name,
                        type=7,
                        action_time=time.time(),
                        source_id=user_id,
                        content='种下了自己的幸福树')
            tree.save()
            ret = '4'
        except ObjectDoesNotExist:
            user = User(openid=user_id,
                        nickname=user_name,
                        time_stamp=time.time(),
                        tree_name=tree_name)
            user.save()
            user.friends.add(user)
            user.save()
            tree = Tree(owner=user,
                        tree_name=tree_name,
                        type=7,
                        action_time=time.time(),
                        read=True,
                        source_id=user_id,
                        content='创建了祝福树')
            tree.save()
            ret = '3'
    else:
        ret = '2'
    response.write(ret)
    return response
Ejemplo n.º 3
0
def visit(request):
    """
    处理访问别人的主页的逻辑
    :param sourceid: 谁的空间
    :return:
    """

    sourceid = request.GET.get('openid', '')
    code = request.GET.get('code', '')

    high_verify = request.GET.get('state', '')
    if high_verify == 'high_verify':
        oauth_vis = WeChatOAuth(appId, appsecret, 'http://1.blesstree.sinaapp.com/wechat/visit'+'?openid='+sourceid)
    else:
        oauth_vis = WeChatOAuth(appId, appsecret,
                                'http://1.blesstree.sinaapp.com/wechat/visit'+'?openid='+sourceid,
                                scope='snsapi_userinfo',
                                state='high_verify')

    # 这里是防止用户种树后取消关注了(不种树了)老的链接被别人点进去了
    error = False
    try:
        client.fetch_access_token()
        owner_info = client.user.get(client, sourceid)
        owner = owner_info['nickname']
        avatar = owner_info['headimgurl']
        owner_db = User.objects.get(openid=sourceid)
        water_time = 0  # TODO:时间转化问题
        count = owner_db.count
        count_bar = count/3000
        tree_name = owner_db.tree_name
    except KeyError:
        pass
    try:
        User.objects.get(openid=sourceid, is_plant=False)
        error = True
    except ObjectDoesNotExist:
        pass

    # ios系统返回按钮出现的bug的解决方法
    try:
        access_token = oauth_vis.fetch_access_token(code)
    except KeyError:
        # access_token = oauth_vis.access_token = code_access_token[code]['access_token']
        # oauth_vis.open_id = code_access_token[code]['openid']
        return HttpResponseRedirect(oauth_vis.authorize_url)

    try:
        flip_id = openid = oauth_vis.open_id
    except AttributeError:
        flip_id = openid = code_access_token[code]['openid']

    # 经过高级用户认证后的访问就有了获取头像与昵称的能力
    flip_nickname = False
    if high_verify == 'high_verify':
        flip_user = oauth_vis.get_user_info(openid=flip_id, access_token=access_token)
        fl_nickname = flip_nickname = flip_user['nickname']
        flip_avatar = flip_user['headimgurl']
        flip_nickname = True
    else:
        # TODO:这里是没有关注公众号的时候用户点进去,想祝福/吐槽/浇水/的时候
        try:
            client.fetch_access_token()
            user_from_wechat = client.user.get(client, flip_id)
            fl_nickname = flip_nickname = user_from_wechat['nickname']
            flip_avatar = user_from_wechat['headimgurl']
        except KeyError:
            flip_nickname = False
            # 获取一个高级认证作为点击浇水/祝福/吐槽事件的跳转链接,认证之后回调到原来的位置
            btn_redirect_url = WeChatOAuth(appId, appsecret,
                                           'http://1.blesstree.sinaapp.com/wechat/visit'+'?openid='+sourceid,
                                           scope='snsapi_userinfo',
                                           state='high_verify').authorize_url

    try:
        user = User.objects.get(openid=openid, is_plant=True)
    except ObjectDoesNotExist:
        user = 0

    if user is not 0:
        # 用户已经中树,因为只有关注用户才能种树,不关注用户只能评论吐槽,但是朋友关系要保存
        try:
            user.friends.get(openid=sourceid)
        except ObjectDoesNotExist:
            user.friends.add(User.objects.get(openid=sourceid))
            user.save()
            msg = Tree(owner=owner_db, tree_name=owner_db.tree_name, type=4, action_time=time.time(),
                           read=False, source_id=user.openid, content='成功添加好友'+user.nickname)
            msg.save()
        my_zone_url = WeChatOAuth(appId, appsecret, 'http://1.blesstree.sinaapp.com/wechat/home/').authorize_url
        return render_to_response('visit.html', locals())

    # 用户没有种树,点击按钮都会跳到认证链接来获取信息,获取的信息要保存
    my_zone_url = WeChatOAuth(appId, appsecret,
                              'http://1.blesstree.sinaapp.com/wechat/home/'+"?visit_index='123'&return_openid="+sourceid)\
        .authorize_url
    if flip_nickname:  # 通过点击别人分享进去的都需要保存,这里互动了的
        try:
            friendship = User.objects.get(openid=flip_id)
            friendship.nickname = fl_nickname
            friendship.avatar_url = flip_avatar
            friendship.save()
        except ObjectDoesNotExist:
            friendship = User(openid=flip_id, nickname=fl_nickname,
                              avatar_url=flip_avatar, time_stamp=time.time(),
                              tree_name='na', is_plant=False)
            friendship.save()
            source_fr = User.objects.get(openid=sourceid)
            friendship.friends.add(source_fr)  # 保存朋友关系,只是此时保存的关系的友人尚未种树
            friendship.save()
    elif request.GET.get('add'):  # 通过点击别人分享进去的都需要保存,这里用户只是点击进去过没有互动
        try:
            friendship = User.objects.get(openid=flip_id)
        except ObjectDoesNotExist:
            friendship = User(openid=oauth_vis.open_id, nickname='na', time_stamp=time.time(), tree_name='na', is_plant=False)
            friendship.save()
            source_fr = User.objects.get(openid=sourceid)
            friendship.friends.add(source_fr)  # 保存朋友关系,只是此时保存的关系的友人尚未种树
            friendship.save()
    return render_to_response('visit.html', locals())
Ejemplo n.º 4
0
def visit(request):
    """
    处理访问别人的主页的逻辑
    :param sourceid: 谁的空间
    :return:
    """

    sourceid = request.GET.get('openid', '')
    code = request.GET.get('code', '')

    high_verify = request.GET.get('state', '')
    if high_verify == 'high_verify':
        oauth_vis = WeChatOAuth(
            appId, appsecret, 'http://1.blesstree.sinaapp.com/wechat/visit' +
            '?openid=' + sourceid)
    else:
        oauth_vis = WeChatOAuth(appId,
                                appsecret,
                                'http://1.blesstree.sinaapp.com/wechat/visit' +
                                '?openid=' + sourceid,
                                scope='snsapi_userinfo',
                                state='high_verify')

    # 这里是防止用户种树后取消关注了(不种树了)老的链接被别人点进去了
    error = False
    try:
        client.fetch_access_token()
        owner_info = client.user.get(client, sourceid)
        owner = owner_info['nickname']
        avatar = owner_info['headimgurl']
        owner_db = User.objects.get(openid=sourceid)
        water_time = 0  # TODO:时间转化问题
        count = owner_db.count
        count_bar = count / 3000
        tree_name = owner_db.tree_name
    except KeyError:
        pass
    try:
        User.objects.get(openid=sourceid, is_plant=False)
        error = True
    except ObjectDoesNotExist:
        pass

    # ios系统返回按钮出现的bug的解决方法
    try:
        access_token = oauth_vis.fetch_access_token(code)
    except KeyError:
        # access_token = oauth_vis.access_token = code_access_token[code]['access_token']
        # oauth_vis.open_id = code_access_token[code]['openid']
        return HttpResponseRedirect(oauth_vis.authorize_url)

    try:
        flip_id = openid = oauth_vis.open_id
    except AttributeError:
        flip_id = openid = code_access_token[code]['openid']

    # 经过高级用户认证后的访问就有了获取头像与昵称的能力
    flip_nickname = False
    if high_verify == 'high_verify':
        flip_user = oauth_vis.get_user_info(openid=flip_id,
                                            access_token=access_token)
        fl_nickname = flip_nickname = flip_user['nickname']
        flip_avatar = flip_user['headimgurl']
        flip_nickname = True
    else:
        # TODO:这里是没有关注公众号的时候用户点进去,想祝福/吐槽/浇水/的时候
        try:
            client.fetch_access_token()
            user_from_wechat = client.user.get(client, flip_id)
            fl_nickname = flip_nickname = user_from_wechat['nickname']
            flip_avatar = user_from_wechat['headimgurl']
        except KeyError:
            flip_nickname = False
            # 获取一个高级认证作为点击浇水/祝福/吐槽事件的跳转链接,认证之后回调到原来的位置
            btn_redirect_url = WeChatOAuth(
                appId,
                appsecret,
                'http://1.blesstree.sinaapp.com/wechat/visit' + '?openid=' +
                sourceid,
                scope='snsapi_userinfo',
                state='high_verify').authorize_url

    try:
        user = User.objects.get(openid=openid, is_plant=True)
    except ObjectDoesNotExist:
        user = 0

    if user is not 0:
        # 用户已经中树,因为只有关注用户才能种树,不关注用户只能评论吐槽,但是朋友关系要保存
        try:
            user.friends.get(openid=sourceid)
        except ObjectDoesNotExist:
            user.friends.add(User.objects.get(openid=sourceid))
            user.save()
            msg = Tree(owner=owner_db,
                       tree_name=owner_db.tree_name,
                       type=4,
                       action_time=time.time(),
                       read=False,
                       source_id=user.openid,
                       content='成功添加好友' + user.nickname)
            msg.save()
        my_zone_url = WeChatOAuth(
            appId, appsecret,
            'http://1.blesstree.sinaapp.com/wechat/home/').authorize_url
        return render_to_response('visit.html', locals())

    # 用户没有种树,点击按钮都会跳到认证链接来获取信息,获取的信息要保存
    my_zone_url = WeChatOAuth(appId, appsecret,
                              'http://1.blesstree.sinaapp.com/wechat/home/'+"?visit_index='123'&return_openid="+sourceid)\
        .authorize_url
    if flip_nickname:  # 通过点击别人分享进去的都需要保存,这里互动了的
        try:
            friendship = User.objects.get(openid=flip_id)
            friendship.nickname = fl_nickname
            friendship.avatar_url = flip_avatar
            friendship.save()
        except ObjectDoesNotExist:
            friendship = User(openid=flip_id,
                              nickname=fl_nickname,
                              avatar_url=flip_avatar,
                              time_stamp=time.time(),
                              tree_name='na',
                              is_plant=False)
            friendship.save()
            source_fr = User.objects.get(openid=sourceid)
            friendship.friends.add(source_fr)  # 保存朋友关系,只是此时保存的关系的友人尚未种树
            friendship.save()
    elif request.GET.get('add'):  # 通过点击别人分享进去的都需要保存,这里用户只是点击进去过没有互动
        try:
            friendship = User.objects.get(openid=flip_id)
        except ObjectDoesNotExist:
            friendship = User(openid=oauth_vis.open_id,
                              nickname='na',
                              time_stamp=time.time(),
                              tree_name='na',
                              is_plant=False)
            friendship.save()
            source_fr = User.objects.get(openid=sourceid)
            friendship.friends.add(source_fr)  # 保存朋友关系,只是此时保存的关系的友人尚未种树
            friendship.save()
    return render_to_response('visit.html', locals())