Пример #1
0
def show_invite_link(invitation_template="facebook/invitation.fbml",
                     show_link=True):
    """display an invite friends link"""
    fb = get_facebook_client()
    current_site = Site.objects.get_current()

    content = render_to_string(invitation_template, {
        'inviter': fb.uid,
        'url': fb.get_add_url(),
        'site': current_site
    })

    from cgi import escape
    content = escape(content, True)

    facebook_uid = fb.uid
    fql = "SELECT uid FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1='%s') AND has_added_app = 1" % fb.uid
    result = fb.fql.query(fql)
    # Extract the user ID's returned in the FQL request into a new array.
    if result and isinstance(result, list):
        friends_list = map(lambda x: str(x['uid']), result)
    else:
        friends_list = []
    # Convert the array of friends into a comma-delimeted string.
    exclude_ids = ','.join(friends_list)

    return {
        'exclude_ids': exclude_ids,
        'content': content,
        'action_url': '',
        'site': current_site,
        'show_link': show_link,
    }
Пример #2
0
def show_invite_link(invitation_template="facebook/invitation.fbml",show_link=True):
    """display an invite friends link"""
    fb = get_facebook_client()
    current_site = Site.objects.get_current()
    
    content = render_to_string(invitation_template,
                               { 'inviter': fb.uid,
                                 'url': fb.get_add_url(),
                                 'site': current_site })
    
    from cgi import escape 
    content = escape(content, True) 

    facebook_uid = fb.uid
    fql = "SELECT uid FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1='%s') AND has_added_app = 1" % fb.uid
    result = fb.fql.query(fql)
    # Extract the user ID's returned in the FQL request into a new array.
    if result and isinstance(result, list):
        friends_list = map(lambda x: str(x['uid']), result)
    else: friends_list = []
    # Convert the array of friends into a comma-delimeted string.
    exclude_ids = ','.join(friends_list) 
    
    return {
        'exclude_ids':exclude_ids,
        'content':content,
        'action_url':'',
        'site':current_site,
        'show_link':show_link,
    }
Пример #3
0
def unregister_fb_profile(sender, **kwargs):
    """call facebook and let them know to unregister the user"""
    fb = get_facebook_client()
    fb.connect.unregisterUser([fb.hash_email(kwargs['instance'].user.email)])


#post_delete.connect(unregister_fb_profile,sender=FacebookProfile)
Пример #4
0
 def get_current(self):
     """Gets a User object for the logged-in Facebook user."""
     facebook = get_facebook_client()
     user, created = self.get_or_create(id=_2int(facebook, "uid"))
     if created:
         # we could do some custom actions for new users here...
         pass
     return user
Пример #5
0
 def get_current(self):
     """Gets a User object for the logged-in Facebook user."""
     facebook = get_facebook_client()
     user, created = self.get_or_create(id=_2int(facebook, 'uid'))
     if created:
         # we could do some custom actions for new users here...
         pass
     return user
Пример #6
0
 def get_current(self):
     """Gets a User object for the logged-in Facebook user."""
     facebook = get_facebook_client()
     user, created = self.get_or_create(id=int(facebook.uid))
     if created:
         # assign experimental group for user 
         user.experiment = random.randint(0,3)
         user.save()
     return user
Пример #7
0
 def authenticate(self, request=None):
     facebook = get_facebook_client()
     facebook.check_session(request)
     if facebook.uid:
         try:
             facebook_profile = FacebookProfile.objects.get(uid=facebook.uid)
             return facebook_profile.user
         except FacebookProfile.DoesNotExist:
             return None
         except User.DoesNotExist:
             return None
Пример #8
0
    def __get_facebook_info(self,fbids):
        """
           Takes an array of facebook ids and caches all the info that comes
           back. Returns a tuple - an array of all facebook info, and info for
           self's fb id.
        """
        _facebook_obj = get_facebook_client()
        all_info = []
        my_info = None
        ids_to_get = []
        for fbid in fbids:
            if fbid == 0 or fbid is None:
                continue
            
            if _facebook_obj.uid is None:
                cache_key = 'fb_user_info_%s' % fbid
            else:
                cache_key = 'fb_user_info_%s_%s' % (_facebook_obj.uid, fbid)
        
            fb_info_cache = cache.get(cache_key)
            if fb_info_cache:
                log.debug("Found %s in cache" % fbid)
                all_info.append(fb_info_cache)
                if fbid == self.facebook_id:
                    my_info = fb_info_cache
            else:
                ids_to_get.append(fbid)
        
        if len(ids_to_get) > 0:
            log.debug("Calling for %s" % ids_to_get)
            tmp_info = _facebook_obj.users.getInfo(
                            ids_to_get, 
                            self.FACEBOOK_FIELDS
                        )
            
            all_info.extend(tmp_info)
            for info in tmp_info:
                if info['uid'] == self.facebook_id:
                    my_info = info
                
                if _facebook_obj.uid is None:
                    cache_key = 'fb_user_info_%s' % fbid
                else:
                    cache_key = 'fb_user_info_%s_%s' % (_facebook_obj.uid,info['uid'])

                cache.set(
                    cache_key, 
                    info, 
                    getattr(settings, 'FACEBOOK_CACHE_TIMEOUT', 1800)
                )
                
        return all_info, my_info
