def error_if_comment_does_not_exist(*args, **kwargs): self = args[0] post_id = kwargs['comment_id'] post = Comment.get_by_id(int(post_id)) if post: fn(self, **kwargs) else: self.abort(404)
def post(self, **kwargs): post_id = kwargs['post_id'] comment_id = kwargs['comment_id'] content = self.request.POST['content'] # Update the comment comment = Comment.get_by_id(int(comment_id)) comment.content = content comment.put() self.redirect('/posts/' + post_id)
def get(self, post_id, comment_id): if self.user: # get current comment! article = Article.get_by_id(int(post_id)) comment = Comment.get_by_id(int(comment_id)) if comment.made_by == self.user.name: self.render("edit_comment.html", title=article.title, comment=comment.comment, user=self.user) else: self.redirect('/blog/broken') else: self.redirect('/blog/login')
def post(self, post_id, comment_id): if self.user: article = Article.get_by_id(int(post_id)) comment_elem = Comment.get_by_id(int(comment_id)) if comment_elem.made_by == self.user.name: if 'back' in self.request.POST: self.redirect('/blog/%s' % str(post_id)) elif 'delete' in self.request.POST: comment_elem.delete() self.redirect('/blog/%s' % str(post_id)) else: self.redirect('/blog/broken') else: self.redirect('/blog/login')
def es_update_comment(id): post: Comment = Comment.get_by_id(id) if not post: return u: User = User.get_by_id(post.user_id) if not u: return p = POST_TYPES.get_post(post.related_type, post.related_id) body = get_post_base_body(post) body.update({ 'user_nickname': u.nickname, 'content': post.content, 'brief': post.content[:100], 'related_title': p.get_title() if p else None, 'related_type': post.related_type, 'related_id': to_hex(post.related_id) }) es.index( index=INDEX_NAME, doc_type="doc", id=to_hex(post.id), body=body )
def fetch_notif_of_log(user_id, last_manage_log_id=b'\x00'): from model.manage_log import ManageLog, MOP if last_manage_log_id is None: last_manage_log_id = b'\x00' item_lst = ManageLog.select().where( ManageLog.related_user_id == user_id, ManageLog.id > last_manage_log_id, ManageLog.operation.in_( (MOP.POST_STATE_CHANGE, MOP.USER_PASSWORD_CHANGE, MOP.USER_PASSWORD_RESET, MOP.USER_KEY_RESET, MOP.USER_GROUP_CHANGE, MOP.USER_CREDIT_CHANGE, MOP.USER_REPUTE_CHANGE, MOP.USER_NICKNAME_CHANGE, MOP.TOPIC_BOARD_MOVE, MOP.TOPIC_AWESOME_CHANGE, MOP.TOPIC_STICKY_WEIGHT_CHANGE) ) ).order_by(ManageLog.id.desc()) moves = [] def wrap(item: ManageLog): # 总不能把MANAGE_OPERATION的内容换个号码,抄一遍写在上面。 # 因此选择过滤掉一些,其他全部归为一类。 if item.operation == MOP.USER_CREDIT_CHANGE: if item.note == '每日签到': # 签到得分忽略 return elif item.operation == MOP.USER_EXP_CHANGE: if item.note == '每日登录': # 签到得分忽略 return if item.operation == MOP.TOPIC_BOARD_MOVE: moves.append([POST_TYPES.BOARD, to_bin(item.value['change'][0])]) moves.append([POST_TYPES.BOARD, to_bin(item.value['change'][1])]) return { 'type': NOTIF_TYPE.MANAGE_INFO_ABOUT_ME, 'time': item.time, 'loc_post_type': item.related_type, 'loc_post_id': item.related_id, 'loc_post_title': None, 'sender_ids': (item.user_id,), 'receiver_id': user_id, 'from_post_type': None, # 来源为managelog,但并非post 'from_post_id': item.id, 'related_type': item.related_type, # 提醒类型较为特殊 'related_id': item.related_id, 'data': { 'op': item.operation, 'role': item.role, 'value': item.value, 'title': None # 进行二次填充 } } ret_items = list(filter(lambda x: x, map(wrap, item_lst))) info = POST_TYPES.get_post_title_by_list(*[[i['related_type'], i['related_id']] for i in ret_items]) info2 = POST_TYPES.get_post_title_by_list(*moves) for i in ret_items: t = info.get(get_bytes_from_blob(i['related_id']), None) if t: i['data']['title'] = t if i['related_type'] == POST_TYPES.COMMENT: # 这里不是批量,可能要付出较大代价 c: Comment = Comment.get_by_id(i['related_id']) if not c: continue p = POST_TYPES.get_post(c.related_type, c.related_id) if not p: continue i['loc_post_type'] = c.related_type i['loc_post_id'] = c.related_id i['loc_post_title'] = p.get_title() i['data']['comment'] = { 'related_type': c.related_type, 'related_id': c.related_id, 'related_title': p.get_title(), 'post_number': c.post_number, 'content': c.content } if i['data']['op'] == MOP.TOPIC_BOARD_MOVE: val = i['data']['value']['change'] i['data']['move_info'] = [ info2.get(to_bin(val[0]), None), info2.get(to_bin(val[1]), None) ] return ret_items