Skip to content

Commit

Permalink
Add streaks view to admin page
Browse files Browse the repository at this point in the history
  • Loading branch information
AndersSeverinsen committed Dec 30, 2024
1 parent 97da154 commit 438ae6f
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 67 deletions.
38 changes: 38 additions & 0 deletions bartenders/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.contrib import admin, messages
from django.contrib.auth import get_user_model
from django.http import HttpResponseRedirect
from django.template.response import TemplateResponse
from django.urls import reverse
from django.utils import timezone
from django.utils.safestring import mark_safe
Expand Down Expand Up @@ -223,3 +224,40 @@ def change_view(self, request, object_id, form_url="", extra_context=None):
@admin.register(Poll)
class PollAdmin(admin.ModelAdmin):
pass


@custom_admin_view("bartenders", "Bartender shift streaks")
def streaks_view(admin, request):
shifts = BartenderShift.objects.all().filter(end_datetime__lte=timezone.now())
shift_streaks = []
for shift in shifts:
shift_streaks.append(shift.streak())
sorted_shift_streaks = sorted(shift_streaks, key=lambda x: x[0], reverse=True)
sorted_shift_streaks_short = []
for shift in sorted_shift_streaks:
found = False
for sorted_shift in sorted_shift_streaks_short:
if shift[1] == sorted_shift[1]:
found = True
break
if not found:
sorted_shift_streaks_short.append(shift)

current_shift = shifts.filter(
start_datetime__lte=timezone.now() + datetime.timedelta(days=2),
end_datetime__gte=timezone.now() - datetime.timedelta(days=5),
)
shift_placement = 0
for i, shift in enumerate(sorted_shift_streaks):
if shift[2] == current_shift[0].end_datetime:
shift_placement = i + 1
break

context = dict(
# Include common variables for rendering the admin template.
admin.admin_site.each_context(request),
# Anything else you want in the context...
shift_streaks=sorted_shift_streaks_short,
shift_placement=shift_placement,
)
return TemplateResponse(request, "streak_admin.html", context)
26 changes: 26 additions & 0 deletions bartenders/templates/streak_admin.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{% extends "admin/base_site.html" %}
{% load i18n %}
{% load static %}
{% load bootstrap_icons %}
{% block content %}
<div style="overflow: auto;">
{% if shift_streaks %}
<table>
<tr>
<th>{% translate "Placering" %}</th>
<th>{% translate "Streak" %}</th>
<th>{% translate "Start - Slut" %}</th>
</tr>
<tbody>
{% for streak, start_date, end_date in shift_streaks %}
<tr>
<td class="col-md-1">{{ forloop.counter }}</td>
<td class="col-md-1">{{ streak}}{% if forloop.counter == shift_placement %}{% bs_icon 'fire' size='0.8em' %}{% endif%}</td>
<td>{{ start_date| date:"d M, Y" }} - {{ end_date| date:"d M, Y" }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
</div>
{% endblock %}
14 changes: 13 additions & 1 deletion locale/da/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-12-30 13:04+0100\n"
"POT-Creation-Date: 2024-12-30 13:27+0100\n"
"PO-Revision-Date: 2024-12-23 21:27+0050\n"
"Last-Translator: <[email protected]>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -362,6 +362,18 @@ msgstr ""
msgid "Bartendertilmeldingen er lukket."
msgstr ""

#: bartenders/templates/streak_admin.html:10
msgid "Placering"
msgstr ""

#: bartenders/templates/streak_admin.html:11
msgid "Streak"
msgstr ""

#: bartenders/templates/streak_admin.html:12
msgid "Start - Slut"
msgstr ""

#: bartenders/views.py:69
msgid "Din ansøgning er blevet indsendt."
msgstr ""
Expand Down
Binary file modified locale/en/LC_MESSAGES/django.mo
Binary file not shown.
Loading

0 comments on commit 438ae6f

Please sign in to comment.