From df2805b586b06b0619fc263ddee9c87b3529cdb2 Mon Sep 17 00:00:00 2001 From: Flavia Missi Date: Wed, 13 Jan 2016 13:58:58 +0100 Subject: [PATCH 1/3] exchange/utils: check whether commit_unless_managed is available on current django installation --- exchange/utils.py | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/exchange/utils.py b/exchange/utils.py index 41c254e..fc0c23d 100644 --- a/exchange/utils.py +++ b/exchange/utils.py @@ -52,9 +52,17 @@ def insert_many(objects, using="default"): table = model._meta.db_table column_names = ",".join(con.ops.quote_name(f.column) for f in fields) placeholders = ",".join(("%s",) * len(fields)) - con.cursor().executemany("insert into %s (%s) values (%s)" - % (table, column_names, placeholders), parameters) - transaction.commit_unless_managed(using=using) + + from django import VERSION + # see https://docs.djangoproject.com/en/1.9/internals/deprecation/#deprecation-removed-in-1-8 + if VERSION[2] < 7: + con.cursor().executemany("insert into %s (%s) values (%s)" + % (table, column_names, placeholders), parameters) + transaction.commit_unless_managed(using=using) + else: + with transaction.atomic(): + con.cursor().executemany("insert into %s (%s) values (%s)" + % (table, column_names, placeholders), parameters) def update_many(objects, fields=[], using="default"): @@ -91,11 +99,22 @@ def update_many(objects, fields=[], using="default"): table = meta.db_table assignments = ",".join(("%s=%%s" % con.ops.quote_name(f.column)) for f in fields) - con.cursor().executemany("update %s set %s where %s=%%s" - % (table, assignments, - con.ops.quote_name(meta.pk.column)), - parameters) - transaction.commit_unless_managed(using=using) + + from django import VERSION + # see https://docs.djangoproject.com/en/1.9/internals/deprecation/#deprecation-removed-in-1-8 + if VERSION[2] < 7: + con.cursor().executemany("update %s set %s where %s=%%s" + % (table, assignments, + con.ops.quote_name(meta.pk.column)), + parameters) + transaction.commit_unless_managed(using=using) + else: + with transaction.atomic(): + con.cursor().executemany("update %s set %s where %s=%%s" + % (table, assignments, + con.ops.quote_name(meta.pk.column)), + parameters) + def memoize(ttl=None): From a889f22a2850c901e835c774d3858e7775825806 Mon Sep 17 00:00:00 2001 From: Flavia Missi Date: Mon, 7 Mar 2016 16:24:37 +0100 Subject: [PATCH 2/3] utils: use get_version() instead of VERSION constant --- exchange/utils.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/exchange/utils.py b/exchange/utils.py index fc0c23d..01fb894 100644 --- a/exchange/utils.py +++ b/exchange/utils.py @@ -53,16 +53,17 @@ def insert_many(objects, using="default"): column_names = ",".join(con.ops.quote_name(f.column) for f in fields) placeholders = ",".join(("%s",) * len(fields)) - from django import VERSION + from distutils.version import StrictVersion + from django import get_version # see https://docs.djangoproject.com/en/1.9/internals/deprecation/#deprecation-removed-in-1-8 - if VERSION[2] < 7: - con.cursor().executemany("insert into %s (%s) values (%s)" - % (table, column_names, placeholders), parameters) - transaction.commit_unless_managed(using=using) - else: + if StrictVersion(get_version()) >= StrictVersion('1.6.0'): with transaction.atomic(): con.cursor().executemany("insert into %s (%s) values (%s)" % (table, column_names, placeholders), parameters) + else: + con.cursor().executemany("insert into %s (%s) values (%s)" + % (table, column_names, placeholders), parameters) + transaction.commit_unless_managed(using=using) def update_many(objects, fields=[], using="default"): From 99224007d1a2e18793b2b4b8f44f1a9693461052 Mon Sep 17 00:00:00 2001 From: Flavia Missi Date: Mon, 7 Mar 2016 16:36:36 +0100 Subject: [PATCH 3/3] utils: update to get_version() in one more place --- exchange/utils.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/exchange/utils.py b/exchange/utils.py index 01fb894..afdd0ee 100644 --- a/exchange/utils.py +++ b/exchange/utils.py @@ -101,20 +101,21 @@ def update_many(objects, fields=[], using="default"): assignments = ",".join(("%s=%%s" % con.ops.quote_name(f.column)) for f in fields) - from django import VERSION + from distutils.version import StrictVersion + from django import get_version # see https://docs.djangoproject.com/en/1.9/internals/deprecation/#deprecation-removed-in-1-8 - if VERSION[2] < 7: - con.cursor().executemany("update %s set %s where %s=%%s" - % (table, assignments, - con.ops.quote_name(meta.pk.column)), - parameters) - transaction.commit_unless_managed(using=using) - else: + if StrictVersion(get_version()) >= StrictVersion('1.6.0'): with transaction.atomic(): con.cursor().executemany("update %s set %s where %s=%%s" % (table, assignments, con.ops.quote_name(meta.pk.column)), parameters) + else: + con.cursor().executemany("update %s set %s where %s=%%s" + % (table, assignments, + con.ops.quote_name(meta.pk.column)), + parameters) + transaction.commit_unless_managed(using=using)