def get(self): """Handle GET requests by passing to SQL wrapper. Respond with JSON.""" self.response.headers['Content-Type'] = 'application/json' print self.request.GET try: email = self.verify_oauth_token(self.request.GET['token']) if email and self.verify_permissions(self.request.GET, email): action = self.request.GET['action'] if action in ['select', 'delete', 'insert', 'update']: table = self.request.GET['table'] wr = RESTWrapper(SQLHost, SQLuser, SQLpass, SQLDB) sql_args = self.request.GET del sql_args['token'] del sql_args['action'] del sql_args['table'] res = wr.query(action, table, **sql_args) if action in ['select', 'insert']: self.response.write(json.dumps({"error": "", "message": res}, default=dec_default)) else: self.response.write(json.dumps({"error": "", "message": "Successful"})) elif action == 'getsalesnearby': lat = float(scrub(self.request.GET['lat'])) lng = float(scrub(self.request.GET['long'])) radius = int(scrub(self.request.GET['r'])) wr = RESTWrapper(SQLHost, SQLuser, SQLpass, SQLDB) res = wr.query_passthru("""SELECT * FROM Sale WHERE Lat IS NOT NULL AND Lng IS NOT NULL""") nearest = [] # Find sales within r miles of user for row in res: if point_near(lat, lng, row['Lat'], row['Lng'], radius): nearest.append(row) self.response.write(json.dumps({"error": "", "message": nearest}, default=dec_default)) else: self.response.write(json.dumps({"error": "Invalid action", "message": ""})) else: self.response.set_status(401) self.response.write(json.dumps({"error": "Unauthorized token or bad permissions (You can only delete and update records you created)", "message": ""})) except KeyError, e: print e print traceback.format_exc() self.response.write(json.dumps({"error": "Malformed GET request", "message": ""}))
from types import * from sqlwrapper import RESTWrapper print 'Testing wrapper insert' wr = RESTWrapper('localhost', 'textbooks', 'textbooks470', 'textbooks') assert True == wr.insert('test', testing='valuepy12345', this='valuepy2') print 'Test succeeded' print 'Testing wrapper select' wr = RESTWrapper('localhost', 'textbooks', 'textbooks470', 'textbooks') l = wr.select_all('test', testing='valuepy12345') assert type(l) is ListType, "l is not a list: %r" % l print 'Test succeeded' # TODO: curl server hits # TODO: json conversion testing # TODO: passthru testing