def post(self): 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()] Comments.update(post_dic) clear_cache_by_pathlist(['post:%s' % id]) self.redirect('%s/admin/comment/%s' % (BASE_URL, id)) return
def comment(): passwd = request.cookies.get('cookie') name = request.cookies.get('uname') data = request.get_json() comments = data['comment'] bid = data['id'] blog = Blog.query.filter_by(id=bid).first() title = blog.title if (passwd in cookies): Time = time.strftime('%Y-%m-%d') Comments.commadd(uname=name, bid=bid, btitle=title, body=comments, time=Time) return jsonify({"msg":"ok"}) return jsonify({"msg":"ee"}),201
def get(self, list_type='', direction='next', page='1', name=''): catobj = None if list_type == 'cat': #objs = Category.get_posts_by_name(name, page) catobj = Categories.get_by_category_name(name) show_type = catobj.showtype elif list_type == 'tag': objs = Tags.get_page_posts_by_tag_name(name, page) catobj = Tags.get_tag_by_name(name) show_type = "list" elif list_type == 'archive': objs = Archives.get_page_posts_by_archive_name(name, page) catobj = Archives.get_by_name(name) show_type = "list" if catobj: pass else: self.redirect(BASE_URL) return each_page_post_num = int(getAttr('EACH_PAGE_POST_NUM')) all_post = catobj.id_num all_page = all_post / each_page_post_num if all_post % each_page_post_num: all_page += 1 output = self.render(show_type + '.html', { 'title': "%s - %s | Part %s" % (catobj.name, getAttr('SITE_TITLE'), page), 'keywords': catobj.name, 'description': getAttr('SITE_DECR'), 'objs': objs, 'cats': Categories.get_all_category_name(), 'tags': Tags.get_hot_tag(), 'archives': Archives.get_by_name(), 'page': int(page), 'allpage': all_page, 'listtype': list_type, 'name': name, 'namemd5': md5(name.encode('utf-8')).hexdigest(), 'comments': Comments.get_recent_comments(), 'links': Links.get_all(), }, layout='_layout.html') self.write(output) return output
def get(self, **kwargs): result = { 'code': 400, 'message': 'bad request' } url = self._removeOverhead(kwargs.get('url')) options = {} for item in self.arguments: options[item] = self.arguments.get(item) comments, cursor, _ = Comments.find(url, options) like_items = [] if self.get_user(): likes = Likes.find(ndb.Key(Users, self.get_user().get('uid')), [comment.key for comment in comments]) like_items = [item.target.id() for item in likes] result['code'] = 200 result['message'] = 'OK' result['Comments'] = self.listToObject(comments) result['Liked'] = like_items result['cursor'] = cursor.to_websafe_string() if cursor else None return self.createRes(200, result)
def get(self, direction='next', page='2', base_id='1'): if page == '1': self.redirect(BASE_URL) return objs = Posts.get_paged(direction, page, base_id) if objs: if direction == 'prev': objs.reverse() fromid = objs[0].id endid = objs[-1].id else: fromid = endid = '' each_page_post_num = int(getAttr('EACH_PAGE_POST_NUM')) all_post = Posts.count_all() all_page = all_post / each_page_post_num if all_post % each_page_post_num: all_page += 1 output = self.render('default.html', { 'title': "%s - %s | Part %s" % (getAttr('SITE_TITLE'), getAttr('SITE_SUB_TITLE'), page), 'keywords': getAttr('KEYWORDS'), 'description': getAttr('SITE_DECR'), 'objs': objs, 'cats': Categories.get_all_category_name(), 'tags': Tags.get_hot_tag(), 'archives': Archives.get_by_name(), 'page': int(page), 'allpage': all_page, 'listtype': 'index', 'fromid': fromid, 'endid': endid, 'comments': Comments.get_recent_comments(), 'links': Links.get_all(), }, layout='_layout.html') self.write(output) return output
def delete(self, **kwargs): result = { 'code': 400, 'message': 'bad request' } cid = int(kwargs.get('cid', 0)) comment = Comments.get(id=cid) if type(comment) == ndb.key.Key: result['code'] = 404 result['message'] = 'not found' return self.createRes(404, result) else: comment.key.delete() result['code'] = 200 result['message'] = 'OK' return self.createRes(200, result)
def post(self, **kwargs): result = { 'code': 400, 'message': 'bad request' } reqInfo = self.convertRequsetParameter(self.arguments, ['owned', 'access_token']) url = self._removeOverhead(kwargs.get('url')) cid = int(kwargs.get('cid', 0)) user = self.get_user() password = self.arguments.get('password', None) reqInfo['Author'] = ndb.Key(Users, user.get('uid')) if user else None reqInfo['url'] = url if hasattr(reqInfo, 'key'): del reqInfo['key'] if cid: comment = Comments.get(id=cid) if type(comment) == ndb.key.Key: result['code'] = 404 result['message'] = 'not found' return self.createRes(404, result) elif password != comment.get('password'): result['code'] = 401 result['message'] = 'not allowed' return self.createRes(401, result) elif user is None and password is None: result['code'] = 400 result['message'] = 'bad request' return self.createRes(400, result) elif self.arguments.get('owned'): comment = Comments(auto_id=True) owned = Comments.get(id=int(self.arguments.get('owned'))) reqInfo['owned'] = owned.key.id() if type(owned) != ndb.key.Key else comment.key.id() else: comment = Comments(auto_id=True) reqInfo['owned'] = comment.key.id() comment.set(reqInfo) result['code'] = 200 result['message'] = 'OK' result['Comments'] = comment.to_obj() return self.createRes(200, result)
def get(self, name=''): if not name: print 'ArchiveDetail name null' name = Archives.get_latest_archive_name() objs = Archives.get_page_posts_by_archive_name(name, 1) archiveobj = Archives.get_by_name(name) if archiveobj: pass else: self.redirect(BASE_URL) return each_page_post_num = int(getAttr('EACH_PAGE_POST_NUM')) all_post = archiveobj.id_num all_page = all_post / each_page_post_num if all_post % each_page_post_num: all_page += 1 output = self.render( 'default.html', { 'title': "%s - %s" % (archiveobj.name, getAttr('SITE_TITLE')), 'keywords': archiveobj.name, 'description': getAttr('SITE_DECR'), 'objs': objs, 'cats': Categories.get_all_category_name(), 'tags': Tags.get_hot_tag(), 'archives': Archives.get_by_name(), 'page': 1, 'allpage': all_page, 'listtype': 'archive', 'name': name, 'namemd5': md5(name.encode('utf-8')).hexdigest(), 'recent_article': Posts.get_last_post(8), 'comments': Comments.get_recent_comments(), 'links': Links.get_all(), }, layout='_layout.html') self.write(output) return output
def action(self, **kwargs): result = { 'code': 400, 'message': 'bad request' } cid = int(kwargs.get('cid', 0)) url = kwargs.get('url') action = kwargs.get('action') if not self.get_user(): result['code'] = 401 result['message'] = 'not allowed' return self.createRes(401, result) user = Users.get(id=self.get_user().get('uid')) comment = Comments.get(id=cid) if action in ['like', 'unlike']: like = Likes.find(ndb.Key(Users, self.get_user().get('uid')), [comment.key]) if len(like) > 0 and action == 'unlike': like[0].key.delete() elif len(like) == 0 and action == 'like': like = Likes(auto_id=True) like.user = user.key like.target = comment.key like.put() else: result['code'] = 500 result['message'] = 'internal error' return self.createRes(500, result) comment.likeCount = int(comment.likeCount if comment.likeCount else 0) + (1 if action == 'like' else -1) comment.put() result['code'] = 200 result['message'] = 'OK' result['Comment'] = comment.to_obj() return self.createRes(200, result)
def post_detail_formate(obj): if obj: slug = slugfy(obj.title) obj.slug = slug obj.absolute_url = '%s/topic/%d/%s' % (BASE_URL, obj.id, slug) obj.shorten_url = '%s/t/%s' % (BASE_URL, obj.id) if '[/code]' in obj.content: obj.highlight = True else: obj.highlight = False obj.content = tran_content(obj.content, obj.highlight) obj.taglist = ', '.join( ["""<a href="%s/tag/%s/" rel="tag">%s</a>""" % (BASE_URL, tag, tag) for tag in obj.tags.split(',')]) obj.add_time_fn = time_from_now(int(obj.add_time)) obj.last_modified = timestamp_to_datetime(obj.edit_time) obj.keywords = obj.tags obj.description = HTML_REG.sub('', obj.content[:DESCRIPTION_CUT_WORDS]) #get prev and next obj obj.prev_obj = sdb.get('SELECT `id`,`title` FROM `cms_posts` WHERE `id` > %s LIMIT 1' % str(obj.id)) if obj.prev_obj: obj.prev_obj.slug = slugfy(obj.prev_obj.title) obj.next_obj = sdb.get( 'SELECT `id`,`title` FROM `cms_posts` WHERE `id` < %s ORDER BY `id` DESC LIMIT 1' % str(obj.id)) if obj.next_obj: obj.next_obj.slug = slugfy(obj.next_obj.title) #get relative obj base tags obj.relative = [] if obj.tags: idlist = [] getit = False for tag in obj.tags.split(','): from model.tags import Tags tagobj = Tags.get_tag_by_name(tag) if tagobj and tagobj.content: pids = tagobj.content.split(',') for pid in pids: if pid != str(obj.id) and pid not in idlist: idlist.append(pid) if len(idlist) >= RELATIVE_POST_NUM: getit = True break if getit: break # if idlist: obj.relative = sdb.query('SELECT `id`,`title` FROM `cms_posts` WHERE `id` in(%s) LIMIT %s' % ( ','.join(idlist), str(len(idlist)))) if obj.relative: for robj in obj.relative: robj.slug = slugfy(robj.title) #get comment obj.coms = [] if obj.comment_num > 0: if obj.comment_num >= EACH_PAGE_COMMENT_NUM: first_limit = EACH_PAGE_COMMENT_NUM else: first_limit = obj.comment_num from model.comments import Comments obj.coms = Comments.get_post_page_comments_by_id(obj.id, 0, first_limit) return obj
def post(self, id='', title=''): action = self.get_argument("act") if action == 'inputpw': wrn = self.get_secure_cookie("wrpw") if wrn is not None and int(wrn) >= 10: self.write('403') return pw = self.get_argument("pw", '') pobj = Posts.get_post_simple(id) if pw: if pobj.password == pw: clear_cache_by_pathlist(['post:%s' % id]) self.set_secure_cookie("rp%s" % id, pobj.password, expires_days=1) else: self.set_secure_cookie("wrpw", str(int(wrn) + 1), expires_days=1) else: self.set_secure_cookie("wrpw", str(int(wrn) + 1), 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", '') all_num = int(self.get_argument("allnum", 0)) each_page_comment_num = getAttr('EACH_PAGE_COMMENT_NUM') showed_num = int( self.get_argument("showednum", each_page_comment_num)) if fromid: rspd['status'] = 200 if (all_num - showed_num) >= each_page_comment_num: limit = each_page_comment_num else: limit = all_num - showed_num cobjs = Comments.get_post_page_comments_by_id( id, fromid, limit) rspd['commentstr'] = self.render('comments.html', {'cobjs': cobjs}) rspd['lavenum'] = all_num - showed_num - limit self.write(json.dumps(rspd)) return # 评论次数限制不好使 usercomnum = self.get_secure_cookie("usercomnum") if usercomnum is not None and int(usercomnum) > getAttr( 'MAX_COMMENT_NUM_A_DAY'): rspd = {'status': 403, 'msg': '403: Forbidden'} self.write(json.dumps(rspd)) return else: usercomnum = 0 try: post_dic = { 'author': self.get_argument("author"), 'email': self.get_argument("email", ''), 'content': safe_encode(self.get_argument("comment").replace('\r', '\n')), 'url': self.get_argument("url", ''), 'postid': self.get_argument("postid"), 'add_time': int(time()), 'toid': self.get_argument("toid", ''), 'visible': getAttr('COMMENT_DEFAULT_VISIBLE') } except: rspd['status'] = 500 rspd['msg'] = '错误:请检查你提交的数据是否正确!' self.write(json.dumps(rspd)) return pobj = Posts.get_post_simple(id) if pobj and not pobj.closecomment: cobjid = Comments.create(post_dic) if cobjid: Posts.update_comment_num(pobj.comment_num + 1, id) self.set_secure_cookie("usercomnum", str(int(usercomnum) + 1), expires_days=1) rspd['status'] = 200 rspd['msg'] = '恭喜您,已成功提交评论!' rspd['msg'] = self.render( 'comment.html', { 'postid': id, 'cobjid': cobjid, 'gravatar': 'http://www.gravatar.com/avatar/%s' % md5(post_dic['email']).hexdigest(), 'url': post_dic['url'], 'author': post_dic['author'], 'add_time': int(time()), 'visible': post_dic['visible'], 'content': post_dic['content'], }) clear_cache_by_pathlist(['/', 'post:%s' % id]) #send mail if not debug: try: NOTICE_MAIL = getAttr('NOTICE_MAIL') if NOTICE_MAIL: tolist = [NOTICE_MAIL] else: tolist = [] if post_dic['toid']: tcomment = Comments.get(post_dic['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, (getAttr('MAIL_SMTP'), int(getAttr('MAIL_PORT')), getAttr('MAIL_FROM'), getAttr('MAIL_KEY'), True)) except: pass else: rspd['msg'] = '保存评论失败!' else: rspd['msg'] = '当前文章禁止评论!' self.write(json.dumps(rspd))
def get(self, post_id='', title=''): tmpl = '' obj = Posts.get_post_detail(post_id) if not obj: self.redirect(BASE_URL) return #redirect to right title try: title = unquote(title).decode('utf-8') except: pass if title != obj.slug: self.redirect(obj.absolute_url, 301) return # if obj.password and THEME == 'default': rp = self.get_secure_cookie("rp%s" % post_id, '') if rp != obj.password: tmpl = '_pw' elif obj.password and getAttr('BLOG_PSW_SUPPORT'): rp = self.get_secure_cookie("rp%s" % post_id, '') print 'rp===%s' % (str(rp)) if rp != obj.password: tmpl = '_pw' keyname = 'pv_%s' % (str(post_id)) increment(keyname) self.set_secure_cookie(keyname, '1', expires_days=1) self.set_header("Last-Modified", obj.last_modified) template_prefix = 'post' if obj.category == '-': template_prefix = 'page' output = self.render( template_prefix + '%s.html' % tmpl, { 'title': "%s - %s" % (obj.title, getAttr('SITE_TITLE')), 'keywords': obj.keywords, 'description': obj.description, 'obj': obj, 'cobjs': obj.coms, 'postdetail': 'postdetail', 'cats': Categories.get_all_category_name(), 'tags': Tags.get_hot_tag(), 'archives': Archives.get_by_name(), 'page': 1, 'allpage': 10, 'comments': Comments.get_recent_comments(), 'links': Links.get_all(), 'hits': get_count(keyname), 'recent_article': Posts.get_last_post(8), 'listtype': '', }, layout='_layout.html') self.write(output) if obj.password and getAttr('BLOG_PSW_SUPPORT'): return output elif obj.password and THEME == 'default': return else: return output