From ec8bb8fc31eab313f417038cef13f61896006cea Mon Sep 17 00:00:00 2001 From: dellsystem Date: Thu, 2 May 2024 11:51:21 -0700 Subject: [PATCH] Add feature for ignoring goodreads book IDs For sync page. Closes #219. Closes #218. Helpful for #173. --- src/books/admin.py | 7 ++++++- src/books/goodreadstools.py | 14 +++++++++++++- src/books/models.py | 9 +++++++++ src/templates/sync_goodreads.html | 5 +++++ 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/books/admin.py b/src/books/admin.py index 34ef467..60a15b2 100644 --- a/src/books/admin.py +++ b/src/books/admin.py @@ -3,7 +3,7 @@ from django.utils.html import mark_safe from .models import GoodreadsAuthor, Author, Book, BookDetails, BookLocation, \ - Note, TagCategory, Tag, ReadingGoal, Section + Note, TagCategory, Tag, ReadingGoal, Section, IgnoredBook class GoodreadsAuthorInline(admin.TabularInline): @@ -74,3 +74,8 @@ def display_tag(self, obj): s=obj, ) ) + + +@admin.register(IgnoredBook) +class IgnoredBookAdmin(admin.ModelAdmin): + list_display = ['description', 'reason'] diff --git a/src/books/goodreadstools.py b/src/books/goodreadstools.py index e416694..2534c76 100644 --- a/src/books/goodreadstools.py +++ b/src/books/goodreadstools.py @@ -3,7 +3,7 @@ import requests import urllib.parse -from books.models import BookDetails, GoodreadsAuthor +from books.models import BookDetails, GoodreadsAuthor, IgnoredBook RESOLUTION = '_SY475_' @@ -108,6 +108,11 @@ def get_books(page): pub_date = row.select('.date_pub_edition .value')[0].text.strip() year = pub_date.split(',')[-1].strip() + ignore_link_params = urllib.parse.urlencode({ + 'goodreads_id': goodreads_id, + 'description': title, + }) + book = { 'id': goodreads_id, 'url': BASE_URL + goodreads_url, @@ -123,6 +128,7 @@ def get_books(page): 'author_url': BASE_URL + author_url, 'author_name': author_name, 'author_id': author_id, + 'ignore_link_params': ignore_link_params, } goodreads_book_ids.add(goodreads_id) goodreads_author_ids.add(author_id) @@ -139,11 +145,17 @@ def get_books(page): for a in author_query: author_dict[a.goodreads_id] = a.author + # Figure out which books need to be ignored + ignored_book_ids = set(IgnoredBook.objects.values_list('goodreads_id', flat=True)) + # Now go back through the books list to update the status # We only need to return books that are either not in the db or that have # some sort of mistake to be corrected. filtered_books = [] for book in books: + if book['id'] in ignored_book_ids: + continue + a = author_dict.get(book['author_id']) if a: book['author'] = a diff --git a/src/books/models.py b/src/books/models.py index f41185c..866a456 100644 --- a/src/books/models.py +++ b/src/books/models.py @@ -626,3 +626,12 @@ def __str__(self): subject=self.subject, book=self.book.title ) + + +class IgnoredBook(models.Model): + goodreads_id = models.CharField(max_length=20, db_index=True) + description = models.CharField(max_length=255) + reason = models.CharField(max_length=20, blank=True) # eg 'audiobook' + + def __str__(self): + return self.description diff --git a/src/templates/sync_goodreads.html b/src/templates/sync_goodreads.html index 936d0db..4759a7c 100644 --- a/src/templates/sync_goodreads.html +++ b/src/templates/sync_goodreads.html @@ -56,6 +56,7 @@ End date Processed? Dates match? + Ignore? @@ -98,6 +99,10 @@ {{ book.dates_comment }} {% endif %} + + + Ignore + {% endfor %}