Display completed goal on Annual Summary page

This commit is contained in:
Joachim 2022-01-01 21:28:14 +01:00
parent cb3d224a5b
commit 358aa142b2
2 changed files with 45 additions and 1 deletions

View file

@ -192,6 +192,31 @@
</div> </div>
</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 %} {% if ratings_total > 0 %}
<div class="columns"> <div class="columns">
<div class="column has-text-centered"> <div class="column has-text-centered">

View file

@ -79,6 +79,9 @@ class AnnualSummary(View):
) )
ratings_stats = ratings.aggregate(Avg("rating")) ratings_stats = ratings.aggregate(Avg("rating"))
# annual goal status
goal_status = get_goal_status(user, year)
data = { data = {
"summary_user": user, "summary_user": user,
"year": year, "year": year,
@ -101,6 +104,7 @@ class AnnualSummary(View):
review.book.id for review in ratings.filter(rating=5) review.book.id for review in ratings.filter(rating=5)
], ],
"paginated_years": paginated_years, "paginated_years": paginated_years,
"goal_status": goal_status,
} }
return TemplateResponse(request, "annual_summary/layout.html", data) return TemplateResponse(request, "annual_summary/layout.html", data)
@ -195,7 +199,8 @@ def is_year_available(user, year):
year = int(year) year = int(year)
if earliest_year <= year < today.year: if earliest_year <= year < today.year:
return True 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 True
return False 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) books = models.Edition.objects.filter(id__in=books_ids).order_by(ordered)
return books 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})