Browse Source

views.py:index() handles linkedin profile through login

mk200789 10 years ago
parent
commit
28c101d919

+ 2 - 1
hackathon_starter/hackathon/admin.py

@@ -1,5 +1,5 @@
 from django.contrib import admin
-from hackathon.models import UserProfile, Profile, InstagramProfile, TwitterProfile, MeetupToken, GithubProfile
+from hackathon.models import UserProfile, Profile, InstagramProfile, TwitterProfile, MeetupToken, GithubProfile, LinkedinProfile
 
 # Register your models here.
 class TwitterProfileAdmin(admin.ModelAdmin):
@@ -11,4 +11,5 @@ admin.site.register(InstagramProfile)
 admin.site.register(TwitterProfile, TwitterProfileAdmin)
 admin.site.register(GithubProfile)
 admin.site.register(MeetupToken)
+admin.site.register(LinkedinProfile)
 

+ 13 - 0
hackathon_starter/hackathon/scripts/linkedin.py

@@ -45,3 +45,16 @@ class LinkedinOauthClient(object):
         content = json.loads(req.content)
         self.access_token = content['access_token']
         self.is_authorized = True
+
+    def getUserInfo(self):
+        link = 'https://api.linkedin.com/v1/people/~?format=json&oauth2_access_token=' + self.access_token
+        headers = {'x-li-format' : 'json',
+                   'content-type' : 'application/json'}
+        req = requests.get(link, headers=headers)
+        content = json.loads(req.content)
+
+        if req.status_code != 200:
+            raise Exception('Invalid response %s' %req.status_code)
+
+        self.user_id = content['id']
+        return content

+ 19 - 1
hackathon_starter/hackathon/views.py

@@ -32,7 +32,7 @@ from rest_framework.renderers import JSONRenderer
 from rest_framework.parsers import JSONParser
 
 # Models
-from hackathon.models import Snippet, Profile, InstagramProfile, TwitterProfile, MeetupToken, GithubProfile
+from hackathon.models import Snippet, Profile, InstagramProfile, TwitterProfile, MeetupToken, GithubProfile, LinkedinProfile
 from hackathon.serializers import SnippetSerializer
 from hackathon.forms import UserForm
 
@@ -101,6 +101,24 @@ def index(request):
             elif profile_track == 'linkedin':
                 code = request.GET['code']
                 getLinkedIn.get_access_token(code)
+                getLinkedIn.getUserInfo()
+
+                try:
+                    user = User.objects.get(username=getLinkedIn.user_id+'_linkedin')
+                except User.DoesNotExist:
+                    username = getLinkedIn.user_id+'_linkedin'
+                    new_user = User.objects.create_user(username, username+'@madwithlinkedin.com', 'password')
+                    new_user.save()
+                    try:
+                        profile =LinkedinProfile.objects.get(user = new_user.id)
+                        profile.access_token = LinkedinProfile.access_token
+                    except LinkedinProfile.DoesNotExist:
+                        profile = LinkedinProfile(user=new_user, access_token=getLinkedIn.access_token, linkedin_user=getLinkedIn.user_id)
+                    profile.save()
+                user = authenticate(username=getLinkedIn.user_id+'_linkedin', password='password')
+                login(request, user)
+
+
     else:
         if request.GET.items():
             user = User.objects.get(username = request.user.username)