| 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 |
||
| 32 | def run(self, submission_id, sample_ids): |
||
| 33 | """Function for batch update attribute in animals |
||
| 34 | Args: |
||
| 35 | submission_id (int): id of submission |
||
| 36 | sample_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 samples") |
||
| 43 | success_ids = list() |
||
| 44 | failed_ids = list() |
||
| 45 | |||
| 46 | for sample_id in sample_ids: |
||
| 47 | try: |
||
| 48 | name = Name.objects.get( |
||
| 49 | name=sample_id, submission=submission_obj) |
||
| 50 | |||
| 51 | sample_obj = Sample.objects.get(name=name) |
||
| 52 | |||
| 53 | with transaction.atomic(): |
||
| 54 | sample_obj.delete() |
||
| 55 | name.delete() |
||
| 56 | success_ids.append(sample_id) |
||
| 57 | |||
| 58 | except Name.DoesNotExist: |
||
| 59 | failed_ids.append(sample_id) |
||
| 60 | |||
| 61 | except Sample.DoesNotExist: |
||
| 62 | failed_ids.append(sample_id) |
||
| 63 | |||
| 64 | # Update submission |
||
| 65 | submission_obj.refresh_from_db() |
||
| 66 | submission_obj.status = NEED_REVISION |
||
| 67 | |||
| 68 | if len(failed_ids) != 0: |
||
| 69 | submission_obj.message = f"You've removed {len(success_ids)} " \ |
||
| 70 | f"samples. It wasn't possible to find records with these " \ |
||
| 71 | f"ids: {', '.join(failed_ids)}. Rerun validation please!" |
||
| 72 | else: |
||
| 73 | submission_obj.message = f"You've removed {len(success_ids)} " \ |
||
| 74 | f"samples. Rerun validation please!" |
||
| 75 | |||
| 76 | submission_obj.save() |
||
| 77 | |||
| 78 | summary_obj, created = ValidationSummary.objects.get_or_create( |
||
| 79 | submission=submission_obj, type='sample') |
||
| 80 | summary_obj.reset_all_count() |
||
| 81 | |||
| 82 | send_message( |
||
| 83 | submission_obj, construct_validation_message(submission_obj) |
||
| 84 | ) |
||
| 85 | |||
| 86 | logger.info("batch delete for samples completed") |
||
| 87 | |||
| 88 | return 'success' |
||
| 89 | |||
| 118 |