import calendar
from datetime import datetime
from pprint import pprint
from flask import Blueprint, render_template
# import pytz
from .dm import dmbp
from .get_period_times import (
get_period_totals,
read_and_extract,
task_file,
task_or_none,
timezone,
get_work_project_tasks,
get_work_period_totals,
)
class DMHTMLCalendar(calendar.HTMLCalendar):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.use_work_projects = False
self.task = None
def setcalmonth(self, month):
self.dmmonth = month
def setcalyear(self, year):
self.dmyear = year
def settask(self, task):
self.task = task
def set_work_projects_mode(self, enabled=True):
self.use_work_projects = enabled
def oneday(self, month, day):
current_year = datetime.today().year
start = datetime(self.dmyear, month, day).replace(
hour=0, minute=0, second=0, tzinfo=timezone
)
end = datetime(self.dmyear, month, day).replace(
hour=23, minute=59, second=59, tzinfo=timezone
)
if self.use_work_projects:
rows = get_work_period_totals(start, end)
else:
rows = get_period_totals(start, end, self.task)
returnstr = "
"
for row in rows:
returnstr += "| {} | {} |
".format(
row["ws"], row["total"]
)
returnstr += "
"
return returnstr
def oneweek(self, month, week):
start_day = None
end_day = None
for d, wd in week:
if d == 0:
continue
else:
start_day = d
break
for d, wd in reversed(week):
if d == 0:
continue
else:
end_day = d
break
start = datetime(self.dmyear, month, start_day).replace(
hour=0, minute=0, second=0, tzinfo=timezone
)
end = datetime(self.dmyear, month, end_day).replace(
hour=23, minute=59, second=59, tzinfo=timezone
)
if self.use_work_projects:
rows = get_work_period_totals(start, end)
else:
rows = get_period_totals(start, end, self.task)
print(rows)
returnstr = ""
for row in rows:
returnstr += "| {} | {} |
".format(
row["ws"], row["total"]
)
returnstr += "
"
return returnstr
def formatweekheader(self):
"""
Return a header for a week as a table row.
"""
s = "".join(self.formatweekday(i) for i in self.iterweekdays())
s += "Week Totals | "
return "%s
" % s
def formatweek(self, theweek):
"""
Return a complete week as a table row.
"""
s = "".join(self.formatday(d, wd) for (d, wd) in theweek)
s += "{} | ".format(self.oneweek(self.dmmonth, theweek))
return "%s
" % s
def formatday(self, day, weekday):
"""
Return a day as a table cell.
"""
if day == 0:
return ' | ' # day outside month
else:
return '%s | ' % (
self.cssclasses[weekday],
self.oneday(self.dmmonth, day),
)
@dmbp.route("/workmonth")
@dmbp.route("/workmonth/")
@dmbp.route("/workmonth//")
def workmonth(month=None, year=None):
usemonth = datetime.today().month
useyear = datetime.today().year
if month:
usemonth = month
if year:
useyear = year
cal = DMHTMLCalendar(calendar.SATURDAY)
cal.set_work_projects_mode(True)
cal.setcalmonth(usemonth)
cal.setcalyear(useyear)
return render_template("calendar.html", content=cal.formatmonth(useyear, usemonth))
@dmbp.route("/month")
@dmbp.route("/month/")
@dmbp.route("/month//")
@dmbp.route("/month/")
@dmbp.route("/month//")
@dmbp.route("/month///")
def month(month=None, year=None, task=None):
usemonth = datetime.today().month
useyear = datetime.today().year
if month:
usemonth = month
if year:
useyear = year
cal = DMHTMLCalendar(calendar.SATURDAY)
cal.settask(task_or_none(task))
cal.setcalmonth(usemonth)
cal.setcalyear(useyear)
return render_template("calendar.html", content=cal.formatmonth(useyear, usemonth))