示例#1
0
def addingFriends(g_id, g_msg, g_add, fb_access_token):
    try:
        from helper import Facebook
        f_profile = json.loads(Facebook.get_profile(fb_access_token,'me'))
        if( 'error' in f_profile):
            return Error.msg_by_code(Error.InvalidFbToken)
        
        if Group.objects.filter(pk=g_id).exists():
            g = Group.objects.get(pk=g_id)
            members = g_add.split('*')
            friends_not_added = []
            for m_id in members:
                if (m_id.strip() == ''):
                    continue
                print m_id
                f_profile = json.loads(Facebook.get_profile(request.session['fb_access_token'], m_id))
                if( 'error' in f_profile):
                    friends_not_added.append(m_id)
                #if (is_fb_user_registered(m_id) == 0):
                #    Facebook.post_feed(request.session['fb_access_token'], m_id, g_msg)
                p_id = create_user(m_id, f_profile["first_name"] + ' ' + f_profile["last_name"])
                create_membership(g_id, p_id, request.session['member_id'], g_msg, 'I')
            res = {}
            res["success"] = 'yes'
            res["group_id"] = str(g_id)
            res["friends_not_added"] = friends_not_added
            return json.dumps(res)
        else:
            return Error.msg_by_code(Error.ErrorAddingFriends)
    except:
        return Error.format_json(str(sys.exc_value), str(sys.exc_type), 'sys')
示例#2
0
def login(request):
    try:
        g = request.POST
        f_access_token = g.get("fb_access_token", None)
        if ((not f_access_token)):
            return HttpResponse(Error.msg_by_code(Error.InvalidRequestTokens))
        from helper import Facebook
        f_profile = json.loads(Facebook.get_profile(f_access_token,'me'))
        if( 'error' in f_profile):
            destroy_session(request)
            return HttpResponse(Error.msg_by_code(Error.InvalidFbToken))
        
        request.session['member_id'] = create_user(f_profile['id'], f_profile["first_name"] + ' ' + f_profile["last_name"])
        request.session['fb_user_id'] = f_profile['id']
        request.session['fb_access_token'] = f_access_token
        request.session['first_name'] = f_profile["first_name"]
        request.session['last_name'] = f_profile["last_name"]
        res = {}
        res["success"] = "yes"
        res["member_id"] = request.session['member_id']
        return HttpResponse(json.dumps(res))
    except:
        destroy_session(request)
        return HttpResponse(Error.format_json(str(sys.exc_value), str(sys.exc_type), 'sys'))