fix stupid bug on save
[redakcja.git] / apps / apiclient / views.py
index f851590..0dbaf5a 100644 (file)
@@ -1,4 +1,5 @@
-import cgi
+# -*- coding: utf-8 -*-
+import urlparse
 
 from django.contrib.auth.decorators import login_required
 from django.core.urlresolvers import reverse
@@ -7,8 +8,7 @@ import oauth2
 
 from apiclient.models import OAuthConnection
 from apiclient import wl_consumer
-from apiclient.settings import (WL_REQUEST_TOKEN_URL, WL_ACCESS_TOKEN_URL, 
-        WL_AUTHORIZE_URL)
+from apiclient.settings import (WL_REQUEST_TOKEN_URL, WL_ACCESS_TOKEN_URL, WL_AUTHORIZE_URL)
 
 
 @login_required
@@ -21,8 +21,8 @@ def oauth(request):
     if resp['status'] != '200':
         raise Exception("Invalid response %s." % resp['status'])
 
-    request_token = dict(cgi.parse_qsl(content))
-    
+    request_token = dict(urlparse.parse_qsl(content))
+
     conn = OAuthConnection.get(request.user)
     # this might reset existing auth!
     conn.access = False
@@ -31,9 +31,9 @@ def oauth(request):
     conn.save()
 
     url = "%s?oauth_token=%s&oauth_callback=%s" % (
-            WL_AUTHORIZE_URL, 
+            WL_AUTHORIZE_URL,
             request_token['oauth_token'],
-            request.build_absolute_uri(reverse("users_oauth_callback")),
+            request.build_absolute_uri(reverse("apiclient_oauth_callback")),
             )
 
     return HttpResponseRedirect(url)
@@ -50,11 +50,14 @@ def oauth_callback(request):
     token.set_verifier(oauth_verifier)
     client = oauth2.Client(wl_consumer, token)
     resp, content = client.request(WL_ACCESS_TOKEN_URL, method="POST")
-    access_token = dict(cgi.parse_qsl(content))
-
-    conn.access = True
-    conn.token = access_token['oauth_token']
-    conn.token_secret = access_token['oauth_token_secret']
-    conn.save()
+    access_token = dict(urlparse.parse_qsl(content))
+
+    if 'oauth_token' in access_token:
+        conn.access = True
+        conn.token = access_token['oauth_token']
+        conn.token_secret = access_token['oauth_token_secret']
+        conn.save()
+    else:
+        return HttpResponse(request.GET.get('error'))
 
     return HttpResponseRedirect('/')