Пример #1
0
 def GET(self, id):
     try:
         id = int(id)
     except:
         raise web.notfound()
     page = web.ctx.orm.query(Post).filter(Post.id == id).first()
     return admin_render.post(post=page, content_type="page", action="edit")
Пример #2
0
    def POST(self):
        i = web.input()
        title = i.title
        content = i.content
        slug = None
        if i.slug.strip():
            slug = i.slug.strip().replace(" ", "-")
        excerpt = i.excerpt
        try:
            menu_order = int(i.order)
        except:
            menu_order = 0
        comment_status = 0
        if i.get("comment_status", "") == "open":
            comment_status = 1  # allow comment
        status = "draft"
        if i.get("publish"):
            status = "publish"  # post be  publish
        post = Post(
            title=title,
            content=content,
            slug=slug,
            content_type="page",
            status=status,
            excerpt=excerpt,
            menu_order=menu_order,
            comment_status=comment_status,
        )

        if not (title and content):
            msg = "Title and page content can not be empty."
            post.status = None  # post did not publish, so change this
            return admin_render.post(post=post, action="add", content_type="page", msg=msg)
        # validates post slug
        if slug:
            if not post_slug_validates(slug):
                msg = "Page not saved! Slug is the same as another post's, please change it."
                post.status = None
                return admin_render.post(post=post, action="add", content_type="page", msg=msg)
        post.author = self.get_admin()
        web.ctx.orm.commit()
        if status == "publish":
            web.ctx.msg = "The page '%s' has been published." % post.title
        else:
            web.ctx.msg = "The page '%s' has been saved." % post.title
        raise web.seeother("/pages")  # redirect to post list page
Пример #3
0
    def POST(self):
        i = web.input(category=[])
        title = i.title
        slug = None
        if i.slug.strip():
            slug = i.slug.strip().replace(" ", "-")

        excerpt = i.excerpt
        tags = i.tags
        category_ids = i.category
        comment_status = 0
        if i.get("comment_status", "") == "open":
            comment_status = 1

        if i.get("publish"):
            status = "publish"

        # create a dict for a category, add a attribute name 'selected'
        def newcategory(cate):
            return {"id": cate.id, "name": cate.name, "slug": cate.slug, selected: cate.id in category_ids}

        if not (title and content):
            msg = "Post not saved! Title and content can not be empty,plz fill them"
            return admin_render.post(
                post=post,
                cates=map(newcategory, self.get_category),
                tags=tags,
                action="add",
                content_type="post",
                msg=msg,
            )

        post.author = self.get_admin

        web.ctx.orm.commit()

        categories = web.ctx.orm.query(Term).filter(Term.type == "category").filter(Term.id.in_(category_ids)).all()

        for cate in categories:
            cate.count += 1
            post.terms.append(cate)

        for item in tags.split(","):
            tag = web.ctx.orm.query(Term).filter(Term.type == "tag").filter(Term.name == item.strip()).first()
            # if tag not exists yet
            if not tag:
                tag = Term(name=item.strip(), count=0, type="tag")
                web.ctx.orm.add(tag)
            tag.count += 1
            post.terms.add(tag)
        web.ctx.org.commit()

        if status == "publish":
            web.ctx.msg = "the post '%s' has been published" % post.title
        else:
            web.ctx.msg = "the post '%s' has been saved!" % post.title

        raise web.seeother("/posts")
Пример #4
0
 def POST(self, id):
     i = web.input()
     title = i.title
     content = i.content
     slug = None
     if i.slug.strip():
         slug = i.slug.strip().replace(" ", "-")
     excerpt = i.excerpt
     try:
         menu_order = int(i.order)
     except:
         menu_order = 0
     comment_status = 0
     if i.get("comment_status", "") == "open":
         comment_status = 1  # allow comment
     status = i.status
     if i.get("unpublish"):
         status = "draft"  # if cancel publish button been press
     if i.get("publish"):
         status = "publish"
     post = web.ctx.orm.query(Post).filter(Post.id == int(id)).first()  # the old page object
     if not (title and content):
         msg = "Title and content can not be empty!"
         return admin_render.post(post=post, action="edit", content_type="page", msg=msg)
     # validates post slug
     if slug:
         if not post_slug_validates(slug, int(id)):
             msg = "Page modification not saved! Slug is the same as another post's, please change it."
             return admin_render.post(post=post, action="edit", content_type="page", msg=msg)
     post.title = title
     post.content = content
     post.slug = slug
     post.excerpt = excerpt
     post.menu_order = menu_order
     post.comment_status = comment_status
     post.status = status
     post.modified = datetime.now()
     web.ctx.orm.commit()
     raise web.seeother("/pages")
Пример #5
0
    def GET(self, id):
        post = web.ctx.orm.query(Post).filter(Post.id == int(id)).first()
        category_ids = []
        for term in post.terms:
            if term.type == "category":
                category_ids.append(term.id)
        cates = self.get_categories()
        tags = [term.name for term in post.terms if term.type == "tag"]
        tags = ",".join(tags)

        # create a dict for a category, add a attribute name 'selected'
        def newcategory(cate):
            return {"id": cate.id, "name": cate.name, "slug": cate.slug, "selected": cate.id in category_ids}

        return admin_render.post(
            post=post, cates=map(newcategory, self.get_categories()), tags=tags, content_type="post", action="edit"
        )
