def get_api_blogs(*, page='1'): page_index = get_page_index(page) blogCount = yield from Blog.findNumber('count(id)') page = PageManager(blogCount, page_index) blogs = yield from Blog.findAll(orderBy='created_at desc', limit=(page.offset, page.limit)) return dict(blogs=blogs, page=page, page_index=page_index)
def index(*, page='1'): ''' users= yield from User.findAll() return { '__template__':'test.html', 'users':users } ''' page_index = get_page_index(page) num = yield from Blog.findNumber('count(id)') page = Page(num) if num == 0: blogs = [] else: blogs = yield from Blog.findAll(orderBy="created_at_desc", limit=(page.offset, page.limit)) ''' 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='Something New', summary=summary, created_at=time.time()-3600), Blog(id='3', name='Learn Swift', summary=summary, created_at=time.time()-720) ] ''' return {'__template__': 'blogs.html', 'blogs': blogs}
def api_blogs(*, page='1'): page_index = get_page_index(page) num = yield from Blog.findNumber('count(id)') p = Page(num, page_index) if num == 0: return dict(page=p, blogs=()) blogs = yield from Blog.findAll(orderBy='creaded_at_desc', limit=(p.offset, p.limit)) return dict(page=p, blogs=blogs)
def api_create_blog(request, *, name, summary, content): check_admin(request) if not name or not name.strip(): raise APIValueError('name', 'Invalid name') if not summary or not summary.strip(): raise APIValueError('summary', 'Invalid summary') if not content or not content.strip(): raise APIValueError('content', 'Invalid content') 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()) yield from blog.save() return blog
def tag_archives(*, page='1', tag): page_index = get_page_index(page) t_tag = '#' + tag tags_arr = yield from Tag.findAll('tag=?', [t_tag]) ids = tags_arr[0].blog_ids id_arr = re.findall(r'#([0-9a-zA-Z]*)', ids) length = len(id_arr) base_sql = 'id=? ' base_arr = [] for i in range(length): base_arr.append(base_sql) sql = 'or '.join(base_arr) temp_blogs = yield from Blog.findAll(sql, id_arr) sort_blogs = [] page = PageManager(len(temp_blogs), page_index, page_base=6) blogs = temp_blogs[page.offset:page.limit] admin = None admin = yield from getAdmin() if admin: admin.tagLen = yield from getTagLen() return { '__template__': 'blog_list.html', 'blogCount': len(temp_blogs), 'page_index': page_index, 'page': page, 'blogs': blogs, 'admin': admin, 'tag': tags_arr[0] }
def archives(*, page='1'): page_index = get_page_index(page) blogCount = yield from Blog.findNumber('count(id)') page = PageManager(blogCount, page_index, page_base=6) blogs = yield from Blog.findAll(orderBy='created_at desc', limit=(page.offset, page.limit)) admin = None admin = yield from getAdmin() if admin: admin.tagLen = yield from getTagLen() return { '__template__': 'blog_list.html', 'blogCount': blogCount, 'page_index': page_index, 'page': page, 'blogs': blogs, 'admin': admin }
def api_create_blog(request, *, blogtitle, blogsummary, blogcontent, tags): # check_admin(request) if not blogtitle or not blogtitle.strip(): raise APIValueError('name', 'name cannot be empty.') if not blogsummary or not blogsummary.strip(): raise APIValueError('summary', 'summary cannot be empty.') if not blogcontent or not blogcontent.strip(): raise APIValueError('content', 'content cannot be empty.') blog = Blog(tag=tags, user_id=request.__user__.id, user_name=request.__user__.name, user_image=request.__user__.image, name=blogtitle.strip(), summary=blogsummary.strip(), content=blogcontent) yield from blog.save() logging.info(blogcontent) yield from addTag(blog, tags) return blog
def get_detailBlog(request, *, id): blog = yield from Blog.find(id) blogCount = yield from Blog.findNumber('count(id)') user = request.__user__ if not user: blog.scan_count = blog.scan_count + 1 yield from blog.update() elif user.admin == 0: blog.scan_count = blog.scan_count + 1 yield from blog.update() admin = None admin = yield from getAdmin() if admin: admin.tagLen = yield from getTagLen() return { '__template__': 'blog.html', 'blog': blog, 'blogCount': blogCount, 'admin': admin }
def manage_blogs(*, page='1'): page_index = get_page_index(page) blogCount = yield from Blog.findNumber('count(id)') page = PageManager(blogCount, page_index) blogs = yield from Blog.findAll(orderBy='created_at desc', limit=(page.offset, page.limit)) admins = yield from User.findAll('admin = 1') admin = None if len(admins): admin = admins[0] if admin: admin.tagLen = yield from getTagLen() return { '__template__': 'manage_blogs.html', 'blogCount': blogCount, 'page_index': page_index, 'page': page, 'blogs': blogs, 'admin': admin }
def tags(request): blogCount = yield from Blog.findNumber('count(id)') admin = yield from getAdmin() tags = yield from Tag.findAll() if admin: admin.tagLen = len(tags) return { '__template__': 'tags.html', 'blogCount': blogCount, 'admin': admin, 'tags': tags, }
async def api_create_blog(request, *, name, summary, content): check_admin(request) if not name or not name.strip(): raise APIValueError('name', 'name cannot be empty.') if not summary or not summary.strip(): raise APIValueError('summary', 'summary cannot be empty.') if not content or not content.strip(): raise APIValueError('content', 'content cannot 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()) await blog.save() return blog
def api_update_blog(id, request, *, name, summary, content, tags): check_admin(request) blog = yield from Blog.find(id) if not name or not name.strip(): raise APIValueError('name', 'name cannot be empty.') if not summary or not summary.strip(): raise APIValueError('summary', 'summary cannot be empty.') if not content or not content.strip(): raise APIValueError('content', 'content cannot be empty.') blog.name = name.strip() blog.summary = summary.strip() blog.content = content.strip() blog.tag = tags yield from blog.update() yield from addTag(blog, tags) return blog
async def api_create_blog(request, *, name, summary, content): # 只有管理员可以写博客 check_admin(request) # name,summary,content 不能为空 if not name or not name.strip(): raise APIValueError('name', 'name cannot be empty') if not summary or not summary.strip(): raise APIValueError('summary', 'summary cannot be empty') if not content or not content.strip(): raise APIValueError('content', 'content cannot 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()) # 保存 await blog.save() return blog
def api_delete_blog(request, *, id): check_admin(request) blog = yield from Blog.find(id) yield from blog.remove() return dict(id=id)
def get_blog(request, *, id): blog = yield from Blog.find(id) return dict(blog=blog)
from coroweb import get, post