import string
import sys
import time
import urllib
import wsgiref.handlers
from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext import webapp
class Logi(webapp.RequestHandler):
"""Fetch data from Google App Engine and write as XML"""
def get(self):
def parse_query_string(strToken):
"""Return a value from the querystring in the request URL"""
strKeys=self.request.GET
if strToken in strKeys:
return strKeys[strToken]
else:
return None
def write_error(strMessage='', strAction='', strLineNo=''):
"""Write out an XML document with a description of the error"""
strError = []
strError.append('\n')
strError.append('\n')
self.response.out.write(''.join(strError))
t1 = time.time()
strUserName = ''
user = users.get_current_user()
if user:
strUserName = user.nickname()
strModuleName = parse_query_string('Module')
strEntityName = parse_query_string('Entity')
if strEntityName == None:
write_error('The Entity parameter must be set')
return
strAction = parse_query_string('Action')
if strAction == None:
write_error('The Action parameter must be set')
return
else:
strAction = strAction.lower()
strFilter = parse_query_string('Filter')
if strFilter == None:
strFilter = ''
else:
strFilter = urllib.unquote(strFilter) # WHERE%20Z%20%3E%2014 -> WHERE Z > 14
try:
# import the (URL-specified) class from the (URL-specified) module
module = __import__(strModuleName)
appEntity = getattr(module, strEntityName)
except:
write_error('Unable to load data module ' + strModuleName + ': ' + str(sys.exc_info()[1]))
return
strOutput = [] #for efficiency, build list, then join together
if strAction=='select':
try:
strOutput.append('\n')
strOutput.append('\n')
intCount = 0
for anEntity in db.GqlQuery('SELECT * FROM ' + strEntityName + ' ' + strFilter):
strOutput.append('\n')
intCount += 1
#add summary record
t2 = time.time()
strTime = str((t2-t1)*1.0)
#strOutput.append('')
strOutput.append('\n')
self.response.out.write(''.join(strOutput))
except:
write_error(str(sys.exc_info()[1]), 'select', str(sys.exc_info()[2].tb_lineno))
return
elif strAction=='insert':
try:
strEvalProps = ''
for item_prop in appEntity.properties():
if str(appEntity.properties()[item_prop]).find('StringProperty') > -1:
newVal = parse_query_string(item_prop)
if newVal != None:
strEvalProps += item_prop + '=u"' + newVal.replace('"', '\\"').replace("'", "\\'") + '",'
elif str(appEntity.properties()[item_prop]).find('IntegerProperty') > -1:
newVal = parse_query_string(item_prop)
if newVal != None and newVal != '':
strEvalProps += item_prop + '=' + `int(newVal)` + ','
else:
newVal = parse_query_string(item_prop)
if newVal != None:
strEvalProps += item_prop + '="' + `newVal`.replace('"', '\\"').replace("'", "\\'") + '",'
strEvalProps = strEvalProps.rstrip(',')
newEntity = eval('getattr(module, strEntityName)(' + strEvalProps +')')
newKey = newEntity.put()
except:
write_error(str(sys.exc_info()[1]), 'insert', str(sys.exc_info()[2].tb_lineno))
return
elif strAction=='update':
try:
for anEntity in db.GqlQuery('SELECT * FROM ' + strEntityName + ' ' + strFilter):
for item_prop in anEntity.properties():
newVal = parse_query_string(item_prop)
if newVal != None:
setattr(anEntity, item_prop, newVal)
anEntity.put()
except:
write_error(str(sys.exc_info()[1]), 'update', str(sys.exc_info()[2].tb_lineno))
return
elif strAction=='delete':
try:
for anEntity in db.GqlQuery('SELECT * FROM ' + strEntityName + ' ' + strFilter):
anEntity.delete()
except:
write_error(str(sys.exc_info()[1]), 'delete', str(sys.exc_info()[2].tb_lineno))
return
def main():
application = webapp.WSGIApplication(
[('/logi', Logi)],
debug=True)
wsgiref.handlers.CGIHandler().run(application)
if __name__ == "__main__":
main()