def rm_reduplicate(): a = set() for i in Notice.where('cid in (%s, %s)' % (CID_NOTICE_WALL, CID_NOTICE_WALL_REPLY)).where(STATE_GTE_APPLY).order_by('id desc'): t = (i.from_id, i.to_id, i.cid, i.rid) if t not in a: if notice_id_count(*t): a.add(t) for from_id, to_id, cid, rid in a: for seq, i in enumerate(Notice.where(from_id=from_id, to_id=to_id, cid=cid, rid=rid).where(STATE_GTE_APPLY).order_by('id desc')): if seq: i.rm(to_id)
def get(self, id): user_id = self.current_user_id n = N.mc_get(id) if n and n.to_id == user_id and n.state >= STATE_APPLY: link = n.link_to n.read(user_id) if link: return self.redirect(link) return self.redirect("/notice")
def get(self, id): user_id = self.current_user_id n = N.mc_get(id) if n and n.to_id == user_id and n.state >= STATE_APPLY: link = n.link_to n.read(user_id) if link: return self.redirect(link) return self.redirect('/notice')
def rm_reduplicate(): a = set() for i in Notice.where( 'cid in (%s, %s)' % (CID_NOTICE_WALL, CID_NOTICE_WALL_REPLY)).where(STATE_GTE_APPLY).order_by('id desc'): t = (i.from_id, i.to_id, i.cid, i.rid) if t not in a: if notice_id_count(*t): a.add(t) for from_id, to_id, cid, rid in a: for seq, i in enumerate( Notice.where( from_id=from_id, to_id=to_id, cid=cid, rid=rid).where(STATE_GTE_APPLY).order_by('id desc')): if seq: i.rm(to_id)
def notice_day(): prev_pos = kv_int.get(KV_NOTICE_POS) c = Notice.raw_sql('select max(id) from notice where create_time<%s', today_seconds()) pos = c.fetchone()[0] if pos > prev_pos: d = defaultdict(OrderedSet) for i in Notice.where(state=STATE_APPLY).where('id>%s and id<=%s', prev_pos, pos): cid = i.cid to_id = i.to_id if cid in NOTICE_MAIL_DAY and mail_notice_state( to_id, CID_MAIL_DAY): d[to_id].add((i.from_id, cid, i.rid)) for to_id, li in d.iteritems(): notice_mail_day(to_id, li) sleep(0.1) kv_int.set(KV_NOTICE_POS, pos)
def notice_day(): prev_pos = kv_int.get(KV_NOTICE_POS) c = Notice.raw_sql( 'select max(id) from notice where create_time<%s', today_seconds() ) pos = c.fetchone()[0] if pos > prev_pos: d = defaultdict(OrderedSet) for i in Notice.where(state=STATE_APPLY).where( 'id>%s and id<=%s', prev_pos, pos ): cid = i.cid to_id = i.to_id if cid in NOTICE_MAIL_DAY and mail_notice_state(to_id, CID_MAIL_DAY): d[to_id].add((i.from_id, cid, i.rid)) for to_id, li in d.iteritems(): notice_mail_day(to_id, li) sleep(0.1) kv_int.set(KV_NOTICE_POS, pos)
def send_comment(): data = request.get_json() print(data) if is_user_login(request): user_id = get_user_id_by_cookie(request) else: return jsonify({'code': 208, 'msg': "登录信息已经过期"}) article_id = data['article_id'] comment_id = data['comment_id'] be_comment_user_id = data['be_comment_user_id'] com_content = data['com_content'] redis = get_redis_cli() user = User.query.filter(User.id == user_id).first() user_name = user.nick_name if exist_user(user_id) and exist_article(article_id): print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) comment_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) if be_comment_user_id == "null": comment = Comment(None, user_id, comment_id, article_id, comment_time, com_content, None) db.session.add(comment) # 评论文章,个人+1积分 user = User.query.filter(User.id == user_id).first() user.effect += 1 db.session.commit() result = Article.query.filter(Article.article_id == article_id).first() result.heat = int(result.heat) + int(5) # 找出作者的id author_user_id = result.user_id notice_content = str(user_name) + "评论了你的文章:" + str(result.title) notice_title = "你收到了一条评论" n1 = Notice(None, author_user_id, notice_title, notice_content, 0, comment_time, -1, 2, user_id) db.session.add(n1) # 有人评论,作者积分+2 author = User.query.filter(User.id == author_user_id).first() author.effect += 3 db.session.commit() status_key = "status:" + "comment:" + str(author_user_id) if redis.keys(status_key): print(redis.keys(status_key)) else: redis.set(status_key, 1) else: if exist_user(be_comment_user_id): comment = Comment(None, user_id, comment_id, article_id, comment_time, com_content, be_comment_user_id) db.session.add(comment) db.session.commit() notice_title = "你收到了一条回复" notice_content = str(user_name) + "回复了你的评论" n1 = Notice(None, be_comment_user_id, notice_title, notice_content, 0, comment_time, -1, 2, user_id) db.session.add(comment) db.session.add(n1) db.session.commit() status_key = "status:" + "comment:" + str(be_comment_user_id) if redis.keys(status_key): print(redis.keys(status_key)) else: redis.set(status_key, 1) else: return jsonify({"code": "203", "msg": "被评论用户不存在,无法发表此评论"}) # 评论数加1,并更新到redis result = db.session.query(Article).get(article_id) result.comment_num = result.comment_num + 1 db.session.commit() return jsonify({"code": "200", "msg": "评论成功"}) else: return jsonify({"code": "203", "msg": "用户不存在,无法发表此评论"})
def like(): data = request.get_json() # 参数:用户id 文章id 点赞状态 点赞数(?) if not is_user_login(request): return jsonify({"code": 208, "msg": "登录信息已经过期"}) user_id = get_user_id_by_cookie(request) if not exist_user(user_id): return {"code": 201, "msg": "用户不存在"} article_id = int(data['article_id']) # action == 0 -> 取消点赞, 1 -> 点赞 action = int(data['like_action']) redis = get_redis_cli() time1 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 是否已经点赞 liked = db.session.query(Fabulous).filter_by(user_id=user_id, article_id=article_id).first() article = db.session.query(Article).filter_by(article_id=article_id).first() if article is None: # 并没有该文章, 报错 return jsonify({"code": 500, "msg": "该文章不存在"}) # 作者id author_id = article.user_id if not exist_user(author_id): return {"code": 203, "msg": "作者不存在"} author = User.query.filter(User.id == author_id).first() # 用户 user = User.query.filter(User.id == user_id).first() notice_title = "你有新的一条点赞信息" notice_content = "用户:" + str(user.nick_name) + "点赞了你的文章:" + str(article.title) status_key = "status:" + "like:" + str(article.user_id) result = Notice.query.filter(Notice.user_id == author_id, Notice.initiative_id == user_id, Notice.sort == '1').first() if liked is not None: if action == 1: if liked.fabulous_status == 0: liked.fabulous_status = -1 article.fabulous_num += 1 # 有人点赞,作者+1积分 author.effect += 2 # 点赞,个人+1积分 user.effect += 1 db.session.commit() like_count = db.session.query(Article).filter_by(article_id=article_id).first().fabulous_num # 发送推送 # 找出被点赞的文章作者的id # 点赞热度文章加3 article.heat = int(article.heat) + int(3) if result is None: n1 = Notice(None, article.user_id, notice_title, notice_content, 0, time1, -1, 1,user_id) db.session.add(n1) else: result.notice_content = "用户:" + str(user.nick_name) + "点赞了你的文章:" + str(article.title) db.session.commit() if redis.keys(status_key): redis.incr(status_key) else: redis.set(status_key, 1) return jsonify({"code": 200, "msg": "点赞成功", "like_count": like_count}) return jsonify({"code": 204, "msg": "已经点过赞"}) else: if liked.fabulous_status == 1 or liked.fabulous_status == -1: article.fabulous_num -= 1 liked.fabulous_status = 0 # 有人取消点赞,作者-1积分 author.effect -= 2 # 取消点赞,个人-1积分 user.effect -= 1 article.heat -= 3 db.session.commit() redis_result=redis.get(status_key) if int(redis_result) == 1 : redis.delete(status_key) if int(redis_result) > 1: redis.decr(status_key) return jsonify({"code": 205, "msg": "取消点赞成功"}) else: if action == 1: new_like = Fabulous(None, user_id=user_id, article_id=article_id, fabulous_time=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), fabulous_status=1) db.session.add(new_like) article.fabulous_num += 1 # 有人点赞,作者+1积分 author.effect += 2 db.session.commit() like_count = db.session.query(Article).filter_by(article_id=article_id).first().fabulous_num # TODO 发推送 # 点赞热度文章加3 article.heat = int(article.heat) + int(3) if result is None: n1 = Notice(None, article.user_id, notice_title, notice_content, 0, time1, -1, 1, user_id) db.session.add(n1) else: result.notice_content = "用户:" + str(user.nick_name) + "点赞了你的文章:" + str(article.title) db.session.commit() if redis.keys(status_key): redis.incr(status_key) else: redis.set(status_key, 1) else: return jsonify({"code": 203, "msg": "没有点赞无法取消点赞"}) return jsonify({"code": 200, "msg": "点赞成功", "like": like_count})