]> ToastFreeware Gitweb - chrisu/seepark.git/blobdiff - web/seepark_web.py
Add link to current and previous month's report.
[chrisu/seepark.git] / web / seepark_web.py
index 1ff515138a159df48396c238a723fc4ef66cdc4d..c6ceffb8614b6f049317983f6372bcc217e12048 100644 (file)
@@ -291,8 +291,15 @@ def currentwatertemperature(sensorid):
     return result.value, result.timestamp
 
 
-def add_month(date):
-    return (date + datetime.timedelta(days=42)).replace(day=1)
+def first_of_month(date, month):
+    date = date.replace(day=1)
+    if month == 0:
+        return date
+    if month == 1:
+        return (date + datetime.timedelta(days=42)).replace(day=1)
+    if month == -1:
+        return (date - datetime.timedelta(days=1)).replace(day=1)
+    assert False
 
 
 @app.route('/api/<version>/sensors/')
@@ -358,14 +365,14 @@ def currentwater(version):
     return jsonify({"value": value, "timestamp": timestamp})
 
 
-@app.route('/report/<int:year>/<int:month>')
+@app.route('/report/<int(fixed_digits=4):year>/<int(fixed_digits=2):month>')
 def report(year, month):
-    """Report for given year and month
+    """Report for given year (4 digits) and month (2 digits)
     """
     paper_size = (29.7 / 2.54, 21. / 2.54)  # A4
 
     begin = datetime.datetime(year, month, 1)
-    end = add_month(begin)
+    end = first_of_month(begin, 1)
 
     water_data = sensordata_to_xy(select_sensordata(mainsensor, 'Wassertemperatur', begin, end))
     air_data = openweatherdata_to_xy(select_openweatherdata(cityid, begin, end))
@@ -460,6 +467,8 @@ def report(year, month):
 def index():
     airvalue, airtime     = currentairtemperature(cityid)
     watervalue, watertime = currentwatertemperature(mainsensor)
+    this_month = first_of_month(datetime.date.today(), 0)
+    last_month = first_of_month(this_month, -1)
 
     return render_template(
         'seepark_web.html',
@@ -468,4 +477,6 @@ def index():
         watertime=watertime,
         airvalue=airvalue,
         airtime=airtime,
+        this_month=this_month,
+        last_month=last_month
     )