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

Import()   D

Complexity

Conditions 12

Size

Total Lines 63
Code Lines 50

Duplication

Lines 63
Ratio 100 %

Importance

Changes 0
Metric Value
eloc 50
dl 63
loc 63
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.lifetechnologies.qubit.qubit.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
""" Life Technologies Qubit
9
"""
10
from bika.lims import bikaMessageFactory as _
11
from bika.lims.utils import t
12
from . import QuBitCSVParser, QuBitImporter
13
import json
14
import traceback
15
16
title = "Life Technolgies - Qubit"
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
    """ Read qubit analysis results
21
    """
22
    infile = request.form['filename']
23
    fileformat = request.form['format']
24
    artoapply = request.form['artoapply']
25
    override = request.form['override']
26
27
    instrument = request.form.get('instrument', None)
28
    errors = []
29
    logs = []
30
    warns = []
31
32
    # Load the suitable parser
33
    parser = None
34
    if not hasattr(infile, 'filename'):
35
        errors.append(_("No file selected"))
36
    elif fileformat == 'csv':
37
        analysis = request.form.get('analysis', None)
38
        if analysis:
39
            parser = QuBitCSVParser(infile, analysis)
40
        else:
41
            errors.append(t(_("No analysis selected")))
42
    else:
43
        errors.append(t(_("Unrecognized file format ${fileformat}",
44
                          mapping={"fileformat": fileformat})))
45
46
    if parser:
47
        # Load the importer
48
        status = ['sample_received', 'attachment_due', 'to_be_verified']
49
        if artoapply == 'received':
50
            status = ['sample_received']
51
        elif artoapply == 'received_tobeverified':
52
            status = ['sample_received', 'attachment_due', 'to_be_verified']
53
54
        over = [False, False]
55
        if override == 'nooverride':
56
            over = [False, False]
57
        elif override == 'override':
58
            over = [True, False]
59
        elif override == 'overrideempty':
60
            over = [True, True]
61
62
        importer = QuBitImporter(parser=parser,
63
                                 context=context,
64
                                 allowed_ar_states=status,
65
                                 allowed_analysis_states=None,
66
                                 override=over,
67
                                 instrument_uid=instrument)
68
69
        tbex = ''
70
        try:
71
            importer.process()
72
        except:
73
            tbex = traceback.format_exc()
74
        errors = importer.errors
75
        logs = importer.logs
76
        warns = importer.warns
77
        if tbex:
78
            errors.append(tbex)
79
80
    results = {'errors': errors, 'log': logs, 'warns': warns}
81
    return json.dumps(results)
82