Skip to content

Commit fd8fa5d

Browse files
Add listview for retired resources, prevent modifications for retired resources
1 parent 1f0627e commit fd8fa5d

File tree

5 files changed

+178
-2
lines changed

5 files changed

+178
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{% extends "list_view.html" %}
2+
3+
{% block title %}
4+
Project List
5+
{% endblock %}
6+
7+
{% block page_title%}Archived Resources{% endblock %}
8+
9+
{% block presearch %}
10+
<div class="card mb-3 bg-light">
11+
<div class="card-body">
12+
<div class="float-left">
13+
<a class="btn btn-primary" href="{% url 'resource-list' %}?{{filter_parameters}}" role="button"><i class="fas fa-arrow-left" aria-hidden="true"></i> Back to active resources</a>
14+
</div>
15+
</div>
16+
</div>
17+
{% endblock %}
18+
19+
20+
{% block list_title %}Resource{{count|pluralize}}: {{count}}{% endblock %}
21+
22+
{% block table_contents %}
23+
<thead>
24+
<tr>
25+
<th scope="col" class="text-nowrap">
26+
ID
27+
<a href="?order_by=id&direction=asc&{{filter_parameters}}"><i class="fas fa-sort-up" aria-hidden="true"></i><span class="sr-only">Sort ID asc</span></a>
28+
<a href="?order_by=id&direction=des&{{filter_parameters}}"><i class="fas fa-sort-down" aria-hidden="true"></i><span class="sr-only">Sort ID desc</span></a>
29+
</th>
30+
<th scope="col" class="text-nowrap">
31+
Resource Name
32+
<a href="?order_by=name&direction=asc&{{filter_parameters}}"><i class="fas fa-sort-up" aria-hidden="true"></i><span class="sr-only">Sort Resource Name asc</span></a>
33+
<a href="?order_by=name&direction=des&{{filter_parameters}}"><i class="fas fa-sort-down" aria-hidden="true"></i><span class="sr-only">Sort Resource Name desc</span></a>
34+
</th>
35+
<th scope="col" class="text-nowrap">
36+
Parent Resource
37+
<a href="?order_by=parent_resource&direction=asc&{{filter_parameters}}"><i class="fas fa-sort-up" aria-hidden="true"></i><span class="sr-only">Sort Parent Resource asc</span></a>
38+
<a href="?order_by=parent_resource&direction=des&{{filter_parameters}}"><i class="fas fa-sort-down" aria-hidden="true"></i><span class="sr-only">Sort Parent Resource desc</span></a>
39+
</th>
40+
<th scope="col" class="text-nowrap">
41+
Resource Type
42+
<a href="?order_by=resource_type__name&direction=asc&{{filter_parameters}}"><i class="fas fa-sort-up" aria-hidden="true"></i><span class="sr-only">Sort Resource Type asc</span></a>
43+
<a href="?order_by=resource_type__name&direction=des&{{filter_parameters}}"><i class="fas fa-sort-down" aria-hidden="true"></i><span class="sr-only">Sort Resource Type desc</span></a>
44+
</th>
45+
</tr>
46+
</thead>
47+
<tbody>
48+
{% for resource in item_list %}
49+
<tr>
50+
<td><a href="/resource/{{resource.id}}/">{{ resource.id }}</a></td>
51+
<td>{{ resource }}</td>
52+
<td>{{ resource.parent_resource }}</td>
53+
<td>{{ resource.resource_type.name }}</td>
54+
</tr>
55+
{% endfor %}
56+
</tbody>
57+
{% endblock %}
58+
59+
{% block activelink %}
60+
$("#navbar-project-menu").addClass("active");
61+
$("#navbar-resource").addClass("active");
62+
63+
{% endblock %}

coldfront/core/resource/templates/resource_detail.html

+5-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212

1313
{% block content %}
1414

