Skip to content

Conversation

Dhanus3133
Copy link
Member

@Dhanus3133 Dhanus3133 commented Sep 8, 2025

Closes #608

Copy link
Member

@nemesifier nemesifier left a comment

Choose a reason for hiding this comment

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

We strive to follow the pattern of fat models, lean views.
Admin, REST API, Celery tasks are views of the business logic.
We prefer to group key logic as this in either models or separate classes.

try:
batch = RadiusBatch.objects.get(pk=batch_id)
batch.status = "processing"
batch.save(update_fields=["status"])
Copy link
Member

Choose a reason for hiding this comment

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

Let's move this logic as a class method and keep this file lean.

RadiusAccounting.objects.bulk_update(updated_sessions, fields=["groupname"])


@shared_task
Copy link
Member

Choose a reason for hiding this comment

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

ASGI_APPLICATION = "openwisp2.routing.application"

if TESTING:
CHANNEL_LAYERS = {"default": {"BACKEND": "channels.layers.InMemoryChannelLayer"}}
Copy link
Member

Choose a reason for hiding this comment

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

Are we sure this wouldn't cause issues with selenium tests?

Copy link
Member

@nemesifier nemesifier left a comment

Choose a reason for hiding this comment

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

So far it seems to almost meet all requirements, I found one blunder regarding the notification not targeting the right object and a few details to improve. See below.

Notification points to org instead of batch object

I created a batch and called it "AsyncTest".

The notification looks like:

The batch user creation "[testing](http://localhost:8000/admin/openwisp_users/organization/51689c38-7a02-4609-9326-a79e1e7f8b1d/change/)" was successfully processed.

And points to the organization.

It should instead point to the Batch User Creation object.

New batch: hide status

The status line doesn't provide any value here and it would be better to hide it either with python code or CSS.

Image

Existing batch

Current batch details page:

Image

I think we should show the status field somewhere on top.

I am also not sure about the line "Completed: This batch operation has completed successfully." provides any real value.

I will leave more comments below about both.

Copy link
Member

@nemesifier nemesifier left a comment

Choose a reason for hiding this comment

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

There's a conflict with master.

@Dhanus3133
Copy link
Member Author

There's a conflict with master.

Resolved it.

@coveralls
Copy link

coveralls commented Sep 29, 2025

Coverage Status

coverage: 97.185% (-1.5%) from 98.713%
when pulling 95e378a on feat/batch-creation-async
into 340a64e on master.

Copy link
Member

@nemesifier nemesifier left a comment

Choose a reason for hiding this comment

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

The status field in the admin change page was still being shown as last, so I fixed this in 6f5a12a (and pushed some other minor improvements).

During testing I found another minor issue I didn't see before, but it should be quick to fix:
when using the API to create a batch operation, the pdf_link in the 202 response refers to an empty PDF (since the batch hasn't been processed yet).

We need to change the get_pdf_link method to return the link only when the status is completed and add a test for this, I think we can add a subTest in the existing test_api_batch_pdf_link.

See my suggestions below to improve the readability and maintenability of the code.

The rest looks good so we can merge after this round of improvements.


batch.refresh_from_db()
response_serializer = self.get_serializer(batch)

Copy link
Member

Choose a reason for hiding this comment

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

Please remove these blank lines as they don't provide any value.

return Response(response.data, status=status.HTTP_201_CREATED)
return Response(
response_serializer.data, status=status.HTTP_201_CREATED
)
Copy link
Member

Choose a reason for hiding this comment

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

Let's reduce repetition:

status_code = status.HTTP_202_ACCEPTED if is_async else status.HTTP_201_CREATED
return Response(
    response_serializer.data, status=status_code
)

items_to_process = app_settings.BATCH_ASYNC_THRESHOLD

is_async = items_to_process >= app_settings.BATCH_ASYNC_THRESHOLD

Copy link
Member

Choose a reason for hiding this comment

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

Too many blank lines.

Suggested change

notify.send(
type="generic_message",
level="error",
message=_(f"The batch creation operation for '{self.name}' failed."),
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
message=_(f"The batch creation operation for '{self.name}' failed."),
message=_(f"The batch creation operation for \"{self.name}\" failed."),

type="generic_message",
level="success",
message=_(
f"The batch creation operation for '{self.name}' "
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
f"The batch creation operation for '{self.name}' "
f"The batch creation operation for \"{self.name}\" "

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[feature/change] Allow running batch creation operations asynchronously
3 participants