Commits

hideki nara committed d0b8c07

新規、カムバック、現在のユーザーに割当の判定切り替え

  • Participants
  • Parent commits 0f6c874

Comments (0)

Files changed (5)

sample/app/github/api.py

 import requests
 import json
 
+def context(request):
+    return  { 'github_user'  : get_social_user(request.user), }
+
 def get_social_user(sys_user=None,social_id = None,provider=GithubBackend.name ):
     ''' UserSocialAuth の取得 '''
     if social_id:
         ret = UserSocialAuth.get_social_auth(GithubBackend.name, social_id )
         return ret
 
-    if sys_user == None:
+    if sys_user == None  or sys_user.is_authenticated() == False:
         return None
 
     ret = sys_user.social_auth.filter( provider=provider )

sample/app/github/templates/github/binding.html

 
 <h1>Binding Your Social Identity</h1>
 
-<a href="{% url socialauth_complete backend='github' %}" >バインド </a>
+<a href="{% url socialauth_complete backend='github' %}" >{{ message }}
+{% if user %}( {{user}} ) {% endif %}
+</a>
 
 {% endblock %}

sample/app/github/views.py

     return  HttpResponseRedirect( reverse('app_github_default'))
 
 def pipeline_binding(*args,**kwargs):
-    '''
+    ''' パイプライン: ソーシャルユーザーを受け入れるかどうかの判断
     '''
     request = kwargs.get('request',None)
     assert request != None
+    assert kwargs['github'] == True
+
+    user = kwargs.get('user',None)
+    social_user = kwargs.get('social_user',None)
+    uid = kwargs.get('uid',None)
+    is_new = kwargs.get('is_new',None)
 
+    #:ゲート(last)を判定
     last = request.session.get('last',None)
     if last != None and (now() - last ).total_seconds() < 100:
         return None  #:つぎへ
 
     #:ゲートをセット
     request.session['last'] = now()
+
+    #:表示メッセージ切り替え
+    if request.user.is_authenticated():
+        message = u"現在のユーザーにバインド"
+    elif user == None and social_user ==None:
+        message = u"新規登録"
+    else:
+        message = u"ソーシャルアカウントでログイン"
+
+    #: UI
     return render_to_response( 'github/binding.html',
             { 
                 'is_authenticated': request.user.is_authenticated(),
+                'is_new': is_new,
+                'user':user,  
+                'social_user': social_user,
+                'uid':uid,
+                'message': message,
             } , 
             context_instance=template.RequestContext(request),)
      

sample/app/settings.py

 
 TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
     'django.core.context_processors.request', # need some security considerations
+#    'social_auth.context_processors.social_auth_backends',  #: "social_auth"
+    'app.github.api.context',   #: "github_user"
 )
 
 ######

sample/app/templates/base.html

   <body>
     <div id="social">
         <div>
-        {% if request.user.is_authenticated %}
-            {{ request.user }} <a href="{% url app_github_gist gid='' command='create'  %}">New Gist</a> 
+        {% if request.user.is_authenticated  and github_user %}
+            {{ request.user }} 
+            <a href="{% url app_github_gist gid='' command='create'  %}">New Gist</a> 
             <a href="{% url app_github_logout %}">ログアウト</a>
-            
         {% else %}
             <a href="{% url socialauth_begin backend='github' %}">Github</a>
         {% endif %}