kourosh

Invite only + django-social-auth

Jun 28th, 2012
1,573
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.07 KB | None | 0 0
  1. This document explains how to put together an invite only plus django social auth.
  2. django social auth : https://github.com/omab/django-social-auth/
  3. invitation application comes from http://code.larlet.fr/django-invitation/overview/
  4. follow the instructions for installing invitation application and social auth.
  5. Now, the only thing you need to change in invitation application is adding one line to save invitation key in request.session.
  6.  
  7. After that you have to write your own piplines, my application with those custom pipelines is called "main" and templates go into "socialauth" folder.
  8.  
  9. #invitation.views
  10. def invited(request, invitation_key=None, extra_context=None):
  11.     if getattr(settings, 'INVITE_MODE', False):
  12.         if invitation_key and is_key_valid(invitation_key):
  13.             template_name = 'invitation/invited.html'
  14.         else:
  15.             template_name = 'invitation/wrong_invitation_key.html'
  16.         extra_context = extra_context is not None and extra_context.copy() or {}
  17.         extra_context.update({'invitation_key': invitation_key})
  18.         request.session['invitation_key']= invitation_key #the only line changed in this view:)
  19.         return direct_to_template(request, template_name, extra_context)
  20.     else:
  21.         return HttpResponseRedirect(reverse('registration_register'))
  22.  
  23.  
  24. # settings.py
  25.  
  26.  
  27.  
  28. SOCIAL_AUTH_PIPELINE = (
  29.     'social_auth.backends.pipeline.social.social_auth_user',
  30.     #'main.pipeline.social_auth_user',
  31.     'social_auth.backends.pipeline.associate.associate_by_email',
  32.     'social_auth.backends.pipeline.misc.save_status_to_session',
  33.     'main.pipeline.redirect_to_form',
  34.     'main.pipeline.username',
  35.     #'social_auth.backends.pipeline.user.get_username',
  36.     'social_auth.backends.pipeline.user.create_user',
  37.     'social_auth.backends.pipeline.social.associate_user',
  38.     'social_auth.backends.pipeline.social.load_extra_data',
  39.     'social_auth.backends.pipeline.user.update_user_details',
  40.     'social_auth.backends.pipeline.misc.save_status_to_session',
  41.     'main.pipeline.redirect_to_form2',
  42.     #'main.pipeline.first_name',
  43.     'main.pipeline.email',
  44. )
  45.  
  46.  
  47. #main.pipeline
  48. from django.http import HttpResponseRedirect
  49.  
  50.  
  51. def redirect_to_form(*args, **kwargs):
  52.     if not kwargs['request'].session.get('saved_username') and \
  53.        kwargs.get('user') is None:
  54.         return HttpResponseRedirect('/formusername/')
  55.  
  56. from django.http import Http404
  57.  
  58. def username(request, *args, **kwargs):
  59.         if kwargs.get('user'):
  60.             username = kwargs['user'].username
  61.         else:
  62.             username = request.session.get('saved_username')
  63.         return {'username': username}
  64.    
  65.  
  66. def redirect_to_form2(*args, **kwargs):
  67.     if not kwargs['request'].session.get('email_address') and not kwargs.get('user').email:
  68.         return HttpResponseRedirect('/formemail/')
  69.  
  70. def email(request, *args, **kwargs):
  71.     if kwargs.get('user') and request.session.get('email_address'):
  72.         email = request.session['email_address']
  73.         user = kwargs['user']
  74.         user.email = email
  75.         user.save()
  76.  
  77.  
  78. def first_name(request, *args, **kwargs):
  79.     if 'saved_first_name' in request.session:
  80.         user = kwargs['user']
  81.         user.first_name = request.session.get('saved_first_name')
  82.         user.save()
  83.  
  84.  
  85. #main.urls
  86.  
  87. #social auth customization
  88. urlpatterns += patterns('main.views',
  89.     url(r'^error/$', 'error', name='error'),
  90.     url(r'^formusername/$', 'formusername', name='formusername'),
  91.     url(r'^formemail/$', 'formemail', name='formemail'),
  92. )
  93.  
  94.  
  95. #main.views
  96.  
  97. """
  98. Social Auth customized
  99. """
  100.  
  101.  
  102. from social_auth.utils import setting
  103.  
  104. def error(request):
  105.     """Error view"""
  106.     messages = get_messages(request)
  107.     return render_to_response('socialauth/error.html', {'version': version,
  108.                                              'messages': messages},
  109.                               RequestContext(request))
  110.  
  111.  
  112.  
  113. from invitation.models import InvitationKey
  114. is_key_valid = InvitationKey.objects.is_key_valid
  115.  
  116. def formusername(request):
  117.     if request.method == 'POST' and request.POST.get('username') and request.session.get('invitation_key'):
  118.         if is_key_valid(request.session['invitation_key']):
  119.             if (not User.objects.filter(username= request.POST['username']).exists()):
  120.                 name = setting('SOCIAL_AUTH_PARTIAL_PIPELINE_KEY', 'partial_pipeline')
  121.                 request.session['saved_username'] = request.POST['username']
  122.                 backend = request.session[name]['backend']
  123.                 return redirect('socialauth_complete', backend=backend)
  124.             else:
  125.                 return render_to_response('socialauth/form.html', {'error': _('Username already exists!')}, RequestContext(request))
  126.         else:
  127.             render_to_response('socialauth/form.html', {'error': _('Invalid Invitation Key')}, RequestContext(request))
  128.     return render_to_response('socialauth/form.html', {}, RequestContext(request))
  129.  
  130.  
  131. def formemail(request):
  132.     if request.method == 'POST' and request.POST.get('email_address'):
  133.         request.session['email_address'] = request.POST['email_address']
  134.         name = setting('SOCIAL_AUTH_PARTIAL_PIPELINE_KEY', 'partial_pipeline')
  135.         backend = request.session[name]['backend']
  136.         return redirect('socialauth_complete', backend=backend)
  137.     return render_to_response('socialauth/form2.html', {}, RequestContext(request))
  138.  
  139.  
  140.  
  141. #templates.socialauth.form.html
  142. {% extends "base.html" %}
  143.  
  144. {% block heading %}User basic form{% endblock %}
  145.  
  146. {% block content %}
  147. <form action="" method="post">
  148.     {% csrf_token %}
  149.     <p>{% if error%} {{error}} {%endif%}</p>
  150.  
  151.     <p>
  152.         <label for="id_username">username</label>
  153.         <input type="text" name="username" id="id_username" value="" />
  154.     </p>
  155.  
  156.     <input type="submit" value="Send" />
  157. </form>
  158. {% endblock %}
  159.  
  160.  
  161. #templates.socialauth.form2.html
  162.  
  163. {% extends "base.html" %}
  164.  
  165. {% block heading %}Email Form{% endblock %}
  166.  
  167. {% block content %}
  168. <form action="" method="post">
  169.     {% csrf_token %}
  170.     <p>
  171.         <label for="id_email">Email Address</label>
  172.         <input type="text" name="email_address" id="id_email" value="" />
  173.     </p>
  174.  
  175.     <input type="submit" value="Send" />
  176. </form>
  177. {% endblock %}
Advertisement
Add Comment
Please, Sign In to add comment