예제 #1
0
def create_blog(
    title: str, text: str, status: str, source: str, img: str, author_id: int
) -> tuple:
    """创建文章。

    Parameters
    ----------
    title : str
        标题
    text : str
        正文
    status : str
        文章状态,草稿、私密、公开等,由1个字母表示
    source : str
        转载源
    img : str
        封面图片地址
    author_id : int
        作者的账号

    Returns
    -------
    tuple
        文章、正文、文章信息 构成的三元组。
    """
    blog = Blog(
        title=title,
        source=source is not None,  # 布尔字段标明博客是否为转载而不用去查另一个表中的转载源
        abstract=text[:255],  # 摘要取前255个字
        check='u',  # 新提交的博客全是未审核的
        author_id=author_id,
        img=img,
        status=status,
    )
    blog.save()
    blog_text = BlogText(
        id=blog.id,
        text=text,
        source=source
    )
    blog_info = BlogInfo(
        id=blog.id
    )
    blog_text.save()
    blog_info.save()
    return blog, blog_text, blog_info
예제 #2
0
def blog_delete(request, blog):
    blog_obj = Blog.get_by_key_name(blog)
    # item not found
    if blog_obj is None:
        raise request.PAGE_NOT_FOUND
    # delete blog
    blog_obj.delete()
    return request.redirect("go back")
예제 #3
0
def get_recent_blogs(self, context):
    '''Get list of latest blog posts'''
    collection = Blog.all().filter("active", True).order("-created").fetch(10)
    if hasattr(self, "varname"):
        context[self.varname] = collection
        return ""
    return render_to_string("blogs/block/blogs_list",
                            {"blogs": collection},
                            context_instance = context)
예제 #4
0
def blog_edit(request, blog):
    blog_obj = Blog.get_by_key_name(blog)
    # item not found
    if blog_obj is None:
        raise request.PAGE_NOT_FOUND
    if request.POST:
        # filled form
        form = BlogEditForm(data=request.POST, instance=blog_obj)
        if form.is_valid():
            form.save()
            return request.redirect("go back")
    else:
        # empty form with initial data
        form = BlogEditForm(instance=blog_obj)
    # render page
    return request.render('blog/blog_edit', form = form)
예제 #5
0
def blogs_list(request):
    blogs = Blog.all().order('-created')
    # show not published blogs
    if request.GET.get("show") == "unpublished" and request.user:
        # show all blogs
        blogs.filter('active', False)
        # show only user blogs
        if not request.user.is_admin:
            blogs.filter('author', request.user)
    # show only active blogs
    else:
        blogs.filter('active', True)
    # render page
    return request.render(
        'blog/blogs_list',
        blogs = Pagination(request, blogs, 20, "blogs_page"),
    )
예제 #6
0
def entities_list(request, blog=None, tags=None):
    blog_obj = blog and Blog.get_by_key_name(blog)
    entities = Entity.all().order('-changed')
    if blog_obj:
        entities.filter('blog', blog_obj)
    if tags:
        entities.filter('tags IN', tags.strip().split(','))
    # show not published entities
    if request.GET.get("show") == "unpublished" and request.user:
        # show all entities
        entities.filter('active', False)
        # show only user entities
        if not request.user.is_admin:
            entities.filter('author', request.user)
    # show only active blogs
    else:
        entities.filter('active', True)
    # render page
    return request.render(
        'blog/entities_list',
        blog = blog_obj,
        entities = Pagination(request, entities, 20, "entities_page"),
    )