Skip to content

Add an Allocation Attribute Edit Page #675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions coldfront/core/allocation/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,24 @@ def clean(self):
allocation_attribute.clean()


class AllocationAttributeEditForm(forms.Form):
attribute_pk = forms.IntegerField(required=False, disabled=True)
name = forms.CharField(max_length=150, required=False, disabled=True)
orig_value = forms.CharField(max_length=150, required=False, disabled=True)
value = forms.CharField(max_length=150, required=False, disabled=False)

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["attribute_pk"].widget = forms.HiddenInput()

def clean(self):
cleaned_data = super().clean()
allocation_attribute = AllocationAttribute.objects.get(pk=cleaned_data.get("attribute_pk"))

allocation_attribute.value = cleaned_data.get("value")
allocation_attribute.clean()


class AllocationChangeForm(forms.Form):
EXTENSION_CHOICES = [(0, "No Extension")]
for choice in ALLOCATION_CHANGE_REQUEST_EXTENSION_DAYS:
Expand Down
3 changes: 3 additions & 0 deletions coldfront/core/allocation/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@

allocation_change_created = django.dispatch.Signal()
# providing_args=["allocation_pk", "allocation_change_pk"]

allocation_attribute_changed = django.dispatch.Signal()
# providing_args=["attribute_pk", "allocation_pk"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{% extends "common/base.html" %} {% load crispy_forms_tags %} {% load static %}
{% block title %} Allocation Change Detail {% endblock %} {% block content %}

<h2>
Edit attributes for {{ allocation.get_parent_resource }} for project: {{ allocation.project.title }}
</h2>
<hr />
<form method="post">
<div class="card mb-3">
<div class="card-header">
<h3 class="d-inline">
<i class="fas fa-info-circle" aria-hidden="true"></i> Allocation
Attributes
</h3>
</div>
<div class="card-body">
{% if attributes %} {% csrf_token %}
<div class="table-responsive">
<table class="table table-bordered table-sm">
<thead>
<tr class="d-flex">
<th class="col-6" scope="col">Attribute</th>
<th class="col-6" scope="col">Set New Value</th>
</tr>
</thead>
<tbody>
{% for form in formset %}
<tr class="d-flex">
<td class="col-6">{{form.name.value}}</td>
<td class="col-6">
{{form.value}}
<span
class="d-none"
id="change-indicator-{{form.attribute_pk.value}}"
>
<i class="fas fa-info-circle" aria-hidden="true"></i>
Value changed
</span>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% else %}
<div class="alert alert-info" role="alert">
<i class="fas fa-info-circle" aria-hidden="true"></i>
This allocation has no attributes.
</div>
{% endif %}

<input class="btn btn-success" type="submit" value="Confirm" />
<a
class="btn btn-secondary"
href="{% url 'allocation-detail' allocation.pk %}"
role="button"
>Cancel</a
>
{{ formset.management_form }}
</div>
</div>
</form>

<script>
$(document).ready(function() {
{% for form in formset %}
$("#{{form.value.auto_id}}").on("input", function(){
let change_indicator = $("#change-indicator-{{form.attribute_pk.value}}");
if ($(this).val() != "{{form.orig_value.value|escapejs}}") {
change_indicator.removeClass("d-none");
} else {
change_indicator.addClass("d-none");
}
});
$("#{{form.value.auto_id}}").trigger("input");
{% endfor %}
})
</script>
{% endblock %}
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ <h3 class="d-inline"><i class="fas fa-info-circle" aria-hidden="true"></i> EULA
<h3 class="d-inline"><i class="fas fa-info-circle" aria-hidden="true"></i> Allocation Attributes</h3>
<div class="float-right">
{% if request.user.is_superuser %}
<a class="btn btn-info" href="{% url 'allocation-attribute-edit' allocation.pk %}" role="button">
<i class="fas fa-edit" aria-hidden="true"></i> Edit Allocation Attributes
</a>
<a class="btn btn-success" href="{% url 'allocation-attribute-add' allocation.pk %}" role="button">
<i class="fas fa-plus" aria-hidden="true"></i> Add Allocation Attribute
</a>
Expand Down Expand Up @@ -299,7 +302,6 @@ <h3 class="d-inline"><i class="fas fa-info-circle" aria-hidden="true"></i> Alloc
{% else %}
<td class="text-info">{{ change_request.status.name }}</td>
{% endif %}
</td>
{% if change_request.notes %}
<td>{{change_request.notes}}</td>
{% else %}
Expand Down
45 changes: 45 additions & 0 deletions coldfront/core/allocation/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,48 @@ def test_allocationchangeview_post_no_change(self):
self.assertEqual(len(AllocationChangeRequest.objects.all()), 0)


