From 3e073a0e4bf769b87a17a2d7d814f37885cf1bc2 Mon Sep 17 00:00:00 2001 From: Mariano Ramon Date: Mon, 4 May 2020 02:21:07 -0300 Subject: [PATCH] calendar mockup --- dmapp/dmweb/__init__.py | 3 +- dmapp/dmweb/dm.py | 32 +++++++------ dmapp/dmweb/dmcal.py | 69 +++++++++++++++++++++++++++++ dmapp/dmweb/static/styles/dm.css | 3 ++ dmapp/dmweb/templates/calendar.html | 21 +++++++++ dmapp/dmweb/templates/layout.html | 10 ++++- 6 files changed, 123 insertions(+), 15 deletions(-) create mode 100644 dmapp/dmweb/dmcal.py create mode 100644 dmapp/dmweb/static/styles/dm.css create mode 100644 dmapp/dmweb/templates/calendar.html diff --git a/dmapp/dmweb/__init__.py b/dmapp/dmweb/__init__.py index 1d0206f..3e49386 100644 --- a/dmapp/dmweb/__init__.py +++ b/dmapp/dmweb/__init__.py @@ -1,10 +1,11 @@ from flask import Flask -from dmweb import dm +from dmweb import dm, dmcal def create_app(): app = Flask("deskmeter") + app.debug = True app.register_blueprint(dm.dmbp) return app diff --git a/dmapp/dmweb/dm.py b/dmapp/dmweb/dm.py index b5b8a40..d5413a9 100644 --- a/dmapp/dmweb/dm.py +++ b/dmapp/dmweb/dm.py @@ -50,7 +50,6 @@ def oneday(month,day): - @dmbp.route("/period//") def period(start,end): @@ -102,6 +101,8 @@ def get_period_totals(start, end): length = switches.count_documents(queries["period"]) docs = switches.find(queries["period"]).sort([("date", 1)]) + if not length: + return [{"ws": "No Data", "total": ""}] #next_doc = switches.find_one(queries["next_doc"]) last_doc = docs[length-1] @@ -116,42 +117,47 @@ def get_period_totals(start, end): for total in switches.aggregate(pipeline=pipe): pre_rows[total["_id"]] = total["totals"] - - time_corrections = [] if first_date > start: - previous_doc = switches.find(queries["previous_doc"]).sort([("date", -1)]).limit(1)[0] - time_corrections.append(Counter({previous_doc["workspace"] : split_entry(previous_doc, start)})) + #TODO if its the first record it fails write test + try: + previous_doc = switches.find(queries["previous_doc"]).sort([("date", -1)]).limit(1)[0] + time_corrections.append(Counter({previous_doc["workspace"] : split_entry(previous_doc, start)})) + except IndexError: + pass + + now = local_date(datetime.datetime.now()) if end > now: - time_corrections.append( Counter({ last_doc["workspace"] : split_entry(last_doc, now, after=False)})) + time_corrections.append(Counter({ last_doc["workspace"] : split_entry(last_doc, now, after=False)})) if end > last_date and now > end: time_corrections.append(Counter({ last_doc["workspace"] : split_entry(last_doc, end, after=False)})) - for correction in time_corrections: pre_rows += correction - day = 0 + #TODO _1 remove + #day = 0 rows = [] for ws, total in pre_rows.items(): - day += total + #TODO _1 remove + #day += total rows.append( {"ws": ws, "total" : datetime.timedelta(seconds=total)}) - - rows.append({"ws":"sum","total": day}) + #TODO _1 remove + #rows.append({"ws":"sum","total": day}) return rows def split_entry(entry, split_time, after=True): """ entry must have a date an number - split time must be timezone aware - after, bolean to return the time after the split time + split_time must be timezone aware + after bolean to return the time after the split time """ first_half = round((split_time - local_date(entry["date"], True)).total_seconds()) last_half = entry["delta"] - first_half diff --git a/dmapp/dmweb/dmcal.py b/dmapp/dmweb/dmcal.py new file mode 100644 index 0000000..5649296 --- /dev/null +++ b/dmapp/dmweb/dmcal.py @@ -0,0 +1,69 @@ +import datetime +from pprint import pprint +from flask import Blueprint, render_template +import pytz + +import calendar + + +from dmweb.dm import dmbp, get_period_totals, local_date + + + +class DMHTMLCalendar(calendar.HTMLCalendar): + + + # def formatmonth(self, theyear, themonth, withyear=True): + # self.dmmonth = themonth + # super().formatmonth(self, theyear, themonth) + + + def setcalmonth(self, month): + self.dmmonth = month + + def oneday(self,month,day): + + start = datetime.datetime(2020, month, day).replace(hour=0, + minute=0, + second=0) + + end = datetime.datetime(2020, month, day).replace (hour=23, + minute=59, + second=59) + + rows = get_period_totals( local_date(start), + local_date(end)) + + returnstr = "" + for row in rows: + returnstr += "".format(row["ws"],row["total"]) + + returnstr += "
{}{}
" + return returnstr + + + 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("/calendar") +@dmbp.route("/calendar/") +def calmain(month=None): + + usemonth = datetime.datetime.today().month + if month: + usemonth = month + + cal = DMHTMLCalendar(calendar.SATURDAY) + + cal.setcalmonth(usemonth) + + return render_template("calendar.html", content=cal.formatmonth(2020,usemonth)) + diff --git a/dmapp/dmweb/static/styles/dm.css b/dmapp/dmweb/static/styles/dm.css new file mode 100644 index 0000000..ee20e41 --- /dev/null +++ b/dmapp/dmweb/static/styles/dm.css @@ -0,0 +1,3 @@ +table { + border: 1px solid black; +} \ No newline at end of file diff --git a/dmapp/dmweb/templates/calendar.html b/dmapp/dmweb/templates/calendar.html new file mode 100644 index 0000000..6603aea --- /dev/null +++ b/dmapp/dmweb/templates/calendar.html @@ -0,0 +1,21 @@ +{% extends 'layout.html' %} + +{% block head %} + + + + +{% endblock head %} + + +{% block content %} + + {{ content | safe }} + +{% endblock content %} \ No newline at end of file diff --git a/dmapp/dmweb/templates/layout.html b/dmapp/dmweb/templates/layout.html index ec210e8..1af2ff9 100644 --- a/dmapp/dmweb/templates/layout.html +++ b/dmapp/dmweb/templates/layout.html @@ -1,6 +1,14 @@ - + + + +{% block head %} +{% endblock %} + + {% block content %}