from datetime import datetime, timedelta, date
import calendar
from calendar import HTMLCalendar
from RIGS.models import BaseEvent, Event, Subhire
def get_html(events, day):
d = ''
for event in events:
# Open shared stuff
d += f'{event}'
elif day == event.end_date.day:
d += f'style="border-top-left-radius: 0; border-bottom-left-radius: 0;"> '
else:
d += f'style="border-radius: 0;"> '
else:
d += f'{event}'
# Close shared stuff
d += ""
return d
class Calendar(HTMLCalendar):
def __init__(self, year=None, month=None):
self.year = year
self.month = month
super(Calendar, self).__init__()
# formats a day as a td
# filter events by day
def formatday(self, day, events, subhires):
events_per_day = events.order_by("start_date").filter(start_date__day__lte=day, end_date__day__gte=day)
subhires_per_day = subhires.order_by("start_date").filter(start_date__day__lte=day, end_date__day__gte=day)
d = get_html(events_per_day, day) + get_html(subhires_per_day, day)
if day != 0:
return f"
{day} {d} | "
return ' | '
# formats a week as a tr
def formatweek(self, theweek, events, subhires):
week = ''
for d, weekday in theweek:
week += self.formatday(d, events, subhires)
return f' {week}
'
# formats a month as a table
# filter events by year and month
def formatmonth(self, withyear=True):
events = Event.objects.filter(start_date__year=self.year, start_date__month=self.month)
subhires = Subhire.objects.filter(start_date__year=self.year, start_date__month=self.month)
cal = f'\n'
cal += f'{self.formatweekheader()}\n'
for week in self.monthdays2calendar(self.year, self.month):
cal += f'{self.formatweek(week, events, subhires)}\n'
return cal
def get_date(req_day):
if req_day:
year, month = (int(x) for x in req_day.split('-'))
return date(year, month, day=1)
return datetime.today()
def prev_month(d):
first = d.replace(day=1)
prev_month = first - timedelta(days=1)
month = 'month=' + str(prev_month.year) + '-' + str(prev_month.month)
return month
def next_month(d):
days_in_month = calendar.monthrange(d.year, d.month)[1]
last = d.replace(day=days_in_month)
next_month = last + timedelta(days=1)
month = 'month=' + str(next_month.year) + '-' + str(next_month.month)
return month