From 85b33b50230ac5b82c55ef3b1fe5ec93338f5348 Mon Sep 17 00:00:00 2001 From: Kresten Laust Date: Fri, 3 Nov 2023 10:15:05 +0100 Subject: [PATCH] Fnugfald razzia (#381) * Added basic foobar razzia copy called fnugfald razzia * Removed unused, duplicated element id: 'result' * Added the razzia to admin menu. * Moved .result div into each razzia type, to allow multiple results * Implemented fnugfald razzia * Made use of guard clauses * Don't store razzia information on users who don't have a ticket * Changed hardcoded values to actual values. * Fixed crash issue when product id isn't recognized --- stregreport/models.py | 5 +- stregreport/urls.py | 4 + stregreport/views.py | 103 ++++++++++++------ stregsystem/templates/admin/index.html | 5 + .../admin/stregsystem/razzia/bread.html | 8 +- .../admin/stregsystem/razzia/fnugfald.html | 64 +++++++++++ .../admin/stregsystem/razzia/foobar.html | 37 ++++--- .../stregsystem/razzia/razzia_search.html | 10 +- .../admin/stregsystem/razzia/wizard_view.html | 6 +- 9 files changed, 175 insertions(+), 67 deletions(-) create mode 100644 stregsystem/templates/admin/stregsystem/razzia/fnugfald.html diff --git a/stregreport/models.py b/stregreport/models.py index 393c2930..9ed1b172 100644 --- a/stregreport/models.py +++ b/stregreport/models.py @@ -5,11 +5,12 @@ class BreadRazzia(models.Model): class Meta: - permissions = (("host_razzia", "Can host a foobar or bread razzia"),) + permissions = (("host_razzia", "Can host a foobar, fnugfald or bread razzia"),) BREAD = 'BR' FOOBAR = 'FB' - RAZZIA_CHOICES = [(BREAD, "Brødrazzia"), (FOOBAR, "Foobar razzia")] + FNUGFALD = 'FF' + RAZZIA_CHOICES = [(BREAD, "Brødrazzia"), (FOOBAR, "Foobar razzia"), (FNUGFALD, "Fnugfald razzia")] members = models.ManyToManyField(Member, through='RazziaEntry') start_date = models.DateTimeField(auto_now_add=True) razzia_type = models.CharField(max_length=2, choices=RAZZIA_CHOICES, default=BREAD) diff --git a/stregreport/urls.py b/stregreport/urls.py index 065a91c7..da36a6c4 100644 --- a/stregreport/urls.py +++ b/stregreport/urls.py @@ -21,12 +21,16 @@ urlpatterns = [ re_path(r'^admin/stregsystem/razzia/bread/(?P\d+)/$', views.razzia, {'razzia_type' : BreadRazzia.BREAD, 'title': 'Brødrazzia'}, name="bread_view"), re_path(r'^admin/stregsystem/razzia/foobar/(?P\d+)/$', views.razzia, {'razzia_type' : BreadRazzia.FOOBAR, 'title': 'Foobar razzia'}, name="foobar_view"), + re_path(r'^admin/stregsystem/razzia/fnugfald/(?P\d+)/$', views.razzia, {'razzia_type' : BreadRazzia.FNUGFALD, 'title': 'Fnugfald razzia'}, name="fnugfald_view"), re_path(r'^admin/stregsystem/razzia/bread/(?P\d+)/members$', views.razzia_members, {'razzia_type' : BreadRazzia.BREAD, 'title': 'Brødrazzia'}), re_path(r'^admin/stregsystem/razzia/foobar/(?P\d+)/members$', views.razzia_members, {'razzia_type' : BreadRazzia.FOOBAR, 'title': 'Foobar razzia'}), + re_path(r'^admin/stregsystem/razzia/fnugfald/(?P\d+)/members$', views.razzia_members, {'razzia_type' : BreadRazzia.FNUGFALD, 'title': 'Fnugfald razzia'}), re_path(r'^admin/stregsystem/razzia/bread/$', views.razzia_menu, {'razzia_type' : BreadRazzia.BREAD, 'new_text': "New bread razzia", 'title': 'Brødrazzia'}), re_path(r'^admin/stregsystem/razzia/foobar/$', views.razzia_menu, {'razzia_type' : BreadRazzia.FOOBAR, 'new_text': "New foobar razzia", 'title': 'Foobar razzia'}), + re_path(r'^admin/stregsystem/razzia/fnugfald/$', views.razzia_menu, {'razzia_type' : BreadRazzia.FNUGFALD, 'new_text': "New fnugfald razzia", 'title': 'Fnugfald razzia'}), re_path(r'^admin/stregsystem/razzia/bread/new$', views.new_razzia, {'razzia_type' : BreadRazzia.BREAD}, name="razzia_new_BR"), re_path(r'^admin/stregsystem/razzia/foobar/new$', views.new_razzia, {'razzia_type' : BreadRazzia.FOOBAR}, name="razzia_new_FB"), + re_path(r'^admin/stregsystem/razzia/fnugfald/new$', views.new_razzia, {'razzia_type' : BreadRazzia.FNUGFALD}, name="razzia_new_FF"), re_path(r'^admin/stregsystem/razzia/wizard_guide/$', views.razzia_wizard), re_path(r'^admin/stregsystem/razzia/wizard/$', views.razzia_view, name="razzia_view"), re_path(r'^admin/stregsystem/report/sales/$', views.sales, name="salesreporting"), diff --git a/stregreport/views.py b/stregreport/views.py index 3ee26ff5..9e646ce4 100644 --- a/stregreport/views.py +++ b/stregreport/views.py @@ -50,32 +50,80 @@ def razzia(request, razzia_id, razzia_type=BreadRazzia.BREAD, title=None): return razzia_view_single(request, razzia_id, None, razzia_type=razzia_type, title=title) +def _sales_to_user_in_period(username, start_date, end_date, product_list, product_dict): + result = ( + Product.objects.filter( + sale__member__username__iexact=username, + id__in=product_list, + sale__timestamp__gte=start_date, + sale__timestamp__lte=end_date, + ) + .annotate(cnt=Count("id")) + .values_list("name", "cnt") + ) + + products_bought = {product: count for product, count in result} + + return {product: products_bought.get(product, 0) for product in product_dict} + + @permission_required("stregreport.host_razzia") def razzia_view_single(request, razzia_id, queryname, razzia_type=BreadRazzia.BREAD, title=None): razzia = get_object_or_404(BreadRazzia, pk=razzia_id, razzia_type=razzia_type) - if queryname is not None: - result = list(Member.objects.filter(username__iexact=queryname)) - if len(result) > 0: - member = result[0] - entries = list(razzia.razziaentry_set.filter(member__pk=member.pk).order_by('-time')) - 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 = { BreadRazzia.BREAD: 'admin/stregsystem/razzia/bread.html', BreadRazzia.FOOBAR: 'admin/stregsystem/razzia/foobar.html', + BreadRazzia.FNUGFALD: 'admin/stregsystem/razzia/fnugfald.html', } + + if queryname is None: + return render(request, templates[razzia_type], locals()) + + result = list(Member.objects.filter(username__iexact=queryname)) + if len(result) == 0: + return render(request, templates[razzia_type], locals()) + + member = result[0] + + if razzia_type == BreadRazzia.FNUGFALD: + username = queryname + member_name = member.firstname + " " + member.lastname + start_date = dateparse.parse_date("2023-9-15") + end_date = dateparse.parse_date("2023-11-4") + product_list = [1910] + product_dict = {k.name: 0 for k in Product.objects.filter(id__in=product_list)} + sales_to_user = _sales_to_user_in_period(queryname, start_date, end_date, product_list, product_dict) + items_bought = sales_to_user.items() + + try: + item_bought_count = sales_to_user[list(sales_to_user.keys())[0]] + if item_bought_count == 0: + return render(request, templates[razzia_type], locals()) + except IndexError: + return render(request, templates[razzia_type], locals()) + + entries = list(razzia.razziaentry_set.filter(member__pk=member.pk).order_by('-time')) + 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 or razzia_type == BreadRazzia.FNUGFALD) + ): + 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 or razzia_type == BreadRazzia.FNUGFALD) and not within_wait + ): + RazziaEntry(member=member, razzia=razzia).save() + return render(request, templates[razzia_type], locals()) @@ -92,7 +140,7 @@ def new_razzia(request, razzia_type=BreadRazzia.BREAD): razzia = BreadRazzia(razzia_type=razzia_type) razzia.save() - views = {BreadRazzia.BREAD: 'bread_view', BreadRazzia.FOOBAR: 'foobar_view'} + views = {BreadRazzia.BREAD: 'bread_view', BreadRazzia.FOOBAR: 'foobar_view', BreadRazzia.FNUGFALD: 'fnugfald_view'} return redirect(views[razzia_type], razzia_id=razzia.pk) @@ -109,23 +157,6 @@ def razzia_members(request, razzia_id, razzia_type=BreadRazzia.BREAD, title=None razzia_members = staff_member_required(razzia_members) -def _sales_to_user_in_period(username, start_date, end_date, product_list, product_dict): - result = ( - Product.objects.filter( - sale__member__username__iexact=username, - id__in=product_list, - sale__timestamp__gte=start_date, - sale__timestamp__lte=end_date, - ) - .annotate(cnt=Count("id")) - .values_list("name", "cnt") - ) - - products_bought = {product: count for product, count in result} - - return {product: products_bought.get(product, 0) for product in product_dict} - - @permission_required("stregreport.host_razzia") def razzia_view(request): default_start = timezone.now().today() - datetime.timedelta(days=-180) diff --git a/stregsystem/templates/admin/index.html b/stregsystem/templates/admin/index.html index 0f929323..b6f957c4 100644 --- a/stregsystem/templates/admin/index.html +++ b/stregsystem/templates/admin/index.html @@ -65,6 +65,11 @@     + + Fnugfald razzia +   +   + Razzia wizard!   diff --git a/stregsystem/templates/admin/stregsystem/razzia/bread.html b/stregsystem/templates/admin/stregsystem/razzia/bread.html index 1943df04..3c25c758 100644 --- a/stregsystem/templates/admin/stregsystem/razzia/bread.html +++ b/stregsystem/templates/admin/stregsystem/razzia/bread.html @@ -3,8 +3,10 @@ {% block breadcrumbs %}{% endblock %} {% block member_present %} -
- +
+
+ +
+ {{member.firstname}} {{member.lastname}} ({{member.username}}) {% if already_checked_in %} already checked in at {{ last_entry.time }} {% endif %}
-{{member.firstname}} {{member.lastname}} ({{member.username}}) {% if already_checked_in %} already checked in at {{ last_entry.time }} {% endif %} {% endblock %} diff --git a/stregsystem/templates/admin/stregsystem/razzia/fnugfald.html b/stregsystem/templates/admin/stregsystem/razzia/fnugfald.html new file mode 100644 index 00000000..24fd101c --- /dev/null +++ b/stregsystem/templates/admin/stregsystem/razzia/fnugfald.html @@ -0,0 +1,64 @@ +{% extends "admin/stregsystem/razzia/razzia_search.html" %} + +{% block breadcrumbs %}{% endblock %} + +{% block member_present %} +
+
+ {% if drunkard %} + + + {% else %} + + {% endif %} +
+ {% if drunkard %} + {{member.firstname}} {{member.lastname}} ({{member.username}}) wait {{ remaining_time_mins }}m {{ remaining_time_secs }}s before next free beer + {% else %} + {{member.firstname}} {{member.lastname}} ({{member.username}}) {% if already_checked_in %} last checked in at {{last_entry.time}} {% endif %} + {% endif %} +
+ +{% if username %} + {% if member_name %} + {% if items_bought %} + {% for item, count in items_bought %} +
+ {% if count > 0 %} +
+ {% else %} +
+ {% endif %} + {{ item }}: {{ count }} +
+ {% endfor %} + {% else %} +
+
+ {{ member_name }} has not bought any products in the list +
+ {% endif %} + {% else %} +
+
+ "{{ username }}" not found +
+ {% endif %} + +{% endif %} + +{% endblock %} + +{% block drunkard_present %} + fa-exclamation-triangle + {% if drunkard %} checked in within the previous hour, wait {{ remaining_time }}{% endif %} +{% endblock %} + diff --git a/stregsystem/templates/admin/stregsystem/razzia/foobar.html b/stregsystem/templates/admin/stregsystem/razzia/foobar.html index dc7e1220..54cd3167 100644 --- a/stregsystem/templates/admin/stregsystem/razzia/foobar.html +++ b/stregsystem/templates/admin/stregsystem/razzia/foobar.html @@ -3,28 +3,29 @@ {% block breadcrumbs %}{% endblock %} {% block member_present %} -
- {% if drunkard %} - +
+
+ {% if drunkard %} + - {% else %} + {% else %} + {% endif %} +
+ {% if drunkard %} + {{member.firstname}} {{member.lastname}} ({{member.username}}) wait {{ remaining_time_mins }}m {{ remaining_time_secs }}s before next free beer + {% else %} + {{member.firstname}} {{member.lastname}} ({{member.username}}) {% if already_checked_in %} last checked in at {{last_entry.time}} {% endif %} {% endif %}
-{% if drunkard %} - {{member.firstname}} {{member.lastname}} ({{member.username}}) wait {{ remaining_time_mins }}m {{ remaining_time_secs }}s 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 %} diff --git a/stregsystem/templates/admin/stregsystem/razzia/razzia_search.html b/stregsystem/templates/admin/stregsystem/razzia/razzia_search.html index 0b6d78a8..647fad87 100644 --- a/stregsystem/templates/admin/stregsystem/razzia/razzia_search.html +++ b/stregsystem/templates/admin/stregsystem/razzia/razzia_search.html @@ -16,16 +16,16 @@

{{title|default:"Razzia"}}!

View registered fembers
{% if queryname %} -
{% if member %} {% block member_present %}{% endblock %} {% else %} {% block member_not_present %} -
-
- {{queryname}} is not a fember!!! +
+
+
+ {{queryname}} is not a fember!!! +
{% endblock %} {% endif %} -
{% endif %} {% endblock %} diff --git a/stregsystem/templates/admin/stregsystem/razzia/wizard_view.html b/stregsystem/templates/admin/stregsystem/razzia/wizard_view.html index 4ba13e0d..766d3a2e 100644 --- a/stregsystem/templates/admin/stregsystem/razzia/wizard_view.html +++ b/stregsystem/templates/admin/stregsystem/razzia/wizard_view.html @@ -31,12 +31,12 @@

{{ razzia_title }}

{% if username %} {% if member_name %} {% if items_bought %} -
+
Bruger: {{ member_name }}
{% for item, count in items_bought %} -
+
{% if count > 0 %}
{% else %} @@ -50,7 +50,7 @@

{{ razzia_title }}

{{ member_name }} has not bought any products in the list {% endif %} {% else %} -
+
"{{ username }}" not found