Commit 92d4cbe1 by Florian Apolloner

Merge pull request #202 from apollo13/atomic2

Prevented infinite loops on Tag.save()
parents 0e9f1172 b32c6cbe
......@@ -47,15 +47,26 @@ class TagBase(models.Model):
# with a multi-master setup, theoretically we could try to
# write and rollback on different DBs
kwargs["using"] = using
i = 0
# Be oportunistic and try to save the tag, this should work for
# most cases ;)
try:
with atomic(using=using):
res = super(TagBase, self).save(*args, **kwargs)
return res
except IntegrityError:
pass
# Now try to find existing slugs with similar names
slugs = set(Tag.objects.filter(slug__startswith=self.slug)\
.values_list('slug', flat=True))
i = 1
while True:
slug = self.slugify(self.name, i)
if slug not in slugs:
self.slug = slug
# We purposely ignore concurrecny issues here for now.
# (That is, till we found a nice solution...)
return super(TagBase, self).save(*args, **kwargs)
i += 1
try:
with atomic(using=using):
res = super(TagBase, self).save(*args, **kwargs)
return res
except IntegrityError:
self.slug = self.slugify(self.name, i)
else:
return super(TagBase, self).save(*args, **kwargs)
......
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