Skip to content

Commit

Permalink
linting and corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
raftmsohani committed Sep 19, 2024
1 parent ed965ac commit 95b6ebd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 62 deletions.
28 changes: 15 additions & 13 deletions tdrs-backend/tdpservice/data_files/admin/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from django.conf import settings
from django.utils.html import format_html
from datetime import datetime, timedelta, timezone
from django.template.response import TemplateResponse

DOMAIN = settings.FRONTEND_BASE_URL

Expand All @@ -29,6 +28,8 @@ class DataFileAdmin(ReadOnlyAdminMixin, admin.ModelAdmin):
"""Admin class for DataFile models."""

class Media:
"""Media class for DataFileAdmin."""

js = ('admin/js/admin/mymodel.js',)

actions = ['reparse_cmd']
Expand All @@ -40,20 +41,21 @@ def reparse_cmd(self, request, queryset):

files = queryset.values_list("id", flat=True)
file_ids = ",".join(map(str, files))
number_of_files = queryset.count()
number_of_records = sum([df.summary.total_number_of_records_in_file for df in queryset])
context = dict(
self.admin_site.each_context(request),
title="Are you sure?",
action="reparse_cmd",
queryset=queryset,
opts=self.model._meta,
msg=f"{number_of_files} datafiles, {number_of_records} records will be lost",
file_ids=file_ids,
from django.core.management import call_command
from django.utils.translation import ngettext
from django.contrib import messages
call_command("clean_and_reparse", f"-f {file_ids}")
self.message_user(
request,
ngettext(
"%d file successfully submitted for reparsing.",
"%d files successfully submitted for reparsing.",
files.count(),
)
% files.count(),
messages.SUCCESS,
)
#return TemplateResponse(request, "admin/action_confirmation.html", context)

# TODO: add tests for this method
def get_actions(self, request):
"""Return the actions."""
actions = super().get_actions(request)
Expand Down
72 changes: 23 additions & 49 deletions tdrs-backend/tdpservice/data_files/static/admin/js/admin/mymodel.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
$(window).on('load', function() {
//your code here
console.log('loaded');
var S=document.querySelector('button[type=submit]');
console.log(S);
// add the first listener
var S=document.querySelector('button[type=submit]'); // add the first listener
var theForm = S.parentNode.parentNode;
console.log(theForm);

for (var i = 0; i < theForm.childNodes.length; i++) {
console.log(theForm.childNodes[i].className)
if (theForm.childNodes[i].className == "actions") {
form_header = theForm.childNodes[i];
break;
}
}
for (var i = 0; i < form_header.childNodes.length; i++) {
console.log(form_header.childNodes[i].className)
if (form_header.childNodes[i].className == "action-counter") {
number_of_files = form_header.childNodes[i];
break;
}
}
S.addEventListener('click', function(e) {
e.preventDefault();
console.log('submitting');
disableFields(); // your own function
e.preventDefault();

alert("Ensure you input a value in both fields!");
theForm.submit(); //this is working
disableFields();
if (confirm("You are about to re-parse " + number_of_files.innerHTML.split(/(\s+)/)[0] + " files. Are you sure you want to continue?")) {
console.log('submitting');
theForm.submit();
} else {
console.log('not submitting');
};
});


Expand All @@ -23,42 +36,3 @@ $(window).on('load', function() {
disableFields = function() {
console.log('disabling fields');
}


reparseFiles = function() {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
alert(xhr.response);
}
}
xhr.open('POST', '/v1/data_files/run_action_reparse_cmd/', false);
xhr.setRequestHeader("X-CSRFToken", '{{ csrf_token }}');
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
formData = new FormData();
formData.append('file_ids', '{{ file_ids }}');
data = {'file_ids': '{{ file_ids }}'};
console.log(xhr)
xhr.send(JSON.stringify(data));
}

/*
<script>
function submit() {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
alert(xhr.response);
}
}
xhr.open('POST', '/v1/data_files/run_action_reparse_cmd/', false);
xhr.setRequestHeader("X-CSRFToken", '{{ csrf_token }}');
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
formData = new FormData();
formData.append('file_ids', '{{ file_ids }}');
data = {'file_ids': '{{ file_ids }}'};
console.log(xhr)
xhr.send(JSON.stringify(data));
}
</script>
*/

0 comments on commit 95b6ebd

Please sign in to comment.