Skip to content

Commit

Permalink
Add feature for ignoring goodreads book IDs
Browse files Browse the repository at this point in the history
For sync page. Closes #219. Closes #218. Helpful for #173.
  • Loading branch information
dellsystem committed May 2, 2024
1 parent 6693749 commit ec8bb8f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/books/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -74,3 +74,8 @@ def display_tag(self, obj):
s=obj,
)
)


@admin.register(IgnoredBook)
class IgnoredBookAdmin(admin.ModelAdmin):
list_display = ['description', 'reason']
14 changes: 13 additions & 1 deletion src/books/goodreadstools.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import requests
import urllib.parse

from books.models import BookDetails, GoodreadsAuthor
from books.models import BookDetails, GoodreadsAuthor, IgnoredBook


RESOLUTION = '_SY475_'
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand All @@ -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
Expand Down
9 changes: 9 additions & 0 deletions src/books/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions src/templates/sync_goodreads.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<th>End date</th>
<th>Processed?</th>
<th>Dates match?</th>
<th>Ignore?<th>
</tr>
</thead>
<tbody>
Expand Down Expand Up @@ -98,6 +99,10 @@
{{ book.dates_comment }}
</td>
{% endif %}

<td>
<a href="{% url 'admin:books_ignoredbook_add' %}?{{ book.ignore_link_params }}">Ignore</a>
</td>
</tr>
{% endfor %}
</tbody>
Expand Down

0 comments on commit ec8bb8f

Please sign in to comment.