From 9f663a3ca0498e8fdec1140774988e5f21056b47 Mon Sep 17 00:00:00 2001
From: v_anne <69829523+v-anne@users.noreply.github.com>
Date: Wed, 8 Jan 2025 17:34:41 -0500
Subject: [PATCH] allowing users to see how many prayers remaining
---
cl/favorites/api_serializers.py | 2 +-
cl/favorites/templates/user_prayers.html | 9 +--------
cl/favorites/tests.py | 8 ++++----
cl/favorites/utils.py | 6 +++---
cl/favorites/views.py | 5 +++--
5 files changed, 12 insertions(+), 18 deletions(-)
diff --git a/cl/favorites/api_serializers.py b/cl/favorites/api_serializers.py
index a12bb4c101..60b9efedbd 100644
--- a/cl/favorites/api_serializers.py
+++ b/cl/favorites/api_serializers.py
@@ -67,7 +67,7 @@ def validate(self, data):
)
# Check if the user is eligible to create a new prayer
- if not async_to_sync(prayer_eligible)(user):
+ if not async_to_sync(prayer_eligible)(user)[0]:
raise ValidationError(
f"You have reached the maximum number of prayers ({settings.ALLOWED_PRAYER_COUNT}) allowed in the last 24 hours."
)
diff --git a/cl/favorites/templates/user_prayers.html b/cl/favorites/templates/user_prayers.html
index 4ada025de6..f010632d16 100644
--- a/cl/favorites/templates/user_prayers.html
+++ b/cl/favorites/templates/user_prayers.html
@@ -14,7 +14,7 @@
{% block content %}
{% if is_page_owner %}Your PACER Document Prayers{% else %}PACER Document Requests for: {{ requested_user }}{% endif %}
- {% if is_page_owner %}{{ count|intcomma }} prayers granted totaling ${{total_cost|floatformat:2 }}.
{% endif %}
+ {% if is_page_owner %}{{ count|intcomma }} prayers granted totaling ${{total_cost|floatformat:2 }} ({{ num_remaining }} remaining today).
{% endif %}
{% if is_page_owner %}Your PACER Docume
hx-trigger="prayersListChanged from:body" hx-swap="none"
{%endif %}
>
-
- {% if is_page_owner %}
-
- {% if is_eligible %}You are eligible to make document requests.{% else %}You have reached your daily limit; wait 24 hours to make new requests.{% endif %}
-
- {% endif %}
-
diff --git a/cl/favorites/tests.py b/cl/favorites/tests.py
index 35c32652a2..e5ad413153 100644
--- a/cl/favorites/tests.py
+++ b/cl/favorites/tests.py
@@ -699,7 +699,7 @@ async def test_prayer_eligible(self) -> None:
current_time = now()
with time_machine.travel(current_time, tick=False):
# No user prayers in the last 24 hours yet for this user.
- user_is_eligible = await prayer_eligible(self.user)
+ user_is_eligible, _ = await prayer_eligible(self.user)
self.assertTrue(user_is_eligible)
# Add prays for this user.
@@ -709,7 +709,7 @@ async def test_prayer_eligible(self) -> None:
user_prays = Prayer.objects.filter(user=self.user)
self.assertEqual(await user_prays.acount(), 1)
- user_is_eligible = await prayer_eligible(self.user)
+ user_is_eligible, _ = await prayer_eligible(self.user)
self.assertTrue(user_is_eligible)
await sync_to_async(PrayerFactory)(
@@ -719,7 +719,7 @@ async def test_prayer_eligible(self) -> None:
# After two prays (ALLOWED_PRAYER_COUNT) in the last 24 hours.
# The user is no longer eligible to create more prays
- user_is_eligible = await prayer_eligible(self.user)
+ user_is_eligible, _ = await prayer_eligible(self.user)
self.assertFalse(user_is_eligible)
with time_machine.travel(
@@ -730,7 +730,7 @@ async def test_prayer_eligible(self) -> None:
user=self.user, recap_document=self.rd_3
)
self.assertEqual(await user_prays.acount(), 3)
- user_is_eligible = await prayer_eligible(self.user)
+ user_is_eligible, _ = await prayer_eligible(self.user)
self.assertTrue(user_is_eligible)
async def test_create_prayer(self) -> None:
diff --git a/cl/favorites/utils.py b/cl/favorites/utils.py
index 9e87389890..85b09c7ad6 100644
--- a/cl/favorites/utils.py
+++ b/cl/favorites/utils.py
@@ -28,7 +28,7 @@
from cl.search.models import RECAPDocument
-async def prayer_eligible(user: User) -> bool:
+async def prayer_eligible(user: User) -> Tuple[bool, int]:
allowed_prayer_count = settings.ALLOWED_PRAYER_COUNT
now = timezone.now()
@@ -39,13 +39,13 @@ async def prayer_eligible(user: User) -> bool:
user=user, date_created__gte=last_24_hours
).acount()
- return prayer_count < allowed_prayer_count
+ return prayer_count < allowed_prayer_count, (allowed_prayer_count - prayer_count)
async def create_prayer(
user: User, recap_document: RECAPDocument
) -> Prayer | None:
- if await prayer_eligible(user) and not recap_document.is_available:
+ if await prayer_eligible(user)[0] and not recap_document.is_available:
new_prayer, created = await Prayer.objects.aget_or_create(
user=user, recap_document=recap_document
)
diff --git a/cl/favorites/views.py b/cl/favorites/views.py
index bd2c8ea5b5..6f72e652b4 100644
--- a/cl/favorites/views.py
+++ b/cl/favorites/views.py
@@ -212,7 +212,7 @@ async def create_prayer_view(
user = request.user
is_htmx_request = request.META.get("HTTP_HX_REQUEST", False)
regular_size = bool(request.POST.get("regular_size"))
- if not await prayer_eligible(request.user):
+ if not await prayer_eligible(request.user)[0]:
if is_htmx_request:
return TemplateResponse(
request,
@@ -291,7 +291,7 @@ async def user_prayers_view(
count, total_cost = await get_user_prayer_history(requested_user)
- is_eligible = await prayer_eligible(requested_user)
+ is_eligible, num_remaining = await prayer_eligible(requested_user)
context = {
"rd_with_prayers": rd_with_prayers,
@@ -300,6 +300,7 @@ async def user_prayers_view(
"count": count,
"total_cost": total_cost,
"is_eligible": is_eligible,
+ "num_remaining": num_remaining,
"private": False,
}