Skip to content
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

4845: Allow users to see how many prayers they have remaining #4902

Merged
merged 5 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cl/favorites/api_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."
)
Expand Down
9 changes: 1 addition & 8 deletions cl/favorites/templates/user_prayers.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
{% block content %}
<div class="col-xs-12">
<h1 class="text-center v-offset-below-3">{% if is_page_owner %}Your PACER Document Prayers{% else %}PACER Document Requests for: {{ requested_user }}{% endif %}</h1>
{% if is_page_owner %}<h3 class="text-center"><b>{{ count|intcomma }}</b> prayers granted totaling <b>${{total_cost|floatformat:2 }}</b>.</h3>{% endif %}
{% if is_page_owner %}<h3 class="text-center"><b>{{ count|intcomma }}</b> prayers granted totaling <b>${{total_cost|floatformat:2 }}</b> (<b>{{ num_remaining }}</b> remaining today).</h3>{% endif %}
</div>

<div class="col-xs-12" id="prayer_summary" hx-swap-oob="true"
Expand All @@ -23,13 +23,6 @@ <h1 class="text-center v-offset-below-3">{% if is_page_owner %}Your PACER Docume
hx-trigger="prayersListChanged from:body" hx-swap="none"
{%endif %}
>
<div class="well well-sm">
{% if is_page_owner %}
<p>
{% 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 %}
</p>
{% endif %}
</div>
</div>

<div class="col-xs-12" id="prayer_list" hx-swap-oob="true">
Expand Down
8 changes: 4 additions & 4 deletions cl/favorites/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)(
Expand All @@ -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(
Expand All @@ -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:
Expand Down
8 changes: 5 additions & 3 deletions cl/favorites/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -39,13 +39,15 @@ 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
)
Expand Down
5 changes: 3 additions & 2 deletions cl/favorites/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
}

Expand Down
Loading