added ghapi app
added GithubAPIMiddleware added ghapi.api.GitHub API calls can be done via request.github.get/get_iter or GitHub().get/get_iter.
This commit is contained in:
parent
f11032202f
commit
fdeab7bee0
5 changed files with 125 additions and 0 deletions
20
ghapi/middlewares.py
Normal file
20
ghapi/middlewares.py
Normal file
|
@ -0,0 +1,20 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from django.utils.functional import SimpleLazyObject
|
||||
from ghapi.api import GitHub
|
||||
from social_auth.db.django_models import UserSocialAuth
|
||||
|
||||
|
||||
def get_github(request):
|
||||
if not request.user.is_authenticated():
|
||||
return GitHub()
|
||||
try:
|
||||
social = UserSocialAuth.objects.get(provider='github', user_id=request.user.pk)
|
||||
except UserSocialAuth.DoesNotExist:
|
||||
return GitHub()
|
||||
token = social.tokens.get('access_token', None)
|
||||
return GitHub(token)
|
||||
|
||||
|
||||
class GithubAPIMiddleware(object):
|
||||
def process_request(self, request):
|
||||
request.user = SimpleLazyObject(lambda : get_github(request))
|
Loading…
Add table
Add a link
Reference in a new issue