def test_POST(self): f_model = sh.model('UserForgetPassword') my_id = self.register() # 设置Userid后便可以删除 new_id = f_model.insert(dict(Userid=my_id, code='c')) self.assertIsNotNone(f_model.get(new_id)) data = {'model_name': 'UserForgetPassword', 'model_id': new_id} self.get(api_url, data) self.assertIsNone(f_model.get(new_id)) # 删除不存在的数据会返回True,但是affected等于0 res = self.get(api_url, data) res = sh.loadsJson(res) self.assertTrue(res.success) self.assertEqual(res.affected, 0) # 如果没有登录的话,是不能删除的 data['model_id'] = f_model.insert(dict(Userid=my_id, code='c')) self.logout() res = self.get(api_url, data) res = sh.loadsJson(res) self.assertFalse(res.success) # 不能删除别人的数据 my_id = self.register() new_id = f_model.insert(dict(Userid=my_id+1, code='c')) data['model_id'] = new_id res = self.get(api_url, data) res = sh.loadsJson(res) self.assertFalse(res.success) # 如果没有Userid属性的话,是不能删除的 s_model = sh.model('SiteConfig') new_id = s_model.insert(dict(name='n', value='v')) res = self.get(api_url, {'model_name': 'SiteConfig', 'model_id': new_id}) res = sh.loadsJson(res) self.assertFalse(res.success) self.assertIsNotNone(s_model.get(new_id))
def test_POST(self): model = sh.model("SiteConfig") new_id = model.insert({"name": "n", "value": "v"}) res = self.get(api_url, {"model_name": "SiteConfig", "model_id": new_id}) res = sh.loadsJson(res) self.assertEqual(res.id, new_id) self.assertEqual(res.SiteConfigid, new_id) self.assertEqual(res.name, "n") res = self.get(api_url, {"model_name": "SiteConfig", "model_id": new_id + 1}) res = sh.loadsJson(res) self.assertEqual(res, "None")
def test_POST(self): model = sh.model('SiteConfig') new_id = model.insert({'name': 'n', 'value': 'v' }) res = self.get(api_url, {'model_name':'SiteConfig', 'model_id': new_id}) res = sh.loadsJson(res) self.assertEqual(res.id, new_id) self.assertEqual(res.SiteConfigid, new_id) self.assertEqual(res.name, 'n') res = self.get(api_url, {'model_name':'SiteConfig', 'model_id': new_id+1}) res = sh.loadsJson(res) self.assertEqual(res, 'None')
def assignUserInfo(self, data, access_token): new_data = sh.copy(data) if data else sh.storage() exists = sh.model(self.MODEL_NAME).getByAccessToken(access_token) if not exists: return new_data res = sh.requestHtmlContent(self.USER_INFO_URL, ( 'access_token', access_token, 'oauth_consumer_key', self.getAppID(), 'uid', exists.uid, )) if not res: return new_data res = sh.loadsJson(res) if res.get('error_code', None): return new_data if not new_data.has_key('name'): new_data['name'] = res.screen_name if res.gender == 'm': new_data['sex'] = '他' elif res.gender == 'f': new_data['sex'] = '她' else: new_data['sex'] = '保密' image_file = sh.requestImageFile(res.avatar_large) if image_file: new_data['image_file'] = image_file return new_data
def assignUserInfo(self, data, access_token): new_data = sh.copy(data) if data else sh.storage() exists = sh.model(self.MODEL_NAME).getByAccessToken(access_token) if not exists: return new_data res = sh.requestHtmlContent(self.USER_INFO_URL, ( 'access_token', access_token, 'oauth_consumer_key', self.getAppID(), 'openid', exists.uid, 'format', 'json', )) if not res: return new_data res = sh.loadsJson(res) if res.ret != 0: return new_data if not new_data.has_key('name'): new_data['name'] = res.nickname if res.gender == '男': new_data['sex'] = '他' elif res.gender == '女': new_data['sex'] = '她' image_file = sh.requestImageFile(res.figureurl_2) if image_file: new_data['image_file'] = image_file return new_data
def login(self, email='', password=''): params = {'action': 'login'} params['email'] = email if email else default_user['email'] params['password'] = password if password else default_user['password'] res = sh.loadsJson(self.post('/api/user/profile', params)) assert res.is_login return res.id
def getFollowUids(self, access_token, uid): res = sh.requestHtmlContent(self.FRIEND_UIDS, { 'access_token': access_token, 'uid': uid, 'count': 5000 }) if not res: return [] res = sh.loadsJson(res) if res.get('error_code', None): return [] return res.ids
def test_POST(self): f_model = sh.model('UserForgetPassword') my_id = self.register() # 设置Userid后便可以更新 new_id = f_model.insert(dict(Userid=my_id, code='c')) self.assertIsNotNone(f_model.get(new_id)) data = {'model_name': 'UserForgetPassword', 'model_id': new_id} data['code'] = 'new_code' res = self.get(api_url, data) res = sh.loadsJson(res) self.assertEqual(f_model.get(new_id).code, 'new_code') self.assertEqual(res.affected, 1) # 更新不存在的数据返回True,但是affected等于0 data['model_id'] = new_id + 1 res = self.get(api_url, data) res = sh.loadsJson(res) self.assertTrue(res.success) self.assertEqual(res.affected, 0) # 如果没有登录的话,是不能更新的 self.logout() data['code'] = 'code_again' res = self.get(api_url, data) res = sh.loadsJson(res) self.assertFalse(res.success) # 不能更新别人的数据 my_id = self.register() new_id = f_model.insert(dict(Userid=my_id+1, code='c')) data['model_id'] = new_id res = self.get(api_url, data) res = sh.loadsJson(res) self.assertFalse(res.success) # 如果没有Userid属性的话,是不能更新的 s_model = sh.model('SiteConfig') new_id = s_model.insert(dict(name='n', value='v')) data = {'model_name': 'SiteConfig', 'model_id': new_id, 'name': 'a'} res = self.get(api_url, data) res = sh.loadsJson(res) self.assertFalse(res.success) self.assertEqual(s_model.get(new_id).name, 'n')
def test_POST(self): model = sh.model('UserForgetPassword') my_id = self.register() res = self.get(api_url, {'model_name': 'UserForgetPassword', 'code': 'c'}) res = sh.loadsJson(res) # 成功插入数据 item = model.get(res.new_id) self.assertIsNotNone(item) self.assertEqual(item.code, 'c') # 自动写入Userid self.assertEqual(item.Userid, my_id) # 不能自定义Userid res = self.get(api_url, {'model_name': 'UserForgetPassword', 'code': 'c', 'Userid': 1}) res = sh.loadsJson(res) self.assertFalse(res.success) # 不登录的话是不能插入数据的 old_count = model.getCount() self.logout() res = self.get(api_url, {'model_name': 'UserForgetPassword', 'code': 'c'}) res = sh.loadsJson(res) self.assertEqual(old_count, model.getCount()) self.assertFalse(res.success) self.assertFalse(res.has_key('new_id'))
def assignUserInfo(self, data, access_token): new_data = sh.copy(data) if data else sh.storage() params = { 'method': 'users.getInfo', 'access_token': access_token, 'fields': 'name,sex,mainurl', } content = sh.loadsJson(self._request(params).partition('[')[2].rpartition(']')[0]) if not new_data.has_key('name'): new_data['username'] = content.name if str(content.sex) == '1': new_data['sex'] = '他' elif str(content.sex) == '2': new_data['sex'] = '她' image_file = sh.requestImageFile(content.mainurl) if image_file: new_data['image_file'] = image_file return new_data
def printDict(handler): html = str(handler()) # 此功能仅对后台管理员开放,以免影响正式调用api时的返回结果 if sh.session.is_admin and html and html[0] == '{' and html[-1] == '}': html = sh.printObject(sh.loadsJson(html), not_user_column_names=True) return html
def pickAccessTokenAndExpires(self, content): res = sh.loadsJson(content) try: return res.access_token, res.expires_in except: raise Exception(content)
def update(self, model_name, item_id, data, extra_environ={}): assert isinstance(model_name, (str, unicode)) assert str(item_id).isdigit() data['model_name'] = model_name data['model_id'] = item_id return sh.loadsJson(self.post('/api/update', data, extra_environ)).affected
def getUserid(self): res = sh.loadsJson(self.post('/api/user/profile', {'action':'isLogin'})) return res.id if res.is_login else 0
def getLoginState(self): return sh.loadsJson(self.post('/api/user/profile', {'action':'isLogin'}))
def insert(self, model_name, data, extra_environ={}): assert isinstance(model_name, (str, unicode)) data['model_name'] = model_name return sh.loadsJson(self.post('/api/insert', data, extra_environ)).new_id
def getItem(self, model_name, item_id): assert isinstance(model_name, (str, unicode)) assert str(item_id).isdigit() item = sh.loadsJson(self.get('/api/get', {'model_name': model_name, 'model_id': item_id})) return ModelData(item, sh.model(model_name))
def pickUid(self, content): return sh.loadsJson(content).uid