Nimrod,

 NAA> On 7/25/07, Andrey Khavryuchenko <[EMAIL PROTECTED]> wrote:
 >> >> Yes, I read carefuly your question and thought the answer was
 >> >> straighforward.   I don't understand why you don't want decorators, but 
 >> >> you
 >> >> could just check the decorator definition to read what it does and copy
 >> >> it's code.  All you need is decorator name and grep over django sources 
 >> >> :)
 >> 
 NAA> Aren't decorators usable only in views?
 >> 
 >> Decorators are python feature.  Django views are regular functions, just as
 >> any other.

 NAA> Sorry I wasn't very clear on that last message, I meant the Django
 NAA> db.transactions decorator functions. I am thinking they only work when
 NAA> you use them in a view. When you try to use them elsewhere, e.g. a
 NAA> Django-based command line app, see below...

No, you're wrong.  Transaction decorators work not only in view functions.
They work anywhere in the web app, provided transaction middleware is
included.

 NAA> At least that's what I think.  I have a project that required a
 NAA> separately running process (running as daemon) that needed access to
 NAA> the Django models. Since the code in the daemon is not run in the
 NAA> context of a view (no triggering of TransactionMiddleware),
 NAA> transactions decorators do not work. I get:
 >> 
 NAA> TransactionManagementError: This code isn't under transaction management
 >> 
 >> Not having read transaction code, can't say what haven't worked in your
 >> case.  Definitely, something wasn't initialized :)

 NAA> You're right. The app I am talking about is not a web app, rather a
 NAA> command line app that I made as a supplement to the web app I made
 NAA> using Django. Since it makes no sense to reinvent the wheel, I just
 NAA> used Django's ORM system in the command line app. When I tried using
 NAA> transactions using the transaction decorator functions I get the error
 NAA> above. Likely cause is that the command line app is not using
 NAA> middleware, ergo not using TransactionMiddleware, then I needed a way
 NAA> to initialize and manage transactions manually.

A ticket in my queue is similar, so I understand you perfectly.

Quick glance on django.middleware.transaction shows:

    def process_request(self, request):
        """Enters transaction management"""
        transaction.enter_transaction_management()
        transaction.managed(True)

    def process_exception(self, request, exception):
        """Rolls back the database and leaves transaction management"""
        if transaction.is_dirty():
            transaction.rollback()
        transaction.leave_transaction_management()

    def process_response(self, request, response):
        """Commits and leaves transaction management."""
        if transaction.is_managed():
            if transaction.is_dirty():
                transaction.commit()
            transaction.leave_transaction_management()
        return response

So in cmd line app you have to call 
   transaction.enter_transaction_management()
   transaction.managed(True)
in initialization and 
   transaction.leave_transaction_management()
in the end.  Not tested, but should work.

-- 
Andrey V Khavryuchenko            
Django NewGate -  http://www.kds.com.ua/djiggit/
Development - http://www.kds.com.ua 
Call akhavr1975 on www.gizmoproject.com

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to