Skip to content

Commit

Permalink
Refactor bartender and deposit shift ordering, update pagination to 1…
Browse files Browse the repository at this point in the history
…5, and add current week page number logic
  • Loading branch information
AndersSeverinsen committed Dec 13, 2024
1 parent 59bb831 commit b18f403
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 9 deletions.
21 changes: 21 additions & 0 deletions bartenders/migrations/0007_auto_20241214_0030.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 3.2.4 on 2024-12-13 23:30

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("bartenders", "0006_alter_boardmemberdepositshift_options"),
]

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

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

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

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

MAILMAN_ALL = (
Expand Down Expand Up @@ -403,7 +403,7 @@ class BartenderShift(models.Model):
)

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

def save(self, *args, **kwargs):
if not self.end_datetime:
Expand Down Expand Up @@ -448,8 +448,8 @@ def is_with_bartender(self, bartender):

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)
days=2
) and self.end_datetime <= timezone.now() + datetime.timedelta(days=5)

def replace(self, b1, b2):
if self.responsible == b1:
Expand Down Expand Up @@ -485,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 Down
32 changes: 30 additions & 2 deletions bartenders/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,27 @@ def get_context_data(self, **kwargs):
bartendershifts = BartenderShift.objects.all()
depositshifts = BoardMemberDepositShift.objects.all()

paginator_bartendershifts = Paginator(bartendershifts, 10)
paginator_depositshifts = Paginator(depositshifts, 10)
paginator_bartendershifts = Paginator(bartendershifts, 15)
paginator_depositshifts = Paginator(depositshifts, 15)

bartendershifts_page_number = self.request.GET.get("shifts_page")
if not bartendershifts_page_number:
bartendershifts_page_number = self.current_week_page_number(
paginator_bartendershifts
)
if not bartendershifts_page_number:
bartendershifts_page_number = paginator_bartendershifts.num_pages
bartendershifts_page_obj = paginator_bartendershifts.get_page(
bartendershifts_page_number
)

depositshifts_page_number = self.request.GET.get("deposit_page")
if not depositshifts_page_number:
depositshifts_page_number = self.current_deposit_week_page_number(
paginator_depositshifts
)
if not depositshifts_page_number:
depositshifts_page_number = paginator_depositshifts.num_pages
depositshifts_page_obj = paginator_depositshifts.get_page(
depositshifts_page_number
)
Expand All @@ -107,6 +119,22 @@ def get_context_data(self, **kwargs):

return context

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)
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):
return i
return None


class UserBarplan(ICalFeed):
product_id = "-//fredagscafeen.dk//UserBarplan//EN"
Expand Down

0 comments on commit b18f403

Please sign in to comment.