Giter Club home page Giter Club logo

Comments (9)

ask avatar ask commented on May 25, 2024

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.

ramusus avatar ramusus commented on May 25, 2024

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.

ask avatar ask commented on May 25, 2024

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.

ramusus avatar ramusus commented on May 25, 2024

I updated patch and published it here http://dpaste.com/276957/

from django-celery.

ramusus avatar ramusus commented on May 25, 2024

Shit, I closed it by mistake... Can you reopen it or... maybe apply my patch ? :-)

from django-celery.

ask avatar ask commented on May 25, 2024

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.

ramusus avatar ramusus commented on May 25, 2024

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.

ask avatar ask commented on May 25, 2024

You can work on either one; release21-maint or master. I will do the merging necessary afterwards :)

from django-celery.

ramusus avatar ramusus commented on May 25, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.