calendar mockup

This commit is contained in:
Mariano Ramon
2020-05-04 02:21:07 -03:00
parent bc1a5370f2
commit 3e073a0e4b
6 changed files with 123 additions and 15 deletions

View File

@@ -1,10 +1,11 @@
from flask import Flask from flask import Flask
from dmweb import dm from dmweb import dm, dmcal
def create_app(): def create_app():
app = Flask("deskmeter") app = Flask("deskmeter")
app.debug = True
app.register_blueprint(dm.dmbp) app.register_blueprint(dm.dmbp)
return app return app

View File

@@ -50,7 +50,6 @@ def oneday(month,day):
@dmbp.route("/period/<start>/<end>") @dmbp.route("/period/<start>/<end>")
def period(start,end): def period(start,end):
@@ -102,6 +101,8 @@ def get_period_totals(start, end):
length = switches.count_documents(queries["period"]) length = switches.count_documents(queries["period"])
docs = switches.find(queries["period"]).sort([("date", 1)]) docs = switches.find(queries["period"]).sort([("date", 1)])
if not length:
return [{"ws": "No Data", "total": ""}]
#next_doc = switches.find_one(queries["next_doc"]) #next_doc = switches.find_one(queries["next_doc"])
last_doc = docs[length-1] last_doc = docs[length-1]
@@ -116,12 +117,16 @@ def get_period_totals(start, end):
for total in switches.aggregate(pipeline=pipe): for total in switches.aggregate(pipeline=pipe):
pre_rows[total["_id"]] = total["totals"] pre_rows[total["_id"]] = total["totals"]
time_corrections = [] time_corrections = []
if first_date > start: if first_date > 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] 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)})) time_corrections.append(Counter({previous_doc["workspace"] : split_entry(previous_doc, start)}))
except IndexError:
pass
now = local_date(datetime.datetime.now()) now = local_date(datetime.datetime.now())
@@ -131,27 +136,28 @@ def get_period_totals(start, end):
if end > last_date and now > end: if end > last_date and now > end:
time_corrections.append(Counter({ last_doc["workspace"] : split_entry(last_doc, end, after=False)})) time_corrections.append(Counter({ last_doc["workspace"] : split_entry(last_doc, end, after=False)}))
for correction in time_corrections: for correction in time_corrections:
pre_rows += correction pre_rows += correction
day = 0 #TODO _1 remove
#day = 0
rows = [] rows = []
for ws, total in pre_rows.items(): for ws, total in pre_rows.items():
day += total #TODO _1 remove
#day += total
rows.append( {"ws": ws, rows.append( {"ws": ws,
"total" : datetime.timedelta(seconds=total)}) "total" : datetime.timedelta(seconds=total)})
#TODO _1 remove
rows.append({"ws":"sum","total": day}) #rows.append({"ws":"sum","total": day})
return rows return rows
def split_entry(entry, split_time, after=True): def split_entry(entry, split_time, after=True):
""" """
entry must have a date an number entry must have a date an number
split time must be timezone aware split_time must be timezone aware
after, bolean to return the time after the split time after bolean to return the time after the split time
""" """
first_half = round((split_time - local_date(entry["date"], True)).total_seconds()) first_half = round((split_time - local_date(entry["date"], True)).total_seconds())
last_half = entry["delta"] - first_half last_half = entry["delta"] - first_half

69
dmapp/dmweb/dmcal.py Normal file
View File

@@ -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 = "<table class='totaltable'>"
for row in rows:
returnstr += "<tr><td>{}</td><td>{}</td></tr>".format(row["ws"],row["total"])
returnstr += "</table>"
return returnstr
def formatday(self, day, weekday):
"""
Return a day as a table cell.
"""
if day == 0:
return '<td class="noday">&nbsp;</td>' # day outside month
else:
return '<td class="%s">%s</td>' % (self.cssclasses[weekday], self.oneday(self.dmmonth, day))
@dmbp.route("/calendar")
@dmbp.route("/calendar/<int:month>")
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))

View File

@@ -0,0 +1,3 @@
table {
border: 1px solid black;
}

View File

@@ -0,0 +1,21 @@
{% extends 'layout.html' %}
{% block head %}
<style type="text/css">
table {
width: 100%;
border: 1px solid black;
}
</style>
{% endblock head %}
{% block content %}
{{ content | safe }}
{% endblock content %}

View File

@@ -1,5 +1,13 @@
<html> <html>
<head> <head>
<!--
<link rel="stylesheet" href="{{ url_for('static', filename='styles/dm.css') }}">
-->
{% block head %}
{% endblock %}
</head> </head>
<body> <body>