Exemplo n.º 1
0
    def post(self, id=''):
        act = self.get_argument("act", '')
        if act == 'findid':
            eid = self.get_argument("id", '')
            self.redirect('%s/admin/comment/%s' % (BASE_URL, eid))
            return

        tf = {'true': 1, 'false': 0}
        post_dic = {
            'author':
            self.get_argument("author"),
            'email':
            self.get_argument("email"),
            'content':
            safe_encode(self.get_argument("content").replace('\r', '\n')),
            'url':
            self.get_argument("url", ''),
            'visible':
            self.get_argument("visible", 'false'),
            'id':
            id
        }
        post_dic['visible'] = tf[post_dic['visible'].lower()]

        Comment.update_comment_edit(post_dic)
        clear_cache_by_pathlist(['post:%s' % id])
        self.redirect('%s/admin/comment/%s' % (BASE_URL, id))
        return
Exemplo n.º 2
0
    def post(self):
        self.set_header('Content-Type', 'application/json')
        rspd = {'status': 201, 'msg': 'ok'}

        try:
            tf = {'true': 1, 'false': 0}
            timestamp = int(time())
            post_dic = {
                'category': self.get_argument("cat"),
                'title': self.get_argument("tit"),
                'content': self.get_argument("con"),
                'tags': self.get_argument("tag", '').replace(u',', ','),
                'closecomment': self.get_argument("clo", '0'),
                'password': self.get_argument("password", ''),
                'add_time': timestamp,
                'edit_time': timestamp,
            }
            if post_dic['tags']:
                tagslist = set(
                    [x.strip() for x in post_dic['tags'].split(',')])
                try:
                    tagslist.remove('')
                except:
                    pass
                if tagslist:
                    post_dic['tags'] = ','.join(tagslist)
            post_dic['closecomment'] = tf[post_dic['closecomment'].lower()]
        except:
            rspd['status'] = 500
            rspd['msg'] = '错误: 注意必填的三项'
            self.write(json.dumps(rspd))
            return

        postid = Article.add_new_article(post_dic)
        if postid:
            Category.add_postid_to_cat(post_dic['category'], str(postid))
            if post_dic['tags']:
                Tag.add_postid_to_tags(post_dic['tags'].split(','),
                                       str(postid))

            rspd['status'] = 200
            rspd[
                'msg'] = '完成: 你已经成功添加了一篇文章 <a href="/t/%s" target="_blank">查看</a>' % str(
                    postid)
            clear_cache_by_pathlist(
                ['/', 'cat:%s' % quoted_string(post_dic['category'])])

            if not debug:
                add_task('default', '/task/pingrpctask')

            self.write(json.dumps(rspd))
            return
        else:
            rspd['status'] = 500
            rspd['msg'] = '错误: 未知错误,请尝试重新提交'
            self.write(json.dumps(rspd))
            return
Exemplo n.º 3
0
    def post(self):
        self.set_header("Content-Type", "application/json")
        rspd = {"status": 201, "msg": "ok"}

        try:
            tf = {"true": 1, "false": 0}
            timestamp = int(time())
            post_dic = {
                "category": self.get_argument("cat"),
                "title": self.get_argument("tit"),
                "content": self.get_argument("con"),
                "tags": self.get_argument("tag", "").replace(u",", ","),
                "closecomment": self.get_argument("clo", "0"),
                "password": self.get_argument("password", ""),
                "add_time": timestamp,
                "edit_time": timestamp,
            }
            if post_dic["tags"]:
                tagslist = set([x.strip() for x in post_dic["tags"].split(",")])
                try:
                    tagslist.remove("")
                except:
                    pass
                if tagslist:
                    post_dic["tags"] = ",".join(tagslist)
            post_dic["closecomment"] = tf[post_dic["closecomment"].lower()]
        except:
            rspd["status"] = 500
            rspd["msg"] = "错误: 注意必填的三项"
            self.write(json.dumps(rspd))
            return

        postid = Article.add_new_article(post_dic)
        if postid:
            Category.add_postid_to_cat(post_dic["category"], str(postid))
            if post_dic["tags"]:
                Tag.add_postid_to_tags(post_dic["tags"].split(","), str(postid))

            rspd["status"] = 200
            rspd["msg"] = '完成: 你已经成功添加了一篇文章 <a href="/t/%s" target="_blank">查看</a>' % str(postid)
            clear_cache_by_pathlist(["/", "cat:%s" % quoted_string(post_dic["category"])])

            if not debug:
                add_task("default", "/task/pingrpctask")

            self.write(json.dumps(rspd))
            return
        else:
            rspd["status"] = 500
            rspd["msg"] = "错误: 未知错误,请尝试重新提交"
            self.write(json.dumps(rspd))
            return