class AllocationAttributeEditViewTest(AllocationViewBaseTest):
"""Tests for AllocationAttributeEditView"""

def setUp(self):
self.client.force_login(self.admin_user, backend=BACKEND)
self.url = f"/allocation/{self.allocation.pk}/allocationattribute/edit"
self.post_data = {
"attributeform-0-value": self.allocation.get_attribute("Storage Quota (TB)"),
"attributeform-INITIAL_FORMS": "1",
"attributeform-MAX_NUM_FORMS": "1",
"attributeform-MIN_NUM_FORMS": "0",
"attributeform-TOTAL_FORMS": "1",
}

def test_allocationattributeeditview_access(self):
"""Test get request"""
self.allocation_access_tstbase(self.url) # Admin/superuser can access
utils.test_user_cannot_access(self, self.pi_user, self.url) # Manager can access
utils.test_user_cannot_access(self, self.allocation_user, self.url) # user can't access

def test_allocationattributeeditview_post_change_attr(self):
"""Test post request to change attribute"""

self.assertEqual(self.allocation.get_attribute("Storage Quota (TB)"), 100)

self.post_data["attributeform-0-value"] = 200
response = self.client.post(self.url, data=self.post_data, follow=True)
self.assertEqual(response.status_code, 200)

self.assertEqual(self.allocation.get_attribute("Storage Quota (TB)"), 200)

def test_allocationattributeeditview_post_no_change(self):
"""Test post request with no change"""

self.assertEqual(self.allocation.get_attribute("Storage Quota (TB)"), 100)

response = self.client.post(self.url, data=self.post_data, follow=True)
self.assertEqual(response.status_code, 200)

self.assertEqual(self.allocation.get_attribute("Storage Quota (TB)"), 100)


class AllocationDetailViewTest(AllocationViewBaseTest):
"""Tests for AllocationDetailView"""

Expand All @@ -214,12 +256,15 @@ def test_allocationdetail_requestchange_button(self):
def test_allocationattribute_button_visibility(self):
"""Test visibility of "Add Attribute" button for different user types"""
# admin
utils.page_contains_for_user(self, self.admin_user, self.url, "Edit Allocation Attribute")
utils.page_contains_for_user(self, self.admin_user, self.url, "Add Allocation Attribute")
utils.page_contains_for_user(self, self.admin_user, self.url, "Delete Allocation Attribute")
# pi
utils.page_does_not_contain_for_user(self, self.pi_user, self.url, "Edit Allocation Attribute")
utils.page_does_not_contain_for_user(self, self.pi_user, self.url, "Add Allocation Attribute")
utils.page_does_not_contain_for_user(self, self.pi_user, self.url, "Delete Allocation Attribute")
# allocation user
utils.page_does_not_contain_for_user(self, self.allocation_user, self.url, "Edit Allocation Attribute")
utils.page_does_not_contain_for_user(self, self.allocation_user, self.url, "Add Allocation Attribute")
utils.page_does_not_contain_for_user(self, self.allocation_user, self.url, "Delete Allocation Attribute")

