~ubuntuone-pqm-team/django-celery/stable

12 by Ask Solem
FAQ: Django specific FAQs moved here from celery/FAQ
1
============================
2
 Frequently Asked Questions
3
============================
4
5
Generating a template in a task doesn't seem to respect my i18n settings?
6
-------------------------------------------------------------------------
7
8
**Answer**: To enable the Django translation machinery you need to activate
9
it with a language. **Note**: Be sure to reset to the previous language when
10
done.
11
12
    >>> from django.utils import translation
13
14
    >>> prev_language = translation.get_language()
15
    >>> translation.activate(language)
16
    >>> try:
17
    ...     render_template()
18
    ... finally:
19
            translation.activate(prev_language)
20
21
The common pattern here would be for the task to take a ``language``
22
argument:
23
24
.. code-block:: python
25
26
    from celery.decorators import task
27
28
    from django.utils import translation
29
    from django.template.loader import render_to_string
30
31
    @task()
32
    def generate_report(template="report.html", language=None):
33
        prev_language = translation.get_language()
34
        language and translation.activate(language)
35
        try:
36
            report = render_to_string(template)
37
        finally:
38
            translation.activate(prev_language)
39
        save_report_somewhere(report)
40
41
The celery test-suite is failing
42
--------------------------------
43
44
**Answer**: If you're running tests from your Django project, and the celery
45
test suite is failing in that context, then follow the steps below. If the
46
celery tests are failing in another context, please report an issue to our
47
issue tracker at GitHub:
48
359 by Ask Solem
Canonical repo moved to github.com/celery/django-celery
49
    http://github.com/celery/celery/issues/
12 by Ask Solem
FAQ: Django specific FAQs moved here from celery/FAQ
50
51
That Django is running tests for all applications in ``INSTALLED_APPS``
52
by default is a pet peeve for many. You should use a test runner that either
53
54
    1) Explicitly lists the apps you want to run tests for, or
55
56
    2) Make a test runner that skips tests for apps you don't want to run.
57
58
For example the test runner that celery is using:
59
359 by Ask Solem
Canonical repo moved to github.com/celery/django-celery
60
    http://github.com/celery/celery/blob/f90491fe0194aa472b5aecdefe5cc83289e65e69/celery/tests/runners.py
12 by Ask Solem
FAQ: Django specific FAQs moved here from celery/FAQ
61
62
To use this test runner, add the following to your ``settings.py``:
63
64
.. code-block:: python
65
242.1.8 by Jonas Haag
Fix #76: Made the test runner compatible with Django 1.4
66
    TEST_RUNNER = "djcelery.tests.runners.CeleryTestSuiteRunner",
12 by Ask Solem
FAQ: Django specific FAQs moved here from celery/FAQ
67
    TEST_APPS = (
68
        "app1",
69
        "app2",
70
        "app3",
71
        "app4",
72
    )
73
74
Or, if you just want to skip the celery tests:
75
76
.. code-block:: python
77
78
    INSTALLED_APPS = (.....)
242.1.8 by Jonas Haag
Fix #76: Made the test runner compatible with Django 1.4
79
    TEST_RUNNER = "djcelery.tests.runners.CeleryTestSuiteRunner",
12 by Ask Solem
FAQ: Django specific FAQs moved here from celery/FAQ
80
    TEST_APPS = filter(lambda k: k != "celery", INSTALLED_APPS)
81