Skip to content
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

3106 reparse django action #3167

Merged
merged 72 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from 70 commits
Commits
Show all changes
72 commits
Select commit Hold shift + click to select a range
2753507
initial commit - added filter
raftmsohani Aug 14, 2024
f912d6b
Merge branch 'develop' into 3076-Admin-Filter-Enhancements-Data-Files…
raftmsohani Aug 15, 2024
b79cbb1
3076 added test
raftmsohani Aug 15, 2024
11b411e
Merge branch 'develop' into 3076-Admin-Filter-Enhancements-Data-Files…
raftmsohani Aug 16, 2024
1ca33a6
linting
raftmsohani Aug 23, 2024
e5b9371
Merge branch '3076-Admin-Filter-Enhancements-Data-Files-Page' of gith…
raftmsohani Aug 23, 2024
4a3a169
move import to top
raftmsohani Aug 23, 2024
f810633
lint
raftmsohani Aug 23, 2024
bc050cf
3076 correction
raftmsohani Aug 27, 2024
16bfbce
3076 Fixed today filter
raftmsohani Aug 28, 2024
3fa6d7a
linting
raftmsohani Aug 28, 2024
f91fc46
Merge branch 'develop' into 3076-Admin-Filter-Enhancements-Data-Files…
raftmsohani Aug 28, 2024
6c0cc70
Merge branch 'develop' into 3076-Admin-Filter-Enhancements-Data-Files…
raftmsohani Aug 29, 2024
3f7690c
Merge branch '3076-Admin-Filter-Enhancements-Data-Files-Page' of gith…
raftmsohani Aug 29, 2024
fb6ecb7
3076 applied a11y review changes
raftmsohani Aug 30, 2024
198b98a
3076 fix failing test
raftmsohani Aug 30, 2024
c61b298
3106 first draft
raftmsohani Sep 3, 2024
b3504b9
Merge branch 'develop' into 3106-reparse-django-action
raftmsohani Sep 5, 2024
e87808c
Merge branch 'develop' into 3106-reparse-django-action
raftmsohani Sep 5, 2024
16ecfdb
Merge branch '3106-reparse-django-action' of github.com:raft-tech/TAN…
raftmsohani Sep 5, 2024
d59af03
clean up
raftmsohani Sep 5, 2024
e1a3bbc
clean up
raftmsohani Sep 5, 2024
d3fd407
clean admin
raftmsohani Sep 5, 2024
2237686
clean admin
raftmsohani Sep 5, 2024
778f706
parsing command
raftmsohani Sep 9, 2024
a5737b4
delete unwanted imports
raftmsohani Sep 9, 2024
8e94085
Merge branch 'develop' into 3106-reparse-django-action
raftmsohani Sep 9, 2024
7fa47a8
added modal form
raftmsohani Sep 11, 2024
dd6d85e
Merge branch '3106-reparse-django-action' of github.com:raft-tech/TAN…
raftmsohani Sep 11, 2024
1463ea4
added styling to confirmation page
raftmsohani Sep 11, 2024
152d439
changes
raftmsohani Sep 11, 2024
d3deddf
3106 middle page works
raftmsohani Sep 13, 2024
f78e5ae
Merge branch 'develop' into 3106-reparse-django-action
raftmsohani Sep 13, 2024
a54f14a
3106 can add files for reparsing
raftmsohani Sep 13, 2024
2b75af8
3106 Correct linting
raftmsohani Sep 13, 2024
626087f
bypassing status check to get this deployed for testing
andrew-jameson Sep 17, 2024
38d50f5
injected working js
raftmsohani Sep 18, 2024
5e6c89c
remove some of the comments
raftmsohani Sep 18, 2024
ed965ac
revert changes
raftmsohani Sep 19, 2024
95b6ebd
linting and corrections
raftmsohani Sep 19, 2024
1c19e87
lint
raftmsohani Sep 19, 2024
98bd857
redirect to meta model
raftmsohani Sep 20, 2024
e09ce5d
Merge branch 'develop' into 3106-reparse-django-action
raftmsohani Sep 20, 2024
e100f6d
Merge branch 'develop' into 3106-reparse-django-action
raftmsohani Sep 23, 2024
3daebc5
3106 replying comments
raftmsohani Sep 25, 2024
4f28656
Merge branch '3106-reparse-django-action' of github.com:raft-tech/TAN…
raftmsohani Sep 25, 2024
f1c22f4
move imports to the top
raftmsohani Sep 25, 2024
6ca4da4
Merge branch 'develop' into 3106-reparse-django-action
raftmsohani Sep 25, 2024
f3a3959
Update admin.py: renamed reparse command
raftmsohani Sep 30, 2024
4945d32
Merge branch 'develop' into 3106-reparse-django-action
raftmsohani Sep 30, 2024
d1fb4d6
3106 removed unused function
raftmsohani Sep 30, 2024
6e723e0
3106 fixed reparsing command issues
raftmsohani Sep 30, 2024
bf2d288
Merge branch 'develop' into 3106-reparse-django-action
raftmsohani Oct 2, 2024
a1c4631
resolve merge conflict errors
raftmsohani Oct 2, 2024
4d6dcb4
Merge branch 'develop' into 3106-reparse-django-action
andrew-jameson Oct 2, 2024
cfc919b
Merge branch 'develop' into 3106-reparse-django-action
raftmsohani Oct 4, 2024
3355b97
Merge branch 'develop' into 3106-reparse-django-action
raftmsohani Oct 9, 2024
0820878
move admin action to celery task
jtimpe Oct 9, 2024
a20b740
rm print
jtimpe Oct 9, 2024
98861c6
rm prints
jtimpe Oct 9, 2024
f758860
lint
jtimpe Oct 9, 2024
b9128dd
rm unused
jtimpe Oct 9, 2024
1681c96
unused imports
jtimpe Oct 9, 2024
59cf447
script styling
jtimpe Oct 9, 2024
b8af66f
Merge branch 'develop' into 3106-reparse-django-action
raftmsohani Oct 15, 2024
72119a8
changed log message
raftmsohani Oct 16, 2024
b08f324
Merge branch '3106-reparse-django-action' of github.com:raft-tech/TAN…
raftmsohani Oct 16, 2024
5c8220e
Merge branch 'develop' into 3106-reparse-django-action
ADPennington Oct 16, 2024
0b2b3f2
Merge branch 'develop' into 3106-reparse-django-action
ADPennington Oct 16, 2024
8f8a575
Merge branch 'develop' into 3106-reparse-django-action
raftmsohani Oct 17, 2024
959ee4e
change log for action
jtimpe Oct 17, 2024
8fbf08a
Merge branch 'develop' into 3106-reparse-django-action
ADPennington Oct 17, 2024
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: 38 additions & 1 deletion tdrs-backend/tdpservice/data_files/admin/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
from django.conf import settings
from django.utils.html import format_html
from datetime import datetime, timedelta, timezone
from django.shortcuts import redirect
from django.utils.translation import ngettext
from django.contrib import messages
from tdpservice.data_files.tasks import reparse_files