Exemplo n.º 4
0
    def post(self):
        self.set_header('Content-Type','application/json')
        rspd = {'status': 201, 'msg':'ok'}

        try:
            tf = {'true':1,'false':0}
            timestamp = int(time())
            post_dic = {
                'category': self.get_argument("cat"),
                'title': self.get_argument("tit"),
                'content': self.get_argument("con"),
                'tags': self.get_argument("tag",'').replace(u',',','),
                'closecomment': self.get_argument("clo",'0'),
                'password': self.get_argument("password",''),
                'add_time': timestamp,
                'edit_time': timestamp,
            }
            if post_dic['tags']:
                tagslist = set([x.strip() for x in post_dic['tags'].split(',')])
                try:
                    tagslist.remove('')
                except:
                    pass
                if tagslist:
                    post_dic['tags'] = ','.join(tagslist)
            post_dic['closecomment'] = tf[post_dic['closecomment'].lower()]
        except:
            rspd['status'] = 500
            rspd['msg'] = '错误: 注意必填的三项'
            self.write(json.dumps(rspd))
            return

        postid = Article.add_new_article(post_dic)
        if postid:
            Category.add_postid_to_cat(post_dic['category'], str(postid))
            if post_dic['tags']:
                Tag.add_postid_to_tags(post_dic['tags'].split(','), str(postid))

            rspd['status'] = 200
            rspd['msg'] = '完成: 你已经成功添加了一篇文章 <a href="/t/%s" target="_blank">查看</a>' % str(postid)
            clear_cache_by_pathlist(['/', 'cat:%s' % quoted_string(post_dic['category'])])

            if not debug:
                add_task('default', '/task/pingrpctask')

            self.write(json.dumps(rspd))
            return
        else:
            rspd['status'] = 500
            rspd['msg'] = '错误: 未知错误,请尝试重新提交'
            self.write(json.dumps(rspd))
            return
Exemplo n.º 5
0
 def get(self, id = ''):
     obj = None
     if id:
         obj = Comment.get_comment_by_id(id)
         if obj:
             act = self.get_argument("act",'')
             if act == 'del':
                 Comment.del_comment_by_id(id)
                 clear_cache_by_pathlist(['post:%d'%obj.postid])
                 self.redirect('%s/admin/comment/'% (BASE_URL))
                 return
     self.echo('admin_comment.html', {
         'title': "管理评论",
         'cats': Category.get_all_cat_name(),
         'tags': Tag.get_all_tag_name(),
         'obj': obj,
         'comments': Comment.get_recent_comments(),
     },layout='_layout_admin.html')
Exemplo n.º 6
0
 def get(self, id = ''):
     obj = None
     if id:
         obj = Comment.get_comment_by_id(id)
         if obj:
             act = self.get_argument("act",'')
             if act == 'del':
                 Comment.del_comment_by_id(id)
                 clear_cache_by_pathlist(['post:%d'%obj.postid])
                 self.redirect('%s/admin/comment/'% (BASE_URL))
                 return
     self.echo('admin_comment.html', {
         'title': "管理评论",
         'cats': Category.get_all_cat_name(),
         'tags': Tag.get_all_tag_name(),
         'obj': obj,
         'comments': Comment.get_recent_comments(),
     },layout='_layout_admin.html')        
