Commit c8257843 by Florian Apolloner

Merge pull request #201 from apollo13/atomic

Use Django's atomic when available.
parents a0b63448 d6117ab9
language: python language: python
python:
- "2.6"
- "2.7"
- "3.2"
- "3.3"
env: env:
- DJANGO=https://github.com/django/django/archive/master.tar.gz - TOXENV=py27-1.7.x
- DJANGO=django==1.6 - TOXENV=py33-1.5.x
- DJANGO=django==1.5.5 - TOXENV=py27-1.6.x
- DJANGO=django==1.4.10 - TOXENV=py33-1.6.x
- TOXENV=py26-1.6.x
- TOXENV=py27-1.5.x
- TOXENV=py27-1.4.x
- TOXENV=py33-1.7.x
- TOXENV=py26-1.4.x
- TOXENV=py26-1.5.x
install: install:
- pip install $DJANGO - pip install tox
- pip install -r requirements/travis-ci.txt --use-mirrors
script: script:
- coverage run --source django_taggit runtests.py - tox
- coverage report
notifications: notifications:
email: false email: false
matrix:
exclude:
- python: "3.2"
env: DJANGO=django==1.4.10
- python: "3.3"
env: DJANGO=django==1.4.10
- python: "2.6"
env: DJANGO=https://github.com/django/django/archive/master.tar.gz
...@@ -9,6 +9,23 @@ from django.utils.translation import ugettext_lazy as _, ugettext ...@@ -9,6 +9,23 @@ from django.utils.translation import ugettext_lazy as _, ugettext
from django.utils.encoding import python_2_unicode_compatible from django.utils.encoding import python_2_unicode_compatible
try:
atomic = transaction.atomic
except AttributeError:
from contextlib import contextmanager
@contextmanager
def atomic(using=None):
sid = transaction.savepoint(using=using)
try:
yield
except IntegrityError:
transaction.savepoint_rollback(sid, using=using)
raise
else:
transaction.savepoint_commit(sid, using=using)
@python_2_unicode_compatible @python_2_unicode_compatible
class TagBase(models.Model): class TagBase(models.Model):
name = models.CharField(verbose_name=_('Name'), unique=True, max_length=100) name = models.CharField(verbose_name=_('Name'), unique=True, max_length=100)
...@@ -30,17 +47,14 @@ class TagBase(models.Model): ...@@ -30,17 +47,14 @@ class TagBase(models.Model):
# with a multi-master setup, theoretically we could try to # with a multi-master setup, theoretically we could try to
# write and rollback on different DBs # write and rollback on different DBs
kwargs["using"] = using kwargs["using"] = using
trans_kwargs = {"using": using}
i = 0 i = 0
while True: while True:
i += 1 i += 1
try: try:
sid = transaction.savepoint(**trans_kwargs) with atomic(using=using):
res = super(TagBase, self).save(*args, **kwargs) res = super(TagBase, self).save(*args, **kwargs)
transaction.savepoint_commit(sid, **trans_kwargs)
return res return res
except IntegrityError: except IntegrityError:
transaction.savepoint_rollback(sid, **trans_kwargs)
self.slug = self.slugify(self.name, i) self.slug = self.slugify(self.name, i)
else: else:
return super(TagBase, self).save(*args, **kwargs) return super(TagBase, self).save(*args, **kwargs)
......
...@@ -58,24 +58,6 @@ deps = ...@@ -58,24 +58,6 @@ deps =
{[testenv]deps} {[testenv]deps}
{[testenv]deps17} {[testenv]deps17}
[testenv:py32-1.5.x]
basepython = python3.2
deps =
{[testenv]deps}
{[testenv]deps15}
[testenv:py32-1.6.x]
basepython = python3.2
deps =
{[testenv]deps}
{[testenv]deps16}
[testenv:py32-1.7.x]
basepython = python3.2
deps =
{[testenv]deps}
{[testenv]deps17}
[testenv:py33-1.5.x] [testenv:py33-1.5.x]
basepython = python3.3 basepython = python3.3
deps = deps =
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment