def get(self): """Public func, no token, with url args: 1. num, 展现的数量,默认是10条,可为all 2. username|email, 用户名或邮箱,数据库主键,唯一。 3. token, if true, display token info. 返回数据样例,{'msg':'success or error(errmsg)', 'code':'http code', 'data':data} """ res = {"code": 200, "msg": None, "data": None} username = request.args.get("username") sql = "SELECT a.username, a.email, a.cname, a.avatar, a.motto, a.url, a.time, a.weibo, a.github, a.gender, a.extra FROM passport.User a INNER JOIN passport.OAuth b ON a.username = b.oauth_username WHERE a.username=%s" if username: data = mysql2().get(sql, username) if not data: sql = "SELECT a.username, a.email, a.cname, a.avatar, a.motto, a.url, a.time, a.weibo, a.github, a.gender, a.extra FROM passport.User a INNER JOIN passport.LAuth b ON a.username = b.lauth_username WHERE a.username=%s" data = mysql2().get(sql, username) logger.info(username) logger.info(sql) logger.debug(data) res.update(data=data) return res
def get(self): """/blog资源,参数是 1.num|limit(int, str), 限制列出数据数量,另外可设置为all,列出所有blog, 全局参数。 2.sort(str), 数据排序, 全局参数。 3.blogId(int), 查询某一个id的文章, 独立参数。 4.get_catalog_list(bool), 列出博客所有目录,独立参数。 5.get_sources_list(bool), 列出博客所有类型,独立参数。 6.get_catalog_data(str), 查询博客某目录下的num个文章。 7.get_sources_data(str), 查询博客某类型下的num个文章。 8.get_index_only(bool),仅仅查询所有博客标题、ID、创建时间。 9.get_user_blog(str),查询某用户的所有博客。 """ num = request.args.get('num', request.args.get('limit', 10)) LIMIT = '' if num in ("all", "All") else "LIMIT " + str(num) sort = request.args.get('sort', 'desc') blogId = request.args.get('blogId') get_catalog_list = True if request.args.get("get_catalog_list") in ("true", "True", True) else False get_sources_list = True if request.args.get("get_sources_list") in ("true", "True", True) else False get_catalog_data = request.args.get("get_catalog_data") get_sources_data = request.args.get("get_sources_data") get_index_only = True if request.args.get("get_index_only") in ("true", "True", True) else False get_user_blog = request.args.get("get_user_blog") res = {"url": request.url, "msg": None, "data": None, "code": 0} logger.debug({"num": num, "blogId": blogId, "get_catalog_list": get_catalog_list, "get_sources_list": get_sources_list, "get_catalog_data": get_catalog_data, "get_sources_data": get_sources_data}) if get_sources_data: if get_sources_data.lower()[:3] == "ori": get_sources_data = '原创' elif get_sources_data.lower()[:3] == "rep": get_sources_data = '转载' elif get_sources_data.lower()[:3] == "tra": get_sources_data = '翻译' #Original reproduced translation if get_index_only: sql = "SELECT id,title,create_time,update_time FROM team.blog ORDER BY id %s %s" %(sort, LIMIT) logger.info("SELECT title only SQL: %s" %sql) try: data = mysql2().query(sql) except Exception,e: logger.error(e, exc_info=True) res.update(data=[], msg="Only title query fail", code=7) else: res.update(data=data) logger.info(res) return res
def post(self): """ 创建博客文章接口 """ #get blog form informations. blog_title = request.form.get('title') blog_content = request.form.get('content') blog_ctime = today() blog_tag = request.form.get("tag") blog_catalog = request.form.get("catalog", "linux") blog_sources = request.form.get("sources", "原创") blog_author = request.form.get("author") logger.info("blog_title:%s, blog_content:%s, blog_ctime:%s, blog_tag:%s, blog_catalog:%s, blog_sources:%s, blog_author:%s" %(blog_title, blog_content, blog_ctime, blog_tag, blog_catalog, blog_sources, blog_author)) if blog_title and blog_content and blog_ctime and blog_author: #sql = 'INSERT INTO blog (title,content,create_time,tag,catalog,sources) VALUES ("%s", "%s", "%s", "%s", "%s", "%s")' sql = 'INSERT INTO blog (title,content,create_time,tag,catalog,sources,author) VALUES (%s, %s, %s, %s, %s, %s, %s)' logger.info(sql %(blog_title, blog_content, blog_ctime, blog_tag, blog_catalog, blog_sources, blog_author)) try: blog_id = mysql2().insert(sql, blog_title, blog_content, blog_ctime, blog_tag, blog_catalog, blog_sources, blog_author) except Exception,e: logger.error(e, exc_info=True) res = {"code": 3, "data": None, "msg": "blog write error."} else: res = {"code": 0, "data": blog_id, "msg": "blog write success."}
class Blog(Resource): def get(self): """/blog资源,参数是 1.num|limit(int, str), 限制列出数据数量,另外可设置为all,列出所有blog, 全局参数。 2.sort(str), 数据排序, 全局参数。 3.blogId(int), 查询某一个id的文章, 独立参数。 4.get_catalog_list(bool), 列出博客所有目录,独立参数。 5.get_sources_list(bool), 列出博客所有类型,独立参数。 6.get_catalog_data(str), 查询博客某目录下的num个文章。 7.get_sources_data(str), 查询博客某类型下的num个文章。 8.get_index_only(bool),仅仅查询所有博客标题、ID、创建时间。 9.get_user_blog(str),查询某用户的所有博客。 """ num = request.args.get('num', request.args.get('limit', 10)) LIMIT = '' if num in ("all", "All") else "LIMIT " + str(num) sort = request.args.get('sort', 'desc') blogId = request.args.get('blogId') get_catalog_list = True if request.args.get("get_catalog_list") in ("true", "True", True) else False get_sources_list = True if request.args.get("get_sources_list") in ("true", "True", True) else False get_catalog_data = request.args.get("get_catalog_data") get_sources_data = request.args.get("get_sources_data") get_index_only = True if request.args.get("get_index_only") in ("true", "True", True) else False get_user_blog = request.args.get("get_user_blog") res = {"url": request.url, "msg": None, "data": None, "code": 0} logger.debug({"num": num, "blogId": blogId, "get_catalog_list": get_catalog_list, "get_sources_list": get_sources_list, "get_catalog_data": get_catalog_data, "get_sources_data": get_sources_data}) if get_sources_data: if get_sources_data.lower()[:3] == "ori": get_sources_data = '原创' elif get_sources_data.lower()[:3] == "rep": get_sources_data = '转载' elif get_sources_data.lower()[:3] == "tra": get_sources_data = '翻译' #Original reproduced translation if get_index_only: sql = "SELECT id,title,create_time,update_time FROM team.blog ORDER BY id %s %s" %(sort, LIMIT) logger.info("SELECT title only SQL: %s" %sql) try: data = mysql2().query(sql) except Exception,e: logger.error(e, exc_info=True) res.update(data=[], msg="Only title query fail", code=7) else: res.update(data=data) logger.info(res) return res if get_catalog_list and get_sources_list: sql="SELECT sources,catalog FROM team.blog" logger.info("get_catalog_sources_list SQL: %s" %sql) data=mysql2().query(sql) sources=set() catalog=set() for i in data: sources.add(i.get("sources")) catalog.add(i.get("catalog")) res.update(data={"sources": sorted(list(sources)), "catalog": sorted(list(catalog))}) logger.info(res) return res if get_catalog_list: #sql = "SELECT GROUP_CONCAT(catalog) FROM blog GROUP BY catalog" sql = 'select catalog from blog' logger.info("SELECT catalog list SQL: %s" %sql) try: data = mysql.get(sql) logger.info(data) data = list(set([ v for _ in data for v in _.values() if v ])) #data = [ v.split(",")[0] for i in data for v in i.values() if v and v.split(",")[0] ] except Exception,e: logger.error(e, exc_info=True) res.update(data=[], msg="Catalog query fail", code=1) else: res.update(data=data) logger.info(res) return res
logger.info(data) #data = [ v.split(",")[0] for i in data for v in i.values() if v and v.split(",")[0] ] data = list(set([ v for _ in data for v in _.values() if v ])) except Exception,e: logger.error(e, exc_info=True) res.update(data=[], msg="Sources query fail", code=2) else: res.update(data=data) logger.info(res) return res if get_catalog_data: sql = "SELECT id,title,content,create_time,update_time,tag,catalog,sources,author FROM team.blog WHERE catalog='%s' ORDER BY id %s %s" %(get_catalog_data, sort, LIMIT) logger.info("SELECT catalog data SQL: %s" %sql) try: data = mysql2().query(sql) logger.info(data) except Exception,e: logger.error(e, exc_info=True) res.update(data=[], msg="Catalog data query fail", code=3) else: res.update(data=data) logger.info(res) return res if get_sources_data: sql = "SELECT id,title,content,create_time,update_time,tag,catalog,sources,author FROM team.blog WHERE sources='%s' ORDER BY id %s %s" %(get_sources_data, sort, LIMIT) logger.info("SELECT sources data SQL: %s" %sql) try: data = mysql2().query(sql) logger.info(data)