Update edition ranks when work is saved

This commit is contained in:
Mouse Reeve 2021-01-11 09:49:32 -08:00
parent 6db64e33e4
commit 83852e29eb
4 changed files with 26 additions and 1 deletions

View file

@ -41,6 +41,7 @@ class Edition(Book):
pages: int = None pages: int = None
physicalFormat: str = '' physicalFormat: str = ''
publishers: List[str] = field(default_factory=lambda: []) publishers: List[str] = field(default_factory=lambda: [])
editionRank: int = 0
type: str = 'Edition' type: str = 'Edition'

View file

@ -8,6 +8,7 @@ def set_rank(app_registry, schema_editor):
db_alias = schema_editor.connection.alias db_alias = schema_editor.connection.alias
books = app_registry.get_model('bookwyrm', 'Edition') books = app_registry.get_model('bookwyrm', 'Edition')
for book in books.objects.using(db_alias): for book in books.objects.using(db_alias):
book.edition_rank = book.get_rank
book.save() book.save()
class Migration(migrations.Migration): class Migration(migrations.Migration):

View file

@ -122,6 +122,13 @@ class Work(OrderedCollectionPageMixin, Book):
load_remote=False load_remote=False
) )
def save(self, *args, **kwargs):
''' set some fields on the edition object '''
# set rank
for edition in self.editions.all():
edition.save()
return super().save(*args, **kwargs)
def get_default_edition(self): def get_default_edition(self):
''' in case the default edition is not set ''' ''' in case the default edition is not set '''
return self.default_edition or self.editions.first() return self.default_edition or self.editions.first()
@ -172,7 +179,7 @@ class Edition(Book):
@property @property
def get_rank(self): def get_rank(self):
''' calculate how complete the data is on this edition ''' ''' calculate how complete the data is on this edition '''
if self.parent_work.default_edition == self: if self.parent_work and self.parent_work.default_edition == self:
# default edition has the highest rank # default edition has the highest rank
return 20 return 20
rank = 0 rank = 0

View file

@ -82,3 +82,19 @@ class Book(TestCase):
self.assertEqual(book.edition_info, 'worm, Glorbish language, 2020') self.assertEqual(book.edition_info, 'worm, Glorbish language, 2020')
self.assertEqual( self.assertEqual(
book.alt_text, 'Test Edition cover (worm, Glorbish language, 2020)') book.alt_text, 'Test Edition cover (worm, Glorbish language, 2020)')
def test_get_rank(self):
''' sets the data quality index for the book '''
# basic rank
self.assertEqual(self.first_edition.edition_rank, 0)
self.first_edition.description = 'hi'
self.first_edition.save()
self.assertEqual(self.first_edition.edition_rank, 1)
# default edition
self.work.default_edition = self.first_edition
self.work.save()
self.first_edition.refresh_from_db()
self.assertEqual(self.first_edition.edition_rank, 20)