Пример #6
0
 def GET(self):
     return admin_render.post(action="add", content_type="page")
Пример #7
0
    def POST(self, id):
        i = web.input(category=[])
        title = i.title
        content = i.content
        slug = None
        if i.slug.strip():
            slug = i.slug.strip().replace(" ", "-")
        excerpt = i.excerpt
        tags = i.tags
        category_ids = i.category  # list of category id
        comment_status = 0
        status = i.status
        if i.get("unpublish"):
            status = "draft"  # if cancel publish button been press
        if i.get("publish"):
            status = "publish"
        if i.get("comment_status", "") == "open":
            comment_status = 1  # allow comment
        post = web.ctx.orm.query(Post).filter(Post.id == int(id)).first()
        if not (title and content):
            msg = "Post modification not saved! Title and content can not be empty."
            return admin_render.post(
                post=post,
                cates=map(newcategory, self.get_categories()),
                tags=tags,
                action="add",
                content_type="post",
                msg=msg,
            )
        # validates post slug
        if slug:
            if not post_slug_validates(slug, int(id)):
                msg = "Post modification not saved! Slug is the same as another post's, please change it."
                return admin_render.post(
                    post=post,
                    cates=map(newcategory, self.get_categories()),
                    tags=tags,
                    action="edit",
                    content_type="post",
                    msg=msg,
                )

        # everything is ok, update the post now
        post.title = title
        post.content = content
        post.slug = slug
        post.excerpt = excerpt
        post.status = status
        post.comment_status = comment_status
        post.modified = datetime.now()

        # update the original terms' count of posts
        for term in post.terms:
            term.count -= 1
        post.terms = []  # reset it
        categories = web.ctx.orm.query(Term).filter(Term.type == "category").filter(Term.id.in_(category_ids)).all()
        for cate in categories:
            cate.count += 1
            post.terms.append(cate)
        for item in tags.split(","):
            tag = web.ctx.orm.query(Term).filter(Term.type == "tag").filter(Term.name == item.strip()).first()
            # tag not exist yet
            if not tag:
                tag = Term(name=item.strip(), count=0)
                web.ctx.orm.add(tag)
            tag.count += 1
            post.terms.append(tag)
        web.ctx.orm.commit()
        web.ctx.msg = "The post '%s' has been modified!" % post.title
        raise web.seeother("/posts")
Пример #8
0
    def POST(self):
        i = web.input(category=[])
        title = i.title
        content = i.content
        slug = None
        if i.slug.strip():
            slug = i.slug.strip().replace(" ", "-")
        excerpt = i.excerpt
        tags = i.tags
        category_ids = i.category  # list of category id
        comment_status = 0
        if i.get("comment_status", "") == "open":
            comment_status = 1  # allow comment
        status = "draft"
        if i.get("publish"):
            status = "publish"  # post should be publish

        # create a Post object
        post = Post(
            title=title, content=content, slug=slug, excerpt=excerpt, status=status, comment_status=comment_status
        )

        # create a dict for a category, add a attribute name 'selected'
        def newcategory(cate):
            return {"id": cate.id, "name": cate.name, "slug": cate.slug, "selected": cate.id in category_ids}

        # title and content can't be empty
        if not (title and content):
            msg = "Post not saved! Title and content can not be empty, please fill them."
            return admin_render.post(
                post=post,
                cates=map(newcategory, self.get_categories()),
                tags=tags,
                action="add",
                content_type="post",
                msg=msg,
            )

        # slug can not be the same with other post slug
        if slug:
            if not post_slug_validates(slug):
                msg = "Post not saved! Slug should be different form other post slug."
                return admin_render.post(
                    post=post,
                    cates=map(newcategory, self.get_categories()),
                    tags=tags,
                    action="add",
                    content_type="post",
                    msg=msg,
                )

        post.author = self.get_admin()
        web.ctx.orm.commit()
        categories = web.ctx.orm.query(Term).filter(Term.type == "category").filter(Term.id.in_(category_ids)).all()
        for cate in categories:
            cate.count += 1
            post.terms.append(cate)
        for item in tags.split(","):
            tag = web.ctx.orm.query(Term).filter(Term.type == "tag").filter(Term.name == item.strip()).first()
            # tag not exist yet
            if not tag:
                tag = Term(name=item.strip(), count=0)
                web.ctx.orm.add(tag)
            tag.count += 1
            post.terms.append(tag)
        web.ctx.orm.commit()
        if status == "publish":
            web.ctx.msg = "The post '%s' has been published." % post.title
        else:
            web.ctx.msg = "The post '%s' has been saved." % post.title
        raise web.seeother("/posts")  # redirect to post list page
Пример #9
0
 def GET(self):
     cates = web.ctx.orm.query(Term).filter(Term.type == "category").all()
     return admin_render.post(cates=cates, action="add", content_type="post")