15-
15+
{% if resource.is_available == False %}
16+
<div class="alert alert-warning" role="alert">
17+
This is a retired resource! You cannot make any changes.
18+
</div>
19+
{% endif %}
1620
<div class="mb-3">
1721
<h2>Resource Detail</h2>
1822
<hr>

coldfront/core/resource/templates/resource_list.html

+10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@
33
{% block title %}Resource List{% endblock %}
44

55
{% block page_title %}Resources{% endblock %}
6+
{% block presearch %}
7+
<div class="card mb-3 bg-light">
8+
<div class="card-body">
9+
<div class="float-right">
10+
<a class="btn btn-primary" href="{% url 'resource-archived-list' %}?{{filter_parameters}}" role="button"><i class="fas fa-archive" aria-hidden="true"></i> View retired resources</a>
11+
</div>
12+
</div>
13+
</div>
14+
{% endblock %}
615

716
{% block list_title %}Resource{{count|pluralize}}: {{count}}{% endblock %}
817

@@ -46,4 +55,5 @@
4655
{% block activelink %}
4756
$("#navbar-project-menu").addClass("active");
4857
$("#navbar-resource").addClass("active");
58+
4959
{% endblock %}

coldfront/core/resource/urls.py

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
urlpatterns = [
66
path('', resource_views.ResourceListView.as_view(),
77
name='resource-list'),
8+
path('archived/', resource_views.ResourceArchivedListView.as_view(), name='resource-archived-list'),
89
path('<int:pk>/', resource_views.ResourceDetailView.as_view(),
910
name='resource-detail'),
1011
path('<int:pk>/resourceattribute/add',

coldfront/core/resource/views.py

+99-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from coldfront.plugins.slurm.utils import SlurmError
3434

3535
from coldfront.core.project.models import ProjectUser, Project
36+
from pandas.io.clipboard import is_available
3637

3738
logger = logging.getLogger(__name__)
3839

@@ -188,6 +189,18 @@ def test_func(self):
188189
messages.error(
189190
self.request, 'You do not have permission to add resource attributes.')
190191

192+
def dispatch(self, request, *args, **kwargs):
193+
resource_obj = get_object_or_404(Resource, pk=self.kwargs.get('pk'))
194+
err = None
195+
if resource_obj.is_available is False:
196+
err = 'You cannot add resource attributes to retired allocations.'
197+
if err:
198+
messages.error(request, err)
199+
return HttpResponseRedirect(
200+
reverse('resource-detail', kwargs={'pk': resource_obj.pk})
201+
)
202+
return super().dispatch(request, *args, **kwargs)
203+
191204
def get_context_data(self, **kwargs):
192205
context = super().get_context_data(**kwargs)
193206
pk = self.kwargs.get('pk')
@@ -222,6 +235,18 @@ def test_func(self):
222235
messages.error(
223236
self.request, 'You do not have permission to delete resource attributes.')
224237

238+
def dispatch(self, request, *args, **kwargs):
239+
resource_obj = get_object_or_404(Resource, pk=self.kwargs.get('pk'))
240+
err = None
241+
if resource_obj.is_available is False:
242+
err = 'You cannot delete resource attributes from retired allocations.'
243+
if err:
244+
messages.error(request, err)
245+
return HttpResponseRedirect(
246+
reverse('resource-detail', kwargs={'pk': resource_obj.pk})
247+
)
248+
return super().dispatch(request, *args, **kwargs)
249+
225250
def get(self, request, *args, **kwargs):
226251
pk = self.kwargs.get('pk')
227252
resource_obj = get_object_or_404(Resource, pk=pk)
@@ -370,15 +395,86 @@ def get_queryset(self):
370395
resource_attribute_type__name='Owner',
371396
resource__resource_type__name='Compute Node'
372397
).exclude(value__in=project_title_list)]
398+
if self.request.user.is_superuser:
399+
return resources.distinct()
373400
return resources.exclude(pk__in=not_owned_compute_nodes).distinct()
374401

