Ticket #2227: django-20060703-nested_transactions.diff

File django-20060703-nested_transactions.diff, 4.9 KB (added by md@…, 18 years ago)

patch adding nested transactions to @commit_on_success

  • django/db/transaction.py

    This patch is  a cruded attemt to make @commit_on_success support
    nested transactions. See http://code.djangoproject.com/ticket/2227
    for an explanation why this is usefull.
    
      --md@hudora.de
    
     
    187187    control in web apps.
    188188    """
    189189    def _commit_on_success(*args, **kw):
    190         try:
    191             enter_transaction_management()
    192             managed(True)
     190        # only use transactions if we don't have already an ongoing transaction
     191        thread_ident = thread.get_ident()
     192        if thread_ident not in state or state[thread_ident] == []:
    193193            try:
    194                 res = func(*args, **kw)
    195             except Exception, e:
    196                 if is_dirty():
    197                     rollback()
    198                 raise
    199             else:
    200                 if is_dirty():
    201                     commit()
    202             return res
    203         finally:
    204             leave_transaction_management()
     194                enter_transaction_management()
     195                managed(True)
     196                try:
     197                    res = func(*args, **kw)
     198                except Exception, e:
     199                    if is_dirty():
     200                        rollback()
     201                    raise
     202                else:
     203                    if is_dirty():
     204                        commit()
     205                return res
     206            finally:
     207                leave_transaction_management()
     208        else:
     209            return func(*args, **kw)
    205210    return _commit_on_success
    206211
    207212def commit_manually(func):
  • django/core/management.py

     
    286286    for klass in app_models:
    287287        if cursor and klass._meta.db_table in table_names:
    288288            # Drop the table now
    289             output.append('%s %s;' % (style.SQL_KEYWORD('DROP TABLE'),
    290                 style.SQL_TABLE(backend.quote_name(klass._meta.db_table))))
    291289            if backend.supports_constraints and references_to_delete.has_key(klass):
    292290                for rel_class, f in references_to_delete[klass]:
    293291                    table = rel_class._meta.db_table
     
    300298                        style.SQL_KEYWORD(backend.get_drop_foreignkey_sql()),
    301299                        style.SQL_FIELD(backend.quote_name("%s_referencing_%s_%s" % (col, r_table, r_col)))))
    302300                del references_to_delete[klass]
     301            output.append('%s %s;' % (style.SQL_KEYWORD('DROP TABLE'),
     302                style.SQL_TABLE(backend.quote_name(klass._meta.db_table))))
    303303
    304304    # Output DROP TABLE statements for many-to-many tables.
    305305    for klass in app_models:
  • django/contrib/comments/views/comments.py

     
    252252        else:
    253253            manipulator.do_html2python(new_data)
    254254            comment = manipulator.save(new_data)
    255         return HttpResponseRedirect("../posted/?c=%s:%s" % (content_type_id, object_id))
     255        return HttpResponseRedirect("../posted/?c=%s:%s&ic=%s" % (content_type_id, object_id, comment.id))
    256256    else:
    257257        raise Http404, _("The comment form didn't provide either 'preview' or 'post'")
    258258
     
    316316        else:
    317317            manipulator.do_html2python(new_data)
    318318            comment = manipulator.save(new_data)
    319         return HttpResponseRedirect("../posted/?c=%s:%s" % (content_type_id, object_id))
     319        return HttpResponseRedirect("../posted/?c=%s:%s&if=%s" % (content_type_id, object_id, comment.id))
    320320    else:
    321321        raise Http404, _("The comment form didn't provide either 'preview' or 'post'")
    322322
     
    330330            The object the comment was posted on
    331331    """
    332332    obj = None
     333    comment = None
    333334    if request.GET.has_key('c'):
    334335        content_type_id, object_id = request.GET['c'].split(':')
    335336        try:
     
    337338            obj = content_type.get_object_for_this_type(pk=object_id)
    338339        except ObjectDoesNotExist:
    339340            pass
    340     return render_to_response('comments/posted.html', {'object': obj}, context_instance=RequestContext(request))
     341        try:
     342            comment_id = request.GET['if']
     343            comment = FreeComment.objects.get(pk=comment_id)
     344        except ObjectDoesNotExist:
     345            pass           
     346        try:
     347            comment_id = request.GET['ic']
     348            comment = Comment.objects.get(pk=comment_id)
     349        except ObjectDoesNotExist:
     350            pass           
     351    return render_to_response('comments/posted.html', {'object': obj, 'comment': comment}, context_instance=RequestContext(request))
Back to Top