Extend and Customize¶

This package is built to be extended. You can use the Zope Component Architecture to provide specific Adapters to both control how a task is processed and to indicate which processes/logic needs to be executed asynchronously by senaite.queue. The process or logic to be handled by senaite.queue can be from either SENAITE LIMS or from any other SENAITE-specific add-on.

Queued task for a workflow action¶

Let’s imagine you have your own add-on with a custom transition/action (e.g. dispatch) in sample’s workflow, that transitions the sample to a dispatched status. The user can choose multiple samples at once from the listing and transition all them at once. This functionality might entail an undesired impact on performance, specially if hundreds of samples are selected at once.

To address this functionality, we can extend senaite.queue in our own add-on. We are not interested in replacing the logic behind such transition, but feed the queue for this action. Therefore, we can make use of the generic adapter WorkflowGenericQueueAdapter that comes by default with senaite.queue and only do the registration in configure.zcml:

<adapter
name="workflow_action_dispatch"
for="*
zope.publisher.interfaces.browser.IBrowserRequest"
permission="zope.Public" />


This is a named adapter, and the name must be the action id with workflow_action prepended. When the workflow action dispatch is triggered, the system looks for registered adapters and if a match is found, the adapter is called. Note that for field is neither context-specific nor layer specific, so this adapter will always be called when the action dispatch is triggered, regardless of context and layer.

Alternatively, you can directly feed the queue programmatically:

from senaite.queue import api


Parameter objects can be either a brain, an object, a uid or a list/tuple of any of them.

Imagine that instead of having a workflow action “dispatch” in place, you rather have a simple view from which the user can choose samples and generate a dispatch pdf from all them at once. Basically you want to feed the queue directly by your own:

class DispatchSamplesView(BrowserView):

def __call__(self):
...

# Get the selected samples from the form
uids = self.request.form.get("selected_uids", [])

params = {"uids": uids}


Note the following:

• We use a “uids” field to store the list of objects to be processed

from bika.lims import api as _api
from Products.Archetypes.interfaces.base import IBaseObject
from senaite.queue import api
from senaite.queue.queue import get_chunks_for

"""

def __init__(self, context):
self.context = context

"""Process the objects from the task
"""
# If there are too many objects to process, split them in chunks to
# prevent the task to take too much time to complete

# Process the first chunk
objects = map(_api.get_object_by_uid, chunks[0])
map(dispatch_sample, objects)

# Add remaining objects to the queue
params = {"uids": chunks[1]}

def dispatch_sample(self, sample):
"""Generates a dispatch report for this sample
"""
# Generate the pdf here
pdf = generate_dispatch_pdf(sample)

# Store the pdf as an attachment to the sample
att = _api.create(sample.aq_parent, "Attachment")
att.setAttachmentFile(open(pdf))
sample.setAttachment(att)


<adapter