Skip to content
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
39 changes: 6 additions & 33 deletions coldfront/core/project/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,39 +40,12 @@ class ProjectAddUserForm(forms.Form):


class ProjectAddUsersToAllocationForm(forms.Form):
allocation = forms.MultipleChoiceField(
widget=forms.CheckboxSelectMultiple(attrs={"checked": "checked"}), required=False
)

def __init__(self, request_user, project_pk, *args, **kwargs):
super().__init__(*args, **kwargs)
project_obj = get_object_or_404(Project, pk=project_pk)

allocation_query_set = project_obj.allocation_set.filter(
resources__is_allocatable=True,
is_locked=False,
status__name__in=["Active", "New", "Renewal Requested", "Payment Pending", "Payment Requested", "Paid"],
)
allocation_choices = [
(
allocation.id,
"%s (%s) %s"
% (
allocation.get_parent_resource.name,
allocation.get_parent_resource.resource_type.name,
allocation.description if allocation.description else "",
),
)
for allocation in allocation_query_set
]
allocation_choices_sorted = []
allocation_choices_sorted = sorted(allocation_choices, key=lambda x: x[1][0].lower())
allocation_choices.insert(0, ("__select_all__", "Select All"))
if allocation_query_set:
self.fields["allocation"].choices = allocation_choices_sorted
self.fields["allocation"].help_text = "<br/>Select allocations to add selected users to."
else:
self.fields["allocation"].widget = forms.HiddenInput()
pk = forms.IntegerField(disabled=True)
selected = forms.BooleanField(initial=False, required=False)
resource = forms.CharField(max_length=50, disabled=True)
details = forms.CharField(max_length=300, disabled=True, required=False)
resource_type = forms.CharField(max_length=50, disabled=True)
status = forms.CharField(max_length=50, disabled=True)


class ProjectRemoveUserForm(forms.Form):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,38 @@
<div class="card bg-light mb-3 {{div_allocation_class}}">
<div class="card-header">Available Allocations</div>
<div class="card-body">
{{allocation_form|crispy}}
<div class="table-responsive">
<table class="table table-sm table-hover">
<thead>
<tr>
<th><input type="checkbox" class="check" id="selectAllAllocations"></th>
<th scope="col">#</th>
<th scope="col">Resource</th>
<th scope="col">Details</th>
<th scope="col">Resource Type</th>
<th scope="col">Status</th>
</tr>
</thead>
<tbody>
{% for form in allocation_formset %}
<tr>
<td>{{ form.selected }}</td>
<td><a href="{% url 'allocation-detail' form.pk.value %}" target="_blank">{{ form.pk.value }}</a></td>
<td>{{ form.resource.value }}</td>
<td>{{ form.details.value }}</td>
<td>{{ form.resource_type.value }}</td>
<td>{{ form.status.value }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
<div class="card-footer">
<i class="fa fa-info-circle" aria-hidden="true"></i>
Select allocations to add selected users to.
</div>
{{ allocation_formset.management_form }}
</div>

<div class="table-responsive">
Expand Down Expand Up @@ -87,14 +117,15 @@
}
});

