| @@ 23-79 (lines=57) @@ | ||
| 20 | logger = get_task_logger(__name__) |
|
| 21 | ||
| 22 | ||
| 23 | class BatchUpdateAnimals(MyTask): |
|
| 24 | name = "Batch update" |
|
| 25 | description = """Batch update of field in animals""" |
|
| 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 submissio 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 update for animals: %s" % (str(exc))) |
|
| 39 | submission_obj.save() |
|
| 40 | ||
| 41 | asyncio.get_event_loop().run_until_complete( |
|
| 42 | send_message_to_websocket( |
|
| 43 | { |
|
| 44 | 'message': STATUSES.get_value_display(ERROR), |
|
| 45 | 'notification_message': submission_obj.message |
|
| 46 | }, |
|
| 47 | args[0] |
|
| 48 | ) |
|
| 49 | ) |
|
| 50 | ||
| 51 | # send a mail to the user with the stacktrace (einfo) |
|
| 52 | submission_obj.owner.email_user( |
|
| 53 | "Error in batch update for animals: %s" % (args[0]), |
|
| 54 | ("Something goes wrong in batch update for animals. Please report " |
|
| 55 | "this to InjectTool team\n\n %s" % str(einfo)), |
|
| 56 | ) |
|
| 57 | ||
| 58 | # TODO: submit mail to admin |
|
| 59 | ||
| 60 | def run(self, submission_id, animal_ids): |
|
| 61 | """a function to upload data into UID""" |
|
| 62 | ||
| 63 | logger.info("Start batch update for animals") |
|
| 64 | ||
| 65 | for animal_id, value in animal_ids.items(): |
|
| 66 | animal = Animal.objects.get(pk=animal_id) |
|
| 67 | animal.birth_location = value |
|
| 68 | animal.save() |
|
| 69 | ||
| 70 | # Update submission |
|
| 71 | submission_obj = Submission.objects.get(pk=submission_id) |
|
| 72 | submission_obj.status = LOADED |
|
| 73 | submission_obj.message = "Data updated, try to rerun validation" |
|
| 74 | submission_obj.save() |
|
| 75 | ||
| 76 | send_message( |
|
| 77 | submission_obj, construct_validation_message(submission_obj) |
|
| 78 | ) |
|
| 79 | return 'success' |
|
| 80 | ||
| 81 | ||
| 82 | # register explicitly tasks |
|
| @@ 23-79 (lines=57) @@ | ||
| 20 | logger = get_task_logger(__name__) |
|
| 21 | ||
| 22 | ||
| 23 | class BatchUpdateSamples(MyTask): |
|
| 24 | name = "Batch update" |
|
| 25 | description = """Batch update of field in 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 update for animals: %s" % (str(exc))) |
|
| 39 | submission_obj.save() |
|
| 40 | ||
| 41 | asyncio.get_event_loop().run_until_complete( |
|
| 42 | send_message_to_websocket( |
|
| 43 | { |
|
| 44 | 'message': STATUSES.get_value_display(ERROR), |
|
| 45 | 'notification_message': submission_obj.message |
|
| 46 | }, |
|
| 47 | args[0] |
|
| 48 | ) |
|
| 49 | ) |
|
| 50 | ||
| 51 | # send a mail to the user with the stacktrace (einfo) |
|
| 52 | submission_obj.owner.email_user( |
|
| 53 | "Error in batch update for samples: %s" % (args[0]), |
|
| 54 | ("Something goes wrong in batch update for samples. Please report " |
|
| 55 | "this to InjectTool team\n\n %s" % str(einfo)), |
|
| 56 | ) |
|
| 57 | ||
| 58 | # TODO: submit mail to admin |
|
| 59 | ||
| 60 | def run(self, submission_id, sample_ids): |
|
| 61 | """a function to upload data into UID""" |
|
| 62 | ||
| 63 | logger.info("Start batch update for animals") |
|
| 64 | ||
| 65 | for sample_id, value in sample_ids.items(): |
|
| 66 | sample = Sample.objects.get(pk=sample_id) |
|
| 67 | sample.collection_place = value |
|
| 68 | sample.save() |
|
| 69 | ||
| 70 | # Update submission |
|
| 71 | submission_obj = Submission.objects.get(pk=submission_id) |
|
| 72 | submission_obj.status = LOADED |
|
| 73 | submission_obj.message = "Data updated, try to rerun validation" |
|
| 74 | submission_obj.save() |
|
| 75 | ||
| 76 | send_message( |
|
| 77 | submission_obj, construct_validation_message(submission_obj) |
|
| 78 | ) |
|
| 79 | return 'success' |
|
| 80 | ||
| 81 | ||
| 82 | # register explicitly tasks |
|