Rename function add_month to first_of_month and add functionality do subtract a month.
[chrisu/seepark.git] / web / seepark_web.py
index 1ff515138a159df48396c238a723fc4ef66cdc4d..1a6c7c6d85d0a754071a84d3cf9ad70798f48dcf 100644 (file)
@@ -291,8 +291,15 @@ def currentwatertemperature(sensorid):
     return result.value, result.timestamp
 
 
     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/')
 
 
 @app.route('/api/<version>/sensors/')
@@ -365,7 +372,7 @@ def report(year, month):
     paper_size = (29.7 / 2.54, 21. / 2.54)  # A4
 
     begin = datetime.datetime(year, month, 1)
     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))
 
     water_data = sensordata_to_xy(select_sensordata(mainsensor, 'Wassertemperatur', begin, end))
     air_data = openweatherdata_to_xy(select_openweatherdata(cityid, begin, end))