Completed
Pull Request — master (#41)
by
unknown
06:35
created

samples.tasks   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 71
Duplicated Lines 63.38 %

Importance

Changes 0
Metric Value
wmc 2
eloc 31
dl 45
loc 71
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A BatchUpdateSamples.on_failure() 19 19 1
A BatchUpdateSamples.run() 12 12 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
"""
4
Created on Wed Feb 27 16:38:37 2019
5
6
@author: Paolo Cozzi <[email protected]>
7
"""
8
9
from celery.utils.log import get_task_logger
10
11
from common.constants import ERROR
12
from common.tasks import BatchUpdateMixin
13
from image.celery import app as celery_app, MyTask
14
from image_app.models import Submission, Sample
15
from submissions.helpers import send_message
16
17
# Get an instance of a logger
18
logger = get_task_logger(__name__)
19
20
21 View Code Duplication
class BatchUpdateSamples(MyTask, BatchUpdateMixin):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
22
    name = "Batch update samples"
23
    description = """Batch update of field in samples"""
24
25
    item_cls = Sample
26
    submission_cls = Submission
27
28
    # Ovverride default on failure method
29
    # This is not a failed validation for a wrong value, this is an
30
    # error in task that mean an error in coding
31
    def on_failure(self, exc, task_id, args, kwargs, einfo):
32
        logger.error('{0!r} failed: {1!r}'.format(task_id, exc))
33
34
        # get submission object
35
        submission_obj = Submission.objects.get(pk=args[0])
36
37
        # mark submission with ERROR
38
        submission_obj.status = ERROR
39
        submission_obj.message = ("Error in batch update for samples: %s"
40
                                  % (str(exc)))
41
        submission_obj.save()
42
43
        send_message(submission_obj)
44
45
        # send a mail to the user with the stacktrace (einfo)
46
        submission_obj.owner.email_user(
47
            "Error in batch update for samples: %s" % (args[0]),
48
            ("Something goes wrong in batch update for samples. Please report "
49
             "this to InjectTool team\n\n %s" % str(einfo)),
50
        )
51
52
        # TODO: submit mail to admin
53
54
    def run(self, submission_id, sample_ids, attribute):
55
        """Function for batch update attribute in samples
56
        Args:
57
            submission_id (int): id of submission
58
            sample_ids (dict): dict with id and values to update
59
            attribute (str): attribute to update
60
        """
61
62
        logger.info("Start batch update for samples")
63
        super(BatchUpdateSamples, self).batch_update(submission_id, sample_ids,
64
                                                     attribute)
65
        return 'success'
66
67
68
# register explicitly tasks
69
# https://github.com/celery/celery/issues/3744#issuecomment-271366923
70
celery_app.tasks.register(BatchUpdateSamples)
71