forked from botswana-harvard/edc-pharmacy
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
integrate celery into repack request processing
- Loading branch information
Showing
21 changed files
with
537 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,74 @@ | ||
from __future__ import annotations | ||
|
||
from celery import current_app | ||
from django.contrib import admin, messages | ||
from django.http import HttpResponseRedirect | ||
from django.urls import reverse | ||
from django.utils.translation import gettext | ||
from django.utils.html import format_html | ||
from edc_utils.celery import run_task_sync_or_async | ||
|
||
from ...utils import process_repack_request | ||
from ...tasks.process_repack_request import process_repack_request_queryset | ||
|
||
|
||
@admin.action(description="Process repack request") | ||
def process_repack_request_action(modeladmin, request, queryset): | ||
if queryset.count() > 1 or queryset.count() == 0: | ||
"""Action to process repack request. | ||
Redirects to process_repack_request. | ||
If celery is running, will run through the entire queryset otherwise | ||
just the first instance in the queryset. | ||
""" | ||
repack_request_pks = [obj.pk for obj in queryset] | ||
|
||
# if celery is not running, just keep the first pk | ||
i = current_app.control.inspect() | ||
if not i.active(): | ||
repack_request_pks = repack_request_pks[:1] | ||
|
||
# run task / func and update or clear the task_id | ||
task = run_task_sync_or_async( | ||
process_repack_request_queryset, repack_request_pks=repack_request_pks | ||
) | ||
task_id = getattr(task, "id", None) | ||
queryset.update(task_id=task_id) | ||
|
||
# add messages for user | ||
messages.add_message( | ||
request, | ||
messages.SUCCESS, | ||
format_html( | ||
"Repack request submitted. <BR>Next, go to the ACTION menu below and " | ||
"(1)`Print labels`. Then (2) Label your stock containers with the printed labels. " | ||
"Once all stock is labelled, go to the ACTION menu below and " | ||
"(3) Select `Confirm repacked and labelled stock`. " | ||
f"Scan in the labels to CONFIRM the stock. ({task_id})" | ||
), | ||
) | ||
if task_id: | ||
messages.add_message( | ||
request, | ||
messages.ERROR, | ||
gettext("Select one and only one item"), | ||
messages.INFO, | ||
f"Task {task_id} is processing your repack requests.", | ||
) | ||
else: | ||
repack_obj = queryset.first() | ||
if repack_obj.processed: | ||
messages.add_message( | ||
request, messages.ERROR, "Nothing to do. Repack request already processed" | ||
) | ||
else: | ||
process_repack_request(repack_obj) | ||
url = reverse("edc_pharmacy_admin:edc_pharmacy_repackrequest_changelist") | ||
url = f"{url}?q={repack_obj.from_stock.code}" | ||
return HttpResponseRedirect(url) | ||
return None | ||
repack_request = queryset.first() | ||
messages.add_message( | ||
request, | ||
messages.INFO, | ||
( | ||
f"Processed only 1 of {queryset.count()} repack requests selected. " | ||
f"See {repack_request}." | ||
), | ||
) | ||
messages.add_message( | ||
request, messages.ERROR, "Task workers not running. Contact data management." | ||
) | ||
|
||
# redirect to changelist | ||
url = reverse("edc_pharmacy_admin:edc_pharmacy_repackrequest_changelist") | ||
if queryset.count() == 1: | ||
repack_request = queryset.first() | ||
url = f"{url}?q={repack_request.from_stock.code}" | ||
return HttpResponseRedirect(url) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
edc_pharmacy/migrations/0027_rename_at_location_historicalstock_transferred_and_more.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Generated by Django 5.1.2 on 2024-11-15 14:46 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("edc_pharmacy", "0026_historicalstockrequest_cutoff_datetime_and_more"), | ||
] | ||
|
||
operations = [ | ||
migrations.RenameField( | ||
model_name="historicalstock", | ||
old_name="at_location", | ||
new_name="transferred", | ||
), | ||
migrations.RenameField( | ||
model_name="stock", | ||
old_name="at_location", | ||
new_name="transferred", | ||
), | ||
] |
21 changes: 21 additions & 0 deletions
21
edc_pharmacy/migrations/0028_remove_historicalstock_transferred_and_more.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Generated by Django 5.1.2 on 2024-11-15 14:54 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("edc_pharmacy", "0027_rename_at_location_historicalstock_transferred_and_more"), | ||
] | ||
|
||
operations = [ | ||
migrations.RemoveField( | ||
model_name="historicalstock", | ||
name="transferred", | ||
), | ||
migrations.RemoveField( | ||
model_name="stock", | ||
name="transferred", | ||
), | ||
] |
Oops, something went wrong.