Completed
Pull Request — devel (#90)
by Paolo
06:18
created

Command.handle()   B

Complexity

Conditions 5

Size

Total Lines 52
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 52
rs 8.7893
c 0
b 0
f 0
cc 5
nop 3

How to fix   Long Method   

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:

1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
"""
4
Created on Tue May 26 17:21:28 2020
5
6
@author: Paolo Cozzi <[email protected]>
7
"""
8
9
import logging
10
11
from pyUSIrest.usi import Root
12
from pyUSIrest.exceptions import USIDataError
13
14
from django.core.management import BaseCommand
15
16
from biosample.helpers import get_manager_auth
17
from biosample.models import OrphanSubmission
18
from biosample.tasks.cleanup import get_orphan_samples
19
from common.constants import SUBMITTED
20
21
# Get an instance of a logger
22
logger = logging.getLogger(__name__)
23
24
25
def create_biosample_submission(root, team):
26
    logger.debug("Creating a new submission")
27
28
    usi_team = root.get_team_by_name(team.name)
29
    usi_submission = usi_team.create_submission()
30
31
    submission = OrphanSubmission(
32
        usi_submission_name=usi_submission.name)
33
    submission.save()
34
35
    logger.debug("Created submission %s" % submission)
36
    return usi_submission, submission
37
38
39
def update_submission_status(submission):
40
    """Set SUBMITTED status to submission"""
41
42
    # update submission status
43
    if submission:
44
        submission.status = SUBMITTED
45
        submission.save()
46
47
48
class Command(BaseCommand):
49
    help = 'Get a JSON for biosample submission'
50
51
    def handle(self, *args, **options):
52
        # call commands and fill tables.
53
        logger.info("Called patch_orphan_samples")
54
55
        # get a new auth object
56
        auth = get_manager_auth()
57
58
        # get a new root object
59
        root = Root(auth)
60
61
        # some variables
62
        count = 1
63
        old_team = None
64
        usi_submission = None
65
        submission = None
66
67
        # iterate among orphan sample, create a BioSample submission
68
        # and then add biosample for patch
69
        for orphan_sample in get_orphan_samples():
70
            data, team = orphan_sample['data'], orphan_sample['team']
71
72
            if count % 100 == 0 or old_team != team:
73
                update_submission_status(submission)
74
75
                # create a new Biosample submission
76
                usi_submission, submission = create_biosample_submission(
77
                    root, team)
78
79
                # reset count and old_team
80
                count = 1
81
                old_team = team
82
83
            # add sample to submission
84
            try:
85
                usi_submission.create_sample(data)
86
87
            except USIDataError:
88
                logger.error("Can't remove %s" % data)
89
                continue
90
91
            # update submission count
92
            submission.samples_count = count
93
            submission.save()
94
95
            # new element
96
            count += 1
97
98
        # update the last submission status
99
        update_submission_status(submission)
100
101
        # end the script
102
        logger.info("patch_orphan_samples ended")
103