def api_blogList(*, page = 1): r = web.Response() num = yield from Blogs.findNumber('count(id)') p = Page(num, int(page)) if num == 0: data = dict(page = class_to_dict(p), blogs = ()) else: blogs = yield from Blogs.findAll(orderBy = 'create_time desc', limit = (p.offset, p.limit)) for row in blogs: #获取文章分类 category_sql = ['%s %s' % ('select GROUP_CONCAT(B.id) category_id,GROUP_CONCAT(B.title) category_title from blog_category as A ', 'left join category as B on A.category_id = B.id')] category_sql.append('where') category_sql.append('blog_id=?') category = yield from select(category_sql, [row.id]) #获取标签分类 tag_sql = ['%s %s' % ('select GROUP_CONCAT(B.id) tag_id,GROUP_CONCAT(B.title) tag_title from blog_tag as A', 'left join tag as B on A.tag_id = B.id ')] tag_sql.append('where') tag_sql.append('blog_id=?') tag = yield from select(tag_sql, [row.id]) #获取文章评论数 comment_num = yield from Comments.findNumber('count(id)', 'display = 1 and blog_id=?', [row.id]) if category[0].get('category_id') is not None: row.category_id = category[0].get('category_id').split(',') if category[0].get('category_title') is not None: row.category_title = category[0].get('category_title').split(',') if tag[0].get('tag_id') is not None: row.tag_id = tag[0].get('tag_id').split(',') if tag[0].get('tag_title') is not None: row.tag_title = tag[0].get('tag_title').split(',') row.comment_num = comment_num data = dict(page = class_to_dict(p), blogs = blogs) result = APIResult(1, data) return jsonResult(r, result)
def edit_article(*, request, blog_id): blog = yield from Blogs.findOne(blog_id) if blog is not None: # 获取文章分类 category_sql = ['%s %s' % ( 'select GROUP_CONCAT(B.id) category_id,GROUP_CONCAT(B.title) category_title from blog_category as A ', 'left join category as B on A.category_id = B.id')] category_sql.append('where') category_sql.append('blog_id=?') category = yield from select(category_sql, [blog_id]) # 获取文章标签 tag_sql = ['%s %s' % ('select GROUP_CONCAT(B.id) tag_id,GROUP_CONCAT(B.title) tag_title from blog_tag as A', 'left join tag as B on A.tag_id = B.id ')] tag_sql.append('where') tag_sql.append('blog_id=?') tag = yield from select(tag_sql, [blog_id]) if len(category) > 0: if category[0].get('category_id') is not None: blog['category_id'] = category[0].get('category_id').split(',') if category[0].get('category_title') is not None: blog['category_title'] = category[0].get('category_title').split(',') if len(tag) > 0: if tag[0].get('tag_id') is not None: blog['tag_id'] = tag[0].get('tag_id').split(',') if tag[0].get('tag_title') is not None: blog['tag_title'] = tag[0].get('tag_title').split(',') logging.info(blog) #blog['content'] = """%s""" % (blog['content']) return display('edit_article', {'blog': blog}) else: return redirect('/404')
def get_blog(*, request, id): blog = yield from Blogs.findOne(id) if blog is not None: # 访问次数加1 sql = 'update blogs set view_count = view_count + 1 where id = ?' yield from execute(sql, [id]) #获取文章分类 category_sql = ['%s %s' % ( 'select GROUP_CONCAT(B.id) category_id,GROUP_CONCAT(B.title) category_title from blog_category as A ', 'left join category as B on A.category_id = B.id')] category_sql.append('where') category_sql.append('blog_id=?') category = yield from select(category_sql, [id]) #获取文章标签 tag_sql = ['%s %s' % ('select GROUP_CONCAT(B.id) tag_id,GROUP_CONCAT(B.title) tag_title from blog_tag as A', 'left join tag as B on A.tag_id = B.id ')] tag_sql.append('where') tag_sql.append('blog_id=?') tag = yield from select(tag_sql, [id]) #获取作者 user = yield from User.findOne(blog['user_id']) #获取评论 comment_sql = ['select *,(select count(*) from comments where parent = A.id) as children from comments as A '] comment_sql.append('where') comment_sql.append('display = 1 and parent = 0 and blog_id=?') comments = yield from select(comment_sql, [id]) blog['comment_num'] = len(comments) if len(comments) > 0: for k in range(len(comments)): comments[k]['child'] = [] child_sql = ['select * from comments'] child_sql.append('where') child_sql.append('display = 1 and parent = ?') children = yield from select(child_sql, comments[k].get('id')) comments[k]['child'] = children blog['comment_num'] = blog['comment_num'] + len(children) blog['comments'] = comments if user is not None: blog['author'] = user['name'] if len(category) > 0: if category[0].get('category_id') is not None: blog['category_id'] = category[0].get('category_id').split(',') if category[0].get('category_title') is not None: blog['category_title'] = category[0].get('category_title').split(',') if len(tag) > 0: if tag[0].get('tag_id') is not None: blog['tag_id'] = tag[0].get('tag_id').split(',') if tag[0].get('tag_title') is not None: blog['tag_title'] = tag[0].get('tag_title').split(',') logging.info(blog) return display('content' ,{'blog': blog}) else: return redirect('/404')
def index(request): try: # 获取博客分类和文章分类下的文章 category_sql = ['%s %s %s' % ( 'select B.id,B.title,count(*) num from blog_category as A', 'left join category as B on A.category_id = B.id where B.is_delete = 0', 'group by A.category_id' )] blog_category = yield from select(category_sql, []) # 获取热门文章 hot_blog_sql = ['select id,title,view_count from blogs order by view_count desc limit 10'] hot_blog = yield from select(hot_blog_sql, []) return display('index', {'blog_category': blog_category, 'hot_blog': hot_blog}) except Exception as e: logging.info(e) return redirect('/404')
def tag(*, request, tag_id): # 获取当前标签分类的名字 tag = yield from Tag.findOne([tag_id]) if tag is not None: # 获取博客分类和文章分类下的文章 category_sql = ['%s %s %s' % ( 'select B.id,B.title,count(*) num from blog_category as A', 'left join category as B on A.category_id = B.id where B.is_delete = 0', 'group by A.category_id' )] blog_category = yield from select(category_sql, []) # 获取热门文章 hot_blog_sql = ['select id,title,view_count from blogs order by view_count desc limit 10'] hot_blog = yield from select(hot_blog_sql, []) return display('tag', {'blog_category': blog_category, 'hot_blog': hot_blog, 'tag': tag}) else: return redirect('/404')
def test_users(): print(orm.select('select * from users', None)) user = models.User(id='001', name='test', passwd='test', admin=False, email='*****@*****.**') print(user)
def ListWeeddayAnime(cls): cur_time = time.time() today_time = cur_time-cur_time%86400-86400*6 weedday_time = today_time-86400 sql = "select a.* from anime a inner join (select seasonId,max(created_at) 'maxgdtime' from anime where created_at>%s and created_at<%s group by seasonId) b on a.seasonId=b.seasonId and a.created_at=b.maxgdtime order by playCount desc" % (weedday_time,today_time) rs = yield from select(sql,[]) return [cls(**r) for r in rs]
def tags(request): # 获取博客分类和文章分类下的文章 category_sql = ['%s %s %s' % ( 'select B.id,B.title,count(*) num from blog_category as A', 'left join category as B on A.category_id = B.id where B.is_delete = 0', 'group by A.category_id' )] blog_category = yield from select(category_sql, []) # 获取热门文章 hot_blog_sql = ['select id,title,view_count from blogs order by view_count desc limit 10'] hot_blog = yield from select(hot_blog_sql, []) # 获取博客分类和文章分类下的文章 tag_sql = ['%s %s %s' % ( 'select A.id,A.title,count(blog_id) num from tag as A', 'left join blog_tag as B on A.id = B.tag_id', 'group by A.id' )] tags = yield from select(tag_sql, []) for v in tags: v['style'] = rand_color() return display('tags', {'data': tags, 'blog_category': blog_category, 'hot_blog': hot_blog})
def api_tag_blogList(*, page = 1, tag_id): r = web.Response() num = yield from BlogTag.findNumber('count(id)', 'tag_id=?', [tag_id]) p = Page(num, int(page)) if num == 0: data = dict(page = class_to_dict(p), blogs = ()) else: blog_tag_sql = ['select B.* from blog_tag as A left join blogs as B on A.blog_id = B.id '] blog_tag_sql.append('where') blog_tag_sql.append('A.tag_id = ?') blog_tag_sql.append('order by B.create_time desc') blog_tag_sql.append('limit ?,?') blogs = yield from select(blog_tag_sql, [tag_id, p.offset, p.limit]) for row in blogs: #获取文章分类 category_sql = ['%s %s' % ('select GROUP_CONCAT(B.id) category_id,GROUP_CONCAT(B.title) category_title from blog_category as A ', 'left join category as B on A.category_id = B.id')] category_sql.append('where') category_sql.append('blog_id=?') category = yield from select(category_sql, [row['id']]) #获取标签分类 tag_sql = ['%s %s' % ('select GROUP_CONCAT(B.id) tag_id,GROUP_CONCAT(B.title) tag_title from blog_tag as A', 'left join tag as B on A.tag_id = B.id ')] tag_sql.append('where') tag_sql.append('blog_id=?') tag = yield from select(tag_sql, [row['id']]) #获取文章评论数 comment_num = yield from Comments.findNumber('count(id)', 'display = 1 and blog_id=?', [row['id']]) row['category_id'] = category[0].get('category_id').split(',') row['category_title'] = category[0].get('category_title').split(',') row['tag_id'] = tag[0].get('tag_id').split(',') row['tag_title'] = tag[0].get('tag_title').split(',') row['comment_num'] = comment_num for k in row: if isinstance(row[k], datetime.datetime): row[k] = row[k].strftime('%Y-%m-%d %H:%M:%S') data = dict(page = class_to_dict(p), blogs = blogs) result = APIResult(1, data) return jsonResult(r, result)
def ListAllAnime(cls,orderBy=None): sql = "select a.* from anime a inner join (select seasonId,max(created_at) 'maxgdtime' from anime group by seasonId) b on a.seasonId=b.seasonId and a.created_at=b.maxgdtime " if orderBy != None: sql= sql+orderBy rs = yield from select(sql,[]) return [cls(**r) for r in rs]
def ListAnimeItems(cls,seasonId): sql = "select a.* from animeitem a inner join (select episodeId,max(created_at) 'maxgdtime' from animeitem where seasonid=%s group by episodeId ) b on a.episodeId=b.episodeId and a.created_at=b.maxgdtime order by pubTime" % seasonId rs = yield from select(sql,[]) return [cls(**r) for r in rs]