def assignUserInfo(self, data, access_token): new_data = site_helper.copy(data) model = getModel(self.MODEL_NAME) open_id = model.getOpenIdByAccessToken(access_token) if open_id is None: return new_data url = self.getUrl('https://graph.qq.com/user/get_user_info', ( 'access_token', access_token, 'oauth_consumer_key', self.APP_ID, 'openid', open_id, 'format', 'json', )) response, content = self.getHtmlContent(url) if response.status != 200: return new_data content = self.loadsJson(content) if content.ret != 0: return new_data new_data['username'] = content.nickname new_data['sex'] = content.gender image_file = self.requestImageFile(content.figureurl_2) if image_file is None: return new_data new_data['imagefile'] = image_file return new_data
def _indentsToLines(indents): for a,b in indents: ret_list.append(sh.copy(queue+[a])) if len(b) != 0: queue.append(a) _indentsToLines(b) queue.pop()
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 all(self, env=None): env = sh.copy(env) if env is not None else {} if not env.has_key('limit') and env.get('paging', self.arguments.paging): env['limit'] = self.__getLimit(self.__getPageNum(env), self.__getVolume(env)) return self.model.all(env)
def replaceInsert(self, data): data = sh.copy(data) self._setUseridToData(data) self._setPrivateidToData(data) self.model.replaceInsert(data) self._incPrivateid(data) return data[self.arguments.primary_key]
def _formatInsertData(self, data): data = sh.copy(data) assert not data.has_key('text_password'), '不能指定text_password' if data.has_key('password'): data['text_password'] = data['password'] data['password'] = self.getMD5Password(data['password']) return data
def _request(self, params): params = site_helper.copy(params) params['v'] = '1.0' params['format'] = 'JSON' params['sig'] = self.sign(params, self.SECRET_KEY) content = urllib.urlopen(self.REQUEST_OPEN_ID, urllib.urlencode(params)).read() return content
def test_insert(self): imgitem_model.use_convert = True imgitem_model.convert_type = 'png' imgitem_model.max_width = 50 imgitem_model.max_height = 60 data = sh.copy(test_data) data[image_model.image_key] = test_image_data old_count = len(image_model.all()) id = imgitem_model.insert(data) # 创建了一张图片 self.assertEqual(len(image_model.all()), old_count+1) # 关联Imageid, 等于最后一张图片的id item = imgitem_model.get(id) self.assertEqual(item.Imageid, image_model.all()[-1].id) # 验证getImageId和getImageUrl self.assertEqual(item.Imageid, imgitem_model.getImageId(id)) self.assertEqual(item.image.url, imgitem_model.getImageUrl(id)) # 转换为png格式 image_path = sh.urlToPath(item.image.url) self.assertTrue(image_path.endswith('/%d.png' % (item.Imageid))) self.assertEqual(imghdr.what(None, open(image_path).read()), 'png') # 压缩为50x50. 因为会保持原有比例,所以压缩后高度依然是50, 不是60 self.assertEqual(Image.open(image_path).size, (50, 50))
def test_update(self): imgitem_model.use_convert = True imgitem_model.convert_type = 'png' imgitem_model.max_width = 50 imgitem_model.max_height = 60 # 插入ImgItem数据 data = sh.copy(test_data) data[image_model.image_key] = test_image_data id = imgitem_model.insert(data) item = imgitem_model.get(id) old_image_id = item.Imageid old_image_path = image_model.getFilePath(old_image_id) old_image_value = open(old_image_path).read() # 更新图片数据 imgitem_model.convert_type = 'jpg' # update时改为jpg imgitem_model.update(id, data) item2 = imgitem_model.get(id) new_image_id = item2.Imageid new_image_path = image_model.getFilePath(new_image_id) # update时新建了Image self.assertEqual(old_image_id + 1, new_image_id) # 删除原Image数据 self.assertIsNone(image_model.get(old_image_id)) # 保留原图片文件 self.assertTrue(os.path.exists(old_image_path)) self.assertEqual(open(old_image_path).read(), old_image_value) # 新图片为jpg格式 self.assertTrue(new_image_path.endswith('/%d.jpg' % (new_image_id))) self.assertEqual(imghdr.what(None, open(new_image_path).read()), 'jpeg') # 新图片被裁剪 self.assertEqual(Image.open(new_image_path).size, (50, 50))
def __writeInfo(self, data): data = sh.copy(data) for key, value in self.arguments.items(): if hasattr(web.ctx, 'env') and sh.getEnv(value, None): data[key] = sh.getEnv(value) elif hasattr(sh, 'session') and sh.session.get(value, None): data[key] = sh.session.get(value) return data
def getCount(self, env={}): env = sh.copy(env) env['select'] = 'count(*)' if env.has_key('limit'): del env['limit'] # mysql语法中count(*)和limit不能一起用 query, argv = self._spliceQuery(env) query = self.replaceAttr(query) return self.db.fetchFirst(query, argv)
def __writeInfo(self, data): data = sh.copy(data) for key, value in self.arguments.items(): if sh.getEnv(value, None): data[key] = sh.getEnv(value) elif sh.session.get(value, None): data[key] = sh.session.get(value) return data
def __writeInfo(self, data): data = sh.copy(data) for key, value in self.arguments.items(): assert not data.has_key(key), u'不应该显示给出%s,小心作弊' % key if sh.getEnv(value, None): data[key] = sh.getEnv(value) elif sh.session.get(value, None): data[key] = sh.session.get(value) return data
def __processData(self, data): data = sh.copy(data) for key, values in self.arguments.items(): assert (hasattr(str, key)) assert (isinstance(values, list) or isinstance(values, tuple)) for v in values: if data.has_key(v): data[v] = getattr(str, key)(sh.unicodeToStr(data[v])) return data
def __processData(self, data): data = sh.copy(data) for key, values in self.arguments.items(): assert(hasattr(str, key)) assert(isinstance(values, list) or isinstance(values, tuple)) for v in values: if data.has_key(v): data[v] = getattr(str, key)(sh.unicodeToStr(data[v])) return data
def convert(image_path, env): env = sh.copy(env) if not os.path.exists(image_path): return image_path assert env.get('resize', '') or env.get('save_file', ''), '必须给出保存路径' save_file = getSavePath(image_path, env) if not env.has_key('save_file') else env['save_file'] if not env.get('use_exists', True) or not os.path.exists(save_file): convert_cmd = makeCommand(image_path, save_file, env) os.system(convert_cmd) return save_file
def _setWhereWithUserid(self, env): env = sh.copy(env) if env else {} if not env.get('_ignore_private_uk', False): uk = self.arguments.user_id_key assert(sh.session.is_login) user_id = sh.session.id if env.has_key('where'): assert re.search(r'\b%s\b' % uk, env['where'][0]) is None, u"想自定义Userid? 请使用env['_ignore_private_uk']=True%s" + env['where'][0] env['where'][0] = '(%s) and %s=%%s' % (env['where'][0], uk) env['where'].append(user_id) else: env['where'] = [uk+'=%s', user_id] return env
def convert(image_path, env): env = sh.copy(env) if not os.path.exists(image_path): return image_path assert env.get('resize', '') or env.get('save_file', ''), '必须给出保存路径' save_file = getSavePath( image_path, env) if not env.has_key('save_file') else env['save_file'] if not env.get('use_exists', True) or not os.path.exists(save_file): convert_cmd = makeCommand(image_path, save_file, env) os.system(convert_cmd) return save_file
def test_ignore_convert_image(self): imgitem_model.use_convert = True imgitem_model.convert_type = 'png' imgitem_model.max_width = 50 imgitem_model.max_height = 60 data = sh.copy(test_data) data[image_model.image_key] = test_image_data data['__ignore_convert_image'] = True id = imgitem_model.insert(data) item = imgitem_model.get(id) image_path = sh.urlToPath(item.image.url) # 保存的文件数据与测试数据一致, 文件没有被convert self.assertEqual(open(image_path).read(), test_image_data.value)
def _setWhereWithUserid(self, env): env = sh.copy(env) if env else {} if not env.get('_ignore_private_uk', False): uk = self.arguments.user_id_key assert (sh.session.is_login) user_id = sh.session.id if env.has_key('where'): assert re.search( r'\b%s\b' % uk, env['where'][0] ) is None, u"想自定义Userid? 请使用env['_ignore_private_uk']=True%s" + env[ 'where'][0] env['where'][0] = '(%s) and %s=%%s' % (env['where'][0], uk) env['where'][1].append(user_id) else: env['where'] = (uk + '=%s', [user_id]) return env
def test_insert(self): decorator = [('Cascade', dict(increase=[ ('Image', 'data_id'), ], ))] # 插入一张图片 image_data = {image_model.image_key: test_image_data, 'data_id': 1} id1 = image_model.insert(image_data) # 对插入的user数据关联Imageid user_data = sh.copy(test_user_data) user_data['Imageid'] = id1 user_model = sh.model('User', decorator) # 插入user数据 user_model.insert(user_data) # 被关联的图片data_id增加了1 item = image_model.get(id1) self.assertEqual(item.data_id, 2)
def test_insert(self): decorator = [('Cascade', dict( increase=[('Image', 'data_id'), ], ))] # 插入一张图片 image_data = {image_model.image_key: test_image_data, 'data_id': 1} id1 = image_model.insert(image_data) # 对插入的user数据关联Imageid user_data = sh.copy(test_user_data) user_data['Imageid'] = id1 user_model = sh.model('User', decorator) # 插入user数据 user_model.insert(user_data) # 被关联的图片data_id增加了1 item = image_model.get(id1) self.assertEqual(item.data_id, 2)
def insert(self, data): assert(self.table_name != '') data = sh.copy(data) img_model = sh.model('Image') new_id = Model.insert(self, data) if data.has_key(img_model.image_key): img_id = img_model.insert(data) Model.update(self, new_id, {'Imageid': img_id}) img_model.update(img_id, {'data_id': new_id, 'data_name': self.table_name}) # convert image ignore_covert = data.get('__ignore_convert_image', False) if self.use_convert and not ignore_covert: self.convertImage(data, img_id) return new_id
def test_delete_decrease(self): decorator = [('Cascade', dict( decrease=[('Image', 'data_id'), ], ))] # 插入一张图片 image_data = {image_model.image_key: test_image_data, 'data_id': 1} id1 = image_model.insert(image_data) # 插入一个user user_data = sh.copy(test_user_data) user_data['Imageid'] = id1 id2 = sh.model('User').insert(user_data) # data_id不变 item1 = image_model.get(id1) self.assertEqual(item1.data_id, 1) # 删除user user_model = sh.model('User', decorator) user_model.delete(id2) # 被关联的图片data_id减少了1 item2 = image_model.get(id1) self.assertEqual(item2.data_id, 0)
def test_delete_decrease(self): decorator = [('Cascade', dict(decrease=[ ('Image', 'data_id'), ], ))] # 插入一张图片 image_data = {image_model.image_key: test_image_data, 'data_id': 1} id1 = image_model.insert(image_data) # 插入一个user user_data = sh.copy(test_user_data) user_data['Imageid'] = id1 id2 = sh.model('User').insert(user_data) # data_id不变 item1 = image_model.get(id1) self.assertEqual(item1.data_id, 1) # 删除user user_model = sh.model('User', decorator) user_model.delete(id2) # 被关联的图片data_id减少了1 item2 = image_model.get(id1) self.assertEqual(item2.data_id, 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
def assignUserInfo(self, data, access_token): new_data = site_helper.copy(data) model = getModel(self.MODEL_NAME) open_id = model.getOpenIdByAccessToken(access_token) if open_id is None: return new_data url = self.getUrl('https://api.weibo.com/2/users/show.json', ( 'access_token', access_token, 'oauth_consumer_key', self.APP_ID, 'uid', open_id, 'format', 'json', )) response, content = self.getHtmlContent(url) if response.status != 200: return new_data content = self.loadsJson(content) if content.get('error_code', None): return new_data new_data['username'] = content.screen_name if content.gender == 'm': new_data['sex'] = '男' elif content.gender == 'f': new_data['sex'] = '女' else: new_data['sex'] = '保密' image_file = self.requestImageFile(content.avatar_large) if image_file is None: return new_data new_data['imagefile'] = image_file return new_data
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 assignUserInfo(self, data, access_token): params = { 'method': 'users.getInfo', 'access_token': access_token, 'fields': 'name,sex,mainurl' } content = self._request(params).partition('[')[2].rpartition(']')[0] content = self.loadsJson(content) new_data = site_helper.copy(data) model = getModel(self.MODEL_NAME) new_data['username'] = content.name if str(content.sex) == '1': new_data['sex'] = '男' elif str(content.sex) == '2': new_data['sex'] = '女' image_file = self.requestImageFile(content.mainurl) if image_file: new_data['imagefile'] = image_file return new_data
def getPaginationHtml(self, env=None): env = sh.copy(env) if env is not None else {} return '<div fx="pagination[style=default;max=%d;displaycount=10;firsttext=第一页;lasttext=末页;]"></div>' % max( 1, math.ceil(self.getCount(env) * 1.0 / self.__getVolume(env))) if env.get( 'paging', self.arguments.paging) else ''
def getPaginationHtml(self, env=None): env = sh.copy(env) if env is not None else {} return '<div fx="paging[style=zarkpy;pageCount=%d;totalCount=%d;displayPages=10;firstText=第一页;lastText=末页;]"></div>' % (env.get('paging_volume', self.arguments.paging_volume), self.getCount(env)) if env.get('paging', self.arguments.paging) else ''
def insert(self, data): assert not data.has_key(self.arguments.attr_name) data = sh.copy(data) data[self.arguments.attr_name] = datetime.datetime.now() return self.model.insert(data)
def _removeNone(self, data): ret_data = sh.copy(data) for k, v in data.items(): if v is None: del ret_data[k] return ret_data
def _removeNone(self, data): ret_data = sh.copy(data) for k,v in data.items(): if v is None: del ret_data[k] return ret_data
def all(self, env=None): assert (isinstance(self.arguments.orderby, str)) env = sh.copy(env) if env is not None else {} if not env.has_key('orderby') and self.arguments.orderby: env['orderby'] = self.arguments.orderby return self.model.all(env)
def update(self, item_id, data): assert not data.has_key(self.arguments.attr_name) data = sh.copy(data) data[self.arguments.attr_name] = datetime.datetime.now() return self.model.update(item_id, data)
def getPaginationHtml(self, env=None): env = sh.copy(env) if env is not None else {} return '<div fx="paging[style=zarkpy;pageCount=%d;totalCount=%d;displayPages=10;firstText=第一页;lastText=末页;]"></div>' % (self.arguments.paging_volume, self.getCount(env)) if env.get('paging', self.arguments.paging) else ''
def _request(self, params): params = sh.copy(params) params['v'] = '1.0' params['format'] = 'JSON' params['sig'] = self.sign(params, self.getAppKey()) return sh.requestHtmlContent(self.UID_URL, params, 'POST')
def update(self, item_id, data): data = sh.copy(data) assert (sh.session.is_login) real_id = self._getRealId(item_id) return self.model.update(real_id, data) if real_id else 0
def all(self, env=None): assert(isinstance(self.arguments.orderby, str)) env = sh.copy(env) if env is not None else {} if not env.has_key('orderby') and self.arguments.orderby: env['orderby'] = self.arguments.orderby return self.model.all(env)