Conditions | 7 |
Total Lines | 72 |
Code Lines | 42 |
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 |
||
54 | def run(self, submission_id, animal_ids): |
||
55 | """Function for batch update attribute in animals |
||
56 | Args: |
||
57 | submission_id (int): id of submission |
||
58 | animal_ids (list): set with ids to delete |
||
59 | """ |
||
60 | |||
61 | # get a submisision object |
||
62 | submission_obj = Submission.objects.get(pk=submission_id) |
||
63 | |||
64 | logger.info("Start batch delete for animals") |
||
65 | success_ids = list() |
||
66 | failed_ids = list() |
||
67 | |||
68 | for animal_id in animal_ids: |
||
69 | try: |
||
70 | name = Name.objects.get( |
||
71 | name=animal_id, submission=submission_obj) |
||
72 | |||
73 | animal_object = Animal.objects.get(name=name) |
||
74 | samples = animal_object.sample_set.all() |
||
75 | |||
76 | with transaction.atomic(): |
||
77 | for sample in samples: |
||
78 | sample_name = sample.name |
||
79 | sample.delete() |
||
80 | sample_name.delete() |
||
81 | |||
82 | logger.debug("Clearing all childs from this animal") |
||
83 | name.mother_of.clear() |
||
84 | name.father_of.clear() |
||
85 | |||
86 | # delete this animal object |
||
87 | logger.debug( |
||
88 | "Deleting animal:%s and name:%s" % ( |
||
89 | animal_object, name)) |
||
90 | animal_object.delete() |
||
91 | name.delete() |
||
92 | |||
93 | success_ids.append(animal_id) |
||
94 | |||
95 | except Name.DoesNotExist: |
||
96 | failed_ids.append(animal_id) |
||
97 | |||
98 | except Animal.DoesNotExist: |
||
99 | failed_ids.append(animal_id) |
||
100 | |||
101 | # Update submission |
||
102 | submission_obj.refresh_from_db() |
||
103 | submission_obj.status = NEED_REVISION |
||
104 | |||
105 | if len(failed_ids) != 0: |
||
106 | submission_obj.message = f"You've removed {len(success_ids)} " \ |
||
107 | f"animals. It wasn't possible to find records with these " \ |
||
108 | f"ids: {', '.join(failed_ids)}. Rerun validation please!" |
||
109 | else: |
||
110 | submission_obj.message = f"You've removed {len(success_ids)} " \ |
||
111 | f"animals. Rerun validation please!" |
||
112 | |||
113 | submission_obj.save() |
||
114 | |||
115 | summary_obj, created = ValidationSummary.objects.get_or_create( |
||
116 | submission=submission_obj, type='animal') |
||
117 | summary_obj.reset_all_count() |
||
118 | |||
119 | send_message( |
||
120 | submission_obj, construct_validation_message(submission_obj) |
||
121 | ) |
||
122 | |||
123 | logger.info("batch delete for animals completed") |
||
124 | |||
125 | return 'success' |
||
126 | |||
131 |