| @@ 21-78 (lines=58) @@ | ||
| 18 | logger = get_task_logger(__name__) |
|
| 19 | ||
| 20 | ||
| 21 | class BatchUpdateAnimals(MyTask): |
|
| 22 | name = "Batch update animals" |
|
| 23 | description = """Batch update of field in animals""" |
|
| 24 | ||
| 25 | # Ovverride default on failure method |
|
| 26 | # This is not a failed validation for a wrong value, this is an |
|
| 27 | # error in task that mean an error in coding |
|
| 28 | def on_failure(self, exc, task_id, args, kwargs, einfo): |
|
| 29 | logger.error('{0!r} failed: {1!r}'.format(task_id, exc)) |
|
| 30 | ||
| 31 | # get submission object |
|
| 32 | submission_obj = Submission.objects.get(pk=args[0]) |
|
| 33 | ||
| 34 | # mark submission with ERROR |
|
| 35 | submission_obj.status = ERROR |
|
| 36 | submission_obj.message = ("Error in batch update for animals: %s" |
|
| 37 | % (str(exc))) |
|
| 38 | submission_obj.save() |
|
| 39 | ||
| 40 | send_message(submission_obj) |
|
| 41 | ||
| 42 | # send a mail to the user with the stacktrace (einfo) |
|
| 43 | submission_obj.owner.email_user( |
|
| 44 | "Error in batch update for animals: %s" % (args[0]), |
|
| 45 | ("Something goes wrong in batch update for animals. Please report " |
|
| 46 | "this to InjectTool team\n\n %s" % str(einfo)), |
|
| 47 | ) |
|
| 48 | ||
| 49 | # TODO: submit mail to admin |
|
| 50 | ||
| 51 | def run(self, submission_id, animal_ids, attribute): |
|
| 52 | """Function for batch update attribute in animals |
|
| 53 | Args: |
|
| 54 | submission_id (int): id of submission |
|
| 55 | animal_ids (dict): dict with id and values to update |
|
| 56 | attribute (str): attribute to update |
|
| 57 | """ |
|
| 58 | ||
| 59 | logger.info("Start batch update for animals") |
|
| 60 | ||
| 61 | for animal_id, value in animal_ids.items(): |
|
| 62 | if value == '': |
|
| 63 | value = None |
|
| 64 | animal = Animal.objects.get(pk=animal_id) |
|
| 65 | if getattr(animal, attribute) != value: |
|
| 66 | setattr(animal, attribute, value) |
|
| 67 | animal.save() |
|
| 68 | ||
| 69 | # Update submission |
|
| 70 | submission_obj = Submission.objects.get(pk=submission_id) |
|
| 71 | submission_obj.status = NEED_REVISION |
|
| 72 | submission_obj.message = "Data updated, try to rerun validation" |
|
| 73 | submission_obj.save() |
|
| 74 | ||
| 75 | send_message( |
|
| 76 | submission_obj, construct_validation_message(submission_obj) |
|
| 77 | ) |
|
| 78 | return 'success' |
|
| 79 | ||
| 80 | ||
| 81 | # register explicitly tasks |
|
| @@ 21-73 (lines=53) @@ | ||
| 18 | logger = get_task_logger(__name__) |
|
| 19 | ||
| 20 | ||
| 21 | class BatchUpdateSamples(MyTask): |
|
| 22 | name = "Batch update samples" |
|
| 23 | description = """Batch update of field in samples""" |
|
| 24 | ||
| 25 | # Ovverride default on failure method |
|
| 26 | # This is not a failed validation for a wrong value, this is an |
|
| 27 | # error in task that mean an error in coding |
|
| 28 | def on_failure(self, exc, task_id, args, kwargs, einfo): |
|
| 29 | logger.error('{0!r} failed: {1!r}'.format(task_id, exc)) |
|
| 30 | ||
| 31 | # get submission object |
|
| 32 | submission_obj = Submission.objects.get(pk=args[0]) |
|
| 33 | ||
| 34 | # mark submission with ERROR |
|
| 35 | submission_obj.status = ERROR |
|
| 36 | submission_obj.message = ("Error in batch update for samples: %s" |
|
| 37 | % (str(exc))) |
|
| 38 | submission_obj.save() |
|
| 39 | ||
| 40 | send_message(submission_obj) |
|
| 41 | ||
| 42 | # send a mail to the user with the stacktrace (einfo) |
|
| 43 | submission_obj.owner.email_user( |
|
| 44 | "Error in batch update for samples: %s" % (args[0]), |
|
| 45 | ("Something goes wrong in batch update for samples. Please report " |
|
| 46 | "this to InjectTool team\n\n %s" % str(einfo)), |
|
| 47 | ) |
|
| 48 | ||
| 49 | # TODO: submit mail to admin |
|
| 50 | ||
| 51 | def run(self, submission_id, sample_ids, attribute): |
|
| 52 | """a function to upload data into UID""" |
|
| 53 | ||
| 54 | logger.info("Start batch update for samples") |
|
| 55 | ||
| 56 | for sample_id, value in sample_ids.items(): |
|
| 57 | if value == '': |
|
| 58 | value = None |
|
| 59 | sample = Sample.objects.get(pk=sample_id) |
|
| 60 | if getattr(sample, attribute) != value: |
|
| 61 | setattr(sample, attribute, value) |
|
| 62 | sample.save() |
|
| 63 | ||
| 64 | # Update submission |
|
| 65 | submission_obj = Submission.objects.get(pk=submission_id) |
|
| 66 | submission_obj.status = NEED_REVISION |
|
| 67 | submission_obj.message = "Data updated, try to rerun validation" |
|
| 68 | submission_obj.save() |
|
| 69 | ||
| 70 | send_message( |
|
| 71 | submission_obj, construct_validation_message(submission_obj) |
|
| 72 | ) |
|
| 73 | return 'success' |
|
| 74 | ||
| 75 | ||
| 76 | # register explicitly tasks |
|