示例#1
0
文件: admin.py 项目: yobin/saepy-log
 def post(self):
     data = self.get_argument("data", "no post data")
     if data:
         data = markdown.markdown(parse_text(data))
     self.echo("admin_preview.html", {
         "title": "Post preview",
         "data": data
     })
示例#2
0
 def post(self):
     data = self.get_argument("data", "no post data")
     if data:
         data = markdown.markdown(parse_text(data))
     self.echo("blog_preview.html", {"title":"Post preview", "data": data})
示例#3
0
    def post(self):
        fid = self.get_argument("fid", None)
        if fid:
            self.redirect("/admin/post?id=" + fid)
            return

        cid = int(self.get_argument("cid", 1))
        title = self.get_argument("title", None)
        if title is None:
            self.redirect("/admin/")
            return
        content = self.get_argument("markdown")
        tags = get_tag(self.get_argument("tags",""))
        id = int(self.get_argument("id"))

        self.set_header('Content-Type','application/json')
        rspd = {'status': 201, 'msg':'ok'}

        if id>0:
            #edit
            entry = self.db.get("SELECT * FROM oppy_post WHERE id = %s", int(id))
            if not entry: raise tornado.web.HTTPError(404)
            old_cid = int(entry["cid"])
            old_tags = entry["tags"]
            if cid==old_cid and title==entry["title"] and content==entry["markdown"] and tags==old_tags:
                self.write(json.dumps({'status': 200, 'msg':'文章没做任何改动'}))
                return
            if content!=entry["markdown"]:
                html = markdown.markdown(parse_text(content))
                self.db.execute("UPDATE oppy_post SET cid = %s, title = %s, markdown = %s, html = %s, tags = %s WHERE id = %s", cid, title, content, html, tags, int(id))
            else:
                self.db.execute("UPDATE oppy_post SET cid = %s, title = %s, tags = %s WHERE id = %s", cid, title, tags, int(id))
            #cid changed
            if old_cid != cid:
                self.db.execute("UPDATE oppy_category SET num = num + 1 WHERE id = %s LIMIT 1", cid)
                self.db.execute("UPDATE oppy_category SET num = num - 1 WHERE id = %s LIMIT 1", old_cid)
            #tags changed
            if old_tags != tags:
                old_tags_set = set(old_tags.split(',')) #.encode("utf-8")
                new_tags_set = set(tags.split(','))

                removed_tags = old_tags_set - new_tags_set
                added_tags = new_tags_set - old_tags_set

                if removed_tags:
                    for tag in removed_tags:
                        tag_obj = self.db.get("SELECT * FROM oppy_tag WHERE name = %s LIMIT 1", tag)
                        if tag_obj:
                            id_list = tag_obj["content"].split(",")
                            if str(id) in id_list:
                                id_list.remove(str(id))
                                num = len(id_list)
                                self.db.execute("UPDATE oppy_tag SET num = %s, content = %s WHERE id = %s LIMIT 1", num, ",".join(id_list), tag_obj["id"])

                if added_tags:
                    for tag in added_tags:
                        tag_obj = self.db.get("SELECT * FROM oppy_tag WHERE name = %s LIMIT 1", tag)
                        if tag_obj:
                            if tag_obj["content"]:
                                id_list = tag_obj["content"].split(",")
                                if str(id) not in id_list:
                                    id_list.insert(0, str(id))
                                    num = len(id_list)
                                    self.db.execute("UPDATE oppy_tag SET num = %s, content = %s WHERE id = %s LIMIT 1", num, ",".join(id_list), tag_obj["id"])
                            else:
                                num = 1
                                content = str(id)
                                self.db.execute("UPDATE oppy_tag SET num = %s, content = %s WHERE id = %s LIMIT 1", num, ",".join(id_list), tag_obj["id"])
                        else:
                            self.db.execute("INSERT INTO oppy_tag (name,num,content) VALUES (%s,%s,%s)",tag, 1, str(id))

            rspd['status'] = 200
            rspd['msg'] = u'完成: 你已经成功编辑了一篇文章 <a href="/t/%d" target="_blank">查看编辑后的文章</a>' % id
        else:
            #add
            html = markdown.markdown(parse_text(content))
            query = "INSERT INTO oppy_post (cid,title,markdown,html,tags,add_time) VALUES (%s,%s,%s,%s,%s,%s)"
            new_post_id = self.db.execute(query, cid, title, content, html, tags, int(time()))
            #category count
            self.db.execute("UPDATE oppy_category SET num = num + 1 WHERE id = %s LIMIT 1", cid)
            #add post id to tag
            for tag in tags.split(","):
                tag_obj = self.db.get("SELECT * FROM oppy_tag WHERE name = %s LIMIT 1", tag)
                if tag_obj:
                    if tag_obj["content"]:
                        id_list = tag_obj["content"].split(",")
                        if str(new_post_id) not in id_list:
                            id_list.insert(0, str(new_post_id))
                            num = len(id_list)
                            self.db.execute("UPDATE oppy_tag SET num = %s, content = %s WHERE id = %s LIMIT 1", num, ",".join(id_list), tag_obj["id"])
                    else:
                        num = 1
                        content = str(new_post_id)
                        self.db.execute("UPDATE oppy_tag SET num = %s, content = %s WHERE id = %s LIMIT 1", num, ",".join(id_list), tag_obj["id"])
                else:
                    self.db.execute("INSERT INTO oppy_tag (name,num,content) VALUES (%s,%s,%s)",tag, 1, str(new_post_id))

            rspd['status'] = 200
            rspd['msg'] = u'完成: 你已经成功添加了一篇文章 <a href="/t/%d" target="_blank">查看</a>' % new_post_id

        self.write(json.dumps(rspd))
