From 358aa142b25fc22c2fa62fdb4930f0a96b99e85c Mon Sep 17 00:00:00 2001 From: Joachim Date: Sat, 1 Jan 2022 21:28:14 +0100 Subject: [PATCH 1/7] Display completed goal on Annual Summary page --- bookwyrm/templates/annual_summary/layout.html | 25 +++++++++++++++++++ bookwyrm/views/annual_summary.py | 21 +++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/bookwyrm/templates/annual_summary/layout.html b/bookwyrm/templates/annual_summary/layout.html index b5ba9cc78..ab25458c8 100644 --- a/bookwyrm/templates/annual_summary/layout.html +++ b/bookwyrm/templates/annual_summary/layout.html @@ -192,6 +192,31 @@ + {% if goal_status and goal_status.percent >= 100 %} +
+
+

+ {% with goal=goal_status.goal goal_percent=goal_status.percent %} + {% blocktrans trimmed count counter=goal %} + {{ display_name }} set a goal of reading {{ goal }} book in {{ year }},
+ and achieved {{ goal_percent }}% of that goal + {% plural %} + {{ display_name }} set a goal of reading {{ goal }} books in {{ year }},
+ and achieved {{ goal_percent }}% of that goal + {% endblocktrans %} + {% endwith %} +

+

{% trans "Way to go!" %}

