53b867b47930c28928dd6f33a7756f370db47d8c
[redakcja.git] / apps / catalogue / tasks.py
1 from celery.task import task
2 from django.utils import translation
3 from django.conf import settings
4
5
6 @task
7 def _refresh_by_pk(cls, pk, language=None):
8     prev_language = translation.get_language()
9     language and translation.activate(language)
10     try:
11         cls._default_manager.get(pk=pk).refresh()
12     finally:
13         translation.activate(prev_language)
14
15 if settings.USE_CELERY:
16     def refresh_instance(instance):
17         _refresh_by_pk.delay(type(instance), instance.pk, translation.get_language())
18 else:
19     def refresh_instance(instance):
20         instance.refresh()
21
22
23 @task
24 def _publishable_error(book, language=None):
25     prev_language = translation.get_language()
26     language and translation.activate(language)
27     try:
28         return book.assert_publishable()
29     except AssertionError, e:
30         return e
31     else:
32        return None
33     finally:
34         translation.activate(prev_language)
35
36 if settings.USE_CELERY:
37     def publishable_error(book):
38         task = _publishable_error.delay(book, translation.get_language())
39         return task.wait()
40 else:
41     def publishable_error(book):
42         return _publishable_error(book)