calendar mockup
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -50,7 +50,6 @@ def oneday(month,day):
|
||||
|
||||
|
||||
|
||||
|
||||
@dmbp.route("/period/<start>/<end>")
|
||||
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:
|
||||
#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
|
||||
|
||||
69
dmapp/dmweb/dmcal.py
Normal file
69
dmapp/dmweb/dmcal.py
Normal 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"> </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))
|
||||
|
||||
3
dmapp/dmweb/static/styles/dm.css
Normal file
3
dmapp/dmweb/static/styles/dm.css
Normal file
@@ -0,0 +1,3 @@
|
||||
table {
|
||||
border: 1px solid black;
|
||||
}
|
||||
21
dmapp/dmweb/templates/calendar.html
Normal file
21
dmapp/dmweb/templates/calendar.html
Normal 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 %}
|
||||
@@ -1,6 +1,14 @@
|
||||
<html>
|
||||
<head>
|
||||
</head>
|
||||
|
||||
<!--
|
||||
<link rel="stylesheet" href="{{ url_for('static', filename='styles/dm.css') }}">
|
||||
-->
|
||||
|
||||
{% block head %}
|
||||
{% endblock %}
|
||||
|
||||
</head>
|
||||
<body>
|
||||
|
||||
{% block content %}
|
||||
|
||||
Reference in New Issue
Block a user