+
+
+ +
+
+
+
+
+ {% endif %} + {% if ratings_total > 0 %}
diff --git a/bookwyrm/views/annual_summary.py b/bookwyrm/views/annual_summary.py index 00ca4210d..0b618c218 100644 --- a/bookwyrm/views/annual_summary.py +++ b/bookwyrm/views/annual_summary.py @@ -79,6 +79,9 @@ class AnnualSummary(View): ) ratings_stats = ratings.aggregate(Avg("rating")) + # annual goal status + goal_status = get_goal_status(user, year) + data = { "summary_user": user, "year": year, @@ -101,6 +104,7 @@ class AnnualSummary(View): review.book.id for review in ratings.filter(rating=5) ], "paginated_years": paginated_years, + "goal_status": goal_status, } return TemplateResponse(request, "annual_summary/layout.html", data) @@ -195,7 +199,8 @@ def is_year_available(user, year): year = int(year) if earliest_year <= year < today.year: return True - if year == today.year and today >= date(today.year, 12, FIRST_DAY): + #if year == today.year and today >= date(today.year, 12, FIRST_DAY): + if year == today.year and today >= date(today.year, 1, 1): return True return False @@ -208,3 +213,17 @@ def get_books_from_shelfbooks(books_ids): books = models.Edition.objects.filter(id__in=books_ids).order_by(ordered) return books + + +def get_goal_status(user, year): + """return a dict with the year's goal status""" + + try: + goal = models.AnnualGoal.objects.get(user=user, year=year) + except: + return None + + if goal.privacy != "public": + return None + + return dict(**goal.progress, **{"goal": goal.goal}) From 864b731cfa230a996f84333ca1664aef5646417f Mon Sep 17 00:00:00 2001 From: Joachim Date: Sat, 1 Jan 2022 21:30:13 +0100 Subject: [PATCH 2/7] remove testing line --- bookwyrm/views/annual_summary.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bookwyrm/views/annual_summary.py b/bookwyrm/views/annual_summary.py index 0b618c218..960b1550a 100644 --- a/bookwyrm/views/annual_summary.py +++ b/bookwyrm/views/annual_summary.py @@ -199,8 +199,7 @@ def is_year_available(user, year): year = int(year) if earliest_year <= year < today.year: return True - #if year == today.year and today >= date(today.year, 12, FIRST_DAY): - if year == today.year and today >= date(today.year, 1, 1): + if year == today.year and today >= date(today.year, 12, FIRST_DAY): return True return False From 29b688b68d0713c85a578f9a1d868fe4464ac5c3 Mon Sep 17 00:00:00 2001 From: Joachim Date: Sat, 1 Jan 2022 21:32:30 +0100 Subject: [PATCH 3/7] Update annual_summary.py --- bookwyrm/views/annual_summary.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bookwyrm/views/annual_summary.py b/bookwyrm/views/annual_summary.py index 960b1550a..9f37a6f1f 100644 --- a/bookwyrm/views/annual_summary.py +++ b/bookwyrm/views/annual_summary.py @@ -216,7 +216,7 @@ def get_books_from_shelfbooks(books_ids): def get_goal_status(user, year): """return a dict with the year's goal status""" - + try: goal = models.AnnualGoal.objects.get(user=user, year=year) except: From fd8423b73c3588033d44b44ecde0ca7fe6e7e865 Mon Sep 17 00:00:00 2001 From: Joachim Date: Sat, 1 Jan 2022 21:37:53 +0100 Subject: [PATCH 4/7] Update annual_summary.py --- bookwyrm/views/annual_summary.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bookwyrm/views/annual_summary.py b/bookwyrm/views/annual_summary.py index 9f37a6f1f..7e8ff0b13 100644 --- a/bookwyrm/views/annual_summary.py +++ b/bookwyrm/views/annual_summary.py @@ -24,7 +24,7 @@ LAST_DAY = 15 class AnnualSummary(View): """display a summary of the year for the current user""" - def get(self, request, username, year): + def get(self, request, username, year): # pylint: disable=too-many-locals """get response""" user = get_user_from_username(request.user, username) @@ -219,7 +219,7 @@ def get_goal_status(user, year): try: goal = models.AnnualGoal.objects.get(user=user, year=year) - except: + except ObjectDoesNotExist: return None if goal.privacy != "public": From 6e8a29aebfa387b968860a8b3fc769b15ee419d6 Mon Sep 17 00:00:00 2001 From: Joachim Date: Sun, 2 Jan 2022 17:52:49 +0100 Subject: [PATCH 5/7] Update annual_summary.py --- bookwyrm/views/annual_summary.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bookwyrm/views/annual_summary.py b/bookwyrm/views/annual_summary.py index 7e8ff0b13..5d8e14089 100644 --- a/bookwyrm/views/annual_summary.py +++ b/bookwyrm/views/annual_summary.py @@ -24,7 +24,7 @@ LAST_DAY = 15 class AnnualSummary(View): """display a summary of the year for the current user""" - def get(self, request, username, year): # pylint: disable=too-many-locals + def get(self, request, username, year): # pylint: disable=too-many-locals """get response""" user = get_user_from_username(request.user, username) @@ -219,7 +219,7 @@ def get_goal_status(user, year): try: goal = models.AnnualGoal.objects.get(user=user, year=year) - except ObjectDoesNotExist: + except models.AnnualGoal.DoesNotExist: return None if goal.privacy != "public": From 9dd2b880544b8979201770960e0613319d233704 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 4 Jan 2022 11:35:12 -0800 Subject: [PATCH 6/7] Updates fragile unit test causing nondeterministic failures --- bookwyrm/tests/models/test_fields.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/bookwyrm/tests/models/test_fields.py b/bookwyrm/tests/models/test_fields.py index 8028a305e..f6c6dcf35 100644 --- a/bookwyrm/tests/models/test_fields.py +++ b/bookwyrm/tests/models/test_fields.py @@ -483,7 +483,6 @@ class ModelFields(TestCase): instance.set_field_from_activity(book, mock_activity) self.assertIsNotNone(book.cover.name) - self.assertEqual(book.cover.size, 43200) @responses.activate def test_image_field_set_field_from_activity_no_overwrite_no_cover(self, *_): @@ -510,7 +509,6 @@ class ModelFields(TestCase): instance.set_field_from_activity(book, mock_activity, overwrite=False) self.assertIsNotNone(book.cover.name) - self.assertEqual(book.cover.size, 43200) @responses.activate def test_image_field_set_field_from_activity_no_overwrite_with_cover(self, *_): @@ -539,14 +537,15 @@ class ModelFields(TestCase): ) book = Edition.objects.create(title="hello") book.cover.save("test.jpg", ContentFile(output.getvalue())) - self.assertEqual(book.cover.size, 2136) + cover_size = book.cover.size + self.assertIsNotNone(cover_size) MockActivity = namedtuple("MockActivity", ("cover")) mock_activity = MockActivity("http://www.example.com/image.jpg") instance.set_field_from_activity(book, mock_activity, overwrite=False) # same cover as before - self.assertEqual(book.cover.size, 2136) + self.assertEqual(book.cover.size, cover_size) @responses.activate def test_image_field_set_field_from_activity_with_overwrite_with_cover(self, *_): @@ -559,7 +558,8 @@ class ModelFields(TestCase): image.save(output, format=image.format) book = Edition.objects.create(title="hello") book.cover.save("test.jpg", ContentFile(output.getvalue())) - self.assertEqual(book.cover.size, 2136) + cover_size = book.cover.size + self.assertIsNotNone(cover_size) another_image_file = pathlib.Path(__file__).parent.joinpath( "../../static/images/logo.png" @@ -583,7 +583,7 @@ class ModelFields(TestCase): instance.set_field_from_activity(book, mock_activity, overwrite=True) # new cover self.assertIsNotNone(book.cover.name) - self.assertEqual(book.cover.size, 376800) + self.assertFalse(book.cover.size, cover_size) def test_datetime_field(self, *_): """this one is pretty simple, it just has to use isoformat""" From cbc8ecb4b32030ce3920045d30779cb722279b3c Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 4 Jan 2022 11:46:44 -0800 Subject: [PATCH 7/7] NotEqual, not False --- bookwyrm/tests/models/test_fields.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bookwyrm/tests/models/test_fields.py b/bookwyrm/tests/models/test_fields.py index f6c6dcf35..940b8e7d9 100644 --- a/bookwyrm/tests/models/test_fields.py +++ b/bookwyrm/tests/models/test_fields.py @@ -583,7 +583,7 @@ class ModelFields(TestCase): instance.set_field_from_activity(book, mock_activity, overwrite=True) # new cover self.assertIsNotNone(book.cover.name) - self.assertFalse(book.cover.size, cover_size) + self.assertNotEqual(book.cover.size, cover_size) def test_datetime_field(self, *_): """this one is pretty simple, it just has to use isoformat"""