def index(*, page='1'): ''' summary = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.' blogs = [ Blog(id='1', name='Test blog', summary = summary, created_at = time.time()-120), Blog(id='2', name='Javascript IT', summary = summary, created_at = time.time()-3600), Blog(id='3', name='Learn Swift', summary = summary, created_at = time.time()-7200), Blog(id='4', name='Test blog', summary = summary, created_at = time.time()-120), Blog(id='5', name='Javascript IT', summary = summary, created_at = time.time()-3600), Blog(id='6', name='Learn Swift', summary = summary, created_at = time.time()-7200), ] ''' page_index = get_page_index(page) num = yield from Blog.find_number('count(id)', where='name!=? and enabled=?', args=['__about__', True]) page = Page(num, page_index) if num <= 0: blogs = [] else: blogs = yield from Blog.find_all(where='name!=? and enabled=?', args=['__about__', True], order_by='created_at desc', limit=(page.offset, page.limit)) for blog in blogs: blog.html_summary = markdown2.markdown( blog.summary, extras=[ 'code-friendly', 'fenced-code-blocks', 'highlightjs-lang', 'tables', 'break-on-newline' ]).replace("<table>", "<table class=\"ui celled table\">") #comments_count = yield from Comment.find_number(select_field='count(id)', where='blog_id=?', args=[blog.id]) comments_count = yield from CommentAnonymous.find_number( select_field='count(id)', where='blog_id=?', args=[blog.id]) blog.comments_count = comments_count #get blog tags tags = [] if blog.tags: for tag_id in blog.tags.split(","): tag = yield from Tags.find(tag_id) if tag: tags.append({ "key": tag.id, "value": tag.name, "color": COLORS[tag.id % len(COLORS)] }) blog.tags = tags return { '__template__': 'blogs.html', 'page': page, 'blogs': blogs, 'meta': { "keywords": configs.web_meta.keywords, "description": configs.web_meta.description } }
def api_edit_blog(request, *, id, name, description, summary, content, category_id, tags): check_admin(request) if not name or not name.strip(): raise APIValueError('name', 'name can not be empty') if not summary or not summary.strip(): raise APIValueError('summary', 'summary can not be empty') if not content or not content.strip(): raise APIValueError('content', 'content can not be empty') #blog = Blog(user_id = request.__user__.id, user_name= request.__user__.name, user_image = request.__user__.image, name = name.strip(), summary = summary.strip(), content = content.strip()) blog = yield from Blog.find(id) if not blog: raise APIValueError('id', 'request path error, id : {}'.format(id)) category = yield from Category.find(category_id) if category: #raise APIValueError('category', 'can not find category, category_id:{}'.format(category_id)) category_id = category.id category_name = category.name else: category_name = '' tag_ids = [] if len(tags) > 0: for tag in tags: if tag["key"]: rs = yield from Tags.find(tag["key"]) if rs: tag_ids.append(rs.id) else: rs = yield from Tags.find_all("name=?", [tag["value"]]) if len(rs) > 0: tag_ids.append(rs[0].id) else: #create new tag tag = Tags(name=tag["value"]) rows, lastrowid = yield from tag.save() tag_ids.append(lastrowid) blog.name = name blog.description = description blog.summary = summary blog.content = content blog.category_id = category_id blog.category_name = category_name blog.tags = ",".join([str(id) for id in tag_ids]) blog.updated_at = time.time() yield from blog.update() return blog
def api_get_blog(*, id): blog = yield from Blog.find(id) tags = [] if blog.tags: for tag_id in blog.tags.split(","): tag = yield from Tags.find(tag_id) if tag: tags.append({"key": tag.id, "value": tag.name}) blog.tags = tags blog.alltags = [{ "key": tag.id, "value": tag.name } for tag in (yield from Tags.find_all())] return blog
def get_category_blogs(request, *, id, page='1'): category = yield from Category.find(id) if not category: raise APIValueError('category id', 'can not find category, by id:{}'.format(id)) page_index = get_page_index(page) num = yield from Blog.find_number('count(id)', 'category_id=?', [id]) page = Page(num, page_index) if num == 0: blogs = [] else: blogs = yield from Blog.find_all(where='category_id=?', args=[id], order_by='created_at desc', limit=(page.offset, page.limit)) for blog in blogs: blog.html_summary = markdown2.markdown( blog.summary, extras=[ 'code-friendly', 'fenced-code-blocks', 'highlightjs-lang', 'tables', 'break-on-newline' ]).replace("<table>", "<table class=\"ui celled table\">") comments_count = yield from Comment.find_number( select_field='count(id)', where='blog_id=?', args=[blog.id]) blog.comments_count = comments_count #get blog tags tags = [] if blog.tags: for tag_id in blog.tags.split(","): tag = yield from Tags.find(tag_id) if tag: tags.append({ "key": tag.id, "value": tag.name, "color": COLORS[tag.id % len(COLORS)] }) blog.tags = tags return { '__template__': 'category.html', 'page': page, 'blogs': blogs, 'category': category }
def get_blog(id): blog = yield from Blog.find(id) logger.info('blog id is :{}'.format(id)) if not blog: raise APIValueError('id', 'can not find blog id is :{}'.format(id)) if not blog.enabled: raise APIResourceNotFoundError( 'id', 'Sorry, This articale can\'t find now, Please try it again later...' ) blog.view_count += 1 yield from blog.update() #comments = yield from Comment.find_all('blog_id=?', [id], order_by='created_at desc') comments = yield from CommentAnonymous.find_all('blog_id=?', [id], order_by='created_at asc') tags = [] if blog.tags: for tag_id in blog.tags.split(","): tag = yield from Tags.find(tag_id) if tag: tags.append({ "key": tag.id, "value": tag.name, "color": COLORS[tag.id % len(COLORS)] }) blog.keywords = ",".join([x["value"] for x in tags]) blog.tags = tags for c in comments: c.html_content = markdown2.markdown( c.content, extras=[ 'code-friendly', 'fenced-code-blocks', 'highlightjs-lang', 'tables', 'break-on-newline' ]).replace("<table>", "<table class=\"ui celled table\">") blog.html_content = markdown2.markdown( blog.content, extras=[ 'code-friendly', 'fenced-code-blocks', 'highlightjs-lang', 'tables', 'break-on-newline' ]).replace("<table>", "<table class=\"ui celled table\">") return {'__template__': 'blog.html', 'blog': blog, 'comments': comments}