Skip to content

Commit

Permalink
Updated jquery DataTable with "selectable rows" plugin, "layout" opti…
Browse files Browse the repository at this point in the history
…on for element placement

Setting year and month by cookie if set; defaults to last month otherwise
Errors only also cookie set
Selected rows in billing record table can be deleted; can reset a lab when combined with the right search
  • Loading branch information
aaronk committed Sep 3, 2024
1 parent 8f38ea3 commit 09c8e95
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 26 deletions.
103 changes: 90 additions & 13 deletions coldfront/plugins/ifx/templates/plugins/ifx/billing_records.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@
{% load crispy_forms_tags %}
{% load static %}

{% block ifx_head %}
<link rel="stylesheet" href="https://cdn.datatables.net/2.1.5/css/dataTables.dataTables.min.css"></style>
<script src="https://cdn.jsdelivr.net/npm/js-cookie@3.0.5/dist/js.cookie.min.js"></script>
<script src="https://cdn.datatables.net/2.1.5/js/dataTables.js"></script>
<script src="https://cdn.datatables.net/select/2.0.5/js/dataTables.select.js"></script>
<script src="https://cdn.datatables.net/select/2.0.5/js/select.dataTables.js"></script>
{% endblock %}

{% block title %}
Billing Records
{% endblock %}


