| Conditions | 6 |
| Total Lines | 57 |
| Code Lines | 31 |
| 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, sample_ids): |
||
| 64 | """Function for batch update attribute in animals |
||
| 65 | Args: |
||
| 66 | submission_id (int): id of submission |
||
| 67 | sample_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 samples") |
||
| 74 | success_ids = list() |
||
| 75 | failed_ids = list() |
||
| 76 | |||
| 77 | for sample_id in sample_ids: |
||
| 78 | try: |
||
| 79 | name = Name.objects.get( |
||
| 80 | name=sample_id, submission=submission_obj) |
||
| 81 | |||
| 82 | sample_obj = Sample.objects.get(name=name) |
||
| 83 | |||
| 84 | with transaction.atomic(): |
||
| 85 | sample_obj.delete() |
||
| 86 | name.delete() |
||
| 87 | success_ids.append(sample_id) |
||
| 88 | |||
| 89 | except Name.DoesNotExist: |
||
| 90 | failed_ids.append(sample_id) |
||
| 91 | |||
| 92 | except Sample.DoesNotExist: |
||
| 93 | failed_ids.append(sample_id) |
||
| 94 | |||
| 95 | # Update submission |
||
| 96 | submission_obj.refresh_from_db() |
||
| 97 | submission_obj.status = NEED_REVISION |
||
| 98 | |||
| 99 | if len(failed_ids) != 0: |
||
| 100 | submission_obj.message = f"You've removed {len(success_ids)} " \ |
||
| 101 | f"samples. It wasn't possible to find records with these " \ |
||
| 102 | f"ids: {', '.join(failed_ids)}. Rerun validation please!" |
||
| 103 | else: |
||
| 104 | submission_obj.message = f"You've removed {len(success_ids)} " \ |
||
| 105 | f"samples. Rerun validation please!" |
||
| 106 | |||
| 107 | submission_obj.save() |
||
| 108 | |||
| 109 | summary_obj, created = ValidationSummary.objects.get_or_create( |
||
| 110 | submission=submission_obj, type='sample') |
||
| 111 | summary_obj.reset_all_count() |
||
| 112 | |||
| 113 | send_message( |
||
| 114 | submission_obj, construct_validation_message(submission_obj) |
||
| 115 | ) |
||
| 116 | |||
| 117 | logger.info("batch delete for samples completed") |
||
| 118 | |||
| 119 | return 'success' |
||
| 120 | |||
| 173 |