Comments (9)
It is certainly useful, but the implementation is maybe a little to specific.
For one if there was more than one such task_with_*
decorator, it wouldn't be possible to use more than one for the same task.
It would be much more useful like this:
from celery.decorators import task
from djcelery.decorators import respects_language
@task
@respects_language
def my_task(**kwargs):
pass
and then I think it would be much better as a contextmanager:
@task
def my_task(x, y, language=None):
with respect_to_language(language):
pass
from django-celery.
Thanks for recommendations. I modify code with respect to them. And add small test for 'with' statement
Diff file here:
diff --git a/djcelery/decorators.py b/djcelery/decorators.py
new file mode 100644
index 0000000..c303bb8
--- /dev/null
+++ b/djcelery/decorators.py
@@ -0,0 +1,55 @@
+from django.utils import translation
+from django.utils.functional import wraps
+
+def respects_language(func):
+ '''
+ Decorator for tasks with respect to site's current language.
+ You can use in tasks.py this decorator @task_respect_to_language instead of default @task
+ Be sure that task method have kwargs argument:
+
+ @task
+ @respects_language
+ def my_task(**kwargs):
+ pass
+
+ You can call this task this way:
+
+ from django.utils import translation
+ tasks.my_task.delay(language=translation.get_language())
+ '''
+ def wrapper(*args, **kwargs):
+ language = kwargs.pop('language', None)
+ prev_language = translation.get_language()
+ language and translation.activate(language)
+ try:
+ return func(*args, **kwargs)
+ finally:
+ translation.activate(prev_language)
+
+ wrapper.__doc__ = func.__doc__
+ wrapper.__name__ = func.__name__
+ wrapper.__module__ = func.__module__
+ return wraps(func)(wrapper)
+
+class respect_to_language:
+ '''
+ Class for 'with' statement.
+ You can use it inside task methods definition this way:
+
+ @task
+ def my_task(language=None):
+ with respect_to_language(language):
+ pass
+ '''
+ language = None
+ language_prev = None
+
+ def __init__(self, language):
+ self.language = language
+
+ def __enter__(self):
+ self.language_prev = translation.get_language()
+ self.language and translation.activate(self.language)
+
+ def __exit__(self, type, value, traceback):
+ translation.activate(self.language_prev)
\ No newline at end of file
diff --git a/djcelery/tests/__init__.py b/djcelery/tests/__init__.py
index e69de29..8b5dc7b 100644
--- a/djcelery/tests/__init__.py
+++ b/djcelery/tests/__init__.py
@@ -0,0 +1,10 @@
+'''
+>>> from djcelery.decorators import respect_to_language
+>>> from django.utils import translation
+>>> language = translation.get_language()
+>>> with respect_to_language('ru'):
+... translation.get_language()
+'ru'
+>>> language == translation.get_language()
+True
+'''
\ No newline at end of file
from django-celery.
Thanks! Sorry for the late reply, seems I missed this for some time. Lots of stuff going on at the same time :(
The documentation seems to be out of date, could you update that, and also document the purpose of respect_for_language
(so it makes more sense if you do help(respect_to_language)
or pydoc celery.decorators.respect_to_language
.
Thanks in advance!
from django-celery.
I updated patch and published it here http://dpaste.com/276957/
from django-celery.
Shit, I closed it by mistake... Can you reopen it or... maybe apply my patch ? :-)
from django-celery.
It doesn't seem like I can reopen it :(
But could you fork the project and make a pull request instead? That is a lot easier to work with, and for me to merge it and maintain author info.
See http://help.github.com/forking/
from django-celery.
I see a little problem with it. Now I use your branch "release20-maint" for working with r13315 revision of Django. Can you suggest to me, what branch is better to patch. I can not use "master" and in this way I prefer "release20-maint". But may be it's not good for you and purposes of creating this branch. What do you think about it?
from django-celery.
You can work on either one; release21-maint or master. I will do the merging necessary afterwards :)
from django-celery.
I have done this - https://github.com/ask/django-celery/pull/31
Merge, please with release21-maint if it's the same with release20-maint.
from django-celery.
Related Issues (20)
- djcelerymon: AttributeError: type object 'Command' has no attribute 'option_list'
- Migrate to github actions from travis HOT 2
- djcelery released a task that has been deleted
- Migrate from djcelery to django_celery_results HOT 6
- Tasks being executed even without djcelery
- incompatibility with celery HOT 1
- Must used `"setuptools<58.0.0"` to install because of `anyjson` HOT 1
- celery-beat process make the CPU usage too high HOT 2
- Some tests misusing assertTrue for comparisons
- The "http://celeryproject.org/" site is gone? HOT 2
- django-celery doesn't work in django 4.0 HOT 5
- Changing instances of ugettext, ugettext_lazy, ungettext, and force_unicode for Django 4.0 HOT 2
- Django celery with redis broker executing same task indefinitely
- This error occurred when I started celery worker
- `pip install django-celery` gives error HOT 4
- Unrecoverable error in Celery worker: ReadOnlyError with Redis backend
- Error loading Celery application with Django module
- celery cannot connect to postgresql HOT 2
- celery and django-celery version do not match,show syntaxError HOT 3
- I use celery in the Django project, but why does celery keep increasing threads?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-celery.