| 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 |
||
| 63 | def run(self, submission_id, animal_ids): |
||
| 64 | """Function for batch update attribute in animals |
||
| 65 | Args: |
||
| 66 | submission_id (int): id of submission |
||
| 67 | animal_ids (list): set with ids to delete |
||
| 68 | """ |
||
| 69 | |||
| 70 | # get a submisision object |
||
| 71 | submission_obj = Submission.objects.get(pk=submission_id) |
||
| 72 | |||
| 73 | logger.info("Start batch delete for animals") |
||
| 74 | success_ids = list() |
||
| 75 | failed_ids = list() |
||
| 76 | |||
| 77 | for animal_id in animal_ids: |
||
| 78 | try: |
||
| 79 | name = Name.objects.get( |
||
| 80 | name=animal_id, submission=submission_obj) |
||
| 81 | |||
| 82 | animal_object = Animal.objects.get(name=name) |
||
| 83 | samples = animal_object.sample_set.all() |
||
| 84 | |||
| 85 | with transaction.atomic(): |
||
| 86 | for sample in samples: |
||
| 87 | sample_name = sample.name |
||
| 88 | sample.delete() |
||
| 89 | sample_name.delete() |
||
| 90 | |||
| 91 | logger.debug("Clearing all childs from this animal") |
||
| 92 | name.mother_of.clear() |
||
| 93 | name.father_of.clear() |
||
| 94 | |||
| 95 | # delete this animal object |
||
| 96 | logger.debug( |
||
| 97 | "Deleting animal:%s and name:%s" % ( |
||
| 98 | animal_object, name)) |
||
| 99 | animal_object.delete() |
||
| 100 | name.delete() |
||
| 101 | |||
| 102 | success_ids.append(animal_id) |
||
| 103 | |||
| 104 | except Name.DoesNotExist: |
||
| 105 | failed_ids.append(animal_id) |
||
| 106 | |||
| 107 | except Animal.DoesNotExist: |
||
| 108 | failed_ids.append(animal_id) |
||
| 109 | |||
| 110 | # Update submission |
||
| 111 | submission_obj.refresh_from_db() |
||
| 112 | submission_obj.status = NEED_REVISION |
||
| 113 | |||
| 114 | if len(failed_ids) != 0: |
||
| 115 | submission_obj.message = f"You've removed {len(success_ids)} " \ |
||
| 116 | f"animals. It wasn't possible to find records with these " \ |
||
| 117 | f"ids: {', '.join(failed_ids)}. Rerun validation please!" |
||
| 118 | else: |
||
| 119 | submission_obj.message = f"You've removed {len(success_ids)} " \ |
||
| 120 | f"animals. Rerun validation please!" |
||
| 121 | |||
| 122 | submission_obj.save() |
||
| 123 | |||
| 124 | summary_obj, created = ValidationSummary.objects.get_or_create( |
||
| 125 | submission=submission_obj, type='animal') |
||
| 126 | summary_obj.reset() |
||
| 127 | |||
| 128 | # after removing animal associated samples, we need to update also |
||
| 129 | # sample all count |
||
| 130 | summary_obj, created = ValidationSummary.objects.get_or_create( |
||
| 131 | submission=submission_obj, type='sample') |
||
| 132 | summary_obj.reset() |
||
| 133 | |||
| 134 | # TODO: validation summary could be updated relying database, instead |
||
| 135 | # doing validation. Define a method in validation.helpers to update |
||
| 136 | # summary relying only on database |
||
| 137 | |||
| 138 | send_message( |
||
| 139 | submission_obj, construct_validation_message(submission_obj) |
||
| 140 | ) |
||
| 141 | |||
| 142 | logger.info("batch delete for animals completed") |
||
| 143 | |||
| 144 | return 'success' |
||
| 145 | |||
| 198 |