Пример #9
0
 def authenticate(self, request=None):
     fb = get_facebook_client()
     fb.check_session(request)
     if fb.uid:
         try:
             logging.debug("Checking for Facebook Profile %s..." % fb.uid)
             fbprofile = FacebookProfile.objects.get(facebook_id=fb.uid)
             return fbprofile.user
         except FacebookProfile.DoesNotExist:
             logging.debug("FB account hasn't been used before...")
             return None
     else:
         logging.debug("Invalid Facebook login for %s" % fb.__dict__)
         return None
Пример #10
0
 def is_authenticated(self):
     """Check if this fb user is logged in"""
     _facebook_obj = get_facebook_client()
     if _facebook_obj.session_key and _facebook_obj.uid:
         try:
             fbid = _facebook_obj.users.getLoggedInUser()
             if int(self.facebook_id) == int(fbid):
                 return True
             else:
                 return False
         except FacebookError,ex:
             if ex.code == 102:
                 return False
             else:
                 raise
Пример #11
0
    def __get_facebook_friends(self):
        """returns an array of the user's friends' fb ids"""
        _facebook_obj = get_facebook_client()
        friends = []
        cache_key = "fb_friends_%s" % (self.facebook_id)

        fb_info_cache = cache.get(cache_key)
        if fb_info_cache:
            friends = fb_info_cache
        else:
            log.debug("Calling for '%s'" % cache_key)
            friends = _facebook_obj.friends.getAppUsers()
            cache.set(cache_key, friends, getattr(settings, "FACEBOOK_CACHE_TIMEOUT", 1800))

        return friends
Пример #12
0
    def __get_facebook_friends(self):
        """returns an array of the user's friends' fb ids"""
        _facebook_obj = get_facebook_client()
        friends = []
        cache_key = 'fb_friends_%s' % (self.facebook_id)

        fb_info_cache = cache.get(cache_key)
        if fb_info_cache:
            friends = fb_info_cache
        else:
            log.debug("Calling for '%s'" % cache_key)
            friends = _facebook_obj.friends.getAppUsers()
            cache.set(cache_key, friends,
                      getattr(settings, 'FACEBOOK_CACHE_TIMEOUT', 1800))

        return friends
Пример #13
0
    def __get_facebook_info(self,fbids):
        """
           Takes an array of facebook ids and caches all the info that comes back.
           Returns a tuple - an array of all facebook info, and info for self's fb id.
        """
        _facebook_obj = get_facebook_client()
        all_info = []
        my_info = self.DUMMY_FACEBOOK_INFO
        ids_to_get = []
        for id in fbids:
            if id is 0:
                ret.append(self.DUMMY_FACEBOOK_INFO)
            
            if _facebook_obj.uid is None:
                cache_key = 'fb_user_info_%s' % id
            else:
                cache_key = 'fb_user_info_%s_%s' % (_facebook_obj.uid,id)
        
            fb_info_cache = cache.get(cache_key)
            if fb_info_cache:
                all_info.append(fb_info_cache)
                if id == self.facebook_id:
                    my_info = fb_info_cache
            else:
                ids_to_get.append(id)
        
        if len(ids_to_get) > 0:
            if getattr(settings,'RANDOM_FACEBOOK_FAIL',False) and random.randint(1,10) is 8:
                raise FacebookError(102,"RANDOM FACEBOOK FAIL!!!",[])
            elif getattr(settings,'RANDOM_FACEBOOK_FAIL',False) and random.randint(1,10) is 3:
                raise URLError(104)
            logging.debug("FBC: Calling for '%s'" % ids_to_get)
            tmp_info = _facebook_obj.users.getInfo(ids_to_get, self.FACEBOOK_FIELDS)
            
            all_info.extend(tmp_info)
            for info in tmp_info:
                if info['uid'] == self.facebook_id:
                    my_info = info
                
                if _facebook_obj.uid is None:
                    cache_key = 'fb_user_info_%s' % id
                else:
                    cache_key = 'fb_user_info_%s_%s' % (_facebook_obj.uid,info['uid'])

                cache.set(cache_key,info,getattr(settings,'FACEBOOK_CACHE_TIMEOUT',1800))
                
        return all_info,my_info
