Commits

Rod Morison  committed 63295b9

-fbcontroller.py: graphapi property moved from current_user to self.
-fbcontroller.py: protect against oauth that doesn't return an access token (thank you fb)
-fbuser.py: protect against various missing attrs in profile return from fb

  • Participants
  • Parent commits 5af0d3e

Comments (0)

Files changed (2)

File coneslib/cones/fbcontroller.py

             user_id = self.session.get('fb_user', None)
             if user_id:
                 self._current_user = FbUserBase.get_by_key_name(user_id)
-                if self._current_user:
-                    self._current_user.graph_api = facebook.GraphAPI(self._current_user.access_token)
         return self._current_user
-
+    
+    @property
+    def graph_api(self):
+        """Returns the logged in Facebook user, or None if unconnected."""
+        if not hasattr(self, "_graph_api"):
+            if self.current_user:
+                self._graph_api = facebook.GraphAPI(self.current_user.access_token)
+            else:
+                self._graph_api = facebook.GraphAPI()
+        return self._graph_api
+    
     def clear_user(self):
         self.session.pop('fb_user')
         
                                          data=urllib.urlencode(dict(client_id=config.FB_APP_ID,
                                                                     client_secret=config.FB_APP_SECRET,
                                                                     sessions=session_key))))
-        if oauth:
+        if oauth and oauth[0].get('access_token'):
             FbUserBase.create_or_update(uid, oauth[0]['access_token'])
             self.session['fb_user'] = uid
             self.session['fb_session_key'] = session_key

File coneslib/cones/fbuser.py

 import facebook
 import config
 
-class ChildMeta(type):
-    def __new__(meta, classname, bases, classDict):
-        if vars(config).get('FbUser'):
-            FbUser = config.FbUser
-        else:
-            FbUser = FbUserBase
-        return type.__new__(meta, classname, bases, classDict)
-
 def _create_or_update_with_uid(uid, access_token):
     graph = facebook.GraphAPI()
     profile = graph.get_object(uid)
     user = FbUserBase.get_by_key_name(uid)
     if user:
-        user.name = profile["name"]
-        user.first_name = profile["first_name"]
-        user.last_name = profile["last_name"]
-        user.link = profile["link"]
-        user.locale=profile["locale"]
+        # fb will spuriously omit some fields
+        user.name = profile.get("name") or user.name
+        user.first_name = profile.get("first_name") or user.first_name
+        user.last_name = profile.get("last_name") or user.last_name
+        user.link = profile.get("link") or user.link
+        user.locale = profile.get("locale") or user.locale
     else:
         user = config.FbUser(key_name=str(profile["id"]),
                              id=str(profile["id"]),
     uid = str(profile["id"])
     user = FbUserBase.get_by_key_name(uid)
     if user:
-        user.name = profile["name"]
-        user.first_name = profile["first_name"]
-        user.last_name = profile["last_name"]
-        user.link = profile["link"]
-        user.locale=profile["locale"]
+        user.name = profile.get("name") or user.name
+        user.first_name = profile.get("first_name") or user.first_name
+        user.last_name = profile.get("last_name") or user.last_name
+        user.link = profile.get("link") or user.link
+        user.locale = profile.get("locale") or user.locale
         user.access_token = access_token
     else:
         user = config.FbUser(key_name=str(profile["id"]),