Beispiel #1
0
 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))
Beispiel #2
0
    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")
Beispiel #3
0
    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')
Beispiel #4
0
    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
Beispiel #5
0
    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
Beispiel #6
0
 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
Beispiel #7
0
    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
Beispiel #8
0
 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')
Beispiel #9
0
 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'))
Beispiel #10
0
    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
Beispiel #11
0
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
Beispiel #12
0
 def pickAccessTokenAndExpires(self, content):
     res = sh.loadsJson(content)
     try:
         return res.access_token, res.expires_in
     except:
         raise Exception(content)
Beispiel #13
0
 def pickAccessTokenAndExpires(self, content):
     res = sh.loadsJson(content)
     try:
         return res.access_token, res.expires_in
     except:
         raise Exception(content)
Beispiel #14
0
 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
Beispiel #15
0
 def getUserid(self):
     res = sh.loadsJson(self.post('/api/user/profile', {'action':'isLogin'}))
     return res.id if res.is_login else 0
Beispiel #16
0
 def getLoginState(self):
     return sh.loadsJson(self.post('/api/user/profile', {'action':'isLogin'}))
Beispiel #17
0
 def getLoginState(self):
     return sh.loadsJson(self.post('/api/user/profile', {'action':'isLogin'}))
Beispiel #18
0
 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
Beispiel #19
0
 def getUserid(self):
     res = sh.loadsJson(self.post('/api/user/profile', {'action':'isLogin'}))
     return res.id if res.is_login else 0
Beispiel #20
0
 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))
Beispiel #21
0
 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
Beispiel #22
0
 def pickUid(self, content):
     return sh.loadsJson(content).uid
Beispiel #23
0
 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
Beispiel #24
0
 def pickUid(self, content):
     return sh.loadsJson(content).uid
Beispiel #25
0
 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))