def post_login(self, userid, password, expiration): """登录""" try: user = db.get(userid) except NotFound: abort(403, "User Not Exists") if not check_password_hash(user["pwdhash"], password): abort(403, "Password Incorrect") auth_exp = expiration * 60 return user, auth.gen_header({"userid": userid}, auth_exp=auth_exp)
def post(self, article, content): """发表评论""" try: db.get(article) except NotFound: abort(400, "Article Not Found") date = util.now() _id = '.'.join([article, date, g.userid]) cmt = { "_id": _id, "type": "comment", "article": article, "date": date, "content": content, "userid": g.userid } db.put(cmt) cmt['user'] = g.user return cmt
def post_reset(self, token, password): """重置密码""" try: token = decode_token(token) except InvalidTokenError as ex: abort(400, str(ex)) if token["type"] != "reset": abort(400, "Token Type Incorrect") user = db.get(token["userid"]) if user is None: abort(400, "User Not Exists") user["pwdhash"] = gen_pwdhash(password) db.put(user) return "OK"
def post(self, catalog, article, **info): """创建或修改文章""" userid = g.user['_id'] _id = '.'.join([userid, catalog, article]) try: origin = db.get(_id) except NotFound: origin = { 'type': 'article', '_id': _id, 'userid': userid, 'catalog': catalog, 'article': article } origin.update(info) db.put(origin) return origin
def get(self, userid): """获取用户信息""" user = db.get(userid) return user
def get(self, userid, catalog, article): """获取一篇文章""" key = ".".join([userid, catalog, article]) result = db.get(key) return result