From 6db64e33e46139deecf7f1c18f033897570d13f5 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 11 Jan 2021 09:29:23 -0800 Subject: [PATCH] set rank on editions --- .../migrations/0035_edition_edition_rank.py | 7 ++++++ bookwyrm/models/book.py | 23 ++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/bookwyrm/migrations/0035_edition_edition_rank.py b/bookwyrm/migrations/0035_edition_edition_rank.py index b0bd51c5..1a75a097 100644 --- a/bookwyrm/migrations/0035_edition_edition_rank.py +++ b/bookwyrm/migrations/0035_edition_edition_rank.py @@ -4,6 +4,12 @@ import bookwyrm.models.fields 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): dependencies = [ @@ -16,4 +22,5 @@ class Migration(migrations.Migration): name='edition_rank', field=bookwyrm.models.fields.IntegerField(default=0), ), + migrations.RunPython(set_rank), ] diff --git a/bookwyrm/models/book.py b/bookwyrm/models/book.py index bfb8f85e..7a9e6ee8 100644 --- a/bookwyrm/models/book.py +++ b/bookwyrm/models/book.py @@ -169,13 +169,34 @@ class Edition(Book): activity_serializer = activitypub.Edition 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): - ''' 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: self.isbn_10 = isbn_13_to_10(self.isbn_13) if self.isbn_10 and not self.isbn_13: self.isbn_13 = isbn_10_to_13(self.isbn_10) + # set rank + self.edition_rank = self.get_rank + return super().save(*args, **kwargs)