Skip to content

Commit

Permalink
Make settings.BACKGROUND_EXPORT_TASK_CALLBACK capable of aborting bac…
Browse files Browse the repository at this point in the history
…kground export
  • Loading branch information
Peter Kubov committed Mar 6, 2024
1 parent de49952 commit 18b39f5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
4 changes: 3 additions & 1 deletion docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,9 @@ These configuration you can use with django-is-core in your django settings file

.. attribute:: IS_CORE_BACKGROUND_EXPORT_TASK_CALLBACK

The path to the function which will be called before export task execution.
The path to the function which will be called before export task execution. The function
is expected to return None on success. Any value other than None causes export to abort
with export_failure signal being sent.

# Django settings
IS_CORE_BACKGROUND_EXPORT_TASK_CALLBACK = 'your.file.perform_action_before_export_task'
Expand Down
3 changes: 1 addition & 2 deletions is_core/contrib/background_export/signals.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import django.dispatch


export_success = django.dispatch.Signal(providing_args=['exported_file'])
export_failure = django.dispatch.Signal(providing_args=['exception'])
export_failure = django.dispatch.Signal(providing_args=['exception', 'error'])
7 changes: 5 additions & 2 deletions is_core/contrib/background_export/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def on_task_success(self, task_id, args, kwargs, retval):

def on_failure(self, exc, task_id, args, kwargs, einfo):
super().on_failure(exc, task_id, args, kwargs, einfo)
export_failure.send(sender=self.__class__, exception=exc)
export_failure.send(sender=self.__class__, exception=exc, error=None)


@shared_task(base=BackgroundSerializationTask,
Expand Down Expand Up @@ -101,12 +101,15 @@ def background_serialization(self, exported_file_pk, rest_context, language, req

# Perform user-defined verification before saving exported file into the database.
if settings.BACKGROUND_EXPORT_TASK_CALLBACK:
import_string(settings.BACKGROUND_EXPORT_TASK_CALLBACK)(
error = import_string(settings.BACKGROUND_EXPORT_TASK_CALLBACK)(
request=request,
queryset=queryset,
filename=filename,
exported_file=exported_file,
)
if error is not None:
export_failure.send(sender=self.__class__, exception=None, error=error)
return

exported_file.file.save(filename, ContentFile(''))

Expand Down

0 comments on commit 18b39f5

Please sign in to comment.