def create_allocation(request, project_id, allocation_id=-1): mapper = ProjectAllocationMapper(request) user = mapper.get_user(request.user.username) if user["piEligibility"].lower() != "eligible": messages.error( request, "Only PI Eligible users can request allocations. If you would " "like to request PI Eligibility, please " '<a href="/user/profile/edit/">submit a PI Eligibility ' "request</a>.", ) return HttpResponseRedirect(reverse("projects:user_projects")) project = mapper.get_project(project_id) allocation = None allocation_id = int(allocation_id) if allocation_id > 0: for a in project.allocations: if a.id == allocation_id: allocation = a # goofiness that we should clean up later; requires data cleansing abstract = project.description if "--- Supplemental details ---" in abstract: additional = abstract.split("\n\n--- Supplemental details ---\n\n") abstract = additional[0] additional = additional[1].split("\n\n--- Funding source(s) ---\n\n") justification = additional[0] if len(additional) > 1: funding_source = additional[1] else: funding_source = "" elif allocation: justification = allocation.justification if "--- Funding source(s) ---" in justification: parts = justification.split("\n\n--- Funding source(s) ---\n\n") justification = parts[0] funding_source = parts[1] else: funding_source = "" else: justification = "" funding_source = "" if request.POST: form = AllocationCreateForm( request.POST, initial={ "description": abstract, "supplemental_details": justification, "funding_source": funding_source, }, ) if form.is_valid(): allocation = form.cleaned_data.copy() allocation["computeRequested"] = 20000 # Also update the project project.description = allocation.pop("description", None) supplemental_details = allocation.pop("supplemental_details", None) logger.error(supplemental_details) funding_source = allocation.pop("funding_source", None) # if supplemental_details == None: # raise forms.ValidationError("Justifcation is required") # This is required if not supplemental_details: supplemental_details = "(none)" logger.error(supplemental_details) if funding_source: allocation[ "justification"] = "%s\n\n--- Funding source(s) ---\n\n%s" % ( supplemental_details, funding_source, ) else: allocation["justification"] = supplemental_details allocation["projectId"] = project_id allocation["requestorId"] = mapper.get_portal_user_id( request.user.username) allocation["resourceId"] = "39" if allocation_id > 0: allocation["id"] = allocation_id try: logger.info( "Submitting allocation request for project %s: %s" % (project.id, allocation)) updated_project = mapper.save_project(project.as_dict()) mapper.save_allocation(allocation, project.chargeCode, request.get_host()) messages.success( request, "Your allocation request has been submitted!") return HttpResponseRedirect( reverse("projects:view_project", args=[updated_project["id"]])) except: logger.exception("Error creating allocation") form.add_error( "__all__", "An unexpected error occurred. Please try again") else: form.add_error( "__all__", "There were errors processing your request. " "Please see below for details.", ) else: form = AllocationCreateForm( initial={ "description": abstract, "supplemental_details": justification, "funding_source": funding_source, }) context = { "form": form, "project": project, "alloc_id": allocation_id, "alloc": allocation, } return render(request, "projects/create_allocation.html", context)
def create_allocation(request, project_id, allocation_id=-1): mapper = ProjectAllocationMapper(request) user = mapper.get_user(request.user.username) if user["piEligibility"].lower() != "eligible": messages.error( request, "Only PI Eligible users can request allocations. If you would " "like to request PI Eligibility, please " '<a href="/user/profile/edit/">submit a PI Eligibility ' "request</a>.", ) return HttpResponseRedirect(reverse("projects:user_projects")) project = mapper.get_project(project_id) allocation = None allocation_id = int(allocation_id) if allocation_id > 0: for a in project.allocations: if a.id == allocation_id: allocation = a abstract = project.description if allocation: justification = allocation.justification else: justification = "" funding_source = [ model_to_dict(f) for f in Funding.objects.filter(project__id=project_id, is_active=True) ] # add extra form funding_source.append({}) if request.POST: form = AllocationCreateForm( request.POST, initial={ "description": abstract, "justification": justification, }, ) formset = FundingFormset( request.POST, initial=funding_source, ) consent_form = ConsentForm(request.POST) if form.is_valid() and formset.is_valid() and consent_form.is_valid(): allocation = form.cleaned_data.copy() allocation["computeRequested"] = 20000 # Also update the project and fundings project.description = allocation.pop("description", None) justification = allocation.pop("justification", None) allocation["projectId"] = project_id allocation["requestorId"] = mapper.get_portal_user_id( request.user.username) allocation["resourceId"] = "39" allocation["justification"] = justification if allocation_id > 0: allocation["id"] = allocation_id try: logger.info( "Submitting allocation request for project %s: %s" % (project.id, allocation)) with transaction.atomic(): updated_project = mapper.save_project(project.as_dict()) mapper.save_allocation(allocation, project.chargeCode, request.get_host()) new_funding_source = _save_fundings(formset, project_id) _remove_fundings(funding_source, new_funding_source) messages.success( request, "Your allocation request has been submitted!") return HttpResponseRedirect( reverse("projects:view_project", args=[updated_project["id"]])) except: logger.exception("Error creating allocation") form.add_error( "__all__", "An unexpected error occurred. Please try again") else: form.add_error( "__all__", "There were errors processing your request. " "Please see below for details.", ) else: form = AllocationCreateForm(initial={ "description": abstract, "justification": justification, }) formset = FundingFormset(initial=funding_source) consent_form = ConsentForm() context = { "form": form, "funding_formset": formset, "consent_form": consent_form, "project": project, "alloc_id": allocation_id, "alloc": allocation, } return render(request, "projects/create_allocation.html", context)