DOMAIN = settings.FRONTEND_BASE_URL

Expand All @@ -22,11 +26,44 @@ def has_change_permission(self, request, obj=None):
"""Read only permissions."""
return False


@admin.register(DataFile)
class DataFileAdmin(ReadOnlyAdminMixin, admin.ModelAdmin):
"""Admin class for DataFile models."""

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

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

actions = ['reparse']

def reparse(self, request, queryset):
elipe17 marked this conversation as resolved.
Show resolved Hide resolved
"""Reparse the selected data files."""
files = queryset.values_list("id", flat=True)
reparse_files.delay(list(files))

self.message_user(
request,
ngettext(
"%d file successfully submitted for reparsing.",
"%d files successfully submitted for reparsing.",
files.count(),
)
% files.count(),
messages.SUCCESS,
)
return redirect("/admin/search_indexes/reparsemeta/")

def get_actions(self, request):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: if not admin, then the reparse command is not shown

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

evidence below that digit cant reparse or view reparse model cc: @ttran-hub

digit_cant_reparse

"""Return the actions."""
actions = super().get_actions(request)
if not request.user.groups.filter(name__in=["OFA System Admin", "OFA Admin"]).exists():
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

user.is_in_group() or user.is_ofa_sys_admin (modified to accept both)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is then it will do two queries instead of one

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might it be worth appending Developer to this list or do you guys change roles frequently for your testing?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I change my role from time to time

