Example #1
0
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))
Example #2
0
def choose_project(request, token=None):
    if request.user.is_authenticated():
        application = UserApplication()
        application.applicant = request.user.get_profile()
    else:
        application = get_object_or_404(UserApplication,
                                        secret_token=token,
                                        state__in=[Application.NEW, Application.OPEN],
                                        expires__gt=datetime.datetime.now())

    institute = application.applicant.institute
    term_error = leader_list = project_error = project = q_project = leader = None
    terms = ""
    project_list = False
    qs = request.META['QUERY_STRING']

    if request.method == 'POST':
        if 'project' in request.REQUEST:
            project = Project.objects.get(pk=request.POST['project'])
            if request.user.is_authenticated():
                if request.user.get_profile() in project.users.all():
                    messages.info(request, "You are already a member of the project %s" % project.pid)
                    return HttpResponseRedirect(reverse('kg_user_profile'))
            application.project = project
            application.state = Application.WAITING_FOR_LEADER
            application.submitted_date = datetime.datetime.now()
            application.needs_account = True
            application.save()
            send_account_request_email(application)

            return HttpResponseRedirect(reverse('kg_application_done', args=[application.secret_token]))
        else:
            return HttpResponseRedirect('%s?%s&error=true' % (reverse('user_choose_project'), qs))

    if 'error' in request.REQUEST:
        project_error = True
    
    if 'leader_q' in request.REQUEST:
        q_project = False
        try:
            q_project = Project.active.get(pid__icontains=request.GET['leader_q'])
        except:
            pass
        leader_list = Person.active.filter(institute=institute, leaders__is_active=True).distinct()
        terms = request.GET['leader_q'].lower()
        if len(terms) >= 3:
            query = Q()
            for term in terms.split(' '):
                q = Q(user__username__icontains=term) | Q(user__first_name__icontains=term) | Q(user__last_name__icontains=term)
                query = query & q
            leader_list = leader_list.filter(query)
            if leader_list.count() == 1:
                leader = leader_list[0]
                project_list = leader.leaders.filter(is_active=True)
                leader_list = False
            elif leader_list.count() == 0 and not q_project:
                term_error = "No projects found."
        else:
            term_error = "Please enter at lease three characters for search."
            leader_list = False
    if 'leader' in request.REQUEST:
        leader = Person.objects.get(pk=request.GET['leader'])
        project_list = leader.leaders.filter(is_active=True)

    if project_list:
        if project_list.count() == 1:
            project = project_list[0]
            project_list = False
                                   
    return render_to_response(
        'applications/choose_project.html',
        {'term_error': term_error, 'terms': terms,
         'leader_list': leader_list, 'project_error': project_error,
         'project_list': project_list, 'project': project, 'q_project': q_project,
         'leader': leader, 'application': application},
        context_instance=RequestContext(request))