Exemplo n.º 7
0
    def post(self):
        act = self.get_argument("act",'')
        id = self.get_argument("id",'')
        name = self.get_argument("name",'')
        sort = self.get_argument("sort",'0')
        url = self.get_argument("url",'')

        if name and url:
            params = {'id': id, 'name': name, 'url': url, 'displayorder': sort}
            if act == 'add':
                Link.add_new_link(params)

            if act == 'edit':
                Link.update_link_edit(params)

            clear_cache_by_pathlist(['/'])

        self.redirect('%s/admin/links'% (BASE_URL))
        return
Exemplo n.º 8
0
 def post(self):
     act = self.get_argument("act",'')
     id = self.get_argument("id",'')
     name = self.get_argument("name",'')
     sort = self.get_argument("sort",'0')
     url = self.get_argument("url",'')
     
     if name and url:
         params = {'id': id, 'name': name, 'url': url, 'displayorder': sort}
         if act == 'add':
             Link.add_new_link(params)
         
         if act == 'edit':
             Link.update_link_edit(params)
         
         clear_cache_by_pathlist(['/'])
         
     self.redirect('%s/admin/links'% (BASE_URL))
     return
Exemplo n.º 9
0
    def post(self):
        act = self.get_argument("act", "")
        id = self.get_argument("id", "")
        name = self.get_argument("name", "")
        sort = self.get_argument("sort", "0")
        url = self.get_argument("url", "")

        if name and url:
            params = {"id": id, "name": name, "url": url, "displayorder": sort}
            if act == "add":
                Link.add_new_link(params)

            if act == "edit":
                Link.update_link_edit(params)

            clear_cache_by_pathlist(["/"])

        self.redirect("%s/admin/links" % (BASE_URL))
        return
Exemplo n.º 10
0
    def get(self):
        act = self.get_argument("act",'')
        id = self.get_argument("id",'')

        obj = None
        if act == 'del':
            if id:
                Link.del_link_by_id(id)
                clear_cache_by_pathlist(['/'])
            self.redirect('%s/admin/links'% (BASE_URL))
            return
        elif act == 'edit':
            if id:
                obj = Link.get_link_by_id(id)
                clear_cache_by_pathlist(['/'])
        self.echo('admin_link.html', {
            'title': "管理友情链接",
            'objs': Link.get_all_links(),
            'obj': obj,
        },layout='_layout_admin.html')
Exemplo n.º 11
0
    def get(self):
        act = self.get_argument("act", "")
        id = self.get_argument("id", "")

        obj = None
        if act == "del":
            if id:
                Link.del_link_by_id(id)
                clear_cache_by_pathlist(["/"])
            self.redirect("%s/admin/links" % (BASE_URL))
            return
        elif act == "edit":
            if id:
                obj = Link.get_link_by_id(id)
                clear_cache_by_pathlist(["/"])
        self.echo(
            "admin_link.html",
            {"title": "管理友情链接", "objs": Link.get_all_links(), "obj": obj},
            layout="_layout_admin.html",
        )
Exemplo n.º 12
0
 def get(self):
     act = self.get_argument("act",'')
     id = self.get_argument("id",'')
     
     obj = None
     if act == 'del':
         if id:
             Link.del_link_by_id(id)
             clear_cache_by_pathlist(['/'])
         self.redirect('%s/admin/links'% (BASE_URL))
         return
     elif act == 'edit':
         if id:
             obj = Link.get_link_by_id(id)
             clear_cache_by_pathlist(['/'])
     self.echo('admin_link.html', {
         'title': "管理友情链接",
         'objs': Link.get_all_links(),
         'obj': obj,
     },layout='_layout_admin.html')        
