0

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!

kxtronic
  • 331
  • 5
  • 16
  • could you please share the "format_as_HHMM" function implementation? – Nicolas Bortolotti Jun 08 '17 at 18:59
  • Hi Nicolas, I haven't implemented the function yet. I could use some of the datetime functions in python. For the moment I only have a stub that returns the string "12:00". – kxtronic Jun 12 '17 at 19:37

1 Answers1

1

The issue with your code is that you try to assign a value that is not an integer to a ndb.IntegerProperty().

Replace

calendar.wtd1 = format_as_HHMM(calendar.wtd1) 

with

calendar.wtd1_HHMM = format_as_HHMM(calendar.wtd1) 

and use calendar.wtd1_HHMM in the template and it will probably work out nicely.

Edit: If you prefer to convert from integer to HH:mm in the template as part of the presentation logic you could do that easily by writing the conversion function and register it as a filter as explained in the documentation - writing filters

When it comes to functions for doing the conversion you are in luck: Python: convert seconds to hh:mm:ss

Arne S
  • 1,004
  • 14
  • 41
  • Hi Arne, Thanks for your feedback. I see what you mean by using calendar.wtd1_HHMM in the template. It means I need to build a new list op parameters for the template, and I was hoping there was some date conversion function that would do the trick. I will try it out as you suggest, thanks! – kxtronic Jun 12 '17 at 19:28