From f79bb60d81a2bff5ac5c6ff8e49a81d67fda0387 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 15 Mar 2021 15:08:20 -0700 Subject: [PATCH] Adds ability to reset default edition based on computer ranks --- bookwyrm/models/book.py | 19 ++++++++++++++++--- bookwyrm/templates/editions.html | 2 +- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/bookwyrm/models/book.py b/bookwyrm/models/book.py index 66b539bb..5306950c 100644 --- a/bookwyrm/models/book.py +++ b/bookwyrm/models/book.py @@ -1,7 +1,7 @@ """ database schema for books and shelves """ import re -from django.db import models +from django.db import models, transaction from model_utils.managers import InheritanceManager from bookwyrm import activitypub @@ -148,6 +148,15 @@ class Work(OrderedCollectionPageMixin, Book): """ in case the default edition is not set """ return self.default_edition or self.editions.order_by("-edition_rank").first() + @transaction.atomic() + def reset_default_edition(self): + """ sets a new default edition based on computed rank """ + self.default_edition = None + # editions are re-ranked implicitly + self.save() + self.default_edition = self.get_default_edition() + self.save() + def to_edition_list(self, **kwargs): """ an ordered collection of editions """ return self.to_ordered_collection( @@ -200,9 +209,13 @@ class Edition(Book): activity_serializer = activitypub.Edition name_field = "title" - def get_rank(self): + def get_rank(self, ignore_default=False): """ calculate how complete the data is on this edition """ - if self.parent_work and self.parent_work.default_edition == self: + if ( + not ignore_default + and self.parent_work + and self.parent_work.default_edition == self + ): # default edition has the highest rank return 20 rank = 0 diff --git a/bookwyrm/templates/editions.html b/bookwyrm/templates/editions.html index 38147a86..f8319757 100644 --- a/bookwyrm/templates/editions.html +++ b/bookwyrm/templates/editions.html @@ -6,7 +6,7 @@ {% block content %}
-

{% blocktrans with path=work.local_path work_title=work.title %}Editions of "{{ work_title }}"{% endblocktrans %}

+

{% blocktrans with work_path=work.local_path work_title=work.title %}Editions of "{{ work_title }}"{% endblocktrans %}

{% include 'snippets/book_tiles.html' with books=editions %}