Exemplo n.º 13
0
    def post(self, id = ''):
        act = self.get_argument("act",'')
        if act == 'findid':
            eid = self.get_argument("id",'')
            self.redirect('%s/admin/comment/%s'% (BASE_URL, eid))
            return

        tf = {'true':1,'false':0}
        post_dic = {
            'author': self.get_argument("author"),
            'email': self.get_argument("email"),
            'content': safe_encode(self.get_argument("content").replace('\r','\n')),
            'url': self.get_argument("url",''),
            'visible': self.get_argument("visible",'false'),
            'id': id
        }
        post_dic['visible'] = tf[post_dic['visible'].lower()]

        Comment.update_comment_edit(post_dic)
        clear_cache_by_pathlist(['post:%s'%id])
        self.redirect('%s/admin/comment/%s'% (BASE_URL, id))
        return
Exemplo n.º 14
0
 def get(self, id=""):
     obj = None
     if id:
         obj = Comment.get_comment_by_id(id)
         if obj:
             act = self.get_argument("act", "")
             if act == "del":
                 Comment.del_comment_by_id(id)
                 clear_cache_by_pathlist(["post:%d" % obj.postid])
                 self.redirect("%s/admin/comment/" % (BASE_URL))
                 return
     self.echo(
         "admin_comment.html",
         {
             "title": "管理评论",
             "cats": Category.get_all_cat_name(),
             "tags": Tag.get_all_tag_name(),
             "obj": obj,
             "comments": Comment.get_recent_comments(),
         },
         layout="_layout_admin.html",
     )
Exemplo n.º 15
0
    def post(self, id=""):
        act = self.get_argument("act", "")
        if act == "findid":
            eid = self.get_argument("id", "")
            self.redirect("%s/admin/comment/%s" % (BASE_URL, eid))
            return

        tf = {"true": 1, "false": 0}
        post_dic = {
            "author": self.get_argument("author"),
            "email": self.get_argument("email"),
            "content": safe_encode(self.get_argument("content").replace("\r", "\n")),
            "url": self.get_argument("url", ""),
            "visible": self.get_argument("visible", "false"),
            "id": id,
        }
        post_dic["visible"] = tf[post_dic["visible"].lower()]

        Comment.update_comment_edit(post_dic)
        clear_cache_by_pathlist(["post:%s" % id])
        self.redirect("%s/admin/comment/%s" % (BASE_URL, id))
        return
Exemplo n.º 16
0
 def get(self, id = ''):
     Article.del_post_by_id(id)
     clear_cache_by_pathlist(['post:%s'%id])
     self.redirect('%s/admin/edit_post/'% (BASE_URL))
Exemplo n.º 17
0
 def post(self, id = ''):
     act = self.get_argument("act",'')
     if act == 'findid':
         eid = self.get_argument("id",'')
         self.redirect('%s/admin/edit_post/%s'% (BASE_URL, eid))
         return
     
     self.set_header('Content-Type','application/json')
     rspd = {'status': 201, 'msg':'ok'}
     oldobj = Article.get_article_by_id_edit(id)
     
     try:
         tf = {'true':1,'false':0}
         timestamp = int(time())
         post_dic = {
             'category': self.get_argument("cat"),
             'title': self.get_argument("tit"),
             'content': self.get_argument("con"),
             'tags': self.get_argument("tag",'').replace(u',',','),
             'closecomment': self.get_argument("clo",'0'),
             'password': self.get_argument("password",''),
             'edit_time': timestamp,
             'id': id
         }
         
         if post_dic['tags']:
             tagslist = set([x.strip() for x in post_dic['tags'].split(',')])
             try:
                 tagslist.remove('')
             except:
                 pass
             if tagslist:
                 post_dic['tags'] = ','.join(tagslist)
         post_dic['closecomment'] = tf[post_dic['closecomment'].lower()]
     except:
         rspd['status'] = 500
         rspd['msg'] = '错误: 注意必填的三项'
         self.write(json.dumps(rspd))
         return
     
     postid = Article.update_post_edit(post_dic)
     if postid:
         cache_key_list = ['/', 'post:%s'% id, 'cat:%s' % quoted_string(oldobj.category)]
         if oldobj.category != post_dic['category']:
             #cat changed 
             Category.add_postid_to_cat(post_dic['category'], str(postid))
             Category.remove_postid_from_cat(post_dic['category'], str(postid))
             cache_key_list.append('cat:%s' % quoted_string(post_dic['category']))
         
         if oldobj.tags != post_dic['tags']:
             #tag changed 
             old_tags = set(oldobj.tags.split(','))
             new_tags = set(post_dic['tags'].split(','))
             
             removed_tags = old_tags - new_tags
             added_tags = new_tags - old_tags
             
             if added_tags:
                 Tag.add_postid_to_tags(added_tags, str(postid))
                     
             if removed_tags:
                 Tag.remove_postid_from_tags(removed_tags, str(postid))                
         
         clear_cache_by_pathlist(cache_key_list)
         rspd['status'] = 200
         rspd['msg'] = '完成: 你已经成功编辑了一篇文章 <a href="/t/%s" target="_blank">查看编辑后的文章</a>' % str(postid)
         self.write(json.dumps(rspd))
         return
     else:
         rspd['status'] = 500
         rspd['msg'] = '错误: 未知错误,请尝试重新提交'
         self.write(json.dumps(rspd))
         return            
