| 1 |  |  | #!/usr/bin/env python3 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | # -*- coding: utf-8 -*- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | Created on Wed Feb 27 16:38:37 2019 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | @author: Paolo Cozzi <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | from celery.utils.log import get_task_logger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | from django.db import transaction | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | from common.constants import ERROR, NEED_REVISION | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | from image.celery import app as celery_app, MyTask | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | from image_app.models import Submission, Animal, Name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | from submissions.helpers import send_message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | from validation.helpers import construct_validation_message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | from validation.models import ValidationSummary | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | # Get an instance of a logger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | logger = get_task_logger(__name__) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 23 |  | View Code Duplication | class BatchDeleteAnimals(MyTask): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     name = "Batch delete animals" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     description = """Batch remove animals and associated samples""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     # Ovverride default on failure method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     # This is not a failed validation for a wrong value, this is an | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 29 |  |  |     # error in task that mean an error in coding | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     def on_failure(self, exc, task_id, args, kwargs, einfo): | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |         logger.error('{0!r} failed: {1!r}'.format(task_id, exc)) | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         # get submission object | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         submission_obj = Submission.objects.get(pk=args[0]) | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         # mark submission with ERROR | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         submission_obj.status = ERROR | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         submission_obj.message = ("Error in batch delete for animals: %s" | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |                                   % (str(exc))) | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         submission_obj.save() | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         send_message(submission_obj) | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         # send a mail to the user with the stacktrace (einfo) | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         submission_obj.owner.email_user( | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |             "Error in batch delete for animals: %s" % (args[0]), | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |             ("Something goes wrong in batch delete for animals. Please report " | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |              "this to InjectTool team\n\n %s" % str(einfo)), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         # TODO: submit mail to admin | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     def run(self, submission_id, animal_ids): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         """Function for batch update attribute in animals | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         Args: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             submission_id (int): id of submission | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             animal_ids (list): set with ids to delete | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         logger.info("Start batch delete for animals") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         success_ids = list() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         failed_ids = list() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         for animal_id in animal_ids: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                 name = Name.objects.get(name=animal_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 animal_object = Animal.objects.get(name=name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 samples = animal_object.sample_set.all() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                 with transaction.atomic(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                     for sample in samples: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                         sample_name = sample.name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                         sample.delete() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                         sample_name.delete() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                     name.mother_of.clear() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                     name.father_of.clear() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                     animal_object.delete() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                     name.delete() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                 success_ids.append(animal_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             except Name.DoesNotExist: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                 failed_ids.append(animal_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             except Animal.DoesNotExist: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 failed_ids.append(animal_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         # Update submission | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         submission_obj = Submission.objects.get(pk=submission_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         submission_obj.status = NEED_REVISION | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         if len(failed_ids) != 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             submission_obj.message = f"You've removed {len(success_ids)} " \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                 f"animals. It wasn't possible to find records with these ids: " \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                 f"{', '.join(failed_ids)}. Rerun validation please!" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             submission_obj.message = f"You've removed {len(success_ids)} " \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                 f"animals. Rerun validation please!" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         submission_obj.save() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         summary_obj, created = ValidationSummary.objects.get_or_create( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             submission=submission_obj, type='animal') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         summary_obj.reset_all_count() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         send_message( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             submission_obj, construct_validation_message(submission_obj) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         return 'success' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | # register explicitly tasks | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | # https://github.com/celery/celery/issues/3744#issuecomment-271366923 | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 107 |  |  | celery_app.tasks.register(BatchDeleteAnimals) | 
            
                                                        
            
                                    
            
            
                | 108 |  |  |  |