deleting works should delete attached objects

I think the only place this should get invoked is via admin
pull/91/head
eric 2018-06-18 22:07:29 -04:00
parent 2f9dda8432
commit 604260363f
2 changed files with 30 additions and 2 deletions

View File

@ -597,7 +597,7 @@ def merge_works(w1, w2, user=None):
for work_relation in w2.works_related_from.all():
work_relation.from_work = w1
work_relation.save()
w2.delete()
w2.delete(cascade=False)
return w1
def detach_edition(e):

View File

@ -22,6 +22,8 @@ from django.db.models import F
from django.db.models.signals import post_save, pre_delete
from django.utils.timezone import now
from django_comments.models import Comment
import regluit
from regluit.marc.models import MARCRecord as NewMARC
from questionnaire.models import Landing
@ -131,13 +133,39 @@ class Work(models.Model):
class Meta:
ordering = ['title']
def __unicode__(self):
return self.title
def __init__(self, *args, **kwargs):
self._last_campaign = None
super(Work, self).__init__(*args, **kwargs)
def delete(self, *args, **kwargs):
if kwargs.get('cascade', True):
if self.offers.all() or self.claim.all() or self.campaigns.all() or self.acqs.all() \
or self.holds.all() or self.landings.all():
return
for wishlist in self.wishlists.all():
wishlist.remove_work(self)
for userprofile in self.contributors.all():
userprofile.works.remove(self)
for identifier in self.identifiers.all():
identifier.delete()
for comment in Comment.objects.for_model(self):
comment.delete()
for edition in self.editions.all():
for ebook in edition.ebooks.all():
ebook.delete()
for ebookfile in edition.ebook_files.all():
ebookfile.delete()
edition.delete()
for work_relation in self.works_related_to.all():
work_relation.delete()
for work_relation in self.works_related_from.all():
work_relation.delete()
super(Work, self).delete(*args, **kwargs) # Call the "real" save() method.
def id_for(self, type):
return id_for(self, type)