Skip to content

Commit

Permalink
Add limit and frontend fluff for foobar check-ins within the hour (#321)
Browse files Browse the repository at this point in the history
* Add limit and frontend fluff for foobar check-ins within the hour

* Fix test, refactor to single place to set timeout

* Make black happy
  • Loading branch information
falkecarlsen authored Nov 11, 2022
1 parent 35d32f9 commit 1d540d2
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 28 deletions.
44 changes: 24 additions & 20 deletions stregreport/tests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import datetime

from django.test import TestCase
from django.urls import reverse
from stregreport import views
from stregreport.models import BreadRazzia
from freezegun import freeze_time


class ParseIdStringTests(TestCase):
Expand Down Expand Up @@ -116,26 +119,27 @@ def test_bread_razzia_member_can_only_register_once(self):
self.assertContains(response_add_2, "already checked in", status_code=200)

def test_foobar_razzia_member_can_register_multiple_times(self):
self.client.login(username="tester", password="treotreo")
response = self.client.get(reverse("razzia_new_FB"), follow=True)
razzia_url, _ = response.redirect_chain[-1]

response_members_0 = self.client.get(razzia_url + "members", follow=True)

response_add_1 = self.client.post(razzia_url, {"username": "jokke"}, follow=True)

response_add_2 = self.client.post(razzia_url, {"username": "jokke"}, follow=True)

response_members_2 = self.client.get(razzia_url + "members", follow=True)

self.assertEqual(response_add_1.status_code, 200)
self.assertEqual(response_add_2.status_code, 200)
self.assertTemplateUsed(response_add_1, "admin/stregsystem/razzia/foobar.html")
self.assertTemplateUsed(response_add_2, "admin/stregsystem/razzia/foobar.html")
self.assertNotContains(response_add_1, "last checked in at", status_code=200)
self.assertContains(response_add_2, "last checked in at", status_code=200)
self.assertContains(response_members_0, "0 fember(s)", status_code=200)
self.assertContains(response_members_2, "2 fember(s)", status_code=200)
with freeze_time() as frozen_datetime:
self.client.login(username="tester", password="treotreo")
response = self.client.get(reverse("razzia_new_FB"), follow=True)
razzia_url, _ = response.redirect_chain[-1]

response_members_0 = self.client.get(razzia_url + "members", follow=True)

response_add_1 = self.client.post(razzia_url, {"username": "jokke"}, follow=True)
frozen_datetime.tick(delta=datetime.timedelta(hours=1, minutes=1))
response_add_2 = self.client.post(razzia_url, {"username": "jokke"}, follow=True)

response_members_2 = self.client.get(razzia_url + "members", follow=True)

self.assertEqual(response_add_1.status_code, 200)
self.assertEqual(response_add_2.status_code, 200)
self.assertTemplateUsed(response_add_1, "admin/stregsystem/razzia/foobar.html")
self.assertTemplateUsed(response_add_2, "admin/stregsystem/razzia/foobar.html")
self.assertNotContains(response_add_1, "last checked in at", status_code=200)
self.assertContains(response_add_2, "last checked in at", status_code=200)
self.assertContains(response_members_0, "0 fember(s)", status_code=200)
self.assertContains(response_members_2, "2 fember(s)", status_code=200)

def test_razzia_registered_member_is_in_member_list(self):
self.client.login(username="tester", password="treotreo")
Expand Down
16 changes: 12 additions & 4 deletions stregreport/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,18 @@ def razzia_view_single(request, razzia_id, queryname, razzia_type=BreadRazzia.BR
if len(result) > 0:
member = result[0]
entries = list(razzia.razziaentry_set.filter(member__pk=member.pk).order_by('-time'))
already_used = len(entries) > 0
if already_used:
entry = entries[0]
if not already_used or razzia_type == BreadRazzia.FOOBAR:
already_checked_in = len(entries) > 0
wait_time = datetime.timedelta(minutes=30)
if already_checked_in:
last_entry = entries[0]
within_wait = last_entry.time > timezone.now() - wait_time
# if member has already checked in within the last hour, don't allow another check in
if already_checked_in and within_wait and razzia_type == BreadRazzia.FOOBAR:
drunkard = True
# time until next check in is legal
remaining_time_secs = int(((last_entry.time + wait_time) - timezone.now()).total_seconds() % 60)
remaining_time_mins = int(((last_entry.time + wait_time) - timezone.now()).total_seconds() // 60)
if not already_checked_in or (razzia_type == BreadRazzia.FOOBAR and not within_wait):
RazziaEntry(member=member, razzia=razzia).save()

templates = {
Expand Down
4 changes: 2 additions & 2 deletions stregsystem/templates/admin/stregsystem/razzia/bread.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

{% block member_present %}
<div class="status">
<div class="fa {% if already_used %} fa-minus-circle failure {% else %} fa-check-circle sucess {% endif %}" aria-hidden="true"></div>
<div class="fa {% if already_checked_in %} fa-minus-circle failure {% else %} fa-check-circle sucess {% endif %}" aria-hidden="true"></div>
</div>
{{member.firstname}} {{member.lastname}} ({{member.username}}) {% if already_used %} already checked in at {{ entry.time }} {% endif %}
{{member.firstname}} {{member.lastname}} ({{member.username}}) {% if already_checked_in %} already checked in at {{ last_entry.time }} {% endif %}
{% endblock %}
28 changes: 26 additions & 2 deletions stregsystem/templates/admin/stregsystem/razzia/foobar.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,31 @@

{% block member_present %}
<div class="status">
<div class="fa {% if already_used %} fa-exclamation-circle sucess {% else %} fa-check-circle sucess {% endif %}" aria-hidden="true"></div>
{% if drunkard %}
<script>
// fade/flash background color to more easily notify foobar crew
let ofs = 0;
let el = document.body;
window.setInterval(function(){
el.style.background = 'rgba(255,255,0,'+Math.abs(Math.sin(ofs))+')';
ofs += 0.02;
}, 10);
</script>
<div class="fa fa-exclamation-triangle failure" aria-hidden="true"></div>
{% else %}
<div class="fa {% if already_used %} fa-exclamation-circle sucess {% else %} fa-check-circle sucess {% endif %}" aria-hidden="true"></div>
{% endif %}
</div>
{{member.firstname}} {{member.lastname}} ({{member.username}}) {% if already_used %} last checked in at {{entry.time}} {% endif %}
{% if drunkard %}
{{member.firstname}} {{member.lastname}} ({{member.username}}) <b>wait {{ remaining_time_mins }}m {{ remaining_time_secs }}s </b> before next free beer
{% else %}
{{member.firstname}} {{member.lastname}} ({{member.username}}) {% if already_checked_in %} last checked in at {{last_entry.time}} {% endif %}
{% endif %}

{% endblock %}

{% block drunkard_present %}
fa-exclamation-triangle
{% if drunkard %} checked in within the previous hour, wait {{ remaining_time }}{% endif %}
{% endblock %}

0 comments on commit 1d540d2

Please sign in to comment.