Merge branch 'main' into annual-goal-migration

This commit is contained in:
Mouse Reeve 2022-01-04 12:24:34 -08:00
commit 24faa45cc7
3 changed files with 50 additions and 7 deletions

View file

@ -192,6 +192,31 @@
</div>
</div>
{% if goal_status and goal_status.percent >= 100 %}
<div class="columns">
<div class="column has-text-centered">
<h2 class="title is-3 is-serif">
{% 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 }},<br />
and achieved {{ goal_percent }}% of that goal
{% plural %}
{{ display_name }} set a goal of reading {{ goal }} books in {{ year }},<br />
and achieved {{ goal_percent }}% of that goal
{% endblocktrans %}
{% endwith %}
</h2>
<p class="subtitle is-5">{% trans "Way to go!" %}</p>
</div>
</div>
<div class="columns">
<div class="column is-one-fifth is-offset-two-fifths">
<hr />
</div>
</div>
{% endif %}
{% if ratings_total > 0 %}
<div class="columns">
<div class="column has-text-centered">

View file

@ -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.assertNotEqual(book.cover.size, cover_size)
def test_datetime_field(self, *_):
"""this one is pretty simple, it just has to use isoformat"""

View file

@ -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)
@ -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)
@ -208,3 +212,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 models.AnnualGoal.DoesNotExist:
return None
if goal.privacy != "public":
return None
return dict(**goal.progress, **{"goal": goal.goal})