Show user his/her requests, sorted by request_type.

def my_requests_view(request):
    '''
    Show user his/her requests, sorted by request_type.
    '''
    page_name = "Your Requests"
    userProfile = UserProfile.objects.get(user=request.user)
    my_requests = Request.objects.filter(owner=userProfile)
    # A pseudo dictionary, actually a list with items of form
    # (request_type.name.title(), request_form, type_manager, [(request,
    # [list_of_request_responses], response_form, upvote, vote_form),...],
    # relevant_managers)
    request_dict = list()
    for request_type in RequestType.objects.all():
        relevant_managers = request_type.managers.filter(active=True)
        type_manager = any(i.incumbent == userProfile for i in
                           relevant_managers)
        # Items are of form (request, [list_of_request_responses],
        # response_form),...])
        requests_list = list()
        type_requests = my_requests.filter(request_type=request_type)
        for req in type_requests:
            responses_list = Response.objects.filter(request=req)
            if type_manager:
                response_form = ManagerResponseForm(
                    request.POST if "add_response-{0}".format(req.pk) in request.POST else None,
                    initial={'action': Response.NONE},
                    profile=userProfile,
                    request=req,
                    prefix="response-{0}".format(req.pk),
                    )
            else:
                response_form = ResponseForm(
                    request.POST if "add_response-{0}".format(req.pk) in request.POST else None,
                    profile=userProfile,
                    request=req,
                    prefix="response-{0}".format(req.pk),
                    )

            upvote = userProfile in req.upvotes.all()
            vote_form = VoteForm(
                request.POST if "vote-{0}".format(req.pk) in request.POST else None,
                request.POST or None,
                profile=userProfile,
                request=req,
                prefix="vote-{0}",
                )

            if response_form.is_valid():
                response_form.save()
                return HttpResponseRedirect(reverse('managers:my_requests'))
            if vote_form.is_valid():
                vote_form.save()
                return HttpResponseRedirect(reverse('managers:my_requests'))

            requests_list.append((req, responses_list, response_form, upvote, vote_form))
        request_form = RequestForm(
            request.POST if "submit_request" in request.POST else None,
            profile=userProfile,
            request_type=request_type,
            prefix="request-{0}".format(request_type.pk),
            )
        if request_form.is_valid():
            request_form.save()
            return HttpResponseRedirect(reverse('managers:my_requests'))
        request_dict.append((request_type, request_form, type_manager,
                             requests_list, relevant_managers))
    return render_to_response('my_requests.html', {
        'page_name': page_name,
        'request_dict': request_dict,
        }, context_instance=RequestContext(request))