Skip to content

Commit

Permalink
Add current week filter and pagination to bartender shifts
Browse files Browse the repository at this point in the history
  • Loading branch information
AndersSeverinsen committed Dec 13, 2024
1 parent 19206df commit 59bb831
Show file tree
Hide file tree
Showing 10 changed files with 255 additions and 102 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 3.2.4 on 2024-12-13 19:30

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("bartenders", "0005_auto_20241212_2345"),
]

operations = [
migrations.AlterModelOptions(
name="boardmemberdepositshift",
options={"ordering": ("-start_date",)},
),
]
19 changes: 15 additions & 4 deletions bartenders/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,15 @@ def symbol(self):

@property
def first_bartender_shift(self):
return BartenderShift.with_bartender(self).first()
return BartenderShift.with_bartender(self).last()

@property
def last_bartender_shift(self):
return BartenderShift.with_bartender(self).last()
return BartenderShift.with_bartender(self).first()

@property
def first_deposit_shift(self):
return BoardMemberDepositShift.with_bartender(self).first()
return BoardMemberDepositShift.with_bartender(self).last()

MAILMAN_ALL = (
settings.MAILMAN_ALL_LIST,
Expand Down Expand Up @@ -446,6 +446,11 @@ def display_str(self):
def is_with_bartender(self, bartender):
return bartender in self.all_bartenders()

def is_current_week(self):
return self.start_datetime >= timezone.now() - datetime.timedelta(
days=7
) and self.end_datetime <= timezone.now() + datetime.timedelta(days=7)

def replace(self, b1, b2):
if self.responsible == b1:
self.responsible = b2
Expand Down Expand Up @@ -480,7 +485,7 @@ class BoardMemberDepositShift(models.Model):
)

class Meta:
ordering = ("start_date",)
ordering = ("-start_date",)

def save(self, *args, **kwargs):
if not self.end_date:
Expand All @@ -495,6 +500,12 @@ def with_bartender(cls, bartender):
def is_with_bartender(self, bartender):
return bartender in self.responsibles.all()

def is_current_week(self):
return (
timezone.now().date() >= self.start_date
and timezone.now().date() <= self.end_date
)

