Skip to content

Commit

Permalink
Merge branch 'czue:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
aidin-leo authored Sep 18, 2021
2 parents 692473c + 659fa12 commit 6663e2f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 8 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ The `initProgressBar` function takes an optional object of options. The followin
| onDataError | function to call on a response that's not JSON or has invalid schema due to a programming error | onError |
| onResult | function to call when returned non empty result | CeleryProgressBar.onResultDefault |
| barColors | dictionary containing color values for various progress bar states. Colors that are not specified will defer to defaults | barColorsDefault |
| defaultMessages | dictionary containing default messages that can be overridden | see below |

The `barColors` option allows you to customize the color of each progress bar state by passing a dictionary of key-value pairs of `state: #hexcode`. The defaults are shown below.

Expand All @@ -178,6 +179,13 @@ The `barColors` option allows you to customize the color of each progress bar st
| progress | #68a9ef | ![#68a9ef](https://placehold.it/15/68a9ef/000000?text=+) |
| ignored | #7a7a7a | ![#7a7a7a](https://placehold.it/15/7a7a7a/000000?text=+) |

The `defaultMessages` option allows you to override some default messages in the UI. At the moment these are:

| Message Id | When Shown | Default Value |
|-------|----------|:-------------:|
| waiting | Task is waiting to start | 'Waiting for task to start...'
| started | Task has started but reports no progress | 'Task started...'

# WebSocket Support

Additionally, this library offers WebSocket support using [Django Channels](https://channels.readthedocs.io/en/latest/)
Expand Down
5 changes: 4 additions & 1 deletion celery_progress/backend.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import datetime
import logging
from abc import ABCMeta, abstractmethod
from decimal import Decimal

from celery.result import EagerResult, allow_join_result
from celery.backends.base import DisabledBackend

logger = logging.getLogger(__name__)

PROGRESS_STATE = 'PROGRESS'

Expand Down Expand Up @@ -102,11 +104,12 @@ def get_info(self):
'progress': _get_unknown_progress(self.result.state),
})
else:
logger.error('Task %s has unknown state %s with metadata %s', self.result.id, self.result.state, self.result.info)
response.update({
'complete': True,
'success': False,
'progress': _get_unknown_progress(self.result.state),
'result': 'Unknown state {}'.format(str(self.result.info)),
'result': 'Unknown state {}'.format(self.result.state),
})
return response

Expand Down
18 changes: 14 additions & 4 deletions celery_progress/static/celery_progress/celery_progress.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,22 @@ class CeleryProgressBar {
ignored: '#7a7a7a'
}
this.barColors = Object.assign({}, barColorsDefault, options.barColors);

let defaultMessages = {
waiting: 'Waiting for task to start...',
started: 'Task started...',
}
this.messages = Object.assign({}, defaultMessages, options.defaultMessages);
}

onSuccessDefault(progressBarElement, progressBarMessageElement, result) {
result = this.getMessageDetails(result);
progressBarElement.style.backgroundColor = this.barColors.success;
progressBarMessageElement.textContent = "Success! " + result;
if (progressBarElement) {
progressBarElement.style.backgroundColor = this.barColors.success;
}
if (progressBarMessageElement) {
progressBarMessageElement.textContent = "Success! " + result;
}
}

onResultDefault(resultElement, result) {
Expand Down Expand Up @@ -75,9 +85,9 @@ class CeleryProgressBar {
var description = progress.description || "";
if (progress.current == 0) {
if (progress.pending === true) {
progressBarMessageElement.textContent = 'Waiting for task to start...';
progressBarMessageElement.textContent = this.messages.waiting;
} else {
progressBarMessageElement.textContent = 'Task started...';
progressBarMessageElement.textContent = this.messages.started;
}
} else {
progressBarMessageElement.textContent = progress.current + ' of ' + progress.total + ' processed. ' + description;
Expand Down
4 changes: 2 additions & 2 deletions celery_progress/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.conf.urls import url
from django.urls import re_path
from . import views

app_name = 'celery_progress'
urlpatterns = [
url(r'^(?P<task_id>[\w-]+)/$', views.get_progress, name='task_status')
re_path(r'^(?P<task_id>[\w-]+)/$', views.get_progress, name='task_status')
]
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

setup(
name='celery-progress',
version='0.0.14',
version='0.1.1',
packages=find_packages(),
include_package_data=True,
license='MIT License',
Expand All @@ -34,6 +34,9 @@
'Programming Language :: Python',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Topic :: Internet :: WWW/HTTP',
'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
],
Expand Down

0 comments on commit 6663e2f

Please sign in to comment.