def add(): form = request.form.to_dict() author = current_user() author_id = author.id form['user_id'] = author_id new_rep = Reply.new(form) # 有新的回复时,更新帖子的last_active_time t: Topic = Topic.one(id=new_rep.topic_id) t.last_active_time = new_rep.created_time t.last_rep_uid = author_id t.reps = t.reps + 1 t.save() with data_cache.pipeline(transaction=False) as pipe: key = 'topic_id_{}.replies'.format(t.id) key2 = 'topic_id_{}.topic_info'.format(t.id) pipe.delete(key) pipe.delete(key2) pipe.execute() inform_at(t, new_rep.content, author) if author.id != t.user_id: # 如果是回复他人的帖子,则对主题主人发系统信息通知 Info.send(title='{}刚刚回复了您的主题'.format(author.username), receiver_id=t.user_id, content='{}在您的主题{} 中发表了一个新回复,查看:\n\r{}/topic/{}' .format(author.username, t.title, web_domain_name, t.id)) key = 'user_id_{}.received_info'.format(t.user_id) data_cache.delete(key) return redirect(url_for('my_topic.detail', topic_id=new_rep.topic_id))
def delete(): """执行系统通知的删除操作""" u = current_user() info_id = int(request.form.get('info_id', -1)) i: Info = Info.one(id=info_id) if i is not None: # 权限验证 if i.receiver_id == u.id: Info.delete(i) key = 'user_id_{}.received_info'.format(u.id) data_cache.delete(key) return redirect(url_for('.info')) return abort(404)
def inform_at(topic, checked_content, caller): # topic:@ 发生的topic # check_content:需要检查 @ 发生的文本 # caller:发送 @ 的用户 # 检查当前topic中的正文或评论中是否有用户使用@功能,有的话则发站内信提醒被@的用户。 called_users = at_users(checked_content) with data_cache.pipeline(transaction=False) as pipe: for called_user in called_users: Info.send(title='{}@了你,快去看看吧。'.format(caller.username), content='用户{}@了你,点击{}/topic/{}查看。'.format( caller.username, web_domain_name, topic.id), receiver_id=called_user.id) key = 'user_id_{}.received_info'.format(called_user.id) pipe.delete(key) pipe.execute()
def sweep(): """清理所有已读信息""" u = current_user() owner_id = int(request.form.get('owner_id', -1)) owner = cached_user_id2user(owner_id) if owner is not None: if owner_id == u.id: read_infos = Info.all(receiver_id=owner_id, been_read=True) with data_cache.pipeline(transaction=False) as pipe: for i in read_infos: Info.delete(i) key = 'user_id_{}.received_info'.format(u.id) pipe.delete(key) pipe.execute() return redirect(url_for('.info'))
def user_info(weixin_id): info = Info.get_by_weixin(weixin_id) if not info: abort(404) if not info.height or not info.birthday: return redirect(url_for('main.base_info')) return tpl("user_info.html", info=info)
def download(): si = StringIO.StringIO() cw = unicodecsv.writer(si) cw.writerow([u"Name", u"出生年月", u"性别", u"身高", u"体重", u"颈围", u"肩宽", u"臂长", u"臂围", u"胸围", u"腰围", u"臀围", u"腿长", u"大腿围"]) for info in Info.all(): cw.writerow(info.str_infos) output = make_response(si.getvalue()) output.headers["Content-Disposition"] = "attachment; filename=befit.csv" output.headers["Content-type"] = "text/csv" return output
def cached_info_id2info(info_id): """ 根据Info id 返回 Info 对象。 如果缓存命中,则从缓存中拉取包含Info信息的字典,使用get_model得到Info对象。 如果缓存穿透,则从数据库查询,拿到数据后将数据序列化后存储到redis。 """ key = 'info_id_{}.info'.format(info_id) try: # 拿到json 格式的数据 v = data_cache[key] except KeyError: # 如果没有缓存 info = Info.one(id=info_id) v = json.dumps(info.json()) data_cache.set(key, v, 3600) log('缓存丢失,向数据库拉取数据,重建缓存') return info else: # json序列化为dict,dict生成Info对象 d = json.loads(v) info = Info.get_model(d) log('缓存命中,直接使用') return info
def request_user(): if current_user and current_user.is_authenticated(): g.info = current_user else: if request.path.startswith(u'/m/u/') or request.path.startswith(u'/static/') or request.path.startswith(u'/admin'): pass else: code = request.values.get('code') if code: from models.info import Info from libs.weixin import get_weixin_user_openid openid = get_weixin_user_openid(code) if openid: info = Info.get_by_weixin(openid) if not info: info = Info.add(openid) login_user(info) g.info = info else: return u"微信登录失败啦" else: from libs.weixin import get_weixin_login_url login_url = get_weixin_login_url(request.url) return redirect(login_url)
def detail(info_id): """查看系统通知详情""" u = current_user() i: Info = Info.one(id=info_id) if i is None or u.id != i.receiver_id: return abort(404) if not i.been_read: i.been_read = True i.save() key = 'info_id_{}.info'.format(i.id) data_cache.delete(key) token = new_csrf_token() return render_template("info/info_detail.html", user=u, info=i, token=token)
def set_read(): """一键标记所有信息为已读""" u = current_user() owner_id = int(request.form.get('owner_id', -1)) owner = cached_user_id2user(owner_id) if owner is not None: if owner_id == u.id: unread_info = Info.all(receiver_id=owner_id, been_read=False) with data_cache.pipeline(transaction=False) as pipe: for i in unread_info: i.been_read = True i.save() key = 'info_id_{}.info'.format(i.id) pipe.delete(key) pipe.execute() return redirect(url_for('.info'))
def cached_received_info(user_id): """缓存该用户收到的所有系统消息的id""" key = 'user_id_{}.received_info'.format(user_id) try: # 缓存命中 info_ids_json = data_cache[key] except KeyError: # 缓存未命中,数据库中拉取数据model info_models = list(Info.all(receiver_id=user_id)) info_models.sort(key=lambda x: x.created_time, reverse=True) # 加到redis缓存中的是id data_cache.set(key, json.dumps([i.id for i in info_models]), 3600) log('缓存丢失,向数据库拉取数据,重建缓存') return info_models else: # 从缓存中拿到多个Info id info_models = [ cached_info_id2info(info_id) for info_id in json.loads(info_ids_json) ] log('缓存命中,直接使用') return info_models
def load_user(userid): from models.info import Info return Info.get_by_weixin(userid) or None
def index(): return tpl("admin.html", infos=Info.all())
smart_bot_database_name, k)) curs.execute(tables[k]) except mc.Error as err: print('smart-bot: "FAILED - {}"'.format(err)) drop_database(curs, smart_bot_database_name) print('smart-bot: "init - cancelled"') break else: print('smart-bot: "OK - all tables were created successfully"') print('smart-bot: "adding basic information about the bot"') try: info = Info() info._conn = conn info.add_info(bot_info) except mc.Error as err: print('smart-bot: "FAILED - {}"'.format(err)) else: print('smart-bot: "OK - basic information was added"') finally: curs.close() finally: conn.close()