From 3a2cece7dd4b3d111854c8e130821f1d484ca607 Mon Sep 17 00:00:00 2001 From: Raymond Yee Date: Mon, 6 Feb 2012 11:26:48 -0800 Subject: [PATCH 1/2] Adding a bit more info to recluster_singletons but now requires a max parameter Also adding __unicode__ method to Identifier --- .../commands/recluster_singletons.py | 23 +++++++++++++++---- core/models.py | 3 +++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/core/management/commands/recluster_singletons.py b/core/management/commands/recluster_singletons.py index 39ac22fe..03614ed3 100644 --- a/core/management/commands/recluster_singletons.py +++ b/core/management/commands/recluster_singletons.py @@ -7,18 +7,31 @@ This script goes through all singleton works and attempts to add_related. 'xx' w from django.core.management.base import BaseCommand from django.db.models import Count from regluit.core import models, bookloader +from itertools import islice class Command(BaseCommand): help = "add and merge editions for singleton works" - args = "" + args = " " + - def handle(self, language, **options): + def handle(self, language, max, **options): print "Number of singleton Works with language = %s: %s" % (language, models.Work.objects.annotate(num_editions=Count('editions')).filter(num_editions=1, language=language).count()) - for work in models.Work.objects.annotate(num_editions=Count('editions')).filter(num_editions=1, language=language): + try: + max = int(max) + except: + max = None + + for (i, work) in enumerate(islice(models.Work.objects.annotate(num_editions=Count('editions')).filter(num_editions=1, language=language),max)): #check that there's still only one edition + print "%d %s id:%s #editions:%d #isbn:%s -->" % (i, work, work.id, work.editions.count(), work.first_isbn_13()), if work.editions.count() != 1: + print continue - new_editions = bookloader.add_related( work.first_isbn_13() ) - print "clustered %s editions for work %s" % (len(new_editions),work ) + if work.first_isbn_13(): + new_editions = bookloader.add_related( work.first_isbn_13() ) + print "clustered %s editions for work %s" % (len(new_editions),work ), \ + "| Corresponding works : ", [(w.id, w.language, w.editions.count()) for w in set([ed.work for ed in new_editions])] + else: + print "no ISBN for this work and therefore no new editions" print "Updated Number of singleton Works with language = %s: %s" % (language,models.Work.objects.annotate(num_editions=Count('editions')).filter(num_editions=1, language=language).count() ) diff --git a/core/models.py b/core/models.py index e21a3094..8ab5bc36 100755 --- a/core/models.py +++ b/core/models.py @@ -227,6 +227,9 @@ class Identifier(models.Model): i=Identifier(type=type, value=value, edition=edition, work=work) i.save() return i + + def __unicode__(self): + return u'{0}:{1}'.format(self.type, self.value) class Work(models.Model): created = models.DateTimeField(auto_now_add=True) From 26e8024eb219607c3c03c9c34e38da281d0c1a6d Mon Sep 17 00:00:00 2001 From: Raymond Yee Date: Mon, 6 Feb 2012 11:34:11 -0800 Subject: [PATCH 2/2] Compute the number of works corresponding to new editions --- core/management/commands/recluster_singletons.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/core/management/commands/recluster_singletons.py b/core/management/commands/recluster_singletons.py index 03614ed3..85d511e0 100644 --- a/core/management/commands/recluster_singletons.py +++ b/core/management/commands/recluster_singletons.py @@ -30,8 +30,10 @@ class Command(BaseCommand): continue if work.first_isbn_13(): new_editions = bookloader.add_related( work.first_isbn_13() ) + corresponding_works = set([ed.work for ed in new_editions]) print "clustered %s editions for work %s" % (len(new_editions),work ), \ - "| Corresponding works : ", [(w.id, w.language, w.editions.count()) for w in set([ed.work for ed in new_editions])] + "| Corresponding works : ", [(w.id, w.language, w.editions.count()) for w in corresponding_works], \ + "#corresponding_works:%s" % (len(corresponding_works)) else: print "no ISBN for this work and therefore no new editions" print "Updated Number of singleton Works with language = %s: %s" % (language,models.Work.objects.annotate(num_editions=Count('editions')).filter(num_editions=1, language=language).count() )