Adding a bit more info to recluster_singletons but now requires a max parameter

Also adding __unicode__ method to Identifier
pull/1/head
Raymond Yee 2012-02-06 11:26:48 -08:00
parent 4a7f82d513
commit 3a2cece7dd
2 changed files with 21 additions and 5 deletions

View File

@ -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 = "<language>"
args = "<language> <max>"
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
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 )
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() )

View File

@ -228,6 +228,9 @@ class Identifier(models.Model):
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)
title = models.CharField(max_length=1000)