Exemplo n.º 18
0
    def post(self, id=""):
        act = self.get_argument("act", "")
        if act == "findid":
            eid = self.get_argument("id", "")
            self.redirect("%s/admin/edit_post/%s" % (BASE_URL, eid))
            return

        self.set_header("Content-Type", "application/json")
        rspd = {"status": 201, "msg": "ok"}
        oldobj = Article.get_article_by_id_edit(id)

        try:
            tf = {"true": 1, "false": 0}
            timestamp = int(time())
            post_dic = {
                "category": self.get_argument("cat"),
                "title": self.get_argument("tit"),
                "content": self.get_argument("con"),
                "tags": self.get_argument("tag", "").replace(u",", ","),
                "closecomment": self.get_argument("clo", "0"),
                "password": self.get_argument("password", ""),
                "edit_time": timestamp,
                "id": id,
            }

            if post_dic["tags"]:
                tagslist = set([x.strip() for x in post_dic["tags"].split(",")])
                try:
                    tagslist.remove("")
                except:
                    pass
                if tagslist:
                    post_dic["tags"] = ",".join(tagslist)
            post_dic["closecomment"] = tf[post_dic["closecomment"].lower()]
        except:
            rspd["status"] = 500
            rspd["msg"] = "错误: 注意必填的三项"
            self.write(json.dumps(rspd))
            return

        postid = Article.update_post_edit(post_dic)
        if postid:
            cache_key_list = ["/", "post:%s" % id, "cat:%s" % quoted_string(oldobj.category)]
            if oldobj.category != post_dic["category"]:
                # cat changed
                Category.add_postid_to_cat(post_dic["category"], str(postid))
                Category.remove_postid_from_cat(post_dic["category"], str(postid))
                cache_key_list.append("cat:%s" % quoted_string(post_dic["category"]))

            if oldobj.tags != post_dic["tags"]:
                # tag changed
                old_tags = set(oldobj.tags.split(","))
                new_tags = set(post_dic["tags"].split(","))

                removed_tags = old_tags - new_tags
                added_tags = new_tags - old_tags

                if added_tags:
                    Tag.add_postid_to_tags(added_tags, str(postid))

                if removed_tags:
                    Tag.remove_postid_from_tags(removed_tags, str(postid))

            clear_cache_by_pathlist(cache_key_list)
            rspd["status"] = 200
            rspd["msg"] = '完成: 你已经成功编辑了一篇文章 <a href="/t/%s" target="_blank">查看编辑后的文章</a>' % str(postid)
            self.write(json.dumps(rspd))
            return
        else:
            rspd["status"] = 500
            rspd["msg"] = "错误: 未知错误,请尝试重新提交"
            self.write(json.dumps(rspd))
            return