Пример #14
0
 def authenticate(self, request=None):
     fb = get_facebook_client()
     fb.check_session(request)
     if fb.uid:
         try:
             log.debug("Checking for Facebook Profile %s..." % fb.uid)
             fbprofile = FacebookProfile.objects.get(facebook_id=fb.uid)
             return fbprofile.user
         except FacebookProfile.DoesNotExist:
             log.debug("FB account hasn't been used before...")
             return None
         except User.DoesNotExist:
             log.error("FB account exists without an account.")
             return None
     else:
         log.debug("Invalid Facebook login for %s" % fb.__dict__)
         return None
Пример #15
0
 def __get_facebook_friends(self):
     _facebook_obj = get_facebook_client()
     friends = []
     cache_key = 'fb_friends_%s' % (self.facebook_id)
 
     fb_info_cache = cache.get(cache_key)
     if fb_info_cache:
         friends = fb_info_cache
     else:
         if getattr(settings,'RANDOM_FACEBOOK_FAIL',False) and random.randint(1,10) is 8:
             raise FacebookError(102,"RANDOM FACEBOOK FAIL!!!",[])
         elif getattr(settings,'RANDOM_FACEBOOK_FAIL',False) and random.randint(1,10) is 3:
             raise URLError(104)
         logging.debug("FBC: Calling for '%s'" % cache_key)
         friends = _facebook_obj.friends.getAppUsers()
         cache.set(cache_key,friends,getattr(settings,'FACEBOOK_CACHE_TIMEOUT',1800))
     
     return friends        
Пример #16
0
 def publish_to_facebook_stream(self, message=None, attachment=None,
                             action_links=None, target_uid=None):
     """
     This function pushes wall posts to users walls identified by uid.  It
     is assumed that the UID is a friend of this user, and that the action
     link is defined correctly.
     
     """
     if message is None and attachment is None and action_link is None and \
            target_uid is None:
         return None
     
     post_id = None
     _facebook_obj = get_facebook_client()
     try:
         post_id = _facebook_obj.stream.publish(message,
                                                attachment,
                                                action_links,
                                                target_uid,
                                                _facebook_obj.uid)
     except (FacebookError, URLError), ex:
         log.error("Fail publishing to stream: %s" % ex)
Пример #17
0
    def __get_facebook_info(self,fbids):
        _facebook_obj = get_facebook_client()
        ret = []
        ids_to_get = []
        for id in fbids:
            if id is 0:
                ret.append(self.DUMMY_FACEBOOK_INFO)
            
            if _facebook_obj.uid is None:
                cache_key = 'fb_user_info_%s' % id
            else:
                cache_key = 'fb_user_info_%s_%s' % (_facebook_obj.uid,id)
        
            fb_info_cache = cache.get(cache_key)
            if fb_info_cache:
                ret.append(fb_info_cache)
            else:
                ids_to_get.append(id)
        
        if len(ids_to_get) > 0:
            if getattr(settings,'RANDOM_FACEBOOK_FAIL',False) and random.randint(1,10) is 8:
                raise FacebookError(102,"RANDOM FACEBOOK FAIL!!!",[])
            elif getattr(settings,'RANDOM_FACEBOOK_FAIL',False) and random.randint(1,10) is 3:
                raise URLError(104)
            logging.debug("FBC: Calling for '%s'" % ids_to_get)
            tmp_info = _facebook_obj.users.getInfo(ids_to_get, self.FACEBOOK_FIELDS)
            
            ret.extend(tmp_info)
            for info in tmp_info:
                if _facebook_obj.uid is None:
                    cache_key = 'fb_user_info_%s' % id
                else:
                    cache_key = 'fb_user_info_%s_%s' % (_facebook_obj.uid,info['uid'])

                cache.set(cache_key,info,getattr(settings,'FACEBOOK_CACHE_TIMEOUT',1800))
                
        return ret
Пример #18
0
def unregister_fb_profile(sender, **kwargs):
    """call facebook and let them know to unregister the user"""
    fb = get_facebook_client()
    fb.connect.unregisterUser([fb.hash_email(kwargs['instance'].user.email)])

#post_delete.connect(unregister_fb_profile,sender=FacebookProfile)
Пример #19
0
    def getCurrentFromFacebook(request):
        
#        print "-------------------------------------------------------"
        
#        for key, value in request.POST.items():
#            if key[:6] == 'fb_sig':
#                print "*** key = %30s %s" % (key, value)
# ___________________________________________________________
#WITH @facebook2.require_login() and Mike Harley's FB account                
#*** key =                    fb_sig_time 1221400257.7585
#*** key =                   fb_sig_added 1
#*** key =                  fb_sig_locale en_US
#*** key =            fb_sig_position_fix 1
#*** key =         fb_sig_in_new_facebook 1
#*** key =     fb_sig_profile_update_time 1216223143
#*** key =          fb_sig_request_method GET
#*** key =                    fb_sig_user 604651074
#*** key =             fb_sig_session_key 8aa936ee8ef2866e28536889-604651074
#*** key =                 fb_sig_expires 0
#*** key =                         fb_sig 0fa1af2b734578f297ae499f05d918bd
#*** key =                 fb_sig_api_key 97d02134a3c07c35ff98b95d7588ea39
#*** key =                 fb_sig_friends 36814473,505844967,506011582,506064455,510521229,512761178,515024155,516605211,534319185,546685637,552962291,568142492,578590155,578748135,595140810,605123451,608045730,608342049,636435211,642800777,651596873,655895568,657680990,662173766,662484026,665870435,
#*** key =               fb_sig_in_canvas 1
        
