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

Import()   D

Complexity

Conditions 12

Size

Total Lines 61
Code Lines 50

Duplication

Lines 61
Ratio 100 %

Importance

Changes 0
Metric Value
eloc 50
dl 61
loc 61
rs 4.8
c 0
b 0
f 0
cc 12
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like bika.lims.exportimport.instruments.thermoscientific.multiskan.go.Import() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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
"""Multiskan GO Microplate Spectrophotometer
9
"""
10
from bika.lims import bikaMessageFactory as _
11
from bika.lims.utils import t
12
from . import ThermoScientificMultiskanCSVParser, ThermoScientificMultiskanImporter
13
import json
14
import traceback
15
16
title = "Thermo Scientific Multiskan - GO Microplate Spectrophotometer"
17
18
19 View Code Duplication
def Import(context, request):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
20
    """ Thermo Scientific Multiskan GO analysis results
21
    """
22
    infile = request.form['data_file']
23
    fileformat = request.form['format']
24
    artoapply = request.form['artoapply']
25
    override = request.form['override']
26
    instrument = request.form.get('instrument', None)
27
    analysis = request.form.get('analysis', None)
28
    errors = []
29
    logs = []
30
    warns = []
31
32
    # Load the most suitable parser according to file extension/options/etc...
33
    parser = None
34
    if not hasattr(infile, 'filename'):
35
        errors.append(_("No file selected"))
36
    if fileformat == 'csv':
37
        if not analysis:
38
            errors.append(_("No analysis service selected"))
39
        parser = ThermoScientificMultiskanGOCSVParser(infile, analysis)
40
    else:
41
        errors.append(t(_("Unrecognized file format ${fileformat}",
42
                          mapping={"fileformat": fileformat})))
43
44
    if parser:
45
        # Load the importer
46
        status = ['sample_received', 'attachment_due', 'to_be_verified']
47
        if artoapply == 'received':
48
            status = ['sample_received']
49
        elif artoapply == 'received_tobeverified':
50
            status = ['sample_received', 'attachment_due', 'to_be_verified']
51
52
        over = [False, False]
53
        if override == 'nooverride':
54
            over = [False, False]
55
        elif override == 'override':
56
            over = [True, False]
57
        elif override == 'overrideempty':
58
            over = [True, True]
59
60
        importer = ThermoScientificMultiskanGOImporter(parser=parser,
61
                                                       context=context,
62
                                                       allowed_ar_states=status,
63
                                                       allowed_analysis_states=None,
64
                                                       override=over,
65
                                                       instrument_uid=instrument)
66
        tbex = ''
67
        try:
68
            importer.process()
69
        except:
70
            tbex = traceback.format_exc()
71
        errors = importer.errors
72
        logs = importer.logs
73
        warns = importer.warns
74
        if tbex:
75
            errors.append(tbex)
76
77
    results = {'errors': errors, 'log': logs, 'warns': warns}
78
79
    return json.dumps(results)
80
81
82
class ThermoScientificMultiskanGOCSVParser(ThermoScientificMultiskanCSVParser):
83
84
    def getAttachmentFileType(self):
85
        return "Thermo Scientific Multiskan - GO Microplate Spectrophotometer"
86
87
88
class ThermoScientificMultiskanGOImporter(ThermoScientificMultiskanImporter):
89
90
    def getKeywordsToBeExcluded(self):
91
        return []
92