def post(self): call_type = self.request.get('call', 'calculate') if not self.is_valid_input(call_type, HOME_CALL_TYPE): return self.render_json({'error': ERROR_INVALID_PARAM % 'call'}) if call_type == 'calculate': calculation_object = self.request.get('cal_obj', '') do_logging(calculation_object) value = None error = None try: value = eval(calculation_object) except SyntaxError: do_logging(traceback.format_exc()) error = "Invalid Syntax" if not error: Logs.add_obj(**{'user_name': self.request.cookies.get('user', 'random'), 'cal_obj': calculation_object + " = %s" % value, 'call_ip': self.request.remote_addr}) return self.render_json({'error': error, 'value': value, 'cal': calculation_object}) elif call_type == 'logs': cursor = self.request.get('cursor', '') logs = Logs.get_logs(cursor) return self.render_json(logs)
def add_obj(cls, **kwarg): '''cal_obj, call_ip are necessary and user_name is optional ''' do_logging("in db") cls(**kwarg).put()
def render_json(self, obj, do_log=True): if do_log: do_logging(obj) response = webapp2.Response(json.dumps(obj)) response.content_type = 'text/json' return response