$("#id_allocationform-allocation_0").click(function () {
$("#selectAllAllocations").click(function () {
$("input[name^='allocationform-']").prop('checked', $(this).prop('checked'));
});

$("input[name^='allocationform-']").click(function (ele) {
var id = $(this).attr('id');
if ( id != "id_allocationform-allocation_0") {
$("#id_allocationform-allocation_0").prop('checked', false);
if ( id != "selectAllAllocations") {
$("#selectAllAllocations").prop('checked', false);
}
});
</script>

Expand Down
70 changes: 59 additions & 11 deletions coldfront/core/project/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,23 @@ def dispatch(self, request, *args, **kwargs):
else:
return super().dispatch(request, *args, **kwargs)

def get_initial_data(self, project_obj):
allocation_objs = project_obj.allocation_set.filter(
resources__is_allocatable=True,
is_locked=False,
status__name__in=["Active", "New", "Renewal Requested", "Payment Pending", "Payment Requested", "Paid"],
)
return [
{
"pk": allocation_obj.pk,
"resource": allocation_obj.get_parent_resource.name,
"details": allocation_obj.get_information,
"resource_type": allocation_obj.get_parent_resource.resource_type.name,
"status": allocation_obj.status.name,
}
for allocation_obj in allocation_objs
]

def post(self, request, *args, **kwargs):
user_search_string = request.POST.get("q")
search_by = request.POST.get("search_by")
Expand Down Expand Up @@ -767,9 +784,12 @@ def post(self, request, *args, **kwargs):
context["div_allocation_class"] = div_allocation_class
###

allocation_form = ProjectAddUsersToAllocationForm(request.user, project_obj.pk, prefix="allocationform")
initial_data = self.get_initial_data(project_obj)
allocation_formset = formset_factory(ProjectAddUsersToAllocationForm, max_num=len(initial_data))
allocation_formset = allocation_formset(initial=initial_data, prefix="allocationform")

context["pk"] = pk
context["allocation_form"] = allocation_form
context["allocation_formset"] = allocation_formset
return render(request, self.template_name, context)


Expand Down Expand Up @@ -800,6 +820,23 @@ def dispatch(self, request, *args, **kwargs):
else:
return super().dispatch(request, *args, **kwargs)

def get_initial_data(self, project_obj):
allocation_objs = project_obj.allocation_set.filter(
resources__is_allocatable=True,
is_locked=False,
status__name__in=["Active", "New", "Renewal Requested", "Payment Pending", "Payment Requested", "Paid"],
)
return [
{
"pk": allocation_obj.pk,
"resource": allocation_obj.get_parent_resource.name,
"details": allocation_obj.get_information,
"resource_type": allocation_obj.get_parent_resource.resource_type.name,
"status": allocation_obj.status.name,
}
for allocation_obj in allocation_objs
]

def post(self, request, *args, **kwargs):
user_search_string = request.POST.get("q")
search_by = request.POST.get("search_by")
Expand All @@ -820,20 +857,31 @@ def post(self, request, *args, **kwargs):
formset = formset_factory(ProjectAddUserForm, max_num=len(matches))
formset = formset(request.POST, initial=matches, prefix="userform")

allocation_form = ProjectAddUsersToAllocationForm(
request.user, project_obj.pk, request.POST, prefix="allocationform"
initial_data = self.get_initial_data(project_obj)
allocation_formset = formset_factory(
ProjectAddUsersToAllocationForm,
max_num=len(initial_data),
)
allocation_formset = allocation_formset(
request.POST,
initial=initial_data,
prefix="allocationform",
)

added_users_count = 0
if formset.is_valid() and allocation_form.is_valid():
if formset.is_valid() and allocation_formset.is_valid():
project_user_active_status_choice = ProjectUserStatusChoice.objects.get(name="Active")
allocation_user_active_status_choice = AllocationUserStatusChoice.objects.get(name="Active")
if ALLOCATION_EULA_ENABLE:
allocation_user_pending_status_choice = AllocationUserStatusChoice.objects.get(name="PendingEULA")

allocation_form_data = allocation_form.cleaned_data["allocation"]
if "__select_all__" in allocation_form_data:
allocation_form_data.remove("__select_all__")
allocations_selected_objs = Allocation.objects.filter(
pk__in=[
allocation_form.cleaned_data.get("pk")
for allocation_form in allocation_formset
if allocation_form.cleaned_data.get("selected")
]
)
for form in formset:
user_form_data = form.cleaned_data
if user_form_data["selected"]:
Expand Down Expand Up @@ -864,7 +912,7 @@ def post(self, request, *args, **kwargs):
# project signals
project_activate_user.send(sender=self.__class__, project_user_pk=project_user_obj.pk)

for allocation in Allocation.objects.filter(pk__in=allocation_form_data):
for allocation in allocations_selected_objs:
has_eula = allocation.get_eula()
user_status_choice = allocation_user_active_status_choice
if allocation.allocationuser_set.filter(user=user_obj).exists():
Expand Down Expand Up @@ -894,8 +942,8 @@ def post(self, request, *args, **kwargs):
for error in formset.errors:
messages.error(request, error)

if not allocation_form.is_valid():
for error in allocation_form.errors:
if not allocation_formset.is_valid():
for error in allocation_formset.errors:
messages.error(request, error)

return HttpResponseRedirect(reverse("project-detail", kwargs={"pk": pk}))
Expand Down