actions.pop("reparse", None)
else:
if "reparse" not in actions:
actions["reparse"] = (self.reparse, "reparse", "Reparse selected data files)")
return actions

def status(self, obj):
"""Return the status of the data file summary."""
return DataFileSummary.objects.get(datafile=obj).status
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
$(window).on('load', function() {
console.log('loaded');
var submitBtn=document.querySelector('button[type=submit]'); // add the first listener
var theForm = submitBtn.parentNode.parentNode;

for (var i = 0; i < theForm.childNodes.length; i++) {
if (theForm.childNodes[i].className == "actions") {
form_header = theForm.childNodes[i];

for (var i = 0; i < form_header.childNodes.length; i++) {
if (form_header.childNodes[i].className == "action-counter") {
number_of_files = form_header.childNodes[i];
break;
}
}

break;
}
}
submitBtn.addEventListener('click', function(e) {
e.preventDefault();
if (confirm("You are about to re-parse " + number_of_files.innerHTML.split(/(\s+)/)[0] + " files. Are you sure you want to continue?")) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

confirm_reparse_DAC

console.log('submitting');
theForm.submit();
} else {
console.log('not submitting');
};
});

});
8 changes: 8 additions & 0 deletions tdrs-backend/tdpservice/data_files/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from datetime import timedelta
from django.utils import timezone
from django.contrib.auth.models import Group
from django.core.management import call_command
from django.db.models import Q, Count
from tdpservice.users.models import AccountApprovalStatusChoices, User
from tdpservice.data_files.models import DataFile
Expand Down Expand Up @@ -46,3 +47,10 @@ def notify_stuck_files():
).values_list('username', flat=True).distinct()

send_stuck_file_email(stuck_files, recipients)


@shared_task
def reparse_files(file_ids):
"""Call the clean_and_reparse management command with a list of file ids."""
file_ids_str = ",".join(map(str, file_ids))
call_command("clean_and_reparse", f"-f {file_ids_str}")
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def add_arguments(self, parser):
parser.add_argument("-y", "--fiscal_year", type=int, help="Reparse all files in the fiscal year, e.g. 2021.")
parser.add_argument("-a", "--all", action='store_true', help="Clean and reparse all datafiles. If selected, "
"fiscal_year/quarter aren't necessary.")
parser.add_argument("-f", "--files", nargs='+', type=str, help="Re-parse specific datafiles by datafile id")
raftmsohani marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Collaborator

@ADPennington ADPennington Oct 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i tested this new option on a subset of files with this command:

python manage.py clean_and_reparse -f "1215,1030,2246,1807,1737,1826,1044"

and this immediately triggered the reparsing process and did not prompt me to confirm that I wanted to reparse X number of files.

vcap@:~$ python manage.py clean_and_reparse -f "1215,1030,2246,1807,1737,1826,1044"
************** reparse all False
************** selected files [1215, 1030, 2246, 1807, 1737, 1826, 1044]


INSIDE FILE COUNTS MATCH:
7, 7, 0




INSIDE FILE COUNTS MATCH:
7, 7, 0


2024-10-17 22:01:51,087 INFO clean_and_reparse.py::_backup:L49 :  Beginning reparse DB Backup.
Beginning reparse DB Backup.
2024-10-17 22:01:51,092 INFO db_backup.py::get_system_values:L54 :  Using postgres client at: /home/vcap/deps/0/apt/usr/lib/postgresql/15/bin/
Using postgres client at: /home/vcap/deps/0/apt/usr/lib/postgresql/15/bin/
2024-10-17 22:01:51,094 INFO db_backup.py::backup_database:L89 :  Executing backup command: /home/vcap/deps/0/apt/usr/lib/postgresql/15/bin/pg_dump -Fc 
...
...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i expected something like this:

vcap@:~$ python manage.py clean_and_reparse -y 2025
************** reparse all False
************** selected files None

