Example #1
0
def page(request, path):
    hierarchy = request.get_host()
    section = get_section_from_path(path, hierarchy=hierarchy)

    root = section.hierarchy.get_root()
    module = get_module(section)
    if not request.stand.can_view(request.user):
        return HttpResponse("you do not have permission")
    can_edit = request.stand.can_edit(request.user)
    can_admin = request.stand.can_admin(request.user)
    if section.id == root.id:
        # trying to visit the root page
        if section.get_next():
            # just send them to the first child
            return HttpResponseRedirect(section.get_next().get_absolute_url())
        else:
            # send them to the stand admin interface
            return HttpResponseRedirect("/_stand/")

    if request.method == "POST":
        # user has submitted a form. deal with it
        if request.POST.get("action", "") == "reset":
            section.reset(request.user)
            return HttpResponseRedirect(section.get_absolute_url())
        proceed = section.submit(request.POST, request.user)
        if proceed:
            return HttpResponseRedirect(section.get_next().get_absolute_url())
        else:
            # giving them feedback before they proceed
            return HttpResponseRedirect(section.get_absolute_url())
    else:
        instructor_link = has_responses(section)
        return dict(
            section=section,
            module=module,
            needs_submit=needs_submit(section),
            is_submitted=submitted(section, request.user),
            stand=request.stand,
            modules=root.get_children(),
            root=section.hierarchy.get_root(),
            can_edit=can_edit,
            can_admin=can_admin,
            instructor_link=instructor_link,
        )
Example #2
0
def page(request, path):
    template_name = "main/page.html"
    section = get_section_from_path(path)
    # redirects to first (welcome) page for parent nodes
    section = section.get_first_leaf()
    h = get_hierarchy()

    profile = get_profile(request, path)

    if request.method == "POST":
        return page_post(request, section)
    else:
        return render(
            request,
            template_name,
            dict(
                section=section,
                needs_submit=needs_submit(section),
                module=get_module(section),
                profile=profile,
                is_submitted=submitted(section, request.user),
                root=h.get_root(),
            ),
        )
Example #3
0
def page(request, path):
    section = get_section_from_path(path)
    section = section.get_first_leaf()  # redirects to first (welcome) page for parent nodes
    h = get_hierarchy()

    if not request.user.is_anonymous():
        try:
            profile = request.user.get_profile()
            profile.current_location = path
            profile.save()
        except UserProfile.DoesNotExist:
            pass
    profile = None

    if not request.user.is_anonymous():
        try:
            profile = request.user.get_profile()
        except UserProfile.DoesNotExist:
            pass

    if request.method == "POST":
        # user has submitted a form. deal with it
        if request.POST.get("action", "") == "reset":
            # it's a reset request
            for p in section.pageblock_set.all():
                if hasattr(p.block(), "needs_submit"):
                    if p.block().needs_submit():
                        p.block().clear_user_submissions(request.user)
            return HttpResponseRedirect(section.get_absolute_url())
        proceed = True
        for p in section.pageblock_set.all():
            if hasattr(p.block(), "needs_submit"):
                if p.block().needs_submit():
                    prefix = "pageblock-%d-" % p.id
                    data = dict()
                    for k in request.POST.keys():
                        if k.startswith(prefix):
                            # handle lists for multi-selects
                            v = request.POST.getlist(k)
                            if len(v) == 1:
                                data[k[len(prefix) :]] = request.POST[k]
                            else:
                                data[k[len(prefix) :]] = v
                    p.block().submit(request.user, data)
                    if hasattr(p.block(), "redirect_to_self_on_submit"):
                        # semi bug here?
                        # proceed will only be set by the last submittable
                        # block on the page. previous ones get ignored.
                        proceed = not p.block().redirect_to_self_on_submit()
        if proceed:
            return HttpResponseRedirect(section.get_next().get_absolute_url())
        else:
            # giving them feedback before they proceed
            return HttpResponseRedirect(section.get_absolute_url())
    else:
        return dict(
            section=section,
            needs_submit=needs_submit(section),
            module=get_module(section),
            profile=profile,
            is_submitted=submitted(section, request.user),
            root=h.get_root(),
        )