{% block content %}
<style>
.dataTables_filter {
display: inline-flex;
margin: 0 0.5em 0 0;
}
.dataTables_length {
display: inline-flex;
margin: 0 0.5em 0 0;
float: right;
}
#notification-errors {
margin: 1em 0 0.5em 1em;
font-size: 10pt;
Expand Down Expand Up @@ -55,11 +54,53 @@
position: relative;
float: right;
}
.dt-input {
margin: 0 0 0 1em;
width: 300px;
}
</style>
<script>
(function($){
$(document).ready(function(){
var token = '{{ auth_token }}'
function deleteSelectedBillingRecords() {
const delete_url_template = '{{ delete_url }}'
const promises = []
const errors = []
billingRecordsTable.rows({selected: true}).data().each((row) => {
const delete_url = delete_url_template.replace('0', row.id)
promises.push(
$.ajax({
headers: {
'X-CSRFToken': Cookies.get('csrftoken'),
'sessionid': Cookies.get('sessionid'),
'Authorization': `Token ${token}`,
},
url: delete_url,
type: 'DELETE',
error: function (jqXHR, status, error) {
errors.push(`Error for row ${row.id}: ${status} ${error}`)
}
})
)

})
Promise.allSettled(promises).then(() => {
if (errors.length) {
alert(errors.join('\n'))
}
billingRecordsTable.ajax.reload()
}).catch(() => {
if (errors.length) {
alert(errors.join('\n'))
}
billingRecordsTable.ajax.reload()
})
}
$("#delete-selected").click((ev) => {
ev.preventDefault()
deleteSelectedBillingRecords()
})
$( "#progressbar" ).progressbar({
value: false
});
Expand Down Expand Up @@ -89,6 +130,20 @@
dialog.dialog("close")
});

// Get month and year cookie and errorsOnly if it exists using js-cookie
var year = Cookies.get('coldfront-billing-year') || new Date().getFullYear()
var month = Cookies.get('coldfront-billing-month') || new Date().getMonth() - 1
$("#year").val(year)
$("#month").val(month)

// Set month and year cookie when changed
$("#year").change(function() {
Cookies.set('coldfront-billing-year', $(this).val())
})
$("#month").change(function() {
Cookies.set('coldfront-billing-month', $(this).val())
})

var organizationSource = []
$.ajax({
url: '/ifx/api/get-org-names/',
Expand Down Expand Up @@ -208,9 +263,21 @@
})
}
})
DataTable.defaults.layout = {
topStart: null,
topEnd: null,
bottomStart: null,
bottomEnd: null,
};
var billingRecordsTable = $("#billing-records").DataTable({
dom: '<fl>tp',
layout: {
topStart: 'search',
topEnd: 'pageLength',
bottomStart: 'paging',
},
columns: [
{render: DataTable.render.select(), orderable: false, targets: 0},
{data: 'id'},
{data: function(item){ return item.current_state.replace(/_/g, ' ')}, width: '200px'},
{data: 'product_usage.product_user.full_name', width: '100px'},
{data: function(item){ return item.account.organization.replace(/ \(a Harvard.*/, '')}, width: '300px'},
Expand All @@ -219,7 +286,11 @@
{data: 'percent', width: '50px'},
{data: 'description', width: '500px'},
],
order: [[ 2, "asc" ], [ 1, "asc" ]],
select: {
style: 'os',
selector: 'td:first-child'
},
order: [[ 4, "asc" ], [ 1, "asc" ]],
ajax: {
url: '/ifx/api/billing/get-billing-record-list/',
type: 'GET',
Expand Down Expand Up @@ -275,6 +346,7 @@

</script>
<div style="font-size: 10pt;">
<h2>Billing Records</h2>
<div id="loading">
<p>
<div id="progressbar"></div>
Expand All @@ -299,23 +371,28 @@
<div style="margin: 1em; width: 100%;">
<div class="lefty">
<label for="year">Year</label>
<input type="text" id="year" name="year" value="2024"/>
<input type="text" id="year" name="year"/>
<label for="month">Month</label>
<input type="text" id="month" name="month" value="5"/>
<input type="text" id="month" name="month"/>
</div>
<div class="lefty">
<input id="billing-record-reload" type="submit" value="Reload Data"></input>
</div>
<div class="righty">
<input id="start-notification" type="submit" value="Notify Lab Managers"></input>
</div>
<div class="righty">
<input id="delete-selected" type="submit" value="Delete Selected"></input>
</div>
<div style="clear: both;"></div>
</div>
<hr/>
<div style="margin: 1em;">
<table id="billing-records" class="display" style="width: 100%" cellpadding="5px">
<thead>
<tr>
<th></th>
<th>Id</th>
<th>State</th>
<th>User</th>
<th>Lab</th>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
{% load static %}

{% block ifx_head %}
<link rel="stylesheet" href="https://cdn.datatables.net/2.1.5/css/dataTables.dataTables.min.css"></style>
<script src="https://cdn.jsdelivr.net/npm/js-cookie@3.0.5/dist/js.cookie.min.js"></script>
<script src="https://cdn.datatables.net/2.1.5/js/dataTables.js"></script>
<script src="https://cdn.datatables.net/select/2.0.5/js/dataTables.select.js"></script>
<script src="https://cdn.datatables.net/select/2.0.5/js/select.dataTables.js"></script>
{% endblock %}

{% block title %}
Expand All @@ -14,23 +18,20 @@

{% block content %}
<style>
.dataTables_filter {
display: inline-flex;
margin: 0 0.5em 0 0;
}
.dataTables_length {
display: inline-flex;
margin: 0 0.5em 0 0;
float: right;
}
label {
margin: 0 0 0 1em;
}
.dt-input {
margin: 0 0 0 1em;
width: 200px;
}
</style>
<script>
(function($){
$(document).ready(function(){



$( "#progressbar" ).progressbar({
value: false
});
Expand Down Expand Up @@ -85,8 +86,18 @@
}
})

DataTable.defaults.layout = {
topStart: null,
topEnd: null,
bottomStart: null,
bottomEnd: null,
};
var productUsagesTable = $("#product-usages").DataTable({
dom: '<fl>tp',
layout: {
topStart: 'search',
topEnd: 'pageLength',
bottomStart: 'paging',
},
columns: [
{data: 'id', width: '100px'},
{data: 'full_name', width: '200px'},
Expand All @@ -95,7 +106,11 @@
{data: 'description', width: '500px'},
{data: 'error_message', width: '400px'},
],
order: [[ 2, "asc" ], [ 1, "asc" ]],
order: [[ 3, "asc" ], [ 2, "asc" ]],
select: {
style: 'os',
selector: 'td:first-child'
},
ajax: {
url: '/ifx/api/get-product-usages/',
type: 'GET',
Expand Down Expand Up @@ -155,6 +170,7 @@

</script>
<div style="font-size: 10pt;">
<h2>Calculate Billing Month</h2>
<div id="loading">
<p>
<div id="progressbar"></div>
Expand Down Expand Up @@ -195,7 +211,7 @@
</div>
<hr/>
<div style="margin: 1em;">
<table id="product-usages" class="display" style="width: 100%" cellpadding="5px">
<table id="product-usages" class="display" cellpadding="5px">
<thead>
<tr>
<th>ID</th>
Expand Down
5 changes: 4 additions & 1 deletion coldfront/plugins/ifx/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from django.db import connection
from django.conf import settings
from django.core.exceptions import PermissionDenied
from django.urls import reverse
from django_q.tasks import async_task
from rest_framework.decorators import api_view, permission_classes, authentication_classes
from rest_framework.response import Response
Expand Down Expand Up @@ -79,7 +80,9 @@ def billing_records(request):
'''
if not request.user.is_superuser:
raise PermissionDenied
return render(request, 'plugins/ifx/billing_records.html')
delete_url = reverse('billing-record-detail', kwargs={'pk': 0})
token = request.user.auth_token.key
return render(request, 'plugins/ifx/billing_records.html', { 'delete_url': delete_url, 'auth_token': token })

@api_view(['GET',])
@authentication_classes([TokenAuthentication, SessionAuthentication, BasicAuthentication])
Expand Down

0 comments on commit 09c8e95

Please sign in to comment.