Exemplo n.º 19
0
 def post(self, id = '', title = ''):
     action = self.get_argument("act")
     
     if action == 'inputpw':
         wrn = self.get_cookie("wrpw", '0')
         if int(wrn)>=10:
             self.write('403')
             return
         
         pw = self.get_argument("pw",'')
         pobj = Article.get_article_by_id_simple(id)
         wr = False
         if pw:             
             if pobj.password == pw:
                 self.set_cookie("rp%s" % id, pobj.password, path = "/", expires_days =1)
             else:
                 wr = True
         else:
             wr = True
         if wr:
             wrn = self.get_cookie("wrpw", '0')
             self.set_cookie("wrpw", str(int(wrn)+1), path = "/", expires_days = 1 )
         
         self.redirect('%s/topic/%d/%s'% (BASE_URL, pobj.id, pobj.title))
         return
     
     self.set_header('Content-Type','application/json')
     rspd = {'status': 201, 'msg':'ok'}
     
     if action == 'readmorecomment':
         fromid = self.get_argument("fromid",'')
         allnum = int(self.get_argument("allnum",0))
         showednum = int(self.get_argument("showednum", EACH_PAGE_COMMENT_NUM))
         if fromid:
             rspd['status'] = 200
             if (allnum - showednum) >= EACH_PAGE_COMMENT_NUM:
                 limit = EACH_PAGE_COMMENT_NUM
             else:
                 limit = allnum - showednum
             cobjs = Comment.get_post_page_comments_by_id( id, fromid, limit )
             rspd['commentstr'] = self.render('comments.html', {'cobjs': cobjs})
             rspd['lavenum'] = allnum - showednum - limit
             self.write(json.dumps(rspd))
         return
     
     #
     usercomnum = self.get_cookie('usercomnum','0')
     if int(usercomnum) > MAX_COMMENT_NUM_A_DAY:
         rspd = {'status': 403, 'msg':'403: Forbidden'}
         self.write(json.dumps(rspd))
         return
     
     try:
         timestamp = int(time())
         post_dic = {
             'author': self.get_argument("author"),
             'email': self.get_argument("email"),
             'content': safe_encode(self.get_argument("con").replace('\r','\n')),
             'url': self.get_argument("url",''),
             'postid': self.get_argument("postid"),
             'add_time': timestamp,
             'toid': self.get_argument("toid",''),
             'visible': COMMENT_DEFAULT_VISIBLE
         }
     except:
         rspd['status'] = 500
         rspd['msg'] = '错误: 注意必填的三项'
         self.write(json.dumps(rspd))
         return
     
     pobj = Article.get_article_by_id_simple(id)
     if pobj and not pobj.closecomment:
         cobjid = Comment.add_new_comment(post_dic)
         if cobjid:
             Article.update_post_comment( pobj.comment_num+1, id)
             rspd['status'] = 200
             #rspd['msg'] = '恭喜: 已成功提交评论'
             
             rspd['msg'] = self.render('comment.html', {
                     'cobjid': cobjid,
                     'gravatar': 'http://www.gravatar.com/avatar/%s'%md5(post_dic['email']).hexdigest(),
                     'url': post_dic['url'],
                     'author': post_dic['author'],
                     'visible': post_dic['visible'],
                     'content': post_dic['content'],
                 })
             
             clear_cache_by_pathlist(['/','post:%s'%id])
             #send mail
             if not debug:
                 try:
                     if NOTICE_MAIL:
                         tolist = [NOTICE_MAIL]
                     else:
                         tolist = []
                     if post_dic['toid']:
                         tcomment = Comment.get_comment_by_id(toid)
                         if tcomment and tcomment.email:
                             tolist.append(tcomment.email)
                     commenturl = "%s/t/%s#r%s" % (BASE_URL, str(pobj.id), str(cobjid))
                     m_subject = u'有人回复您在 《%s》 里的评论 %s' % ( pobj.title,str(cobjid))
                     m_html = u'这是一封提醒邮件(请勿直接回复): %s ,请尽快处理: %s' % (m_subject, commenturl)
                     
                     if tolist:
                         import sae.mail
                         sae.mail.send_mail(','.join(tolist), m_subject, m_html,(MAIL_SMTP, int(MAIL_PORT), MAIL_FROM, MAIL_PASSWORD, True))          
                     
                 except:
                     pass
         else:
             rspd['msg'] = '错误: 未知错误'
     else:
         rspd['msg'] = '错误: 未知错误'
     self.write(json.dumps(rspd))