# ___________________________________________________________
#WITHOUT @facebook2.require_login() and Mike Harley's FB account                
#*** key =                    fb_sig_time 1221400211.7892
#*** key =                   fb_sig_added 1
#*** key =                  fb_sig_locale en_US
#*** key =            fb_sig_position_fix 1
#*** key =         fb_sig_in_new_facebook 1
#*** key =     fb_sig_profile_update_time 1216223143
#*** key =          fb_sig_request_method GET
#*** key =                    fb_sig_user 604651074
#*** key =             fb_sig_session_key 8aa936ee8ef2866e28536889-604651074
#*** key =                 fb_sig_expires 0
#*** key =                         fb_sig c3b20aca7944e9cf02098161d59e98b7
#*** key =                 fb_sig_api_key 97d02134a3c07c35ff98b95d7588ea39
#*** key =                 fb_sig_friends 36814473,505844967,506011582,506064455,510521229,512761178,515024155,516605211,534319185,546685637,552962291,568142492,578590155,578748135,595140810,605123451,608045730,608342049,636435211,642800777,651596873,655895568,657680990,662173766,662484026,665870435,
#*** key =               fb_sig_in_canvas 1
        
# ___________________________________________________________
#WITH @facebook2.require_login() and RA SUN FB account     
# NOTHING BECAUS HE HASN'T ADDED THE APPLICATION    
# NOTHING BECAUS HE HASN'T ADDED THE APPLICATION    
# NOTHING BECAUS HE HASN'T ADDED THE APPLICATION    
# NOTHING BECAUS HE HASN'T ADDED THE APPLICATION    
        
# ___________________________________________________________
#WITHout @facebook2.require_login() and RA SUN FB account     
#*** key =                    fb_sig_time 1221400397.421
#*** key =                   fb_sig_added 0
#*** key =                  fb_sig_locale en_US
#*** key =            fb_sig_position_fix 1
#*** key =         fb_sig_in_new_facebook 1
#*** key =          fb_sig_request_method GET
#*** key =                         fb_sig 3b34aa11f635cd65a21c06973133c2e2
#*** key =                 fb_sig_api_key 97d02134a3c07c35ff98b95d7588ea39
#*** key =               fb_sig_in_canvas 1     

#        print "*** request.facebook.users.isAppUser() = %s" % (request.facebook.users.isAppUser())  
        
        
#request.facebook.users.getInfo(title=title, body=body)
#            ('uids', list, []),
#            ('fields', list, [('default', ['name'])]),
        

#http://www.facebook.com/login.php?api_key=97d02134a3c07c35ff98b95d7588ea39&v=1.0        
#http://www.facebook.com/login.php?api_key=97d02134a3c07c35ff98b95d7588ea39&v=1.0        
#http://www.facebook.com/login.php?api_key=97d02134a3c07c35ff98b95d7588ea39&v=1.0        
#http://www.facebook.com/login.php?api_key=97d02134a3c07c35ff98b95d7588ea39&v=1.0        
        
        
#        print "-------------------------------------------------------"
        
        facebook = get_facebook_client()
        fbId = facebook.uid
        
#        if not fbId: 
##            fbId = 604651074
#            fbId = 1099808809
        try:
            instance = a_citizen_02.objects.get(facebook=fbId)
        except ObjectDoesNotExist:
#            instance = a_citizen_02.objects.create(facebook=int(facebook.uid), name=a_citizen_02.generateFacebookName((int(facebook.uid))))
            instance = a_citizen_02.birthNewCitizen(request, facebook=fbId)
        return instance
Пример #20
0
def unregister_fb_profile(sender, **kwargs):
    """call facebook and let them know to unregister the user"""
    fb = get_facebook_client()
    fb.connect.unregisterUser([fb.hash_email(kwargs["instance"].user.email)])
Пример #21
0
 def get_ext_perms(self):
     _facebook_obj = get_facebook_client()
     return _facebook_obj.ext_perms
Пример #22
0
 def get_add_url(self):
     _facebook_obj = get_facebook_client()
     return _facebook_obj.get_add_url()
Пример #23
0
 def __has_app_permission(self, permission):
     _facebook_obj = get_facebook_client()
     return _facebook_obj.users.hasAppPermission(permission)