Completed
Branch master (9edffc)
by Jordi
04:36
created

bika.lims.exportimport.instruments.instrument   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 15
eloc 59
dl 0
loc 88
rs 10
c 0
b 0
f 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A getFileFormat() 0 2 1
A getResultsInputFile() 0 2 1
C GenericImport() 0 53 11
A format_keyword() 0 12 2
1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of SENAITE.CORE
4
#
5
# Copyright 2018 by it's authors.
6
# Some rights reserved. See LICENSE.rst, CONTRIBUTORS.rst.
7
8
""" Generic controller for instrument results import view
9
"""
10
import json
11
import traceback
12
from bika.lims.exportimport.instruments.resultsimport import AnalysisResultsImporter
13
14
def getFileFormat(request):
15
    return request.form['instrument_results_file_format']
16
17
def getResultsInputFile(request):
18
    return request.form['instrument_results_file']
19
20
21
def GenericImport(context, request, parser, importer=None):
22
    infile = getResultsInputFile(request)
23
    fileformat = getFileFormat(request)
24
    artoapply = request.form['artoapply']
25
    override = request.form['results_override']
26
27
    instrument = request.form.get('instrument', None)
28
    errors = []
29
    logs = []
30
    warns = []
31
    # Load the most suitable parser according to file extension/options/etc...
32
    if not hasattr(infile, 'filename'):
33
        errors.append(_("No file selected"))
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable '_'
Loading history...
Comprehensibility Best Practice introduced by
The variable _ does not seem to be defined.
Loading history...
34
35
    if parser:
36
        # Load the importer
37
        status = ['sample_received', 'attachment_due', 'to_be_verified']
38
        if artoapply == 'received':
39
            status = ['sample_received']
40
        elif artoapply == 'received_tobeverified':
41
            status = ['sample_received', 'attachment_due', 'to_be_verified']
42
43
        over = [False, False]
44
        if override == 'nooverride':
45
            over = [False, False]
46
        elif override == 'override':
47
            over = [True, False]
48
        elif override == 'overrideempty':
49
            over = [True, True]
50
51
        imp = importer
52
        if not imp:
53
            imp = AnalysisResultsImporter(parser=parser,
54
                                          context=context,
55
                                          allowed_ar_states=status,
56
                                          allowed_analysis_states=None,
57
                                          override=over,
58
                                          instrument_uid=instrument)
59
60
        tbex = ''
61
        try:
62
            imp.process()
63
        except:
64
            tbex = traceback.format_exc()
65
        errors = imp.errors
66
        logs = imp.logs
67
        warns = imp.warns
68
        if tbex:
69
            errors.append(tbex)
70
71
    results = {'errors': errors, 'log': logs, 'warns': warns}
72
73
    return json.dumps(results)
74
75
76
def format_keyword(keyword):
77
    """
78
    Removing special character from a keyword. Analysis Services must have
79
    this kind of keywords. E.g. if assay name from the Instrument is
80
    'HIV-1 2.0', an AS must be created on Bika with the keyword 'HIV120'
81
    """
82
    import re
83
    result = ''
84
    if keyword:
85
        result = re.sub(r"\W", "", keyword)
86
        result = re.sub("_", "", result)
87
    return result
88