def initialize(self, method=''): """Init private params """ self._session = Session( self, config_base.store['memcached_user']['session']['app_time_out']) self._locale = self.get_user_locale() self._userLang = self._locale.code self._ret = { 'SESSIONID': tornado.escape.url_escape(self._session.get_sessionid()), 'code': msg.API_Code.SYS_OK, 'message': msg.API_Desc.SYS_OK, 'data': None } self._validate = dict(valid='false', message='Post argument missing.') # 版本信息 self._version = self.get_argument('_vs_', '1')
def initialize(self, method = ''): """Init private params """ self._session = Session(self, config_base.store['memcached_user']['session']['app_time_out']) self._locale = self.get_user_locale() self._userLang = self._locale.code self._ret = {'SESSIONID':tornado.escape.url_escape(self._session.get_sessionid()), 'code':msg.API_Code.SYS_OK, 'message' : msg.API_Desc.SYS_OK, 'data' : None} self._validate = dict(valid= 'false', message='Post argument missing.') # 版本信息 self._version = self.get_argument('_vs_', '1')
class BaseHandler(RequestHandler): """Base Handler For API Controller """ def initialize(self, method=''): """Init private params """ self._session = Session( self, config_base.store['memcached_user']['session']['app_time_out']) self._locale = self.get_user_locale() self._userLang = self._locale.code self._ret = { 'SESSIONID': tornado.escape.url_escape(self._session.get_sessionid()), 'code': msg.API_Code.SYS_OK, 'message': msg.API_Desc.SYS_OK, 'data': None } self._validate = dict(valid='false', message='Post argument missing.') # 版本信息 self._version = self.get_argument('_vs_', '1') def get_user_locale(self): """Return current request locale """ localeLang = self.get_argument('_i18n_', '') if localeLang <> '': # self.request.headers["Accept-Language"] = localeLang return locale.get(localeLang) else: return self.get_browser_locale() def is_login(self): if self.get_current_user() is None: return False else: return True def get_current_user(self): """ @overrite Return current sign in user info """ return self._session.get('user') def get_current_uid(self): """Return current sign in user's id """ user = self.get_current_user() if not user: return None return user['_id'] def get_num(self, param, default=0): """Return integer param from the request """ num = self.get_argument(param, default) ret = 0 try: ret = int(num) except: pass return ret def pack_args(self, args=[]): """Parse 's' param from the request """ # 验证s参数是否合法 in_vals = self.get_argument("s", "{}") if in_vals.strip() == '' or in_vals.strip() == 'null': in_vals = "{}" try: in_vals = json.loads(in_vals) except Exception, e: return [False, "JSON Format Error."] if not isinstance(in_vals, dict): return [False, "JSON Format Error."] values = {} for (k, v) in args.items(): val = None # 如果未传值,验证是否必须传值或者设置默认值 if not in_vals.has_key(k): if v.has_key('required') and v['required'] == 1: return [False, "key: %s is required" % k] if v.has_key('default'): values[k] = v['default'] continue else: if v['type'] == 'd': try: val = int(in_vals[k]) except Exception as e: return [False, "key: %s's value is not an integer" % k] # 长度验证 if v.has_key('max'): if val > v['max']: return [ False, "key: %s's larger than %d " % (k, v['max']) ] if v.has_key('min'): if val < v['min']: return [ False, "key: %s's less than %d " % (k, v['min']) ] if v.has_key('range'): if val > v['range'][1] or val < v['range'][0]: return [ False, "key: %s's is not in range [%d,%d] " % (k, v['range'][0], v['range'][1]) ] if v.has_key('in'): if not val in v['in']: return [ False, "key: %s's value must be in array %s " % (k, str(v['in'])) ] elif v['type'] == 'f': try: val = float(in_vals[k]) except Exception as e: return [False, "key: %s's value is not a float" % k] elif v['type'] == 'dict': try: if isinstance(in_vals[k], dict): val = in_vals[k] elif isinstance(in_vals[k], str): val = json.dumps(in_vals[k]) except Exception as e: return [ False, "key: %s's value is not a dict or dict json string" % k ] elif v['type'] == 'list': try: if isinstance(in_vals[k], list): val = in_vals[k] elif isinstance(in_vals[k], str): val = json.dumps(in_vals[k]) else: raise Exception() if v.has_key('maxsize'): if len(val) > v['maxsize']: return [ False, "key: %s's size is more than %d " % (k, v['maxsize']) ] except Exception as e: return [ False, "key: %s's value is not a list or list json string" % k ] else: val = str(in_vals[k]) length = len(val) # 长度验证 if v.has_key('maxlen'): if length > v['maxlen']: return [ False, "key: %s's length is more than %d " % (k, v['maxlen']) ] if v.has_key('minlen'): if length < v['minlen']: return [ False, "key: %s's length is less than %d " % (k, v['minlen']) ] if v.has_key('range'): if length > v['range'][1] or length < v['range'][0]: return [ False, "key: %s's length is not in range [%d,%d] " % (k, v['range'][0], v['range'][1]) ] if v.has_key('in'): if not val in v['in']: return [ False, "key: %s's value must be in array %s " % (k, str(v['in'])) ] values[k] = val return [True, values]
class BaseHandler(RequestHandler): """Base Handler For API Controller """ def initialize(self, method = ''): """Init private params """ self._session = Session(self, config_base.store['memcached_user']['session']['app_time_out']) self._locale = self.get_user_locale() self._userLang = self._locale.code self._ret = {'SESSIONID':tornado.escape.url_escape(self._session.get_sessionid()), 'code':msg.API_Code.SYS_OK, 'message' : msg.API_Desc.SYS_OK, 'data' : None} self._validate = dict(valid= 'false', message='Post argument missing.') # 版本信息 self._version = self.get_argument('_vs_', '1') def get_user_locale(self): """Return current request locale """ localeLang = self.get_argument('_i18n_', '') if localeLang <> '': # self.request.headers["Accept-Language"] = localeLang return locale.get(localeLang) else: return self.get_browser_locale() def is_login(self): if self.get_current_user() is None: return False else: return True def get_current_user(self): """ @overrite Return current sign in user info """ return self._session.get('user') def get_current_uid(self): """Return current sign in user's id """ user = self.get_current_user() if not user: return None return user['_id'] def get_num(self, param, default = 0): """Return integer param from the request """ num = self.get_argument(param, default) ret = 0 try: ret = int(num) except: pass return ret def pack_args(self, args=[]): """Parse 's' param from the request """ # 验证s参数是否合法 in_vals = self.get_argument("s", "{}") if in_vals.strip() == '' or in_vals.strip() == 'null': in_vals = "{}" try: in_vals = json.loads(in_vals) except Exception,e: return [False, "JSON Format Error."] if not isinstance(in_vals, dict): return [False, "JSON Format Error."] values = {} for (k,v) in args.items(): val = None # 如果未传值,验证是否必须传值或者设置默认值 if not in_vals.has_key(k): if v.has_key('required') and v['required'] == 1: return [False, "key: %s is required" % k] if v.has_key('default'): values[k] = v['default'] continue else: if v['type'] == 'd': try: val = int(in_vals[k]) except Exception as e: return [False, "key: %s's value is not an integer" % k] # 长度验证 if v.has_key('max'): if val > v['max']: return [False, "key: %s's larger than %d " % (k, v['max'])] if v.has_key('min'): if val < v['min']: return [False, "key: %s's less than %d " % (k, v['min'])] if v.has_key('range'): if val > v['range'][1] or val < v['range'][0]: return [False, "key: %s's is not in range [%d,%d] " % (k, v['range'][0], v['range'][1])] if v.has_key('in'): if not val in v['in']: return [False, "key: %s's value must be in array %s " % (k, str(v['in']))] elif v['type'] == 'f': try: val = float(in_vals[k]) except Exception as e: return [False, "key: %s's value is not a float" % k] elif v['type'] == 'dict': try: if isinstance(in_vals[k], dict): val = in_vals[k] elif isinstance(in_vals[k], str): val = json.dumps(in_vals[k]) except Exception as e: return [False, "key: %s's value is not a dict or dict json string" % k] elif v['type'] == 'list': try: if isinstance(in_vals[k], list): val = in_vals[k] elif isinstance(in_vals[k], str): val = json.dumps(in_vals[k]) else: raise Exception() if v.has_key('maxsize'): if len(val) > v['maxsize']: return [False, "key: %s's size is more than %d " % (k, v['maxsize'])] except Exception as e: return [False, "key: %s's value is not a list or list json string" % k] else: val = str(in_vals[k]) length = len(val) # 长度验证 if v.has_key('maxlen'): if length > v['maxlen']: return [False, "key: %s's length is more than %d " % (k, v['maxlen'])] if v.has_key('minlen'): if length < v['minlen']: return [False, "key: %s's length is less than %d " % (k, v['minlen'])] if v.has_key('range'): if length > v['range'][1] or length<v['range'][0]: return [False, "key: %s's length is not in range [%d,%d] " % (k, v['range'][0], v['range'][1])] if v.has_key('in'): if not val in v['in']: return [False, "key: %s's value must be in array %s " % (k, str(v['in']))] values[k] = val; return [True, values]