コード例 #1
0
ファイル: user.py プロジェクト: generica/karaage
def do_userapplication(request, token=None, saml=False,
                       application_form=UserApplicationForm):
    if request.user.is_authenticated():
        messages.warning(request, "You are already logged in")
        return HttpResponseRedirect(reverse('kg_user_profile'))

    if saml:
        response = ensure_shib_session(request)
        if response:
            return response

    if token:
        try:
            application = UserApplication.objects.get(
                                        secret_token=token,
                                        state__in=[Application.NEW, Application.OPEN],
                                        expires__gt=datetime.datetime.now())
        except UserApplication.DoesNotExist:
            return render_to_response('applications/old_userapplication.html',
                                        {'help_email': settings.ACCOUNTS_EMAIL},
                                        context_instance=RequestContext(request))
        applicant = application.applicant
        application.state = Application.OPEN
        application.save()
        captcha = False
    else:
        if not settings.ALLOW_REGISTRATIONS:
            return render_to_response('applications/registrations_disabled.html', {}, context_instance=RequestContext(request))
        application = UserApplication()
        applicant = None
        captcha = True
    if saml:
        captcha = False
        saml_user = get_saml_user(request)
    else:
        saml_user = None

    if application.content_type and application.content_type.model == 'person':
        return existing_user_application(request, token)
    init_institute = request.GET.get('institute', '')

    if request.method == 'POST':
        form = application_form(request.POST, instance=application, captcha=captcha)
        if saml:
            applicant_form = SAMLApplicantForm(request.POST, instance=applicant)
        else:
            applicant_form = UserApplicantForm(request.POST, instance=applicant)
        if form.is_valid() and applicant_form.is_valid():
            applicant = applicant_form.save(commit=False)
            if saml:
                applicant = add_saml_data(applicant, request)
                applicant.email_verified = True
            applicant.save()
            application = form.save(commit=False)
            application.applicant = applicant
            application.save()
            if not application.project:
                application.state = Application.OPEN
                application.save()
                return HttpResponseRedirect(reverse('kg_application_choose_project', args=[application.secret_token]))
            application.submitted_date = datetime.datetime.now()
            application.state = Application.WAITING_FOR_LEADER
            application.save()
            send_account_request_email(application)
            return HttpResponseRedirect(reverse('kg_application_done', args=[application.secret_token]))
    else:
        form = application_form(instance=application, captcha=captcha)
        if saml:
            applicant_form = SAMLApplicantForm(instance=applicant)
        else:
            applicant_form = UserApplicantForm(instance=applicant, initial={'institute': init_institute})
    return render_to_response('applications/userapplication_form.html',
                              {'form': form, 'applicant_form': applicant_form, 'application': application,
                               'saml': saml, 'saml_user': saml_user, },
                              context_instance=RequestContext(request))
コード例 #2
0
ファイル: projects.py プロジェクト: generica/karaage
def do_projectapplication(request, token=None, application_form=ProjectApplicationForm,
                          mc=MachineCategory.objects.get_default(), saml=False):

    if request.user.is_authenticated():
        messages.warning(request, "You are already logged in")
        return HttpResponseRedirect(reverse('kg_user_profile'))

    if saml:
        response = ensure_shib_session(request)
        if response:
            return response

    if token:
        application = get_object_or_404(ProjectApplication, secret_token=token)
        if application.state not in (Application.NEW, Application.OPEN):
            raise Http404
        applicant = application.applicant
        application.state = Application.OPEN
        application.save()
        captcha = False
    else:
        if not settings.ALLOW_REGISTRATIONS:
            return render_to_response('applications/registrations_disabled.html', {}, context_instance=RequestContext(request))
        application = None
        applicant = None
        captcha = True

    if saml:
        captcha = False
        saml_user = get_saml_user(request)
    else:
        saml_user = None
    init_institute = request.GET.get('institute', '')
    if request.method == 'POST':
        form = application_form(request.POST, instance=application, captcha=captcha)
        if saml:
            applicant_form = SAMLApplicantForm(request.POST, instance=applicant)
        else:
            applicant_form = UserApplicantForm(request.POST, instance=applicant)

        if form.is_valid() and applicant_form.is_valid():
            applicant = applicant_form.save(commit=False)
            if saml:
                applicant = add_saml_data(applicant, request)
                applicant.email_verified = True
            applicant.save()
            application = form.save(commit=False)
            application.applicant = applicant
            application.submitted_date = datetime.datetime.now()
            application.state = Application.WAITING_FOR_DELEGATE
            application.save()
            application.machine_categories.add(mc)
            send_project_request_email(application)
            return HttpResponseRedirect(reverse('kg_application_done', args=[application.secret_token]))
    else:
        form = application_form(instance=application, captcha=captcha, initial={'institute': init_institute})
        if saml:
            applicant_form = SAMLApplicantForm(instance=applicant)
        else:
            applicant_form = UserApplicantForm(instance=applicant, initial={'institute': init_institute})

    return render_to_response(
        'applications/projectapplication_form.html',
        {'form': form, 'applicant_form': applicant_form, 'application': application, 'saml': saml, 'saml_user': saml_user},
        context_instance=RequestContext(request))