Skip to content

Commit

Permalink
Started on auto importer for timeseries "instrument"
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejmets committed Nov 20, 2024
1 parent 2fba87b commit 81370dc
Showing 1 changed file with 57 additions and 20 deletions.
77 changes: 57 additions & 20 deletions src/senaite/timeseries/importer/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from bika.lims import bikaMessageFactory as _
from senaite.core.exportimport.instruments import IInstrumentAutoImportInterface
from senaite.core.exportimport.instruments import IInstrumentImportInterface
from senaite.core.exportimport.instruments.importer import ALLOWED_ANALYSIS_STATES
from senaite.core.exportimport.instruments.importer import ALLOWED_SAMPLE_STATES
from senaite.core.exportimport.instruments.importer import AnalysisResultsImporter
from bika.lims.utils import t
from senaite.instruments.instrument import InstrumentXLSResultsFileParser
Expand Down Expand Up @@ -141,36 +143,52 @@ class timeseries_import(object):
def __init__(self, context):
self.context = context
self.request = None
self.allowed_sample_states = ALLOWED_SAMPLE_STATES
self.allowed_analysis_states = ALLOWED_ANALYSIS_STATES

def Import(self, context, request):
"""Import Form"""
infile = request.form["instrument_results_file"]
fileformat = request.form.get("instrument_results_file_format", "xlsx")
artoapply = request.form.get("artoapply")
override = request.form.get("results_override")
instrument_uid = request.form.get("instrument")
worksheet = int(request.form.get("worksheet", "2"))
if request is not None:
infile = request.form["instrument_results_file"]
fileformat = request.form.get("instrument_results_file_format", "xlsx")
artoapply = request.form.get("artoapply")
override = request.form.get("results_override")
instrument_uid = request.form.get("instrument")
worksheet = int(request.form.get("worksheet", "2"))
else:
# Auto_importer hack
artoapply = "received_tobeverified"
override = "overrideempty"
worksheet = 2

errors = []
logs = []
warns = []

# Load the most suitable parser according to file extension/options/etc...
parser = None
if not hasattr(infile, "filename"):
errors.append(_("No file selected"))
if fileformat in ("xls", "xlsx"):
parser = TimeSeriesParser(
infile, worksheet, encoding=fileformat, instrument_uid=instrument_uid
)
if hasattr(self, "parser"):
# Auto improt hack
parser = self.parser
else:
errors.append(
t(
_(
"Unrecognized file format ${fileformat}",
mapping={"fileformat": fileformat},
# Load the most suitable parser according to file extension/options/etc...
parser = None
if not hasattr(infile, "filename"):
errors.append(_("No file selected"))
if fileformat in ("xls", "xlsx"):
parser = TimeSeriesParser(
infile,
worksheet,
encoding=fileformat,
instrument_uid=instrument_uid,
)
else:
errors.append(
t(
_(
"Unrecognized file format ${fileformat}",
mapping={"fileformat": fileformat},
)
)
)
)

if parser:
# Load the importer
Expand Down Expand Up @@ -209,3 +227,22 @@ def Import(self, context, request):
results = {"errors": errors, "log": logs, "warns": warns}

return json.dumps(results)

def get_automatic_importer(self, instrument, parser, **kw):
"""Called during automated results import"""
# initialize the base class with the required parameters
self.parser = parser
return self

def get_automatic_parser(self, infile):
"""Called during automated results import
Returns the parser to be used by default for the file passed in when
automatic results import for this instrument interface is enabled
"""
return TimeSeriesParser(infile, encoding="xlsx")

def process(self):
results = self.Import(self.context, request=None)
import pdb; pdb.set_trace() # fmt: skip
return results

0 comments on commit 81370dc

Please sign in to comment.