tangentlabs/django-oscar — pull request #857 by alexcrawley (2013-09-30 15:01:59+00:00)
Title: Handle sorl integrity error issues

The sandbox site raises lots of exceptions like this one:
```
Traceback (most recent call last):

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/Django-1.5.2-py2.6.egg/django/core/handlers/base.py", line 140, in get_response
response = response.render()

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/Django-1.5.2-py2.6.egg/django/template/response.py", line 105, in render
self.content = self.rendered_content

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/Django-1.5.2-py2.6.egg/django/template/response.py", line 82, in rendered_content
content = template.render(context)

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/Django-1.5.2-py2.6.egg/django/template/base.py", line 140, in render
return self._render(context)

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/Django-1.5.2-py2.6.egg/django/template/base.py", line 134, in _render
return self.nodelist.render(context)

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/Django-1.5.2-py2.6.egg/django/template/base.py", line 830, in render
bit = self.render_node(node, context)

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/Django-1.5.2-py2.6.egg/django/template/base.py", line 844, in render_node
return node.render(context)

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/Django-1.5.2-py2.6.egg/django/template/loader_tags.py", line 124, in render
return compiled_parent._render(context)

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/Django-1.5.2-py2.6.egg/django/template/base.py", line 134, in _render
return self.nodelist.render(context)

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/Django-1.5.2-py2.6.egg/django/template/base.py", line 830, in render
bit = self.render_node(node, context)

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/Django-1.5.2-py2.6.egg/django/template/base.py", line 844, in render_node
return node.render(context)

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/Django-1.5.2-py2.6.egg/django/template/loader_tags.py", line 124, in render
return compiled_parent._render(context)

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/Django-1.5.2-py2.6.egg/django/template/base.py", line 134, in _render
return self.nodelist.render(context)

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/Django-1.5.2-py2.6.egg/django/template/base.py", line 830, in render
bit = self.render_node(node, context)

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/Django-1.5.2-py2.6.egg/django/template/base.py", line 844, in render_node
return node.render(context)

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/Django-1.5.2-py2.6.egg/django/template/loader_tags.py", line 63, in render
result = block.nodelist.render(context)

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/Django-1.5.2-py2.6.egg/django/template/base.py", line 830, in render
bit = self.render_node(node, context)

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/Django-1.5.2-py2.6.egg/django/template/base.py", line 844, in render_node
return node.render(context)

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/Django-1.5.2-py2.6.egg/django/template/loader_tags.py", line 63, in render
result = block.nodelist.render(context)

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/Django-1.5.2-py2.6.egg/django/template/base.py", line 830, in render
bit = self.render_node(node, context)

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/Django-1.5.2-py2.6.egg/django/template/base.py", line 844, in render_node
return node.render(context)

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/Django-1.5.2-py2.6.egg/django/template/loader_tags.py", line 63, in render
result = block.nodelist.render(context)

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/Django-1.5.2-py2.6.egg/django/template/base.py", line 830, in render
bit = self.render_node(node, context)

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/Django-1.5.2-py2.6.egg/django/template/base.py", line 844, in render_node
return node.render(context)

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/Django-1.5.2-py2.6.egg/django/template/defaulttags.py", line 483, in render
output = self.nodelist.render(context)

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/Django-1.5.2-py2.6.egg/django/template/base.py", line 830, in render
bit = self.render_node(node, context)

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/Django-1.5.2-py2.6.egg/django/template/base.py", line 844, in render_node
return node.render(context)

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/sorl_thumbnail-11.12-py2.6.egg/sorl/thumbnail/templatetags/thumbnail.py", line 45, in render
return self._render(context)

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/sorl_thumbnail-11.12-py2.6.egg/sorl/thumbnail/templatetags/thumbnail.py", line 97, in _render
file_, geometry, **options

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/sorl_thumbnail-11.12-py2.6.egg/sorl/thumbnail/base.py", line 65, in get_thumbnail
default.kvstore.get_or_set(source)

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/sorl_thumbnail-11.12-py2.6.egg/sorl/thumbnail/kvstores/base.py", line 49, in get_or_set
self.set(image_file)

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/sorl_thumbnail-11.12-py2.6.egg/sorl/thumbnail/kvstores/base.py", line 33, in set
self._set(image_file.key, image_file)

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/sorl_thumbnail-11.12-py2.6.egg/sorl/thumbnail/kvstores/base.py", line 138, in _set
self._set_raw(add_prefix(key, identity), s)

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/sorl_thumbnail-11.12-py2.6.egg/sorl/thumbnail/kvstores/cached_db_kvstore.py", line 36, in _set_raw
kv = KVStoreModel.objects.get_or_create(key=key)[0]

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/Django-1.5.2-py2.6.egg/django/db/models/manager.py", line 146, in get_or_create
return self.get_query_set().get_or_create(**kwargs)

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/Django-1.5.2-py2.6.egg/django/db/models/query.py", line 501, in get_or_create
six.reraise(*exc_info)

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/Django-1.5.2-py2.6.egg/django/db/models/query.py", line 491, in get_or_create
obj.save(force_insert=True, using=self.db)

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/Django-1.5.2-py2.6.egg/django/db/models/base.py", line 546, in save
force_update=force_update, update_fields=update_fields)

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/Django-1.5.2-py2.6.egg/django/db/models/base.py", line 650, in save_base
result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/Django-1.5.2-py2.6.egg/django/db/models/manager.py", line 215, in _insert
return insert_query(self.model, objs, fields, **kwargs)

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/Django-1.5.2-py2.6.egg/django/db/models/query.py", line 1675, in insert_query
return query.get_compiler(using=using).execute_sql(return_id)

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/Django-1.5.2-py2.6.egg/django/db/models/sql/compiler.py", line 937, in execute_sql
cursor.execute(sql, params)

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/Django-1.5.2-py2.6.egg/django/db/backends/mysql/base.py", line 122, in execute
six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/Django-1.5.2-py2.6.egg/django/db/backends/mysql/base.py", line 120, in execute
return self.cursor.execute(query, args)

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/MySQLdb/cursors.py", line 174, in execute
self.errorhandler(self, exc, value)

File "/var/www/oscar/virtualenvs/sandbox/lib/python2.6/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue

IntegrityError: (1062, "Duplicate entry 'oscar-sandbox||image||54ae37ded22894b9f773ccaf7217e78e' for key 'PRIMARY'")
```

I think this is a race condition that isn't handled in Sorl. I suspect the right fix is to write our own sorl wrapper that catches this exception. See https://github.com/sorl/sorl-thumbnail/issues/162