Expand Down
5 changes: 5 additions & 0 deletions coldfront/core/allocation/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
name="allocation-attribute-add",
),
path("<int:pk>/change-request", allocation_views.AllocationChangeView.as_view(), name="allocation-change"),
path(
"<int:pk>/allocationattribute/edit",
allocation_views.AllocationAttributeEditView.as_view(),
name="allocation-attribute-edit",
),
path(
"<int:pk>/allocationattribute/delete",
allocation_views.AllocationAttributeDeleteView.as_view(),
Expand Down
105 changes: 105 additions & 0 deletions coldfront/core/allocation/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
AllocationAttributeChangeForm,
AllocationAttributeCreateForm,
AllocationAttributeDeleteForm,
AllocationAttributeEditForm,
AllocationAttributeUpdateForm,
AllocationChangeForm,
AllocationChangeNoteForm,
Expand Down Expand Up @@ -58,6 +59,7 @@
from coldfront.core.allocation.signals import (
allocation_activate,
allocation_activate_user,
allocation_attribute_changed,
allocation_change_approved,
allocation_change_created,
allocation_disable,
Expand Down Expand Up @@ -1891,6 +1893,11 @@ def post(self, request, *args, **kwargs):
for attribute_change in attribute_change_list:
attribute_change.allocation_attribute.value = attribute_change.new_value
attribute_change.allocation_attribute.save()
allocation_attribute_changed.send(
sender=self.__class__,
attribute_pk=attribute_change.allocation_attribute.pk,
allocation_pk=attribute_change.allocation.pk,
)

messages.success(
request,
Expand Down Expand Up @@ -2118,6 +2125,104 @@ def post(self, request, *args, **kwargs):
return HttpResponseRedirect(reverse("allocation-detail", kwargs={"pk": pk}))


class AllocationAttributeEditView(LoginRequiredMixin, UserPassesTestMixin, FormView):
formset_class = AllocationAttributeEditForm
template_name = "allocation/allocation_attribute_edit.html"

def test_func(self):
"""UserPassesTestMixin Tests"""
user = self.request.user
if user.is_superuser or user.is_staff:
return True

messages.error(self.request, "You do not have permission to edit this allocation's attributes.")

return False

def get_allocation_attributes_to_change(self, allocation_obj):
attributes_to_change = allocation_obj.allocationattribute_set.all()

attributes_to_change = [
{
"attribute_pk": attribute.pk,
"name": attribute.allocation_attribute_type.name,
"orig_value": attribute.value,
"value": attribute.value,
}
for attribute in attributes_to_change
]

return attributes_to_change

def get(self, request, *args, **kwargs):
context = {}
allocation_obj = get_object_or_404(Allocation, pk=self.kwargs.get("pk"))
allocation_attributes_to_change = self.get_allocation_attributes_to_change(allocation_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"] = allocation_obj
context["attributes"] = allocation_attributes_to_change
return render(request, self.template_name, context)

def post(self, request, *args, **kwargs):
attribute_changes_to_make = set()

pk = self.kwargs.get("pk")
allocation_obj = get_object_or_404(Allocation, pk=pk)

allocation_attributes_to_change = self.get_allocation_attributes_to_change(allocation_obj)
error_redirect = HttpResponseRedirect(reverse("allocation-attribute-edit", kwargs={"pk": pk}))

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 not formset.is_valid():
attribute_errors = ""
for error in formset.errors:
if error:
attribute_errors += error.get("__all__")
messages.error(request, attribute_errors)
return error_redirect

for entry in formset:
formset_data = entry.cleaned_data

value = formset_data.get("value")

if value != "":
allocation_attribute = AllocationAttribute.objects.get(pk=formset_data.get("attribute_pk"))
if allocation_attribute.value != value:
attribute_changes_to_make.add((allocation_attribute, value))

for allocation_attribute, value in attribute_changes_to_make:
allocation_attribute.value = value
allocation_attribute.save()
allocation_attribute_changed.send(
sender=self.__class__,
attribute_pk=allocation_attribute.pk,
allocation_pk=pk,
)

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


class AllocationChangeDeleteAttributeView(LoginRequiredMixin, UserPassesTestMixin, View):
login_url = "/"

Expand Down