improve merge_works

work_relations were not being updated
pull/43/head
eric 2017-09-04 16:10:24 -04:00
parent 91ae62e757
commit 5bbeb45053
4 changed files with 19 additions and 14 deletions

View File

@ -413,7 +413,6 @@ def relate_isbn(isbn, cluster_size=1):
elif related_edition.work.id != edition.work.id:
logger.debug("merge_works path 1 %s %s", edition.work.id, related_edition.work.id )
merge_works(related_edition.work, edition.work)
if related_edition.work.editions.count()>cluster_size:
return related_edition.work
return edition.work
@ -452,7 +451,7 @@ def add_related(isbn):
related_edition.save()
elif related_edition.work.id != work.id:
logger.debug("merge_works path 1 %s %s", work.id, related_edition.work.id )
merge_works(work, related_edition.work)
work = merge_works(work, related_edition.work)
else:
if other_editions.has_key(related_language):
other_editions[related_language].append(related_edition)
@ -469,12 +468,15 @@ def add_related(isbn):
works_to_merge = set([ed.work for ed in lang_group[1:]]) - set([lang_edition.work])
for w in works_to_merge:
logger.debug("merge_works path 2 %s %s", lang_edition.work.id, w.id )
merge_works(lang_edition.work, w)
models.WorkRelation.objects.get_or_create(to_work=lang_edition.work, from_work=work, relation='translation')
merged_work = merge_works(lang_edition.work, w)
models.WorkRelation.objects.get_or_create(
to_work=lang_group[0].work,
from_work=work,
relation='translation'
)
return new_editions
def thingisbn(isbn):
"""given an ISBN return a list of related edition ISBNs, according to
Library Thing. (takes isbn_10 or isbn_13, returns isbn_10, except for 979 isbns, which come back as isbn_13')
@ -492,7 +494,7 @@ def merge_works(w1, w2, user=None):
logger.info("merging work %s into %s", w2.id, w1.id)
# don't merge if the works are the same or at least one of the works has no id (for example, when w2 has already been deleted)
if w1 is None or w2 is None or w1.id == w2.id or w1.id is None or w2.id is None:
return
return w1
if w2.selected_edition != None and w1.selected_edition == None:
#the merge should be reversed
temp = w1
@ -546,9 +548,14 @@ def merge_works(w1, w2, user=None):
for subject in w2.subjects.all():
if subject not in w1.subjects.all():
w1.subjects.add(subject)
for work_relation in w2.works_related_to.all():
work_relation.to_work = w1
work_relation.save()
for work_relation in w2.works_related_from.all():
work_relation.from_work = w1
work_relation.save()
w2.delete()
return w1
def detach_edition(e):
"""will detach edition from its work, creating a new stub work. if remerge=true, will see if there's another work to attach to

View File

@ -130,9 +130,9 @@ def add_all_isbns(isbns, work, language=None, title=None):
first_edition = first_edition if first_edition else edition
if work and (edition.work.id != work.id):
if work.created < edition.work.created:
merge_works(work, edition.work)
work = merge_works(work, edition.work)
else:
merge_works(edition.work, work)
work = merge_works(edition.work, work)
else:
work = edition.work
return first_edition

View File

@ -220,9 +220,7 @@ def load_from_books(books):
for isbn in isbns:
edition = add_by_isbn_from_google(isbn, work=work)
if edition and edition.work != work:
merge_works(work, edition.work)
work = work if work.pk is not None else edition.work
edition.work=work # because integrity errors if not
work = merge_works(work, edition.work)
if not edition:
edition= Edition(title=title, work=work)
edition.save()

View File

@ -968,7 +968,7 @@ class MergeView(FormView):
context = self.get_context_data()
if self.request.POST.has_key('confirm_merge_works'):
context['old_work_id'] = other_work.id
merge_works(self.work, other_work, self.request.user)
self.work = merge_works(self.work, other_work, self.request.user)
context['merge_complete'] = True
else:
context['form'] = WorkForm(initial={'other_work':other_work})