def get_arena(self, uid): arenas = yield self.redis.get('arenas:%s' % uid) if arenas: arenas = pickle.loads(arenas) yield self.set_arena(uid) else: res = yield self.sql.runQuery("SELECT a.id, b.arena_coin, b.now_rank, a.jheros, b.jguards, b.formation, a.xp," " a.nickname, a.avat, b.before_rank, b.last_rank FROM core_user AS a, core_arena AS b WHERE a.id=b.user_id AND a.id=%s LIMIT 1", (uid, )) if res: for r in res: arenas = dict(uid=r[0], arena_coin=r[1], now_rank=r[2], heros=r[3] and escape.json_decode(r[3]) or {}, guards=r[4] and escape.json_decode(r[4]) or {}, win_times = 0, formation = r[5], xp=r[6], nickname=r[7], avat=r[8], before_rank=r[9], last_rank=r[10] ) else: arenas = None defer.returnValue(arenas)
def get_user(self, uid): user = yield self.get_cache("user:%s" % uid) if not user: res = yield self.sql.runQuery("SELECT name, nickname, avat, xp, gold, rock, feat, book, jextra, jheros, jprods, " "jbatts, jseals, jtasks, jworks, jmails, jdoors, vrock FROM core_user WHERE id=%s " "LIMIT 1", (uid,)) if not res: user = None else: r = res[0] try: user = dict(uid=uid, name=r[0], nickname=r[1], avat=r[2], xp=r[3], gold=r[4], rock=r[5], feat=r[6], book=r[7], vrock=r[17]) user['extra'] = r[8] and escape.json_decode(r[8]) or {} user['heros'] = r[9] and escape.json_decode(r[9]) or {} user['prods'] = r[10] and escape.json_decode(r[10]) or {} user['batts'] = r[11] and escape.json_decode(r[11]) or {} user['seals'] = r[12] and escape.json_decode(r[12]) or {} user['tasks'] = r[13] and escape.json_decode(r[13]) or {} user['works'] = r[14] and escape.json_decode(r[14]) or {} user['mails'] = r[15] and escape.json_decode(r[15]) or {} user['doors'] = r[16] and escape.json_decode(r[16]) or {} yield self.set_cache("user:%s" % uid, user) except Exception: user = None defer.returnValue(user)
def _on_friendfeed_request(self, callback, response): if response.error: log.msg("Error response %s fetching %s" % (response.error, response.request.url)) callback(None) return callback(escape.json_decode(response.body))
async def handlePage_(self): try: await api_req_limit.CheckHTTP(self) sid = await CheckRight(self) ulang = GetLangCode(self) temp = self.get_argument('ids', '') sensorids = c_escape.json_decode(c_escape.url_unescape(temp)) if len(sensorids) < 1: raise cyclone_web.HTTPError(404, 'no sensor') sql_str = rg_lib.Sqlite.GenInSql( """select COALESCE(name,'') name, id from rgw_sensor where id in """, sensorids) sensors = await api_core.BizDB.Query([sql_str, sensorids]) sensors_tbl = {i['id']: i for i in sensors} if len(sensors) > 0: self.render(rgw_consts.TPL_NAMES.VIEW_SENSOR_MINS_AVG_DATA, app_js_dir=settings.WEB['js_dir'], app_css_dir=settings.WEB['css_dir'], app_template_dir=settings.WEB['template_dir'], title=self.GetTitle(), sessionid=sid, user_lang=ulang, sensorids=sensorids, sensors_tbl=sensors_tbl, mins_interval_tbls=self.GetMinsInterval()) else: raise cyclone_web.HTTPError(404, 'no sensor') except models.AccessOverLimit: self.finish(rgw_consts.WebContent.ACCESS_OVER_LIMIT) except models.NoRightError: self.redirect(rgw_consts.URLs.APP_EM_LOGIN)
def get_authenticated_user(self, code): """Handles the login for the Dropbox user, returning a user object.""" body = urllib_parse.urlencode({ "redirect_uri": self.oauth_settings["redirect"], "code": code, "client_id": self.oauth_settings['key'], "client_secret": self.oauth_settings['secret'], "grant_type": "authorization_code", }) print body response = yield cyclone.httpclient.fetch( self._OAUTH_ACCESS_TOKEN_URL, method="POST", headers={'Content-Type': ['application/x-www-form-urlencoded']}, postdata=body ) if response.error: msg = 'Dropbox auth error: {}'.format(str(response)) cyclone.auth.AuthError(msg) defer.returnValue(None) args = escape.json_decode(response.body) defer.returnValue(args)
def _on_facebook_request(self, callback, response): if response.error: log.msg("Error response %s fetching %s" % (response.error, response.request.url), logLevel=logging.WARNING) callback(None) return callback(escape.json_decode(response.body))
def get_authenticated_user(self, code): """Handles the login for the Dropbox user, returning a user object.""" body = urllib_parse.urlencode({ "redirect_uri": self.oauth_settings["redirect"], "code": code, "client_id": self.oauth_settings['key'], "client_secret": self.oauth_settings['secret'], "grant_type": "authorization_code", }) print body response = yield cyclone.httpclient.fetch( self._OAUTH_ACCESS_TOKEN_URL, method="POST", headers={'Content-Type': ['application/x-www-form-urlencoded']}, postdata=body) if response.error: msg = 'Dropbox auth error: {}'.format(str(response)) cyclone.auth.AuthError(msg) defer.returnValue(None) args = escape.json_decode(response.body) defer.returnValue(args)
def _on_facebook_request(self, callback, response): if response.error: log.warning("Error response %s fetching %s", response.error, response.request.url) callback(None) return callback(escape.json_decode(response.body))
def inner(self, response): self.assertTrue(response) try: response = json_decode(response) except ValueError: # No json object could be decoded. self.fail('response %s was not json decodable.' % repr(response)) else: return func(self, response)
def _success(response, deferred): if response.code == 200: data = escape.json_decode(response.body) error = data.get("error") if error: deferred.errback(Exception(error)) else: deferred.callback(data.get("result")) else: deferred.errback(HTTPError(response.code, response.phrase))
def set_arena(self, uid): res = yield self.sql.runQuery("SELECT a.id, b.arena_coin, b.now_rank, a.jheros, b.jguards, b.formation, a.xp," " a.nickname, a.avat, b.before_rank, b.last_rank FROM core_user AS a, core_arena AS b WHERE a.id=b.user_id AND a.id=%s", (uid, )) if res: arenas = {r[0]: dict(uid=r[0], arena_coin=r[1], now_rank=r[2], heros=r[3] and escape.json_decode(r[3]) or {}, guards=r[4] and escape.json_decode(r[4]) or {}, win_times = 0, formation = r[5], xp=r[6], nickname=r[7], avat=r[8], before_rank=r[9], last_rank=r[10] ) for r in res} for k, v in arenas.iteritems(): yield self.redis.set('arenas:%s' % k, pickle.dumps(v)) defer.returnValue(None)
def put(self): """ Processes PUT requests: * /config """ if not self.request.headers.get('Settings'): return self.error('invalid query') try: self.write(self.result(self.controller.set( json_decode(self.request.headers['Settings'])))) except ValueError as exc: return self.write(self.error(exc))
def get_hunt(self, uid): hunts = yield self.redis.get('hunts:%s' % uid) if hunts: hunts = pickle.loads(hunts) # yield self.set_arena(uid) else: res = yield self.sql.runQuery("SELECT a.id, a.jheros, b.jguards, b.formation, a.xp," " a.nickname, a.avat FROM core_user AS a, core_hunt AS b WHERE a.id=b.user_id AND a.id=%s LIMIT 1", (uid, )) if res: for r in res: hunts = dict(uid=r[0], heros=r[1] and escape.json_decode(r[1]) or {}, guards=r[2] and escape.json_decode(r[2]) or {}, formation = r[3], xp=r[4], nickname=r[5], avat=r[6], ) else: arenas = None defer.returnValue(hunts)
def update_mails(self, user): mails = [key for key in user['mails'] if user['mails'][key] == 0] allmails = {} if mails: res = yield self.sql.runQuery("SELECT id, sender, title, content, jawards, created_at FROM core_mail WHERE to_id=%s AND id in %s ORDER BY created_at DESC", (user['uid'], tuple(mails))) if res: for r in res: mid = str(r[0]) mail = dict(mid=mid, sender=r[1], title=r[2], content=r[3], timestamp=time.mktime(r[5].timetuple())) mail['awards'] = r[4] and escape.json_decode(r[4]) or {} allmails[mid] = mail yield self.redis.set("allmails:%s" % user['uid'], pickle.dumps(allmails)) defer.returnValue(None)
def put(self): """ Processes PUT requests: * /config """ if not self.request.headers.get('Settings'): return self.error('invalid query') try: self.write( self.result( self.controller.set( json_decode(self.request.headers['Settings'])))) except ValueError as exc: return self.write(self.error(exc))
def post(self): """ Processes POST requests: * /config <hellais> maker: sure. Though it's recommended to also create the equivalent POST based method since certain browsers/HTTP clients don't support PUT method """ if not self.request.body: return self.error('invalid query') try: self.write(json_decode(self.request.body)) except ValueError as exc: return self.error(exc)
def get_authenticated_user(self, callback): """Fetches the authenticated Facebook user. The authenticated user includes the special Facebook attributes 'session_key' and 'facebook_uid' in addition to the standard user attributes like 'name'. """ self.require_setting("facebook_api_key", "Facebook Connect") session = escape.json_decode(self.get_argument("session")) self.facebook_request( method="facebook.users.getInfo", callback=self.async_callback(self._on_get_user_info, callback, session), session_key=session["session_key"], uids=session["uid"], fields="uid,first_name,last_name,name,locale,pic_square," "profile_url,username", )
def _parse_response(self, callback, response): if response.error: log.msg("HTTP error from Facebook: %s" % response.error) callback(None) return try: json = escape.json_decode(response.body) except: log.msg("Invalid JSON from Facebook: %r" % response.body) callback(None) return if isinstance(json, dict) and json.get("error_code"): log.msg("Facebook error: %d: %r" % (json["error_code"], json.get("error_msg"))) callback(None) return callback(json)
def _success(response, deferred): if response.code == 200: data = escape.json_decode(response.body) error = data.get("error") if error: if isinstance(error, DictType) and 'message' in error: # JSON-RPC spec is not very verbose about error schema, # but it should look like {'code': 0, 'message': 'msg'} deferred.errback(Exception(error['message'])) else: # For backward compatibility with previous versions of # cyclone.jsonrpc.JsonrpcRequestHandler deferred.errback(Exception(error)) else: deferred.callback(data.get("result")) else: deferred.errback(HTTPError(response.code, response.phrase))
def get_authenticated_user(self, callback): """Fetches the authenticated Facebook user. The authenticated user includes the special Facebook attributes 'session_key' and 'facebook_uid' in addition to the standard user attributes like 'name'. """ self.require_setting("facebook_api_key", "Facebook Connect") session = escape.json_decode(self.get_argument("session")) self.facebook_request( method="facebook.users.getInfo", callback=self.async_callback(self._on_get_user_info, callback, session), session_key=session["session_key"], uids=session["uid"], fields="uid,first_name,last_name,name,locale,pic_square," "profile_url,username")
def __cbResponse(self, response): error = None result = None try: data = escape.json_decode(response.body) except ValueError: error = "Invalid response: {0!r}".format(response.body) else: if data["error"] is None: result = data["result"] else: error = data["error"] if error: return failure.Failure(JsonrpcError(error)) else: return result
async def __get(self): try: self.set_header('Content-Type', 'image/svg+xml') temp = self.get_argument('sensorids') sensorids = c_escape.json_decode(c_escape.url_unescape(temp)) para = { 'width': float(self.get_argument('width')), 'height': float(self.get_argument('height')), 'hours': int(self.get_argument('hours')), 'mins': int(self.get_argument('mins')), 'tz_offset': int(self.get_argument('tz_offset', 0)), 'sensorids': sensorids } res = await plotting_api.SensorRecentHoursLog(para) self.finish(res) except Exception: log.err() raise cyclone_web.HTTPError(400)
def update_huntguard(self, uid, heros, formation): res = yield self.sql.runQuery("SELECT a.id, a.jheros, b.jguards, b.formation, a.xp, a.nickname, \ a.avat FROM core_user AS a, core_hunt AS b WHERE a.id=b.user_id AND a.id=%s" % uid) if res: for r in res: jheros = r[1] and escape.json_decode(r[1]) or {} jguards = {} jguards_list = filter(lambda j:j in jheros, heros) for j in jguards_list: jguards[j] = jheros[j] query = "UPDATE core_hunt SET jguards=%s, formation=%s WHERE user_id=%s RETURNING id" params = (escape.json_encode(jguards), formation, uid) for i in range(5): try: yield self.sql.runQuery(query, params) break except storage.IntegrityError: log.msg("SQL integrity error, retry(%i): %s" % (i, (query % params))) continue #yield self.set_arena(uid) defer.returnValue(jguards.keys())
def post(self, *args): self._auto_finish = False try: req = cyclone_escape.json_decode(self.request.body) jsonid = req["id"] method = req["method"] assert isinstance(method, str), \ "Invalid method type: %s" % type(method) params = req.get("params", []) assert isinstance(params, (list, tuple)), \ "Invalid params type: %s" % type(params) except Exception as e: log.msg("Bad Request: %s" % str(e)) raise cyclone_web.HTTPError(400) func = self.GetFunc(method) if callable(func): args = list(args) + params d = defer.ensureDeferred(func(*args)) d.addBoth(self._cbResult, jsonid) else: self._cbResult(AttributeError("method not found: %s" % method), jsonid)
def post(self): """ Processes asyncronous request: * GET /auth/login """ if self.action != 'login': raise web.HTTPError(404) # if self.request.remote_ip == '127.0.0.1': # self.set_secure_cookie(self._uid_cookie, config.custom['passwd']) # return self.write(self.result(True)) if not self.application.conf['remote_login']: raise web.HTTPAuthenticationRequired request = json_decode(self.request.body) if 'passwd' not in request: return self.error('invalid request') elif not self.get_current_user(request['passwd']): return self.write(self.error('login failed')) else: self.set_secure_cookie('auth', request['passwd']) return self.write(self.result(True))
def post(self, *args): jsonid = None try: req = escape.json_decode(self.request.body) method = req["method"] assert isinstance(method, types.StringTypes), "{0}: {1}".format(method, type(method)) params = req["params"] assert isinstance(params, (types.ListType, types.TupleType)), "{0}: {1}".format(params, type(params)) jsonid = req["id"] assert isinstance(jsonid, (types.NoneType, types.IntType, types.StringTypes)), "{0}: {1}".format(jsonid, type(jsonid)) except Exception as e: self.log(e, "bad request", isError=True) d = defer_fail(JsonrpcError("bad request")) else: # XXX: not supports nested services. e.g.: foo.bar function = getattr(self, "jsonrpc_{0}".format(method), None) if callable(function): args = list(args) + list(params) d = mustbe_deferred(function, *args) else: d = defer_fail(JsonrpcError("method not found: {0}".format(method))) d.addBoth(self._cbResult, jsonid) return d
async def handlePage_(self): try: await api_req_limit.CheckHTTP(self) sid = await CheckRight(self) ulang = GetLangCode(self) temp_str = self.get_argument('sensorids') sensorids = c_escape.json_decode(c_escape.url_unescape(temp_str)) plotting_no = self.get_argument('plotting_no', '1') if len(sensorids) < 1: raise cyclone_web.HTTPError(404, 'no sensor') sql_str = rg_lib.Sqlite.GenInSql( """select COALESCE(name,'') name, id from rgw_sensor where id in """, sensorids) sensors = await api_core.BizDB.Query([sql_str, sensorids]) if len(sensors) > 0: label_tbl = self.GetLabelTbl()[ulang] self.render(rgw_consts.TPL_NAMES.VIEW_SENSORS_RECENT_TREND, app_js_dir=settings.WEB['js_dir'], app_css_dir=settings.WEB['css_dir'], app_template_dir=settings.WEB['template_dir'], title=self.GetTitle(), sessionid=sid, user_lang=ulang, sensorids=sensorids, hours_tbls=self.GetHoursTbls(), mins_interval_tbls=self.GetMinsInterval(), label_tbl=label_tbl, plotting_no=plotting_no, sensor_recent_hours_plotting_url=rgw_consts.URLs. VIEW_RECENT_HOURS_SENSOR_DATA_PLOTTING[1:]) else: raise cyclone_web.HTTPError(404, 'no sensor') except models.AccessOverLimit: self.finish(rgw_consts.WebContent.ACCESS_OVER_LIMIT) except models.NoRightError: self.redirect(rgw_consts.URLs.APP_EM_LOGIN)
def set_user(self, uid, name=None, nickname=None, avat=None, xp=None, gold=None, rock=None, feat=None, book=None, extra=None, heros=None, prods=None, batts=None, seals=None, tasks=None, works=None, mails=None, doors=None, vrock=None): suser = {'uid': uid} subqueries = [] if name is not None: suser['name'] = name subqueries.append("name=%(name)s") if nickname is not None: suser['nickname'] = nickname subqueries.append("nickname=%(nickname)s") if avat is not None: suser['avat'] = avat subqueries.append("avat=%(avat)s") if xp is not None: suser['xp'] = xp subqueries.append("xp=%(xp)s") if gold is not None: suser['gold'] = gold subqueries.append("gold=%(gold)s") if rock is not None: suser['rock'] = rock subqueries.append("rock=%(rock)s") if feat is not None: suser['feat'] = feat subqueries.append("feat=%(feat)s") if book is not None: suser['book'] = book subqueries.append("book=%(book)s") if vrock is not None: suser['vrock'] = vrock subqueries.append("vrock=%(vrock)s") if extra is not None: suser['jextra'] = escape.json_encode(extra) subqueries.append("jextra=%(jextra)s") if heros is not None: suser['jheros'] = escape.json_encode(heros) subqueries.append("jheros=%(jheros)s") if prods is not None: suser['jprods'] = escape.json_encode(prods) subqueries.append("jprods=%(jprods)s") if batts is not None: suser['jbatts'] = escape.json_encode(batts) subqueries.append("jbatts=%(jbatts)s") if seals is not None: suser['jseals'] = escape.json_encode(seals) subqueries.append("jseals=%(jseals)s") if tasks is not None: suser['jtasks'] = escape.json_encode(tasks) subqueries.append("jtasks=%(jtasks)s") if works is not None: suser['jworks'] = escape.json_encode(works) subqueries.append("jworks=%(jworks)s") if mails is not None: suser['jmails'] = escape.json_encode(mails) subqueries.append("jmails=%(jmails)s") if doors is not None: suser['jdoors'] = escape.json_encode(doors) subqueries.append("jdoors=%(jdoors)s") suser['timestamp'] = str(int(time.time())) subqueries.append("timestamp=%(timestamp)s") # SQL UPDATE START query = "UPDATE core_user SET " + ",".join(subqueries) + " WHERE id=%(uid)s RETURNING name, nickname, avat, xp, gold, rock, feat, book, jextra," \ "jheros, jprods, jbatts, jseals, jtasks, jworks, jmails, jdoors, vrock" params = suser user = None for i in range(5): try: res = yield self.sql.runQuery(query, params) if not res: user = None yield self.del_cache("user:%s" % uid) else: r = res[0] user = dict(uid=uid, name=r[0], nickname=r[1], avat=r[2], xp=r[3], gold=r[4], rock=r[5], feat=r[6], book=r[7], vrock=r[17]) user['extra'] = r[8] and escape.json_decode(r[8]) or {} user['heros'] = r[9] and escape.json_decode(r[9]) or {} user['prods'] = r[10] and escape.json_decode(r[10]) or {} user['batts'] = r[11] and escape.json_decode(r[11]) or {} user['seals'] = r[12] and escape.json_decode(r[12]) or {} user['tasks'] = r[13] and escape.json_decode(r[13]) or {} user['works'] = r[14] and escape.json_decode(r[14]) or {} user['mails'] = r[15] and escape.json_decode(r[15]) or {} user['doors'] = r[16] and escape.json_decode(r[16]) or {} yield self.set_cache("user:%s" % uid, user) break except storage.IntegrityError: log.msg("SQL integrity error, retry(%i): %s" % (i, (query % params))) continue # SQL UPDATE END defer.returnValue(user)
def test_json_decode(self): data = '{"a": "b"}' result = json_decode(data) self.assertEqual(result, {"a": "b"})
def test_post_auth_login(self, response): self.assertEqual(json_decode(response), {'result':True})
async def __get(self): try: self.set_header('Content-Type', 'image/svg+xml') temp = self.get_argument('sensorids') sensorids = c_escape.json_decode(c_escape.url_unescape(temp)) para = { 'width': float(self.get_argument('width')), 'height': float(self.get_argument('height')), 'hours': int(self.get_argument('hours')), 'mins': int(self.get_argument('mins')), 'tz_offset': int(self.get_argument('tz_offset', 0)), 'sensorids': sensorids } svg_height = para.get('height') svg_width = para.get('width') hours = para.get('hours') tz_offset = para.get('tz_offset', 0) mins = para.get('mins') sensorids = para.get('sensorids') sql_str = rg_lib.Sqlite.GenInSql( """select COALESCE(r1.name,'') name, r1.id, r1.data_no,r1.val_precision, r1.val_unit from rgw_sensor r1 where r1.id in """, sensorids) sensors = await api_core.BizDB.Query([sql_str, sensorids]) sensors_tbl = {s['id']: s for s in sensors} curr = rg_lib.DateTime.ts() start_ts = curr - hours * 3600 start_ts = rg_lib.DateTime.dt2ts( rg_lib.DateTime.ts2dt(start_ts).replace(minute=0, second=0)) dt_series = rg_lib.DateTime.GetMinSeries(start_ts, curr, mins, 'datetime') ts_series = [rg_lib.DateTime.dt2ts(i) for i in dt_series] rows = await api_sensor_avg_data.QueryMinAvg( start_ts, curr, sensorids, mins, 2000) rows_tbl = collections.OrderedDict() for r in rows: if r['cts'] in rows_tbl: rows_tbl[r['cts']].append(r) else: rows_tbl[r['cts']] = [r] if len(dt_series) > 9: steps = 1 + len(dt_series) // 9 else: steps = 1 chart_obj = pygal.Line(x_labels_major_every=steps, x_label_rotation=20, show_minor_x_labels=False, dots_size=1, height=svg_height, width=svg_width, legend_at_bottom=True, show_y_guides=False, y_labels_major_every=3, show_minor_y_labels=False, allow_interruptions=True, style=pygal_style.CleanStyle) chart_obj.x_labels = [ (rg_lib.DateTime.ts2dt(i) + datetime.timedelta(hours=tz_offset)).strftime('%H:%M') for i in dt_series ] for sensorid in sensorids: vals = [{'value': None}] * len(chart_obj.x_labels) sensor = sensors_tbl[sensorid] for _, cts in enumerate(rows_tbl.keys()): for row in rows_tbl[cts]: if row['sensorid'] == sensorid and cts in ts_series: idx = ts_series.index(cts) vals[idx] = { 'value': row['avg_val'], 'formatter': functools.partial(GetValLabel, sensor_mdl=sensor) } chart_obj.add(sensors_tbl[sensorid]['name'], vals) temp = chart_obj.render(True) self.finish(temp) except Exception: log.err() raise cyclone_web.HTTPError(400)
def test_post_auth_login(self, response): self.assertEqual(json_decode(response), {'result': True})