示例#4
0
文件: admin.py 项目: kkkier/ijd8
    def post(self):
        fid = self.get_argument("fid", None)
        if fid:
            self.redirect("/admin/post?id=" + fid)
            return

        cid = int(self.get_argument("cid", 1))
        title = self.get_argument("title", None)
        if title is None:
            self.redirect("/admin/")
            return
        content = self.get_argument("markdown")
        tags = get_tag(self.get_argument("tags",""))
        id = int(self.get_argument("id"))

        self.set_header('Content-Type','application/json')
        rspd = {'status': 201, 'msg':'ok'}

        if id>0:
            #edit
            entry = self.db.get("SELECT * FROM oppy_post WHERE id = %s", int(id))
            if not entry: raise tornado.web.HTTPError(404)
            old_cid = int(entry["cid"])
            old_tags = entry["tags"]
            if cid==old_cid and title==entry["title"] and content==entry["markdown"] and tags==old_tags:
                self.write(json.dumps({'status': 200, 'msg':'文章没做任何改动'}))
                return
            if content!=entry["markdown"]:
                html = markdown.markdown(parse_text(content))
                self.db.execute("UPDATE oppy_post SET cid = %s, title = %s, markdown = %s, html = %s, tags = %s WHERE id = %s", cid, title, content, html, tags, int(id))
            else:
                self.db.execute("UPDATE oppy_post SET cid = %s, title = %s, tags = %s WHERE id = %s", cid, title, tags, int(id))
            #cid changed
            if old_cid != cid:
                self.db.execute("UPDATE oppy_category SET num = num + 1 WHERE id = %s LIMIT 1", cid)
                self.db.execute("UPDATE oppy_category SET num = num - 1 WHERE id = %s LIMIT 1", old_cid)
            #tags changed
            if old_tags != tags:
                old_tags_set = set(old_tags.split(',')) #.encode("utf-8")
                new_tags_set = set(tags.split(','))

                removed_tags = old_tags_set - new_tags_set
                added_tags = new_tags_set - old_tags_set

                if removed_tags:
                    for tag in removed_tags:
                        tag_obj = self.db.get("SELECT * FROM oppy_tag WHERE name = %s LIMIT 1", tag)
                        if tag_obj:
                            id_list = tag_obj["content"].split(",")
                            if str(id) in id_list:
                                id_list.remove(str(id))
                                num = len(id_list)
                                self.db.execute("UPDATE oppy_tag SET num = %s, content = %s WHERE id = %s LIMIT 1", num, ",".join(id_list), tag_obj["id"])

                if added_tags:
                    for tag in added_tags:
                        tag_obj = self.db.get("SELECT * FROM oppy_tag WHERE name = %s LIMIT 1", tag)
                        if tag_obj:
                            if tag_obj["content"]:
                                id_list = tag_obj["content"].split(",")
                                if str(id) not in id_list:
                                    id_list.insert(0, str(id))
                                    num = len(id_list)
                                    self.db.execute("UPDATE oppy_tag SET num = %s, content = %s WHERE id = %s LIMIT 1", num, ",".join(id_list), tag_obj["id"])
                            else:
                                num = 1
                                content = str(id)
                                self.db.execute("UPDATE oppy_tag SET num = %s, content = %s WHERE id = %s LIMIT 1", num, content, tag_obj["id"])
                        else:
                            self.db.execute("INSERT INTO oppy_tag (name,num,content) VALUES (%s,%s,%s)",tag, 1, str(id))

            rspd['status'] = 200
            rspd['msg'] = u'完成: 你已经成功编辑了一篇文章 <a href="/t/%d" target="_blank">查看编辑后的文章</a>' % id
        else:
            #add
            html = markdown.markdown(parse_text(content))
            query = "INSERT INTO oppy_post (cid,title,markdown,html,tags,add_time) VALUES (%s,%s,%s,%s,%s,%s)"
            new_post_id = self.db.execute(query, cid, title, content, html, tags, int(time()))
            #category count
            self.db.execute("UPDATE oppy_category SET num = num + 1 WHERE id = %s LIMIT 1", cid)
            #add post id to tag
            for tag in tags.split(","):
                tag_obj = self.db.get("SELECT * FROM oppy_tag WHERE name = %s LIMIT 1", tag)
                if tag_obj:
                    if tag_obj["content"]:
                        id_list = tag_obj["content"].split(",")
                        if str(new_post_id) not in id_list:
                            id_list.insert(0, str(new_post_id))
                            num = len(id_list)
                            self.db.execute("UPDATE oppy_tag SET num = %s, content = %s WHERE id = %s LIMIT 1", num, ",".join(id_list), tag_obj["id"])
                    else:
                        num = 1
                        content = str(new_post_id)
                        self.db.execute("UPDATE oppy_tag SET num = %s, content = %s WHERE id = %s LIMIT 1", num, content, tag_obj["id"])
                else:
                    self.db.execute("INSERT INTO oppy_tag (name,num,content) VALUES (%s,%s,%s)",tag, 1, str(new_post_id))

            rspd['status'] = 200
            rspd['msg'] = u'完成: 你已经成功添加了一篇文章 <a href="/t/%d" target="_blank">查看</a>' % new_post_id

        self.write(json.dumps(rspd))