def my_requests_view(request): ''' Show user his/her requests, sorted by request_type. ''' page_name = "My Requests" userProfile = UserProfile.objects.get(user=request.user) request_form = RequestForm( request.POST if 'submit_request' in request.POST else None, profile=userProfile, ) response_form = ManagerResponseForm( request.POST if 'add_response' in request.POST else None, profile=userProfile, ) if request_form.is_valid(): request_form.save() return HttpResponseRedirect(reverse('my_requests')) if response_form.is_valid(): response_form.save() return HttpResponseRedirect(reverse('my_requests')) my_requests = Request.objects.filter(owner=userProfile) request_dict = list() # 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) 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) requests_list = list() # Items are of form (request, [list_of_request_responses], response_form),...]) type_requests = my_requests.filter(request_type=request_type) for req in type_requests: responses_list = Response.objects.filter(request=req) if type_manager: form = ManagerResponseForm(initial={ 'request_pk': req.pk, 'mark_filled': req.filled, 'mark_closed': req.closed, }, profile=userProfile, ) else: form = ResponseForm( initial={'request_pk': req.pk}, profile=userProfile, ) upvote = userProfile in req.upvotes.all() vote_form = VoteForm( initial={'request_pk': req.pk}, profile=userProfile, ) requests_list.append((req, responses_list, form, upvote, vote_form)) request_form = RequestForm( initial={'type_pk': request_type.pk}, profile=userProfile, ) 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))
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))
def requests_view(request, requestType): ''' Generic request view. Parameters: request is the HTTP request requestType is URL name of a RequestType. e.g. "food", "maintenance", "network", "site" ''' userProfile = UserProfile.objects.get(user=request.user) request_type = get_object_or_404(RequestType, url_name=requestType) page_name = "{0} Requests".format(request_type.name.title()) if not request_type.enabled: message = "{0} requests have been disabled.".format(request_type.name.title()) return red_home(request, message) relevant_managers = request_type.managers.filter(active=True) manager = any(i.incumbent == userProfile for i in relevant_managers) request_form = RequestForm( request.POST if "submit_request" in request.POST else None, profile=userProfile, request_type=request_type, ) if request_form.is_valid(): request_form.save() return HttpResponseRedirect(reverse('managers:requests', kwargs={'requestType': requestType})) # number of requests loaded x = 0 # A pseudo-dictionary, actually a list with items of form (request, # [request_responses_list], response_form, upvote, vote_form) requests_dict = list() requests = Request.objects.filter(request_type=request_type) if not request_type.managers.filter(incumbent__user=request.user): requests = requests.exclude( ~Q(owner__user=request.user), private=True, ) for req in requests: request_responses = Response.objects.filter(request=req) if manager: response_form = ManagerResponseForm( request.POST if "add_response-{0}".format(req.pk) in request.POST else None, initial={'action': Response.NONE}, prefix="{0}".format(req.pk), profile=userProfile, request=req, ) else: response_form = ResponseForm( request.POST if "add_response-{0}".format(req.pk) in request.POST else None, prefix="{0}".format(req.pk), profile=userProfile, request=req, ) upvote = userProfile in req.upvotes.all() vote_form = VoteForm( request.POST if "vote-{0}".format(req.pk) in request.POST else None, profile=userProfile, request=req, ) if response_form.is_valid(): response_form.save() return HttpResponseRedirect(reverse('managers:requests', kwargs={'requestType': requestType})) if vote_form.is_valid(): vote_form.save() return HttpResponseRedirect(reverse('managers:requests', kwargs={'requestType': requestType})) requests_dict.append((req, request_responses, response_form, upvote, vote_form)) x += 1 if x >= settings.MAX_REQUESTS: break return render_to_response('requests.html', { 'manager': manager, 'request_type': request_type, 'page_name': page_name, 'request_form': request_form, 'requests_dict': requests_dict, 'relevant_managers': relevant_managers, }, context_instance=RequestContext(request))
def requests_view(request, requestType): ''' Generic request view. Parameters: request is the HTTP request requestType is URL name of a RequestType. e.g. "food", "maintenance", "network", "site" ''' userProfile = UserProfile.objects.get(user=request.user) request_type = get_object_or_404(RequestType, url_name=requestType) page_name = "%s Requests" % request_type.name.title() if not request_type.enabled: message = "%s requests have been disabled." % request_type.name.title() return red_home(request, message) relevant_managers = request_type.managers.filter(active=True) manager = any(i.incumbent == userProfile for i in relevant_managers) request_form = RequestForm( request.POST if 'submit_request' in request.POST else None, profile=userProfile, request_type=request_type, ) if manager: form_class = ManagerResponseForm else: form_class = ResponseForm response_form = form_class( request.POST if 'add_response' in request.POST else None, profile=userProfile, ) vote_form = VoteForm( request.POST if 'upvote' in request.POST else None, profile=userProfile, ) if request_form.is_valid(): request_form.save() return HttpResponseRedirect(reverse('requests', kwargs={'requestType': requestType})) if response_form.is_valid(): response_form.save() return HttpResponseRedirect(reverse('requests', kwargs={'requestType': requestType})) if vote_form.is_valid(): vote_form.save() return HttpResponseRedirect(reverse('requests', kwargs={'requestType': requestType})) x = 0 # number of requests loaded requests_dict = list() # A pseudo-dictionary, actually a list with items of form (request, [request_responses_list], response_form, upvote, vote_form) for req in Request.objects.filter(request_type=request_type): request_responses = Response.objects.filter(request=req) if manager: resp_form = ManagerResponseForm( initial={ 'request_pk': req.pk, 'mark_filled': req.filled, 'mark_closed': req.closed, }, profile=userProfile, ) else: resp_form = ResponseForm( initial={'request_pk': req.pk}, profile=userProfile, ) upvote = userProfile in req.upvotes.all() vote_form = VoteForm( initial={'request_pk': req.pk}, profile=userProfile, ) requests_dict.append((req, request_responses, resp_form, upvote, vote_form)) x += 1 if x >= settings.MAX_REQUESTS: break return render_to_response('requests.html', { 'manager': manager, 'request_type': request_type.name.title(), 'page_name': page_name, 'request_form': request_form, 'requests_dict': requests_dict, 'relevant_managers': relevant_managers, }, context_instance=RequestContext(request))