375-
376402
def get_context_data(self, **kwargs):
377403
context = super().get_context_data(
378404
SearchFormClass=ResourceSearchForm, **kwargs)
379405
return context
380406

381407

408+
class ResourceArchivedListView(ResourceListView):
409+
template_name = 'resource_archived_list.html'
410+
411+
def get_queryset(self):
412+
413+
order_by = self.return_order()
414+
resource_search_form = ResourceSearchForm(self.request.GET)
415+
416+
if order_by == 'name':
417+
direction = self.request.GET.get('direction')
418+
if direction == 'asc':
419+
resources = Resource.objects.all().order_by(Lower('name'))
420+
elif direction == 'des':
421+
resources = (Resource.objects.all().order_by(Lower('name')).reverse())
422+
else:
423+
resources = Resource.objects.all().order_by(order_by)
424+
else:
425+
resources = Resource.objects.all().order_by(order_by)
426+
if resource_search_form.is_valid():
427+
data = resource_search_form.cleaned_data
428+
429+
if data.get('show_allocatable_resources'):
430+
resources = resources.filter(is_allocatable=True)
431+
if data.get('resource_name'):
432+
resources = resources.filter(
433+
name__icontains=data.get('resource_name')
434+
)
435+
if data.get('resource_type'):
436+
resources = resources.filter(
437+
resource_type=data.get('resource_type')
438+
)
439+
440+
if data.get('model'):
441+
resources = resources.filter(
442+
Q(resourceattribute__resource_attribute_type__name='Model') &
443+
Q(resourceattribute__value=data.get('model'))
444+
)
445+
if data.get('serialNumber'):
446+
resources = resources.filter(
447+
Q(resourceattribute__resource_attribute_type__name='SerialNumber') &
448+
Q(resourceattribute__value=data.get('serialNumber'))
449+
)
450+
if data.get('installDate'):
451+
resources = resources.filter(
452+
Q(resourceattribute__resource_attribute_type__name='InstallDate') &
453+
Q(resourceattribute__value=data.get('installDate').strftime('%m/%d/%Y'))
454+
)
455+
if data.get('serviceStart'):
456+
resources = resources.filter(
457+
Q(resourceattribute__resource_attribute_type_name='ServiceStart') &
458+
Q(resourceattribute__value=data.get('serviceStart').strftime('%m/%d/%Y'))
459+
)
460+
if data.get('serviceEnd'):
461+
resources = resources.filter(
462+
Q(resourceattribute__resource_attribute_type__name='ServiceEnd') &
463+
Q(resourceattribute__value=data.get('serviceEnd').strftime('%m/%d/%Y'))
464+
)
465+
if data.get('warrantyExpirationDate'):
466+
resources = resources.filter(
467+
Q(resourceattribute__resource_attribute_type__name='WarrantyExpirationDate') &
468+
Q(resourceattribute__value=data.get('warrantyExpirationDate').strftime('%m/%d/%Y'))
469+
)
470+
if data.get('vendor'):
471+
resources = resources.filter(
472+
Q(resourceattribute__resource_attribute_type__name='Vendor') &
473+
Q(resourceattribute__value=data.get('vendor'))
474+
)
475+
return resources.exclude(is_available=True).distinct()
476+
477+
382478
class ResourceAllocationsEditView(LoginRequiredMixin, UserPassesTestMixin, TemplateView):
383479
template_name = 'resource_allocations_edit.html'
384480

@@ -396,6 +492,8 @@ def dispatch(self, request, *args, **kwargs):
396492
err = None
397493
if 'Storage' in resource_obj.resource_type.name:
398494
err = 'You cannot bulk-edit storage allocations.'
495+
if resource_obj.is_available is False:
496+
err = 'You cannot edit retired allocations.'
399497
if err:
400498
messages.error(request, err)
401499
return HttpResponseRedirect(

0 commit comments

Comments
 (0)