Skip to content

Commit

Permalink
Refactor current week logic to use compare_to_current_week method and…
Browse files Browse the repository at this point in the history
… update related templates and views
  • Loading branch information
AndersSeverinsen committed Dec 14, 2024
1 parent b18f403 commit fc4cfbc
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 22 deletions.
29 changes: 20 additions & 9 deletions bartenders/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,10 +446,16 @@ 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=2
) and self.end_datetime <= timezone.now() + datetime.timedelta(days=5)
def compare_to_current_week(self):
date = timezone.now().date()
less_than_week = self.start_datetime.date() < date + datetime.timedelta(5)
greater_than_week = self.end_datetime.date() > date - datetime.timedelta(2)
if less_than_week and greater_than_week:
return 0
elif less_than_week:
return -1
else:
return 1

def replace(self, b1, b2):
if self.responsible == b1:
Expand Down Expand Up @@ -500,11 +506,16 @@ 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 compare_to_current_week(self):
date = timezone.now().date()
less_than_week = self.start_date < date
greater_than_week = self.end_date > date
if less_than_week and greater_than_week:
return 0
elif less_than_week:
return -1
else:
return 1

def __str__(self):
return (
Expand Down
12 changes: 6 additions & 6 deletions bartenders/templates/barplan.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{% load i18n %}
{% load static %}
{% load bootstrap5 bootstrap_icons %}
{% load is_with_user is_current_week %}
{% load is_with_user compare_to_current_week %}

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

Expand All @@ -22,15 +22,15 @@ <h2>{% translate "Barplan" %}</h2>
<tbody>
{% for shift in bartendershifts %}
<tr>
<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">
<td {% if shift|compare_to_current_week == 0 %}title="{% translate "This weeks friday" %}"{% endif %} class="col-md-2" style="{% if shift|is_with_user:user %}background-color: skyblue;{% endif %}{% if shift|compare_to_current_week == 0 %}font-weight: bold;{% elif shift|compare_to_current_week == -1 %}opacity:0.5;{% endif %}">{{ shift.display_str }} {% if show_all %} {{ shift.start_datetime.year }}{% endif %}</td>
<td style="{% if shift|is_with_user:user %}background-color: skyblue;{% endif %}{% if shift|compare_to_current_week == -1 %}opacity:0.5;{% endif %}" class="col-md-3">{{ shift.responsible.name }}</td>
<td style="{% if shift|is_with_user:user %}background-color: skyblue;{% endif %}{% if shift|compare_to_current_week == -1 %}opacity:0.5;{% endif %}" class="col-md-7">
{% for bartender in shift.other_bartenders.all %}
{% if bartender.first_bartender_shift == shift %}<b title="{% translate "Bartender's first shift" %}">{{ bartender.name }}</b>{% else %}{{ bartender.name }}{% endif %}{% if not forloop.last %},{% endif %}
{% endfor %}
</td>
{% if user.is_staff %}
<td {% if shift|is_with_user:user %}style="background-color: skyblue;"{% endif %} class="hidden-xs col-md-1"><a href="/admin/bartenders/bartendershift/{{ shift.id }}">{% translate "Edit" %}</a></td>
<td style="{% if shift|is_with_user:user %}background-color: skyblue;{% endif %}{% if shift|compare_to_current_week == -1 %}opacity:0.5;{% endif %}" class="hidden-xs col-md-1"><a href="/admin/bartenders/bartendershift/{{ shift.id }}">{% translate "Edit" %}</a></td>
{% endif %}
</tr>
{% endfor %}
Expand Down Expand Up @@ -89,7 +89,7 @@ <h2>{% translate "Pantvagter" %}</h2>
</thead>
<tbody>
{% for shift in boardmemberdepositshifts %}
<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 %}">
<tr {% if shift|compare_to_current_week == 0 %}title="{% translate "Current week" %}"{% endif %} style="{% if shift|is_with_user:user %}background-color: skyblue;{% endif %}{% if shift|compare_to_current_week == 0 %}font-weight: bold;{% elif shift|compare_to_current_week == -1 %}opacity:0.5;{% 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 Down
6 changes: 2 additions & 4 deletions bartenders/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,14 @@ def get_context_data(self, **kwargs):
def current_week_page_number(self, paginator):
for i in range(1, paginator.num_pages + 1):
page = paginator.get_page(i)
if any(shift.is_current_week for shift in page):
print(i)
if any(shift.compare_to_current_week() == 0 for shift in page):
return i
print("None")
return None

def current_deposit_week_page_number(self, paginator):
for i in range(1, paginator.num_pages + 1):
page = paginator.get_page(i)
if any(shift.is_current_week for shift in page):
if any(shift.compare_to_current_week() == 0 for shift in page):
return i
return None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
register = template.Library()


@register.filter(name="is_current_week")
def is_current_week(shift):
@register.filter(name="compare_to_current_week")
def compare_to_current_week(shift):
try:
return shift.is_current_week()
return shift.compare_to_current_week()
except BartenderShift.DoesNotExist:
return False

0 comments on commit fc4cfbc

Please sign in to comment.