def __str__(self):
return (
f'{self.start_date}: {", ".join(b.name for b in self.responsibles.all())}'
Expand Down
87 changes: 72 additions & 15 deletions bartenders/templates/barplan.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@
{% block content %}
{% load i18n %}
{% load static %}
{% load is_with_user %}
{% load bootstrap5 bootstrap_icons %}
{% load is_with_user is_current_week %}

<h2>{% translate "Barplan" %}</h2>

{% if show_all %}
<a href=".">{% translate "Vis nuværende" %}</a>
{% else %}
<a href="?show_all">{% translate "Vis også tidligere" %}</a>
{% endif %}

<table class="table table-striped tablesorter" id="items">
<thead>
<tr>
Expand All @@ -27,7 +22,7 @@ <h2>{% translate "Barplan" %}</h2>
<tbody>
{% for shift in bartendershifts %}
<tr>
<td {% if shift|is_with_user:user %}style="background-color: skyblue;"{% endif %}class="col-md-2">{{ shift.display_str }} {% if show_all %} {{ shift.start_datetime.year }}{% endif %}</td>
<td {% if shift|is_current_week %}title="{% translate "This weeks friday" %}"{% endif %} class="col-md-2" style="{% if shift|is_with_user:user %}background-color: skyblue;{% endif %}{% if shift|is_current_week %}font-weight: bold;{% endif %}">{{ shift.display_str }} {% if show_all %} {{ shift.start_datetime.year }}{% endif %}</td>
<td {% if shift|is_with_user:user %}style="background-color: skyblue;"{% endif %} class="col-md-3">{{ shift.responsible.name }}</td>
<td {% if shift|is_with_user:user %}style="background-color: skyblue;"{% endif %} class="col-md-7">
{% for bartender in shift.other_bartenders.all %}
Expand All @@ -42,18 +37,46 @@ <h2>{% translate "Barplan" %}</h2>
</tbody>
</table>

<div class="pagination center-align">
<span class="step-links">
{% if bartendershifts.has_previous %}
<a href="?shifts_page={{ bartendershifts.previous_page_number }}"><i>{% bs_icon 'chevron-double-left' size='0.7em' %}</i></a>

{% if bartendershifts.number > 3 %}
<a href="?shifts_page=1">1</a>
{% if bartendershifts.number > 4 %}
<span>...</span>
{% endif %}
{% endif %}
{% endif %}

{% for num in bartendershifts.paginator.page_range %}
{% if bartendershifts.number == num %}
<a href="?shifts_page={{ num }}" class="active">{{ num }}</a>
{% elif num > bartendershifts.number|add:'-3' and num < bartendershifts.number|add:'3' %}
<a href="?shifts_page={{ num }}">{{ num }}</a>
{% endif %}
{% endfor %}

{% if bartendershifts.has_next %}
{% if bartendershifts.number < bartendershifts.paginator.num_pages|add:'-3' %}
<span>...</span>
<a href="?shifts_page={{ bartendershifts.paginator.num_pages }}">{{ bartendershifts.paginator.num_pages }}</a>
{% elif bartendershifts.number < bartendershifts.paginator.num_pages|add:'-2' %}
<a href="?shifts_page={{ bartendershifts.paginator.num_pages }}">{{ bartendershifts.paginator.num_pages }}</a>
{% endif %}

<a href="?shifts_page={{ bartendershifts.next_page_number }}"><i>{% bs_icon 'chevron-double-right' size='0.7em' %}</i></a>
{% endif %}
</span>
</div>

<a href="/barplan/shifts.ics">iCal feed</a>

<hr>

<h2>{% translate "Pantvagter" %}</h2>

{% if show_all %}
<a href=".">{% translate "Vis nuværende" %}</a>
{% else %}
<a href="?show_all">{% translate "Vis også tidligere" %}</a>
{% endif %}

<table class="table table-striped tablesorter" id="items">
<thead>
<tr>
Expand All @@ -66,7 +89,7 @@ <h2>{% translate "Pantvagter" %}</h2>
</thead>
<tbody>
{% for shift in boardmemberdepositshifts %}
<tr {% if shift|is_with_user:user %}style="background-color: skyblue;"{% endif %}>
<tr {% if shift|is_current_week %}title="{% translate "Current week" %}"{% endif %} style="{% if shift|is_with_user:user %}background-color: skyblue;{% endif %}{% if shift|is_current_week %}font-weight: bold;{% endif %}">
<td class="col-md-2">{{ shift.start_date | date:"d M" }}{% if show_all %} {{ shift.start_date.year }}{% endif %} - {{ shift.end_date | date:"d M" }}{% if show_all %} {{ shift.end_date.year }}{% endif %}</td>
<td class="col-md-9">
{% for responsible in shift.responsibles.all %}
Expand All @@ -82,6 +105,40 @@ <h2>{% translate "Pantvagter" %}</h2>
</tbody>
</table>

<div class="pagination center-align">
<span class="step-links">
{% if boardmemberdepositshifts.has_previous %}
<a href="?deposit_page={{ boardmemberdepositshifts.previous_page_number }}"><i>{% bs_icon 'chevron-double-left' size='0.7em' %}</i></a>

{% if boardmemberdepositshifts.number > 3 %}
<a href="?deposit_page=1">1</a>
{% if boardmemberdepositshifts.number > 4 %}
<span>...</span>
{% endif %}
{% endif %}
{% endif %}

{% for num in boardmemberdepositshifts.paginator.page_range %}
{% if boardmemberdepositshifts.number == num %}
<a href="?deposit_page={{ num }}" class="active">{{ num }}</a>
{% elif num > boardmemberdepositshifts.number|add:'-3' and num < boardmemberdepositshifts.number|add:'3' %}
<a href="?deposit_page={{ num }}">{{ num }}</a>
{% endif %}
{% endfor %}

{% if boardmemberdepositshifts.has_next %}
{% if boardmemberdepositshifts.number < boardmemberdepositshifts.paginator.num_pages|add:'-3' %}
<span>...</span>
<a href="?deposit_page={{ boardmemberdepositshifts.paginator.num_pages }}">{{ boardmemberdepositshifts.paginator.num_pages }}</a>
{% elif boardmemberdepositshifts.number < boardmemberdepositshifts.paginator.num_pages|add:'-2' %}
<a href="?deposit_page={{ boardmemberdepositshifts.paginator.num_pages }}">{{ boardmemberdepositshifts.paginator.num_pages }}</a>
{% endif %}

<a href="?deposit_page={{ boardmemberdepositshifts.next_page_number }}"><i>{% bs_icon 'chevron-double-right' size='0.7em' %}</i></a>
{% endif %}
</span>
</div>

<a href="/barplan/deposit_shifts.ics">iCal feed</a>

{% endblock %}
30 changes: 17 additions & 13 deletions bartenders/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
from django.contrib import messages
from django.contrib.admin.views.decorators import staff_member_required
from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin
from django.core.paginator import Paginator
from django.db import IntegrityError
from django.shortcuts import get_object_or_404, redirect, reverse
from django.shortcuts import get_object_or_404, redirect, render, reverse
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from django.views.generic import (
Expand Down Expand Up @@ -84,23 +85,26 @@ class Barplan(TemplateView):

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
bartendershifts = BartenderShift.objects.all()
depositshifts = BoardMemberDepositShift.objects.all()

show_all = "show_all" in self.request.GET
paginator_bartendershifts = Paginator(bartendershifts, 10)
paginator_depositshifts = Paginator(depositshifts, 10)

context["show_all"] = show_all

if show_all:
end_datetime = timezone.make_aware(datetime.datetime.utcfromtimestamp(0))
else:
end_datetime = timezone.now() - datetime.timedelta(1)

context["bartendershifts"] = BartenderShift.objects.filter(
end_datetime__gte=end_datetime
bartendershifts_page_number = self.request.GET.get("shifts_page")
bartendershifts_page_obj = paginator_bartendershifts.get_page(
bartendershifts_page_number
)
context["boardmemberdepositshifts"] = BoardMemberDepositShift.objects.filter(
end_date__gte=end_datetime

depositshifts_page_number = self.request.GET.get("deposit_page")
depositshifts_page_obj = paginator_depositshifts.get_page(
depositshifts_page_number
)

context["bartendershifts"] = bartendershifts_page_obj

context["boardmemberdepositshifts"] = depositshifts_page_obj

return context


Expand Down
Binary file modified locale/da/LC_MESSAGES/django.mo
Binary file not shown.
Loading

0 comments on commit 59bb831

Please sign in to comment.