I'm stuck with how to process the results of a query before sending it to Jinja2 for rendering on the browser. I'm developing the app on Google App Engine for Python.
I store the amount of working hours in a "calendar" entity as amount of seconds, this will then allow me to make a number of calculations about time. The problem I have is how to modify the result of the query, so that I pass to the Jinja2 html file the time not in seconds (Integer) but as HH:MM (String)
My models.py file is as follows:
'calendars_list': calendars_list,
class Calendar(ndb.Model):
wtd1 = ndb.IntegerProperty() # Working seconds day 1
wtd2 = ndb.IntegerProperty() # " 2
wtd3 = ndb.IntegerProperty() # " 3
wtd4 = ndb.IntegerProperty() # " 4
wtd5 = ndb.IntegerProperty() # " 5
wtd6 = ndb.IntegerProperty() # " 6
wtd7 = ndb.IntegerProperty() # " 7
In the main request handler I get the query (after defining ancestor etc) and need to change the seconds to HH:MM before rendering in the browser.
calendars_query = models.Calendar.query(ancestor = get_calendars_key()).order(models.Calendar.calendar_id)
calendars_list = calendars_query.fetch(10)
# Now: convert working times per day (sec) to HH:MM
for calendar in calendars_list:
calendar.wtd1 = format_as_HHMM(calendar.wtd1)
--> Gives an error: calendar.wtd1 needs to be an integer, can't be a string
template_values = {
'calendars_list': calendars_list,
}
template = JINJA_ENVIRONMENT.get_template('calendars.html')
self.response.write(template.render(template_values))
As shown above, when I modify the calendar.wtd1 element to change it from say 3600 to 01:00 I get an error because it needs to be an integer.
Any ideas how I can change the code so that I can process the query results appropriately?
Thanks!