Code Duplication    Length = 73-80 lines in 2 locations

django-data/image/animals/tasks.py 1 location

@@ 23-102 (lines=80) @@
20
logger = get_task_logger(__name__)
21
22
23
class BatchDeleteAnimals(MyTask):
24
    name = "Batch delete animals"
25
    description = """Batch remove animals and associated samples"""
26
27
    # Ovverride default on failure method
28
    # This is not a failed validation for a wrong value, this is an
29
    # error in task that mean an error in coding
30
    def on_failure(self, exc, task_id, args, kwargs, einfo):
31
        logger.error('{0!r} failed: {1!r}'.format(task_id, exc))
32
33
        # get submission object
34
        submission_obj = Submission.objects.get(pk=args[0])
35
36
        # mark submission with ERROR
37
        submission_obj.status = ERROR
38
        submission_obj.message = ("Error in batch delete for animals: %s"
39
                                  % (str(exc)))
40
        submission_obj.save()
41
42
        send_message(submission_obj)
43
44
        # send a mail to the user with the stacktrace (einfo)
45
        submission_obj.owner.email_user(
46
            "Error in batch delete for animals: %s" % (args[0]),
47
            ("Something goes wrong in batch delete for animals. Please report "
48
             "this to InjectTool team\n\n %s" % str(einfo)),
49
        )
50
51
        # TODO: submit mail to admin
52
53
    def run(self, submission_id, animal_ids):
54
        """Function for batch update attribute in animals
55
        Args:
56
            submission_id (int): id of submission
57
            animal_ids (list): set with ids to delete
58
        """
59
60
        logger.info("Start batch delete for animals")
61
        success_ids = list()
62
        failed_ids = list()
63
        for animal_id in animal_ids:
64
            try:
65
                name = Name.objects.get(name=animal_id)
66
                animal_object = Animal.objects.get(name=name)
67
                samples = animal_object.sample_set.all()
68
                with transaction.atomic():
69
                    for sample in samples:
70
                        sample_name = sample.name
71
                        sample.delete()
72
                        sample_name.delete()
73
                    name.mother_of.clear()
74
                    name.father_of.clear()
75
                    animal_object.delete()
76
                    name.delete()
77
                success_ids.append(animal_id)
78
            except Name.DoesNotExist:
79
                failed_ids.append(animal_id)
80
            except Animal.DoesNotExist:
81
                failed_ids.append(animal_id)
82
        # Update submission
83
        submission_obj = Submission.objects.get(pk=submission_id)
84
        submission_obj.status = NEED_REVISION
85
        if len(failed_ids) != 0:
86
            submission_obj.message = f"You've removed {len(success_ids)} " \
87
                f"animals. It wasn't possible to find records with these ids: " \
88
                f"{', '.join(failed_ids)}. Rerun validation please!"
89
        else:
90
            submission_obj.message = f"You've removed {len(success_ids)} " \
91
                f"animals. Rerun validation please!"
92
        submission_obj.save()
93
94
        summary_obj, created = ValidationSummary.objects.get_or_create(
95
            submission=submission_obj, type='animal')
96
        summary_obj.reset_all_count()
97
98
        send_message(
99
            submission_obj, construct_validation_message(submission_obj)
100
        )
101
102
        return 'success'
103
104
105
# register explicitly tasks

django-data/image/samples/tasks.py 1 location

@@ 23-95 (lines=73) @@
20
logger = get_task_logger(__name__)
21
22
23
class BatchDeleteSamples(MyTask):
24
    name = "Batch delete samples"
25
    description = """Batch remove samples"""
26
27
    # Ovverride default on failure method
28
    # This is not a failed validation for a wrong value, this is an
29
    # error in task that mean an error in coding
30
    def on_failure(self, exc, task_id, args, kwargs, einfo):
31
        logger.error('{0!r} failed: {1!r}'.format(task_id, exc))
32
33
        # get submission object
34
        submission_obj = Submission.objects.get(pk=args[0])
35
36
        # mark submission with ERROR
37
        submission_obj.status = ERROR
38
        submission_obj.message = ("Error in batch delete for samples: %s"
39
                                  % (str(exc)))
40
        submission_obj.save()
41
42
        send_message(submission_obj)
43
44
        # send a mail to the user with the stacktrace (einfo)
45
        submission_obj.owner.email_user(
46
            "Error in batch delete for samples: %s" % (args[0]),
47
            ("Something goes wrong in batch delete for samples. Please report "
48
             "this to InjectTool team\n\n %s" % str(einfo)),
49
        )
50
51
        # TODO: submit mail to admin
52
53
    def run(self, submission_id, sample_ids):
54
        """Function for batch update attribute in animals
55
        Args:
56
            submission_id (int): id of submission
57
            sample_ids (list): set with ids to delete
58
        """
59
60
        logger.info("Start batch delete for samples")
61
        success_ids = list()
62
        failed_ids = list()
63
        for sample_id in sample_ids:
64
            try:
65
                name = Name.objects.get(name=sample_id)
66
                object = Sample.objects.get(name=name)
67
                with transaction.atomic():
68
                    object.delete()
69
                    name.delete()
70
                success_ids.append(sample_id)
71
            except Name.DoesNotExist:
72
                failed_ids.append(sample_id)
73
            except Sample.DoesNotExist:
74
                failed_ids.append(sample_id)
75
        # Update submission
76
        submission_obj = Submission.objects.get(pk=submission_id)
77
        submission_obj.status = NEED_REVISION
78
        if len(failed_ids) != 0:
79
            submission_obj.message = f"You've removed {len(success_ids)} " \
80
                f"samples. It wasn't possible to find records with these ids:" \
81
                f" {', '.join(failed_ids)}. Rerun validation please!"
82
        else:
83
            submission_obj.message = f"You've removed {len(success_ids)} " \
84
                f"samples. Rerun validation please!"
85
        submission_obj.save()
86
87
        summary_obj, created = ValidationSummary.objects.get_or_create(
88
            submission=submission_obj, type='sample')
89
        summary_obj.reset_all_count()
90
91
        send_message(
92
            submission_obj, construct_validation_message(submission_obj)
93
        )
94
95
        return 'success'
96
97
98
# register explicitly tasks