You have selected to reparse datafiles for FY 2025 and Q1-4. The reparsed files will NOT be stored in new indices and the old indices
These options will delete and reparse (2) datafiles.
Continue [y/n]?

Copy link
Collaborator

@ADPennington ADPennington Oct 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

per Jan:

-f is only used by the action (which confirms via javascript), which is why the additional conditional checks weren't added

evidence here


def _get_log_context(self, system_user):
"""Return logger context."""
Expand Down Expand Up @@ -263,32 +264,20 @@ def _handle_input(self, testing, continue_msg):
print('Cancelled.')
exit(0)

def handle(self, *args, **options):
"""Delete and reparse datafiles matching a query."""
fiscal_year = options.get('fiscal_year', None)
fiscal_quarter = options.get('fiscal_quarter', None)
reparse_all = options.get('all', False)
new_indices = reparse_all is True

# Option that can only be specified by calling `handle` directly and passing it.
testing = options.get('testing', False)
##

args_passed = fiscal_year is not None or fiscal_quarter is not None or reparse_all

if not args_passed:
logger.warn("No arguments supplied.")
self.print_help("manage.py", "clean_and_parse")
return

def get_files_to_reparse(case, fiscal_year, fiscal_quarter, selected_files, reparse_all):
"""Get the files to reparse."""
backup_file_name = "/tmp/reparsing_backup"
files = DataFile.objects.all()
continue_msg = "You have selected to reparse datafiles for FY {fy} and {q}. The reparsed files "
if selected_files:
files = files.filter(id__in=selected_files)
backup_file_name += "_selected_files"
continue_msg = continue_msg.format(fy=f"selected files: {str(selected_files)}", q="Q1-4")
if reparse_all:
backup_file_name += "_FY_All_Q1-4"
continue_msg = continue_msg.format(fy="All", q="Q1-4")
ADPennington marked this conversation as resolved.
Show resolved Hide resolved
else:
if not fiscal_year and not fiscal_quarter:
if not fiscal_year and not fiscal_quarter and not selected_files:
ADPennington marked this conversation as resolved.
Show resolved Hide resolved
print(
'Options --fiscal_year and --fiscal_quarter not set. '
'Provide either option to continue, or --all to wipe all submissions.'
Expand All @@ -306,6 +295,36 @@ def handle(self, *args, **options):
files = files.filter(quarter=fiscal_quarter)
backup_file_name += f"_FY_All_{fiscal_quarter}"
continue_msg = continue_msg.format(fy="All", q=fiscal_quarter)
return files, backup_file_name, continue_msg

def handle(self, *args, **options):
"""Delete and reparse datafiles matching a query."""
fiscal_year = options.get('fiscal_year', None)
fiscal_quarter = options.get('fiscal_quarter', None)
reparse_all = options.get('all', False)
selected_files = options.get('files', None)
selected_files = [int(file) for file in selected_files[0].split(',')] if selected_files else None
new_indices = reparse_all is True

# Option that can only be specified by calling `handle` directly and passing it.
testing = options.get('testing', False)
##

args_passed = fiscal_year is not None or fiscal_quarter is not None or reparse_all or selected_files

if not args_passed:
logger.warning("No arguments supplied.")
self.print_help("manage.py", "clean_and_parse")
return

# Set up the backup file name and continue message
files, backup_file_name, continue_msg = self.get_files_to_reparse(
fiscal_year,
fiscal_quarter,
selected_files,
reparse_all)

# end of the if statement

fmt_str = "be" if new_indices else "NOT be"
continue_msg += "will {new_index} stored in new indices and the old indices ".format(new_index=fmt_str)
Expand All @@ -314,7 +333,8 @@ def handle(self, *args, **options):
fmt_str = f"ALL ({num_files})" if reparse_all else f"({num_files})"
continue_msg += "\nThese options will delete and reparse {0} datafiles.".format(fmt_str)

self._handle_input(testing, continue_msg)
if not selected_files:
self._handle_input(testing, continue_msg)

system_user, created = User.objects.get_or_create(username='system')
if created:
Expand Down
Loading