Browse Source

documented GithubOauthClient, and cleaned up view.py's index()

mk200789 10 years ago
parent
commit
ee4ec3b0f9
2 changed files with 42 additions and 10 deletions
  1. 41 6
      hackathon_starter/hackathon/scripts/github.py
  2. 1 4
      hackathon_starter/hackathon/views.py

+ 41 - 6
hackathon_starter/hackathon/scripts/github.py

@@ -17,25 +17,53 @@ AUTHORIZE_URL = 'https://github.com/login/oauth/authorize'
 ACCESS_TOKEN_URL = 'https://github.com/login/oauth/access_token'
 
 class GithubOauthClient(object):
-    
+    '''
+    Python Client for Github API.
+    '''    
     access_token = None
     token_type = None
 
     def __init__(self, client_id, client_secret):
+        '''
+        Parameters:
+            client_id: String
+                - The client_id from registering application
+                  on Github.
+            client_secret: String
+                - The client_secret from registering application
+                  on Github.
+        '''
         self.client_id = client_id
         self.client_secret = client_secret
 
     def get_authorize_url(self):
+        '''
+        Obtains authorize url link with given client_id.
+
+        Returns:
+            authURL: String
+                - The authorization url.
+        '''
+
         auth_setting = {'client_id': self.client_id,
                         'redirect_uri': 'http://127.0.0.1:8000/hackathon/',
                         'scope': 'user, public_repo, repo, repo_deployment, notifications, gist'}
         params = urllib.urlencode(auth_setting)
-        auth_link = AUTHORIZE_URL + '?' + params
-        print auth_link
+        authURL = AUTHORIZE_URL + '?' + params
+        print authURL
         
-        return auth_link
+        return authURL
 
     def get_access_token(self, code):
+        '''
+        Obtains access token.
+
+        Parameters:
+            code: String
+                - The code is retrieved from the authorization url parameter
+                  to obtain access_token.
+        '''
+
         settings = {'client_id': self.client_id,
                     'client_secret': self.client_secret,
                     'code': code,
@@ -49,14 +77,20 @@ class GithubOauthClient(object):
         if int(req.status_code) != 200:
             raise Exception('Invalid response %s' %req.status_code)
 
-        print req.content
         content = urlparse.parse_qs(req.content)
-        print content
         self.access_token = content['access_token'][0]
         self.token_type = content['token_type'][0]
         self.scopes = content['scope'][0]
 
     def getUserInfo(self):
+        '''
+        Obtains user information.
+
+        Returns:
+            content: Dictionary
+                - A dictionary containing user information.
+        '''
+
         link = 'https://api.github.com/user?access_token=' + self.access_token
         req = requests.get(link)
 
@@ -65,6 +99,7 @@ class GithubOauthClient(object):
 
         content = json.loads(req.content)
         self.username = content['login']
+        return content
 
 
 

+ 1 - 4
hackathon_starter/hackathon/views.py

@@ -50,7 +50,6 @@ def index(request):
     if not request.user.is_active:
         if request.GET.items():
             if profile_track == 'github':
-                print "GITHUB"
                 code = request.GET['code']
                 getGithub.get_access_token(code)
                 getGithub.getUserInfo()
@@ -95,11 +94,11 @@ def index(request):
                 login(request, user)
     else:
         if request.GET.items():
+            user = User.objects.get(username = request.user.username)
             if profile_track == 'github':
                 code = request.GET['code']
                 getGithub.get_access_token(code)
                 getGithub.getUserInfo()
-                user = User.objects.get(username = request.user.username)
 
                 try:
                     githubUser = GithubProfile.objects.get(user=user.id)
@@ -109,7 +108,6 @@ def index(request):
             elif profile_track == 'twitter':
                 oauth_verifier = request.GET['oauth_verifier']
                 getTwitter.get_access_token_url(oauth_verifier)
-                user = User.objects.get(username = request.user.username)
 
                 try:
                     twitterUser = TwitterProfile.objects.get(user = user.id)
@@ -119,7 +117,6 @@ def index(request):
             elif profile_track == 'instagram':
                 code = request.GET['code']
                 getInstagram.get_access_token(code)
-                user = User.objects.get(username = request.user.username)
 
                 try: 
                     instagramUser = InstagramProfile.objects.get(user= user.id)