Conditions | 7 |
Total Lines | 82 |
Code Lines | 45 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | #!/usr/bin/env python3 |
||
32 | def run(self, submission_id, animal_ids): |
||
33 | """Function for batch update attribute in animals |
||
34 | Args: |
||
35 | submission_id (int): id of submission |
||
36 | animal_ids (list): set with ids to delete |
||
37 | """ |
||
38 | |||
39 | # get a submisision object |
||
40 | submission_obj = Submission.objects.get(pk=submission_id) |
||
41 | |||
42 | logger.info("Start batch delete for animals") |
||
43 | success_ids = list() |
||
44 | failed_ids = list() |
||
45 | |||
46 | for animal_id in animal_ids: |
||
47 | try: |
||
48 | name = Name.objects.get( |
||
49 | name=animal_id, submission=submission_obj) |
||
50 | |||
51 | animal_object = Animal.objects.get(name=name) |
||
52 | samples = animal_object.sample_set.all() |
||
53 | |||
54 | with transaction.atomic(): |
||
55 | for sample in samples: |
||
56 | sample_name = sample.name |
||
57 | sample.delete() |
||
58 | sample_name.delete() |
||
59 | |||
60 | logger.debug("Clearing all childs from this animal") |
||
61 | name.mother_of.clear() |
||
62 | name.father_of.clear() |
||
63 | |||
64 | # delete this animal object |
||
65 | logger.debug( |
||
66 | "Deleting animal:%s and name:%s" % ( |
||
67 | animal_object, name)) |
||
68 | animal_object.delete() |
||
69 | name.delete() |
||
70 | |||
71 | success_ids.append(animal_id) |
||
72 | |||
73 | except Name.DoesNotExist: |
||
74 | failed_ids.append(animal_id) |
||
75 | |||
76 | except Animal.DoesNotExist: |
||
77 | failed_ids.append(animal_id) |
||
78 | |||
79 | # Update submission |
||
80 | submission_obj.refresh_from_db() |
||
81 | submission_obj.status = NEED_REVISION |
||
82 | |||
83 | if len(failed_ids) != 0: |
||
84 | submission_obj.message = f"You've removed {len(success_ids)} " \ |
||
85 | f"animals. It wasn't possible to find records with these " \ |
||
86 | f"ids: {', '.join(failed_ids)}. Rerun validation please!" |
||
87 | else: |
||
88 | submission_obj.message = f"You've removed {len(success_ids)} " \ |
||
89 | f"animals. Rerun validation please!" |
||
90 | |||
91 | submission_obj.save() |
||
92 | |||
93 | summary_obj, created = ValidationSummary.objects.get_or_create( |
||
94 | submission=submission_obj, type='animal') |
||
95 | summary_obj.reset() |
||
96 | |||
97 | # after removing animal associated samples, we need to update also |
||
98 | # sample all count |
||
99 | summary_obj, created = ValidationSummary.objects.get_or_create( |
||
100 | submission=submission_obj, type='sample') |
||
101 | summary_obj.reset() |
||
102 | |||
103 | # TODO: validation summary could be updated relying database, instead |
||
104 | # doing validation. Define a method in validation.helpers to update |
||
105 | # summary relying only on database |
||
106 | |||
107 | send_message( |
||
108 | submission_obj, construct_validation_message(submission_obj) |
||
109 | ) |
||
110 | |||
111 | logger.info("batch delete for animals completed") |
||
112 | |||
113 | return 'success' |
||
114 | |||
143 |