Ejemplo n.º 1
0
def course_request_create(request, slug):
    """
    File a :model:`tiger.CourseRequest` for a given :model:`tiger.Course`
    """

    initial_form_data = {}

    if not request.user.is_authenticated():
        request.flash["messages"] = [
            ("S Please login or create an account to request this course")
        ]
        return HttpResponseRedirect('/login/?next=%s' % request.path)

    course = lookup_object(Course, None, slug, 'slug')
    initial_form_data['course'] = course.id

    # Lookup profile or redirect to create_profile
    try:
        person = request.user.get_profile()
        initial_form_data['person'] = person.id
    except:
        request.flash["messages"] = [(
            "S First, we'll need some contact information to contact you about your request"
        )]
        return create_profile(request,
                              success_url=reverse('course_request_create',
                                                  args=[course.slug]))

    model, form_class = CourseRequest, CourseRequestForm

    if request.method == 'POST':
        form = form_class(request.POST,
                          request.FILES,
                          initial=initial_form_data,
                          min_students=course.min_required_students)

        if form.is_valid():
            new_course_request = form.save(commit=False)
            new_course_request.person = person
            new_course_request.course = course
            new_course_request.save()

            request.user.message_set.create(
                message=_("S The %(verbose_name)s was created successfully.") %
                {"verbose_name": model._meta.verbose_name})
            return redirect_to(
                request,
                reverse('course_request_detail', args=[new_course_request.id]))
    else:
        form = form_class(initial=initial_form_data)

    # Create the template, context, response
    template_name = "%s/%s_form.html" % (model._meta.app_label,
                                         model._meta.object_name.lower())
    t = loader.get_template(template_name)
    c = RequestContext(request, {'form': form, 'object': course})
    return HttpResponse(t.render(c))
Ejemplo n.º 2
0
def course_request_list(request):
    """
    Select and show all :model:`tiger.CourseRequest` objects
    """
    try:
        person = request.user.get_profile()
    except:
        request.flash["messages"] = [
            _("S Please update your contact information.")
        ]
        return create_profile(request,
                              success_url=reverse('course_request_list'))

    return object_list(
        request, CourseRequest.objects.filter(status__gt=0, person=person))
Ejemplo n.º 3
0
def register(request, backend, success_url=None, form_class=myRegistrationForm,
             disallowed_url='registration_disallowed',
             template_name='registration/registration_form.html',
             extra_context=None):
    """
    Allow a new user to register an account.

    The actual registration of the account will be delegated to the
    backend specified by the ``backend`` keyword argument (see below);
    it will be used as follows:

    1. The backend's ``registration_allowed()`` method will be called,
       passing the ``HttpRequest``, to determine whether registration
       of an account is to be allowed; if not, a redirect is issued to
       the view corresponding to the named URL pattern
       ``registration_disallowed``. To override this, see the list of
       optional arguments for this view (below).

    2. The form to use for account registration will be obtained by
       calling the backend's ``get_form_class()`` method, passing the
       ``HttpRequest``. To override this, see the list of optional
       arguments for this view (below).

    3. If valid, the form's ``cleaned_data`` will be passed (as
       keyword arguments, and along with the ``HttpRequest``) to the
       backend's ``register()`` method, which should return the new
       ``User`` object.

    4. Upon successful registration, the backend's
       ``post_registration_redirect()`` method will be called, passing
       the ``HttpRequest`` and the new ``User``, to determine the URL
       to redirect the user to. To override this, see the list of
       optional arguments for this view (below).
    
    **Required arguments**
    
    None.
    
    **Optional arguments**

    ``backend``
        The dotted Python import path to the backend class to use.

    ``disallowed_url``
        URL to redirect to if registration is not permitted for the
        current ``HttpRequest``. Must be a value which can legally be
        passed to ``django.shortcuts.redirect``. If not supplied, this
        will be whatever URL corresponds to the named URL pattern
        ``registration_disallowed``.
    
    ``form_class``
        The form class to use for registration. If not supplied, this
        will be retrieved from the registration backend.
    
    ``extra_context``
        A dictionary of variables to add to the template context. Any
        callable object in this dictionary will be called to produce
        the end result which appears in the context.

    ``success_url``
        URL to redirect to after successful registration. Must be a
        value which can legally be passed to
        ``django.shortcuts.redirect``. If not supplied, this will be
        retrieved from the registration backend.
    
    ``template_name``
        A custom template to use. If not supplied, this will default
        to ``registration/registration_form.html``.
    
    **Context:**
    
    ``form``
        The registration form.
    
    Any extra variables supplied in the ``extra_context`` argument
    (see above).
    
    **Template:**
    
    registration/registration_form.html or ``template_name`` keyword
    argument.
    
    """
    backend = get_backend(backend)
    if not backend.registration_allowed(request):
        return redirect(disallowed_url)
    if form_class is None:
        form_class = backend.get_form_class(request)

    if request.method == 'POST':
        form = form_class(data=request.POST, files=request.FILES)
        if form.is_valid():
            new_user = backend.register(request, **form.cleaned_data)
            new_user.first_name = request.POST['first_name']
            new_user.last_name = request.POST['last_name']
            new_user.save()
            request.user = new_user
            new_profile = create_profile(request)
            if success_url is None:
                to, args, kwargs = backend.post_registration_redirect(request, new_user)
                return redirect(to, *args, **kwargs)
            else:
                return redirect(success_url)
    else:
        form = form_class()
    
    if extra_context is None:
        extra_context = {}
    context = RequestContext(request)
    for key, value in extra_context.items():
        context[key] = callable(value) and value() or value

    return render_to_response(template_name,
                              {'form': form},
                              context_instance=context)
Ejemplo n.º 4
0
def create_profile(request, form_class=ProfileForm, **kwargs):
    return prof_views.create_profile(request, form_class=form_class, **kwargs)