Exemplo n.º 20
0
 def get(self, id = ''):
     Article.del_post_by_id(id)
     clear_cache_by_pathlist(['post:%s'%id])
     self.redirect('%s/admin/edit_post/'% (BASE_URL))
Exemplo n.º 21
0
 def get(self, id=""):
     Article.del_post_by_id(id)
     clear_cache_by_pathlist(["post:%s" % id])
     self.redirect("%s/admin/edit_post/" % (BASE_URL))
Exemplo n.º 22
0
    def post(self, id='', title=''):
        action = self.get_argument("act")

        if action == 'inputpw':
            wrn = self.get_cookie("wrpw", '0')
            if int(wrn) >= 10:
                self.write('403')
                return

            pw = self.get_argument("pw", '')
            pobj = Article.get_article_by_id_simple(id)
            wr = False
            if pw:
                if pobj.password == pw:
                    self.set_cookie("rp%s" % id,
                                    pobj.password,
                                    path="/",
                                    expires_days=1)
                else:
                    wr = True
            else:
                wr = True
            if wr:
                wrn = self.get_cookie("wrpw", '0')
                self.set_cookie("wrpw",
                                str(int(wrn) + 1),
                                path="/",
                                expires_days=1)

            self.redirect('%s/topic/%d/%s' % (BASE_URL, pobj.id, pobj.title))
            return

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

        if action == 'readmorecomment':
            fromid = self.get_argument("fromid", '')
            allnum = int(self.get_argument("allnum", 0))
            showednum = int(
                self.get_argument("showednum", EACH_PAGE_COMMENT_NUM))
            if fromid:
                rspd['status'] = 200
                if (allnum - showednum) >= EACH_PAGE_COMMENT_NUM:
                    limit = EACH_PAGE_COMMENT_NUM
                else:
                    limit = allnum - showednum
                cobjs = Comment.get_post_page_comments_by_id(id, fromid, limit)
                rspd['commentstr'] = self.render('comments.html',
                                                 {'cobjs': cobjs})
                rspd['lavenum'] = allnum - showednum - limit
                self.write(json.dumps(rspd))
            return

        #
        usercomnum = self.get_cookie('usercomnum', '0')
        if int(usercomnum) > MAX_COMMENT_NUM_A_DAY:
            rspd = {'status': 403, 'msg': '403: Forbidden'}
            self.write(json.dumps(rspd))
            return

        try:
            timestamp = int(time())
            post_dic = {
                'author':
                self.get_argument("author"),
                'email':
                self.get_argument("email"),
                'content':
                safe_encode(self.get_argument("con").replace('\r', '\n')),
                'url':
                self.get_argument("url", ''),
                'postid':
                self.get_argument("postid"),
                'add_time':
                timestamp,
                'toid':
                self.get_argument("toid", ''),
                'visible':
                COMMENT_DEFAULT_VISIBLE
            }
        except:
            rspd['status'] = 500
            rspd['msg'] = '错误: 注意必填的三项'
            self.write(json.dumps(rspd))
            return

        pobj = Article.get_article_by_id_simple(id)
        if pobj and not pobj.closecomment:
            cobjid = Comment.add_new_comment(post_dic)
            if cobjid:
                Article.update_post_comment(pobj.comment_num + 1, id)
                rspd['status'] = 200
                #rspd['msg'] = '恭喜: 已成功提交评论'

                rspd['msg'] = self.render(
                    'comment.html', {
                        'cobjid':
                        cobjid,
                        'gravatar':
                        'http://www.gravatar.com/avatar/%s' %
                        md5(post_dic['email']).hexdigest(),
                        'url':
                        post_dic['url'],
                        'author':
                        post_dic['author'],
                        'visible':
                        post_dic['visible'],
                        'content':
                        post_dic['content'],
                    })

                clear_cache_by_pathlist(['/', 'post:%s' % id])
                #send mail
                if not debug:
                    try:
                        if NOTICE_MAIL:
                            tolist = [NOTICE_MAIL]
                        else:
                            tolist = []
                        if post_dic['toid']:
                            tcomment = Comment.get_comment_by_id(toid)
                            if tcomment and tcomment.email:
                                tolist.append(tcomment.email)
                        commenturl = "%s/t/%s#r%s" % (BASE_URL, str(
                            pobj.id), str(cobjid))
                        m_subject = u'有人回复您在 《%s》 里的评论 %s' % (pobj.title,
                                                              str(cobjid))
                        m_html = u'这是一封提醒邮件(请勿直接回复): %s ,请尽快处理: %s' % (
                            m_subject, commenturl)

                        if tolist:
                            import sae.mail
                            sae.mail.send_mail(
                                ','.join(tolist), m_subject, m_html,
                                (MAIL_SMTP, int(MAIL_PORT), MAIL_FROM,
                                 MAIL_PASSWORD, True))

                    except:
                        pass
            else:
                rspd['msg'] = '错误: 未知错误'
        else:
            rspd['msg'] = '错误: 未知错误'
        self.write(json.dumps(rspd))
