Skip to content

Commit

Permalink
move admin action to celery task
Browse files Browse the repository at this point in the history
  • Loading branch information
jtimpe committed Oct 9, 2024
1 parent 3355b97 commit 0820878
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
9 changes: 6 additions & 3 deletions tdrs-backend/tdpservice/data_files/admin/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
from django.utils.html import format_html
from datetime import datetime, timedelta, timezone
from django.shortcuts import redirect
from django.core.management import call_command
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 Down Expand Up @@ -40,8 +40,8 @@ class Media:
def reparse(self, request, queryset):
"""Reparse the selected data files."""
files = queryset.values_list("id", flat=True)
file_ids = ",".join(map(str, files))
call_command("clean_and_reparse", f"-f {file_ids}")
reparse_files.delay(list(files))

self.message_user(
request,
ngettext(
Expand All @@ -57,9 +57,12 @@ def reparse(self, request, queryset):
def get_actions(self, request):
"""Return the actions."""
actions = super().get_actions(request)
print('get actions')
if not request.user.groups.filter(name__in=["OFA System Admin", "OFA Admin"]).exists():
print('not allowed')
actions.pop("reparse", None)
else:
print('allowed')
if "reparse" not in actions:
actions["reparse"] = (self.reparse, "reparse", "Reparse selected data files)")
return actions
Expand Down
7 changes: 7 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,9 @@ def notify_stuck_files():
).values_list('username', flat=True).distinct()

send_stuck_file_email(stuck_files, recipients)


@shared_task
def reparse_files(file_ids):
file_ids_str = ",".join(map(str, file_ids))
call_command("clean_and_reparse", f"-f {file_ids_str}")

0 comments on commit 0820878

Please sign in to comment.