<commit_before>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
remaining_invitations_for_user = InvitationKey.objects.remaining_invitations_for_user

# TODO: move the authorization control to a dedicated decorator

def invited(request, invitation_key=None, extra_context=None):
    if 'INVITE_MODE' in settings.get_all_members() and settings.INVITE_MODE:
        if invitation_key and is_key_valid(invitation_key):
            template_name = 'invitation/invited.html'
        else:
            template_name = 'invitation/wrong_invitation_key.html'
        if extra_context is None:
            extra_context = {}
        extra_context.update({'invitation_key': invitation_key})
        return direct_to_template(request, template_name, extra_context)
    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 extra_context is None:
        extra_context = {}
    if 'INVITE_MODE' in settings.get_all_members() and settings.INVITE_MODE:
        if 'invitation_key' in request.REQUEST:
            if is_key_valid(request.REQUEST['invitation_key']):
                invitation_key = request.REQUEST['invitation_key']
                extra_context.update({'invitation_key': invitation_key})
                return registration_register(request, success_url, form_class, 
                            profile_callback, template_name, extra_context)
            else:
                extra_context.update({'invalid_key': True})
        else:
            extra_context.update({'no_key': True})
        template_name = 'invitation/wrong_invitation_key.html'
        return direct_to_template(request, template_name, extra_context)
    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', 
            extra_context=None):
    if extra_context is None:
        extra_context = {}
    remaining_invitations = remaining_invitations_for_user(request.user)
    if request.method == 'POST':
        form = form_class(data=request.POST, files=request.FILES)
        if remaining_invitations > 0 and 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()
    extra_context.update({
            'form': form,
            'remaining_invitations': remaining_invitations,
        })
    return direct_to_template(request, template_name, extra_context)
invite = login_required(invite)<commit_msg>Yet another bug spoted by Zebuline, thanks<commit_after>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
remaining_invitations_for_user = InvitationKey.objects.remaining_invitations_for_user

# TODO: move the authorization control to a dedicated decorator

def invited(request, invitation_key=None, extra_context=None):
    if 'INVITE_MODE' in settings.get_all_members() and settings.INVITE_MODE:
        if invitation_key and is_key_valid(invitation_key):
            template_name = 'invitation/invited.html'
        else:
            template_name = 'invitation/wrong_invitation_key.html'
        extra_context = extra_context is not None and extra_context.copy() or {}
        extra_context.update({'invitation_key': invitation_key})
        return direct_to_template(request, template_name, extra_context)
    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):
    extra_context = extra_context is not None and extra_context.copy() or {}
    if 'INVITE_MODE' in settings.get_all_members() and settings.INVITE_MODE:
        if 'invitation_key' in request.REQUEST:
            if is_key_valid(request.REQUEST['invitation_key']):
                invitation_key = request.REQUEST['invitation_key']
                extra_context.update({'invitation_key': invitation_key})
                return registration_register(request, success_url, form_class, 
                            profile_callback, template_name, extra_context)
            else:
                extra_context.update({'invalid_key': True})
        else:
            extra_context.update({'no_key': True})
        template_name = 'invitation/wrong_invitation_key.html'
        return direct_to_template(request, template_name, extra_context)
    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', 
            extra_context=None):
    extra_context = extra_context is not None and extra_context.copy() or {}
    remaining_invitations = remaining_invitations_for_user(request.user)
    if request.method == 'POST':
        form = form_class(data=request.POST, files=request.FILES)
        if remaining_invitations > 0 and 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()
    extra_context.update({
            'form': form,
            'remaining_invitations': remaining_invitations,
        })
    return direct_to_template(request, template_name, extra_context)
invite = login_required(invite)
{'fraction_non_alphanumeric': 0.07283702213279677, 'fraction_numerical': 0.0002682763246143528, 'mean_word_length': 3.39882005899705, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4252815', 'n_tokens_mistral': 1960, 'n_tokens_neox': 1929, 'n_words': 440}