Skip to content

Commit

Permalink
ubccr#294: Allocation detail page now has a mechanism for admins to e…
Browse files Browse the repository at this point in the history
…dit allocation change requests
  • Loading branch information
brisco17 committed Dec 20, 2021
1 parent 08a4470 commit 3754bac
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 79 deletions.
1 change: 1 addition & 0 deletions coldfront/core/allocation/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ class AllocationChangeForm(forms.Form):
justification = forms.CharField(
label='Justification for Changes',
widget=forms.Textarea,
required=False,
help_text='Justification for requesting this allocation change request.')

def __init__(self, *args, **kwargs):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ <h4 class="alert-heading">{{allocation_change.status.name}}</h4>
</div>
{% endif %}


<form method="post">
<div class="card mb-3">
<div class="card-header">
<h3 class="d-inline"><i class="fas fa-list" aria-hidden="true"></i> Allocation Information</h3>
Expand Down Expand Up @@ -133,16 +133,23 @@ <h3 class="d-inline"><i class="fas fa-info-circle" aria-hidden="true"></i> Alloc
</tr>
</thead>
<tbody>
{% for attribute in attribute_changes %}
{% for form in formset %}
<tr>
<td>{{attribute.allocation_attribute}}</td>
<td>{{form.name.value}}</td>
{% if allocation_change.status.name == 'Pending' %}
<td>{{attribute.allocation_attribute.value}}</td>
{% endif %}
{% if attribute.new_value == '' %}
<td>None</td>
<td>{{form.value.value}}</td>
<td>
{{form.new_value}}
<a href="" class="float-right">
<i class="far fa-trash-alt fa-lg"></i>
</a>
</td>
{% else %}
<td>{{attribute.new_value}}</td>
{% if form.new_value.value == '' %}
<td>None</td>
{% else %}
<td>{{form.new_value.value}}</td>
{% endif %}
{% endif %}
</tr>
{% endfor %}
Expand All @@ -155,6 +162,7 @@ <h3 class="d-inline"><i class="fas fa-info-circle" aria-hidden="true"></i> Alloc
There are no requested allocation attribute changes to display.
</div>
{% endif %}
{{ formset.management_form }}
</div>
</div>

Expand All @@ -169,7 +177,7 @@ <h3 class="d-inline"><i class="fas fa-info-circle" aria-hidden="true"></i> Actio
</div>
<div class="card-body">

<form method="post">

{% csrf_token %}
{{note_form.notes | as_crispy_field}}
<div style="float: right;">
Expand All @@ -181,11 +189,12 @@ <h3 class="d-inline"><i class="fas fa-info-circle" aria-hidden="true"></i> Actio
<i class="fas fa-sync" aria-hidden="true"></i> Update
</button>
</div>
</form>
</div>
</div>
</div>
{% endif %}
</form>


<a class="btn btn-secondary" href="{% url 'allocation-detail' allocation_change.allocation.pk %}" role="button">
View Allocation
Expand Down
214 changes: 145 additions & 69 deletions coldfront/core/allocation/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1614,6 +1614,7 @@ def get_queryset(self):


class AllocationChangeDetailView(LoginRequiredMixin, UserPassesTestMixin, FormView):
formset_class = AllocationAttributeChangeForm
template_name = 'allocation/allocation_change_detail.html'

def test_func(self):
Expand Down Expand Up @@ -1644,16 +1645,42 @@ def test_func(self):

return False


def get_allocation_attributes_to_change(self, allocation_change_obj):
attributes_to_change = allocation_change_obj.allocationattributechangerequest_set.all()

attributes_to_change = [

{'pk': attribute_change.pk,
'name': attribute_change.allocation_attribute.allocation_attribute_type.name,
'value': attribute_change.allocation_attribute.value,
'new_value': attribute_change.new_value,
}

for attribute_change in attributes_to_change
]

return attributes_to_change

def get_context_data(self, **kwargs):
context = {}

allocation_change_obj = get_object_or_404(
AllocationChangeRequest, pk=self.kwargs.get('pk'))

attribute_changes = allocation_change_obj.allocationattributechangerequest_set.all()


allocation_attributes_to_change = self.get_allocation_attributes_to_change(
allocation_change_obj)

if allocation_attributes_to_change:
formset = formset_factory(self.formset_class, max_num=len(
allocation_attributes_to_change))
formset = formset(
initial=allocation_attributes_to_change, prefix='attributeform')
context['formset'] = formset

