DaClown DaClown - 2 months ago 8
Python Question

How to store and update a configuration in Google App Engine (Python)

Given a configuration consisting of a few values, say displayName and startingDate, that is constant most of the time but shall also be editable by the user via a sub page of a google app, what would be the best way to store and modify this configuration?

At the moment I have something like this:

class Configuration(ndb.Model):
displayName = ndb.StringProperty()
starting = ndb.DateTimeProperty()


and modify it like this:

allConfigurations = Configuration.query().fetch(1)
current = None
try:
current = allConfigurations[0]
except:
current = Configuration()
current.displayName = self.request.get('newDisplayName')
current.starting = self.request.get('newStartingDate')
current.put()


Basically this retrieves all configurations, limits the result to just one (or creates a new config if the data store is empty), modifies the values and writes it back to the data store. But to me this seems to complicated to be the prefered way.

Edit:
In another environment I maybe would have just used an .ini file for this. I'm basically looking for the google app engine equivalent.

Answer

AFAIK there isn't any other way to achieve what you want, though I'd probably do it more like..

current = Configuration.query().get() or Configuration()
current.displayName = self.request.get('newDisplayName')
current.starting = self.request.get('newStartingDate')
current.put()

but I couldn't help myself and would have to allow for alternate configs..

id = self.request.get('id') or 'default'
config = Configuration.get_by_id(id) or Configuration(id = id)
config.displayName = self.request.get('newDisplayName')
config.starting = self.request.get('newStartingDate')
config.put()