Exemplo n.º 23
0
    def post(self, id = ''):
        act = self.get_argument("act",'')
        if act == 'findid':
            eid = self.get_argument("id",'')
            self.redirect('%s/admin/edit_post/%s'% (BASE_URL, eid))
            return

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

        try:
            tf = {'true':1,'false':0}
            timestamp = int(time())
            post_dic = {
                'category': self.get_argument("cat"),
                'title': self.get_argument("tit"),
                'content': self.get_argument("con"),
                'tags': self.get_argument("tag",'').replace(u',',','),
                'closecomment': self.get_argument("clo",'0'),
                'password': self.get_argument("password",''),
                'edit_time': timestamp,
                'id': id
            }

            if post_dic['tags']:
                tagslist = set([x.strip() for x in post_dic['tags'].split(',')])
                try:
                    tagslist.remove('')
                except:
                    pass
                if tagslist:
                    post_dic['tags'] = ','.join(tagslist)
            post_dic['closecomment'] = tf[post_dic['closecomment'].lower()]
        except:
            rspd['status'] = 500
            rspd['msg'] = '错误: 注意必填的三项'
            self.write(json.dumps(rspd))
            return

        postid = Article.update_post_edit(post_dic)
        if postid:
            cache_key_list = ['/', 'post:%s'% id, 'cat:%s' % quoted_string(oldobj.category)]
            if oldobj.category != post_dic['category']:
                #cat changed
                Category.add_postid_to_cat(post_dic['category'], str(postid))
                Category.remove_postid_from_cat(post_dic['category'], str(postid))
                cache_key_list.append('cat:%s' % quoted_string(post_dic['category']))

            if oldobj.tags != post_dic['tags']:
                #tag changed
                old_tags = set(oldobj.tags.split(','))
                new_tags = set(post_dic['tags'].split(','))

                removed_tags = old_tags - new_tags
                added_tags = new_tags - old_tags

                if added_tags:
                    Tag.add_postid_to_tags(added_tags, str(postid))

                if removed_tags:
                    Tag.remove_postid_from_tags(removed_tags, str(postid))

            clear_cache_by_pathlist(cache_key_list)
            rspd['status'] = 200
            rspd['msg'] = '完成: 你已经成功编辑了一篇文章 <a href="/t/%s" target="_blank">查看编辑后的文章</a>' % str(postid)
            self.write(json.dumps(rspd))
            return
        else:
            rspd['status'] = 500
            rspd['msg'] = '错误: 未知错误,请尝试重新提交'
            self.write(json.dumps(rspd))
            return