Skip to content

Commit

Permalink
ubccr#294: Fixed allocation change form submission issue ('Management…
Browse files Browse the repository at this point in the history
…Form data is missing or has been tampered with'). Also balanced form tag in allocation_change.html to fix invalid HTML error
  • Loading branch information
brisco17 committed Nov 16, 2021
1 parent 44abfbf commit fc4cc37
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ <h2>Request change to {{ allocation.get_parent_resource }} for project: {{ alloc
you must provide a justification.
</p>

<form action="{% url 'allocation-change' allocation.pk %}" method="post">
<div class="card mb-3">
<div class="card-header">
<h3><i class="fas fa-list" aria-hidden="true"></i> Allocation Information</h3>
</div>

<div class="card-body">
<form action="{% url 'allocation-change' allocation.pk %}" method="post">
{% csrf_token %}
<div class="table-responsive">
<table class="table table-bordered table-sm">
Expand Down Expand Up @@ -130,7 +130,7 @@ <h3 class="d-inline"><i class="fas fa-info-circle" aria-hidden="true"></i> Alloc
<a class="btn btn-secondary" href="{% url 'allocation-detail' allocation.pk %}" role="button">Back to
Allocation</a><br>
</div>
</form>
</form>

<script>
</script>
Expand Down
122 changes: 77 additions & 45 deletions coldfront/core/allocation/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1827,65 +1827,97 @@ def post(self, request, *args, **kwargs):
allocation_attributes_to_change = self.get_allocation_attributes_to_change(
allocation_obj)

formset = formset_factory(self.formset_class, max_num=len(
allocation_attributes_to_change))
formset = formset(
request.POST, initial=allocation_attributes_to_change, prefix='attributeform')
if allocation_attributes_to_change:
formset = formset_factory(self.formset_class, max_num=len(
allocation_attributes_to_change))
formset = formset(
request.POST, initial=allocation_attributes_to_change, prefix='attributeform')

if form.is_valid() and formset.is_valid():
form_data = form.cleaned_data
if form.is_valid() and formset.is_valid():
form_data = form.cleaned_data

if form_data.get('end_date_extension') != 0: change_requested = True

if form_data.get('end_date_extension') != 0: change_requested = True
for entry in formset:
formset_data = entry.cleaned_data

for entry in formset:
formset_data = entry.cleaned_data
new_value = formset_data.get('new_value')

if new_value != "":
change_requested = True

new_value = formset_data.get('new_value')
allocation_attribute = AllocationAttribute.objects.get(pk=formset_data.get('pk'))
attribute_changes_to_make.add((allocation_attribute, new_value))

if change_requested == True:

if new_value != "":
change_requested = True
end_date_extension = form_data.get('end_date_extension')
justification = form_data.get('justification')

change_request_status_obj = AllocationChangeStatusChoice.objects.get(
name='Pending')

allocation_attribute = AllocationAttribute.objects.get(pk=formset_data.get('pk'))
attribute_changes_to_make.add((allocation_attribute, new_value))
allocation_change_request_obj = AllocationChangeRequest.objects.create(
allocation=allocation_obj,
end_date_extension=end_date_extension,
justification=justification,
status=change_request_status_obj
)

if change_requested == True:
for attribute in attribute_changes_to_make:
attribute_change_request_obj = AllocationAttributeChangeRequest.objects.create(
allocation_change_request=allocation_change_request_obj,
allocation_attribute=attribute[0],
new_value=attribute[1]
)
messages.success(
request, 'Allocation change request successfully submitted.')

return HttpResponseRedirect(reverse('allocation-detail', kwargs={'pk': pk}))

else:
messages.error(request, 'You must request a change.')
return HttpResponseRedirect(reverse('allocation-change', kwargs={'pk': pk}))

end_date_extension = form_data.get('end_date_extension')
justification = form_data.get('justification')
else:
attribute_errors = ""
for error in form.errors:
messages.error(request, error)
for error in formset.errors:
if error: attribute_errors += error.get('__all__')
messages.error(request, attribute_errors)
return HttpResponseRedirect(reverse('allocation-change', kwargs={'pk': pk}))
else:
if form.is_valid():
form_data = form.cleaned_data

change_request_status_obj = AllocationChangeStatusChoice.objects.get(
name='Pending')
if form_data.get('end_date_extension') != 0:

end_date_extension = form_data.get('end_date_extension')
justification = form_data.get('justification')

allocation_change_request_obj = AllocationChangeRequest.objects.create(
allocation=allocation_obj,
end_date_extension=end_date_extension,
justification=justification,
status=change_request_status_obj
)
change_request_status_obj = AllocationChangeStatusChoice.objects.get(
name='Pending')

for attribute in attribute_changes_to_make:
attribute_change_request_obj = AllocationAttributeChangeRequest.objects.create(
allocation_change_request=allocation_change_request_obj,
allocation_attribute=attribute[0],
new_value=attribute[1]
allocation_change_request_obj = AllocationChangeRequest.objects.create(
allocation=allocation_obj,
end_date_extension=end_date_extension,
justification=justification,
status=change_request_status_obj
)
messages.success(
request, 'Allocation change request successfully submitted.')

return HttpResponseRedirect(reverse('allocation-detail', kwargs={'pk': pk}))

messages.success(
request, 'Allocation change request successfully submitted.')

return HttpResponseRedirect(reverse('allocation-detail', kwargs={'pk': pk}))

else:
messages.error(request, 'You must request a change.')
return HttpResponseRedirect(reverse('allocation-change', kwargs={'pk': pk}))

else:
messages.error(request, 'You must request a change.')
for error in form.errors:
messages.error(request, error)
return HttpResponseRedirect(reverse('allocation-change', kwargs={'pk': pk}))

else:
attribute_errors = ""
for error in form.errors:
messages.error(request, error)
for error in formset.errors:
if error: attribute_errors += error.get('__all__')
messages.error(request, attribute_errors)
return HttpResponseRedirect(reverse('allocation-change', kwargs={'pk': pk}))


class AllocationChangeActivateView(LoginRequiredMixin, UserPassesTestMixin, View):
Expand Down

0 comments on commit fc4cc37

Please sign in to comment.