Skip to content

Commit

Permalink
Drop the old delete hack that was used for Django 1.4. Allows enforci…
Browse files Browse the repository at this point in the history
…ng non-null FK to master.
  • Loading branch information
spectras committed Jun 13, 2016
1 parent 1b94f8a commit 9e143b8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 29 deletions.
12 changes: 12 additions & 0 deletions docs/public/release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@
Release Notes
#############


*****************************
1.6.0 - upcoming release
*****************************

Fixes:

- No longer set ``master`` to ``NULL`` before clearing translations when using
:meth:`~hvad.manager.TranslationManager.delete_translations`. This only
triggers one query instead of two, and allows enforcing non-null foreign key
at the database level.

.. release 1.5.1
*****************************
Expand Down
57 changes: 29 additions & 28 deletions hvad/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import django
from django.conf import settings
from django.core.exceptions import FieldError
from django.db import models, transaction, IntegrityError
from django.db import connections, models, transaction, IntegrityError
if django.VERSION >= (1, 9):
from django.db.models.query import QuerySet
elif django.VERSION >= (1, 8):
Expand Down Expand Up @@ -545,31 +545,27 @@ def iterator(self):
objects = super(TranslationQueryset, qs).iterator()

for obj in objects:
# non-cascade-deletion hack:
if not obj.master:
yield obj
else:
for name in self._hvad_switch_fields:
try:
setattr(obj.master, name, getattr(obj, name))
except AttributeError: # pragma: no cover
pass
else:
delattr(obj, name)
obj = combine(obj, qs.shared_model)
# use known objects from self, not qs as we cleared it earlier
for field, rel_objs in self._known_related_objects.items():
if hasattr(obj, field.get_cache_name()):
# should not happen, but we conform to Django behavior
continue #pragma: no cover
pk = getattr(obj, field.get_attname())
try:
rel_obj = rel_objs[pk]
except KeyError: #pragma: no cover
pass
else:
setattr(obj, field.name, rel_obj)
yield obj
for name in self._hvad_switch_fields:
try:
setattr(obj.master, name, getattr(obj, name))
except AttributeError: # pragma: no cover
pass
else:
delattr(obj, name)
obj = combine(obj, qs.shared_model)
# use known objects from self, not qs as we cleared it earlier
for field, rel_objs in self._known_related_objects.items():
if hasattr(obj, field.get_cache_name()):
# should not happen, but we conform to Django behavior
continue #pragma: no cover
pk = getattr(obj, field.get_attname())
try:
rel_obj = rel_objs[pk]
except KeyError: #pragma: no cover
pass
else:
setattr(obj, field.name, rel_obj)
yield obj

def create(self, **kwargs):
if 'language_code' in kwargs:
Expand Down Expand Up @@ -687,8 +683,13 @@ def delete(self):
delete.queryset_only = True

def delete_translations(self):
self.update(master=None)
self.model.objects.filter(master__isnull=True).delete()
if connections[self._db].features.update_can_self_select:
qs = self._clone()._add_language_filter()
super(TranslationQueryset, qs).delete()
else:
with transaction.atomic(using=self._db, savepoint=False):
pks = list(super(TranslationQueryset, self).values_list('pk', flat=True))
self.model._base_manager.filter(pk__in=pks).delete()
delete_translations.alters_data = True

def update(self, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion hvad/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def create_translations_model(self, model, related_name):
'language_code': models.CharField(max_length=15, db_index=True),
# Nullable so we can prevent cascade deletion
'master': models.ForeignKey(model, related_name=related_name, editable=False,
null=True, on_delete=models.CASCADE),
on_delete=models.CASCADE),
})

# Create the new model
Expand Down

0 comments on commit 9e143b8

Please sign in to comment.