forked from celery/django-celery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.py
72 lines (52 loc) · 1.84 KB
/
common.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from __future__ import absolute_import, unicode_literals
from contextlib import contextmanager
from functools import wraps
from django.utils import translation
@contextmanager
def respect_language(language):
"""Context manager that changes the current translation language for
all code inside the following block.
Can e.g. be used inside tasks like this::
from celery import task
from djcelery.common import respect_language
@task
def my_task(language=None):
with respect_language(language):
pass
"""
if language:
prev = translation.get_language()
translation.activate(language)
try:
yield
finally:
translation.activate(prev)
else:
yield
def respects_language(fun):
"""Decorator for tasks with respect to site's current language.
You can use this decorator on your tasks together with default @task
decorator (remember that the task decorator must be applied last).
See also the with-statement alternative :func:`respect_language`.
**Example**:
.. code-block:: python
@task
@respects_language
def my_task()
# localize something.
The task will then accept a ``language`` argument that will be
used to set the language in the task, and the task can thus be
called like:
.. code-block:: python
from django.utils import translation
from myapp.tasks import my_task
# Pass the current language on to the task
my_task.delay(language=translation.get_language())
# or set the language explicitly
my_task.delay(language='no.no')
"""
@wraps(fun)
def _inner(*args, **kwargs):
with respect_language(kwargs.pop('language', None)):
return fun(*args, **kwargs)
return _inner