def POST(self, id=1): pre_articals, pre_links, pre_comments, pre_categories = get_left_objs() id = int(id) comment_children = [] artical_handler = get_col_handler('artical') artical = artical_handler.find_one({'id':id}) comment_handler = get_col_handler('comment') comments = comment_handler.find({'artical_id':id}) if comments.count(): for comment in comments: children = get_comments_in_comment(comment['id']) comment_children.append([comment, children]) form = commentf() if not form.validates(): return self.render.artical_detail(pre_articals=pre_articals, pre_links=pre_links, pre_comments=pre_comments, pre_categories=pre_categories, artical=artical, form=form, comments=comment_children) check = web.input().get('remember_me', 'false') artical_id = web.input().get('artical_id', 1) name = web.input().get('name', '') email = web.input().get('email', '') website = web.input().get('website', '') content = web.input().get('content', '') comment_id = web.input().get('comment_id', 0) params = dict(name=urllib.quote(smart_str(name)), email=email, website=website) cookie_value = pickle.dumps(params) if check == 'false': pass else: web.setcookie(COOKIE_NAME, cookie_value, expires=2592000) comment = Comment(artical_id, name, email, website, content, comment_id) comment.save() # TODO:Package this part comment_obj = comment_handler.find_one({'id':int(comment_id)}) if int(comment_id) != 0: # comment_obj = comment_handler.find_one({'id':int(comment_id)}) send_email(comment_obj.get('email', ''), u'来自%s的回复' % name, \ u'%s\n详情请看:http://www.cj53.com/click/%s' % (content, artical_id)) send_email('*****@*****.**', u'有留言', \ u'%s\n详情请看:http://www.cj53.com/click/%s' % (content, artical_id)) if id == 1: raise web.seeother("/board") return web.seeother("/artical/%s" % id)
def save(self): col = self.__class__.__name__.lower() if self.id != 0: col_handler = get_col_handler(col) artical = col_handler.find_one({"id": self.id}) self.fields["times"] = artical["times"] self.fields["instime"] = artical["instime"] self.fields["insdate"] = artical["insdate"] self.fields["update"] = datetime.date.today().isoformat() self.fields["uptime"] = datetime.datetime.now() get_col_handler(col).save(self.fields) flush_tags() return "OK"
def get_category_name(id): """根据artical_id找到category """ try: id = int(id) artical_handler = get_col_handler('artical') category_handler = get_col_handler('category') category_id = artical_handler.find_one({'id':id})['category_id'] name = category_handler.find_one({'id':category_id})['name'] return name except: return 'Unknown'
def get_left_objs(): objs = [] artical_handler = get_col_handler('artical') tmp = artical_handler.find({'id':{'$ne':1}, 'status':True}).sort('uptime', \ pymongo.DESCENDING).limit(7) objs.append(tmp) link_handler = get_col_handler('link') tmp = link_handler.find().sort('instime', pymongo.DESCENDING).limit(7) objs.append(tmp) commt_handler = get_col_handler('comment') tmp = commt_handler.find({'status':True}).sort('id', pymongo.DESCENDING).limit(7) objs.append(tmp) category_handler = get_col_handler('category') tmp = category_handler.find({'id':{'$ne':1}}).sort('id', pymongo.DESCENDING) objs.append(tmp) return objs
def db_init(): for col in settings.COLLECTIONS: if col_handler.find_one({'name':col}): pass else: col_handler.insert({'name':col, 'id':0}) this_col_handler = get_col_handler(col) this_col_handler.create_index('id')
def count_artical_by_category(id): """根据category id计算该category下的文章数量,不包括删除的文章 """ try: id = int(id) artical_handler = get_col_handler('artical') sum = artical_handler.find({'status':True, 'category_id':id}).count() return sum except: return 0
def count_comment_by_artical(id): """根据artical id计算评论数量,包括删除的评论 """ try: id = int(id) comment_handler = get_col_handler('comment') sum = comment_handler.find({'artical_id':id}).count() return sum except: return 0
def get_comments_in_comment(comment_id): global commt_ids commt_handler = get_col_handler('comment') comment = commt_handler.find_one({'id':int(comment_id)}) comment_to_id = comment['comment_id'] or 0 if int(comment_to_id) == 0: tmp = commt_ids commt_ids = [] return tmp else: commt_ids.append(commt_handler.find_one({'id':int(comment_to_id)})) return get_comments_in_comment(comment_to_id)
def GET(self, id=1): pre_articals, pre_links, pre_comments, pre_categories= get_left_objs() id = int(id) comment_children = [] artical_handler = get_col_handler('artical') comment_handler = get_col_handler('comment') comments = comment_handler.find({'artical_id':id}) if comments.count(): for comment in comments: children = get_comments_in_comment(comment['id']) comment_children.append([comment, children]) artical = artical_handler.find_one({'id':id}) form = commentf() cookie = web.cookies().get(COOKIE_NAME, None) name = email = website = '' if cookie: cookie = pickle.loads(cookie) name = urllib.unquote(cookie.get('name', '')) email = cookie.get('email', '') website = cookie.get('website', '') form.fill({'artical_id':int(id), 'id':0, 'comment_id':0, 'name':name, 'email':email, 'website':website}) if not artical: return "<script>alert('别乱点!');window.location.href='/';</script>" return self.render.artical_detail(pre_articals=pre_articals, pre_links=pre_links, pre_comments=pre_comments, pre_categories=pre_categories, artical=artical, form=form, comments=comment_children)
def flush_tags(): """ 刷新文章标签,每次写一篇文章后,刷新标签 """ artical_handler = get_col_handler('artical') articals = artical_handler.find({'status':True}) tags = set([]) for artical in articals: [tags.add(urllib.quote(smart_str(i))) for i in artical['tags'].split(' ') if i.strip()] tags_file = open(settings.PROJECT_PATH + '/templates/tags.html', 'w') content = render_to_response('tags_tmp', {'tags':tags}) tags_file.write(content) tags_file.close()
def check_existed(col, fields): if not isinstance(fields, dict): return False id = fields.pop('id', 0) status = 'add' if int(id) != 0: status = 'edit' col_handler = get_col_handler(col) if status == 'add': result = col_handler.find_one(fields) if result: return False else: fields.update({'id':{'$ne':int(id)}}) result = col_handler.find_one(fields) if result: return False return True
def GET(self): args = web.input(page='1') if not args.page.isdigit(): return web.notfound() page = int(args.page) per_page = 7 artical_handler = get_col_handler('artical') total = artical_handler.find({'id':{'$ne':1}}).sort('uptime', \ pymongo.DESCENDING).count() paginator = Paginator(total, page, per_page=per_page) pre_articals, pre_links, pre_comments, pre_categories = get_left_objs() articals = artical_handler.find({'id':{'$ne':1}}).sort('uptime', \ pymongo.DESCENDING).limit(per_page).skip((page-1)*per_page) return self.render.index( pre_articals=pre_articals, pre_links=pre_links, pre_comments=pre_comments, pre_categories=pre_categories, articals=articals, paginator=paginator, tip='')
def save(self): col = self.__class__.__name__.lower() get_col_handler(col).save(self.fields) return "OK"
def remove(self): col = self.__class__.__name__.lower() get_col_handler(col).remove(self.fields)
#coding:utf-8 import settings from utils.mongodb import get_col_handler col_name = 'ids' col_handler = get_col_handler(col_name) def db_init(): for col in settings.COLLECTIONS: if col_handler.find_one({'name':col}): pass else: col_handler.insert({'name':col, 'id':0}) this_col_handler = get_col_handler(col) this_col_handler.create_index('id')