context['allocation_change'] = allocation_change_obj
context['attribute_changes'] = attribute_changes
context['attribute_changes'] = allocation_attributes_to_change

return context

Expand All @@ -1666,7 +1693,8 @@ def get(self, request, *args, **kwargs):
initial={'justification': allocation_change_obj.justification,
'end_date_extension': allocation_change_obj.end_date_extension})
allocation_change_form.fields['justification'].disabled = True
allocation_change_form.fields['end_date_extension'].disabled = True
if allocation_change_obj.status.name != 'Pending':
allocation_change_form.fields['end_date_extension'].disabled = True

note_form = AllocationChangeNoteForm(
initial={'notes': allocation_change_obj.notes})
Expand All @@ -1678,6 +1706,7 @@ def get(self, request, *args, **kwargs):
return render(request, self.template_name, context)

def post(self, request, *args, **kwargs):

pk = self.kwargs.get('pk')
allocation_change_obj = get_object_or_404(
AllocationChangeRequest, pk=pk)
Expand All @@ -1686,82 +1715,105 @@ def post(self, request, *args, **kwargs):
request, 'You do not have permission to update the allocation change request')
return HttpResponseRedirect(reverse('allocation-change-detail', kwargs={'pk': pk}))

initial_data = {
'notes': allocation_change_obj.notes,
}
note_form = AllocationChangeNoteForm(request.POST, initial=initial_data)
allocation_change_form = AllocationChangeForm(request.POST,
initial={'justification': allocation_change_obj.justification,
'end_date_extension': allocation_change_obj.end_date_extension})

if note_form.is_valid():
if request.POST.get('choice') == 'approve':
form_data = note_form.cleaned_data
notes = form_data.get('notes')
allocation_attributes_to_change = self.get_allocation_attributes_to_change(
allocation_change_obj)

print('length of allocation att list: ' + str(len(allocation_attributes_to_change)))

allocation_change_obj.notes = notes
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')

allocation_change_status_active_obj = AllocationChangeStatusChoice.objects.get(
name='Approved')

allocation_change_obj.status = allocation_change_status_active_obj
note_form = AllocationChangeNoteForm(
request.POST, initial={'notes': allocation_change_obj.notes})

if allocation_change_obj.end_date_extension != 0:
new_end_date = allocation_change_obj.allocation.end_date + relativedelta(
days=allocation_change_obj.end_date_extension)
if note_form.is_valid():
notes = note_form.cleaned_data.get('notes')

allocation_change_obj.allocation.end_date = new_end_date
if request.POST.get('choice') == 'approve':
if allocation_change_form.is_valid() and formset.is_valid():
allocation_change_obj.notes = notes

allocation_change_obj.allocation.save()
allocation_change_obj.save()
allocation_change_status_active_obj = AllocationChangeStatusChoice.objects.get(
name='Approved')

attribute_change_list = allocation_change_obj.allocationattributechangerequest_set.all()
allocation_change_obj.status = allocation_change_status_active_obj

for attribute_change in attribute_change_list:
attribute_change.allocation_attribute.value = attribute_change.new_value
attribute_change.allocation_attribute.save()
form_data = allocation_change_form.cleaned_data

messages.success(request, 'Allocation change request to {} has been APPROVED for {} {} ({})'.format(
allocation_change_obj.allocation.get_parent_resource,
allocation_change_obj.allocation.project.pi.first_name,
allocation_change_obj.allocation.project.pi.last_name,
allocation_change_obj.allocation.project.pi.username)
)
if form_data.get('end_date_extension') != allocation_change_obj.end_date_extension:
allocation_change_obj.end_date_extension = form_data.get('end_date_extension')
new_end_date = allocation_change_obj.allocation.end_date + relativedelta(
days=form_data.get('end_date_extension'))

resource_name = allocation_change_obj.allocation.get_parent_resource
domain_url = get_domain_url(self.request)
allocation_url = '{}{}'.format(domain_url, reverse(
'allocation-detail', kwargs={'pk': allocation_change_obj.allocation.pk}))
allocation_change_obj.allocation.end_date = new_end_date
allocation_change_obj.allocation.save()

if EMAIL_ENABLED:
template_context = {
'center_name': EMAIL_CENTER_NAME,
'resource': resource_name,
'allocation_url': allocation_url,
'signature': EMAIL_SIGNATURE,
'opt_out_instruction_url': EMAIL_OPT_OUT_INSTRUCTION_URL
}
allocation_change_obj.save()

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

