2012-01-29 5 views
0

これは私がDjango RemoteUserBackendからインスピレーションを得たコードです。これはまだ完全ではありません。通常のバックエンドやremoteuserbackendでは、 ?申し訳ありませんが、私はジャンゴに新しいですし、プロセス内のユーザ・ログは魔法djangoでfacebook認証を書こうとしている

from django.contrib import ModelBackend  
from django.contrib.auth.models import User, Permission 

def facebook_login_required(orig_view): 
    def wrapper(request): 
     if not request.user.is_authenticated(): 
      redirect_url = 'https://www.facebook.com/dialog/oauth?client_id=%s&redirect_uri=%s&scope=email,read_stream'%(SETTINGS.FB_APPID,request.getlocation) 
      HttpResponseRedirect(redirect_url) 
     else: 
      # user is logged in, its safe to process the view 
      return orig_view 
    return wrapper 

class FacebookAuthBackend(ModelBackend): 
    def authenticate(self,userid): 
     """ 
     The ``userid`` passed here is considered trusted.This method 
     simply returns the ``User`` objects with the given id, else 
     it creates a new user with the this ``userid`` if the it does 
     not existz 
     """ 
     if not userid: 
      user = User(userid=userid) 
      user.save() 
     user = None 
     try: 
      user = User.objects.get(userid=userid) 
     except User.DoesNotExist: 
      pass 
     return user 

    def get_user(self,userid): 
     try: 
      User.objects.get(userid=userid) 
     except User.DoesNotExist: 
      return None 
+1

なぜdjango social authのようなものを使用しないのですか? https://github.com/omab/django-social-auth;) – patrick

+0

+1の 'django-social-auth'です。設定は非常に簡単で、既存の 'django.contrib.auth'と統合されており、Convoreに関する積極的なフォーラムで非常によく文書化されています –

+0

私はこれを学習の目的のために自分で行いたいと思います。 –

答えて