from django.conf import settings
from django.views.generic.simple import direct_to_template
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required

from registration.views import register as registration_register
from registration.forms import RegistrationForm

from invitation.models import InvitationKey
from invitation.forms import InvitationKeyForm

is_key_valid = InvitationKey.objects.is_key_valid

def invited(request, invitation_key=None):
    if 'INVITE_MODE' in settings.get_all_members() and settings.INVITE_MODE:
        if invitation_key and is_key_valid(invitation_key):
            template = 'invitation/invited.html'
        else:
            template = 'invitation/wrong_invitation_key.html'
        return direct_to_template(request, template)
    else:
        return HttpResponseRedirect(reverse('registration_register'))

def register(request, success_url=None,
            form_class=RegistrationForm, profile_callback=None,
            template_name='registration/registration_form.html',
            extra_context=None):
    if 'INVITE_MODE' in settings.get_all_members() and settings.INVITE_MODE:
        if 'invitation_key' in request.REQUEST \
            and is_key_valid(request.REQUEST['invitation_key']):
            return registration_register(request, success_url, form_class, 
                        profile_callback, template_name, extra_context)
        else:
            return direct_to_template(request, 'invitation/wrong_invitation_key.html')
    else:
        return registration_register(request, success_url, form_class, 
                            profile_callback, template_name, extra_context)

def invite(request, success_url=None,
            form_class=InvitationKeyForm,
            template_name='invitation/invitation_form.html',):
    if request.method == 'POST':
        form = form_class(data=request.POST, files=request.FILES)
        if form.is_valid():
            invitation = InvitationKey.objects.create_invitation(request.user)
            invitation.send_to(form.cleaned_data["email"])
            # success_url needs to be dynamically generated here; setting a
            # a default value using reverse() will cause circular-import
            # problems with the default URLConf for this application, which
            # imports this file.
            return HttpResponseRedirect(success_url or reverse('invitation_complete'))
    else:
        form = form_class()
    return direct_to_template(request, template_name, {'form': form})
invite = login_required(invite)

@@ -847,16 +847,52 @@
 template
+, %7B'invitation_key': invitation_key%7D
 )%0A    el
@@ -1359,16 +1359,231 @@
 key'%5D):%0A
+            if extra_context is None:%0A                extra_context = %7B'invitation_key': request.REQUEST%5B'invitation_key'%5D%7D%0A            else:%0A                extra_context.update(%7B'invitation_key': invitation_key%7D)%0A
         
@@ -2828,16 +2828,25 @@
 _name, %7B
+%0A        
 'form': 
@@ -2849,16 +2849,123 @@
 m': form
+,%0A        'remaining_invitations': InvitationKey.objects.remaining_invitations_for_user(request.user),%0A    
 %7D)%0Ainvit

Add useful keys in context