attribute_change = AllocationAttributeChangeRequest.objects.get(pk=formset_data.get('pk'))

if new_value != attribute_change.new_value:
attribute_change.new_value = new_value
attribute_change.save()

for allocation_user in allocation_change_obj.allocation.allocationuser_set.exclude(status__name__in=['Removed', 'Error']):
allocation_activate_user.send(
sender=self.__class__, allocation_user_pk=allocation_user.pk)
if allocation_user.allocation.project.projectuser_set.get(user=allocation_user.user).enable_notifications:
email_receiver_list.append(allocation_user.user.email)
attribute_change_list = allocation_change_obj.allocationattributechangerequest_set.all()

send_email_template(
'Allocation Change Approved',
'email/allocation_change_approved.txt',
template_context,
EMAIL_SENDER,
email_receiver_list
for attribute_change in attribute_change_list:
attribute_change.allocation_attribute.value = attribute_change.new_value
attribute_change.allocation_attribute.save()

messages.success(request, 'Allocation change request to {} has been APPROVED for {} {} ({})'.format(
allocation_change_obj.allocation.get_parent_resource,
allocation_change_obj.allocation.project.pi.first_name,
allocation_change_obj.allocation.project.pi.last_name,
allocation_change_obj.allocation.project.pi.username)
)

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

resource_name = allocation_change_obj.allocation.get_parent_resource
domain_url = get_domain_url(self.request)
allocation_url = '{}{}'.format(domain_url, reverse(
'allocation-detail', kwargs={'pk': allocation_change_obj.allocation.pk}))

if EMAIL_ENABLED:
template_context = {
'center_name': EMAIL_CENTER_NAME,
'resource': resource_name,
'allocation_url': allocation_url,
'signature': EMAIL_SIGNATURE,
'opt_out_instruction_url': EMAIL_OPT_OUT_INSTRUCTION_URL
}

email_receiver_list = []

for allocation_user in allocation_change_obj.allocation.allocationuser_set.exclude(status__name__in=['Removed', 'Error']):
allocation_activate_user.send(
sender=self.__class__, allocation_user_pk=allocation_user.pk)
if allocation_user.allocation.project.projectuser_set.get(user=allocation_user.user).enable_notifications:
email_receiver_list.append(allocation_user.user.email)

send_email_template(
'Allocation Change Approved',
'email/allocation_change_approved.txt',
template_context,
EMAIL_SENDER,
email_receiver_list
)

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

if request.POST.get('choice') == 'deny':
form_data = note_form.cleaned_data
notes = form_data.get('notes')

if request.POST.get('choice') == 'deny':
allocation_change_obj.notes = notes

allocation_change_status_denied_obj = AllocationChangeStatusChoice.objects.get(
Expand Down Expand Up @@ -1808,15 +1860,39 @@ def post(self, request, *args, **kwargs):
return HttpResponseRedirect(reverse('allocation-change-detail', kwargs={'pk': pk}))

if request.POST.get('choice') == 'update':
form_data = note_form.cleaned_data
notes = form_data.get('notes')
if allocation_change_form.is_valid() and formset.is_valid():
allocation_change_obj.notes = notes

allocation_change_obj.notes = notes
allocation_change_obj.save()
form_data = allocation_change_form.cleaned_data

if form_data.get('end_date_extension') != allocation_change_obj.end_date_extension:
allocation_change_obj.end_date_extension = form_data.get('end_date_extension')


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

attribute_change = AllocationAttributeChangeRequest.objects.get(pk=formset_data.get('pk'))

if new_value != attribute_change.new_value:
attribute_change.new_value = new_value
attribute_change.save()

allocation_change_obj.save()

messages.success(
request, 'Allocation change request updated!')
return HttpResponseRedirect(reverse('allocation-change-detail', kwargs={'pk': pk}))
else:
attribute_errors = ""
for error in allocation_change_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-detail', kwargs={'pk': pk}))

messages.success(
request, 'Allocation change request updated!')
return HttpResponseRedirect(reverse('allocation-change-detail', kwargs={'pk': pk}))
else:
allocation_change_form = AllocationChangeForm(
initial={'justification': allocation_change_obj.justification})
Expand Down

0 comments on commit 3754bac

Please sign in to comment.