-
Notifications
You must be signed in to change notification settings - Fork 170
Book percentage progress #1245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
connorjburton
wants to merge
6
commits into
FuzzyGrim:dev
Choose a base branch
from
connorjburton:book-percentage-progress
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Book percentage progress #1245
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
97f6169
add migration for default book progress and set existing books progre…
connorjburton 4f78873
preferences option for default book progress
connorjburton 6f37cfb
add support for percentage on media modals, including converting betw…
connorjburton 4818341
support displaying % or pages throughout the different views of data
connorjburton d8dcb44
add test for pages/% feature for books model
connorjburton 3abfb88
ruff changes
connorjburton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/app/migrations/0056_book_progress_unit_historicalbook_progress_unit.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # Generated by Django 5.2.11 on 2026-03-10 00:34 | ||
|
|
||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ('app', '0055_alter_item_media_type'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AddField( | ||
| model_name='book', | ||
| name='progress_unit', | ||
| field=models.CharField(blank=True, default=None, max_length=20, null=True), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='historicalbook', | ||
| name='progress_unit', | ||
| field=models.CharField(blank=True, default=None, max_length=20, null=True), | ||
| ), | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| from unittest.mock import patch | ||
|
|
||
| from django.contrib.auth import get_user_model | ||
| from django.test import TestCase | ||
|
|
||
| from app.models import ( | ||
| Book, | ||
| Item, | ||
| MediaTypes, | ||
| Sources, | ||
| Status, | ||
| ) | ||
| from users.models import ProgressUnit | ||
|
|
||
|
|
||
| @patch("app.providers.services.get_media_metadata") | ||
| class BookModelTests(TestCase): | ||
| """Test case for the Book model methods.""" | ||
|
|
||
| def setUp(self): | ||
| """Set up test data for Book model tests.""" | ||
| self.credentials = {"username": "test", "password": "12345"} | ||
| self.user = get_user_model().objects.create_user(**self.credentials) | ||
|
|
||
| self.item = Item.objects.create( | ||
| media_id="book123", | ||
| source=Sources.OPENLIBRARY.value, | ||
| media_type=MediaTypes.BOOK.value, | ||
| title="Test Book", | ||
| ) | ||
|
|
||
| def test_get_progress_unit_fallback(self, mock_metadata): | ||
| """Test that get_progress_unit falls back to user preference.""" | ||
| mock_metadata.return_value = {"max_progress": 200} | ||
| book = Book.objects.create( | ||
| item=self.item, | ||
| user=self.user, | ||
| status=Status.PLANNING.value, | ||
| ) | ||
|
|
||
| # Default user preference is PAGES | ||
| self.assertEqual(book.get_progress_unit(), ProgressUnit.PAGES) | ||
|
|
||
| # Update user preference to PERCENTAGE | ||
| self.user.book_progress_unit = ProgressUnit.PERCENTAGE | ||
| self.user.save() | ||
| self.assertEqual(book.get_progress_unit(), ProgressUnit.PERCENTAGE) | ||
|
|
||
| # Override on book specifically | ||
| book.progress_unit = ProgressUnit.PAGES | ||
| book.save() | ||
| self.assertEqual(book.get_progress_unit(), ProgressUnit.PAGES) | ||
|
|
||
| def test_process_progress_percentage(self, mock_metadata): | ||
| """Test progress processing when using percentage unit.""" | ||
| mock_metadata.return_value = {"max_progress": 200} | ||
|
|
||
| book = Book.objects.create( | ||
| item=self.item, | ||
| user=self.user, | ||
| status=Status.IN_PROGRESS.value, | ||
| progress_unit=ProgressUnit.PERCENTAGE, | ||
| ) | ||
|
|
||
| # Set progress to 50% | ||
| book.progress = 50 | ||
| book.save() | ||
| self.assertEqual(book.status, Status.IN_PROGRESS.value) | ||
|
|
||
| # Set progress to 100% | ||
| book.progress = 100 | ||
| book.save() | ||
| self.assertEqual(book.status, Status.COMPLETED.value) | ||
| self.assertIsNotNone(book.end_date) | ||
|
|
||
| # Set progress > 100% should be capped | ||
| book.status = Status.IN_PROGRESS.value | ||
| book.progress = 150 | ||
| book.save() | ||
| self.assertEqual(book.progress, 100) | ||
|
|
||
| def test_formatted_progress_percentage(self, mock_metadata): | ||
| """Test formatting of progress when using percentage unit.""" | ||
| mock_metadata.return_value = {"max_progress": 200} | ||
| book = Book.objects.create( | ||
| item=self.item, | ||
| user=self.user, | ||
| status=Status.IN_PROGRESS.value, | ||
| progress=45, | ||
| progress_unit=ProgressUnit.PERCENTAGE, | ||
| ) | ||
| self.assertEqual(book.formatted_progress, "45%") | ||
|
|
||
| def test_formatted_progress_pages(self, mock_metadata): | ||
| """Test formatting of progress when using pages unit.""" | ||
| mock_metadata.return_value = {"max_progress": 200} | ||
| book = Book.objects.create( | ||
| item=self.item, | ||
| user=self.user, | ||
| status=Status.IN_PROGRESS.value, | ||
| progress=150, | ||
| progress_unit=ProgressUnit.PAGES, | ||
| ) | ||
| # Mock max_progress annotation which usually comes from MediaManager | ||
| book.max_progress = 300 | ||
| self.assertEqual(book.formatted_progress, "150 / 300") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method contains duplicated logic for marking an item as completed. To improve maintainability and adhere to the DRY principle, you could extract the completion logic into a local function.