set rank on editions

This commit is contained in:
Mouse Reeve 2021-01-11 09:29:23 -08:00
parent b44b42be7e
commit 6db64e33e4
2 changed files with 29 additions and 1 deletions

View file

@ -4,6 +4,12 @@ import bookwyrm.models.fields
from django.db import migrations from django.db import migrations
def set_rank(app_registry, schema_editor):
db_alias = schema_editor.connection.alias
books = app_registry.get_model('bookwyrm', 'Edition')
for book in books.objects.using(db_alias):
book.save()
class Migration(migrations.Migration): class Migration(migrations.Migration):
dependencies = [ dependencies = [
@ -16,4 +22,5 @@ class Migration(migrations.Migration):
name='edition_rank', name='edition_rank',
field=bookwyrm.models.fields.IntegerField(default=0), field=bookwyrm.models.fields.IntegerField(default=0),
), ),
migrations.RunPython(set_rank),
] ]

View file

@ -169,13 +169,34 @@ class Edition(Book):
activity_serializer = activitypub.Edition activity_serializer = activitypub.Edition
name_field = 'title' name_field = 'title'
@property
def get_rank(self):
''' calculate how complete the data is on this edition '''
if self.parent_work.default_edition == self:
# default edition has the highest rank
return 20
rank = 0
rank += int(bool(self.cover)) * 3
rank += int(bool(self.isbn_13))
rank += int(bool(self.isbn_10))
rank += int(bool(self.oclc_number))
rank += int(bool(self.pages))
rank += int(bool(self.physical_format))
rank += int(bool(self.description))
# max rank is 9
return rank
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
''' calculate isbn 10/13 ''' ''' set some fields on the edition object '''
# calculate isbn 10/13
if self.isbn_13 and self.isbn_13[:3] == '978' and not self.isbn_10: if self.isbn_13 and self.isbn_13[:3] == '978' and not self.isbn_10:
self.isbn_10 = isbn_13_to_10(self.isbn_13) self.isbn_10 = isbn_13_to_10(self.isbn_13)
if self.isbn_10 and not self.isbn_13: if self.isbn_10 and not self.isbn_13:
self.isbn_13 = isbn_10_to_13(self.isbn_10) self.isbn_13 = isbn_10_to_13(self.isbn_10)
# set rank
self.edition_rank = self.get_rank
return super().save(*args, **kwargs) return super().save(*args, **kwargs)