def post_update(self, local=False, rebuild=False): """强制更新数据库数据 """ user_id = unicode(request.me["id"]) with db_session: u = model.BlogUser.get(user_id=user_id) if not u: abort(404) repo_url = u.article_repo old_commit = u.latest_commit if rebuild: clear_db(user_id) old_commit = "" msg = "local" err = None if not local: __, owner, __ = gitutil.parse_giturl(repo_url) err, ret = update_local(repo_url, owner) msg = err.message if err else ret if not err: mddir = get_mddir(repo_url) update_db(mddir, user_id, old_commit) logs = gitutil.git_log(mddir) if logs: latest_commit = logs[0][0] with db_session: u = model.BlogUser.get(user_id=user_id) u.latest_commit = latest_commit return {"message": msg, "success": bool(not err)}
def get(self, git_username, subdir, filename): """获取一篇文章""" art = get_article() if not art: abort(404) else: return art
def clear_db(user_id): with db_session: u = model.BlogUser.get(user_id=unicode(user_id)) if not u: abort(404) for m in u.article_metas: m.article.delete() u.latest_commit = ""
def post_login(self, username, password): """登录""" with db_session: u = model.User.get(username=username) if _login_auth(u, password): me = {"id": u.id} header = api.gen_auth_header(me, auth_exp=3600) return _out_user(u), header abort(403)
def post_fogot_password(self, email): """忘记密码/申请重新设置密码""" with db_session: u = model.UserInfo.get(email=email) if u: token = "id+exp+modify_date+hash" # 发送邮件 return {"message": "重置密码链接已发送到您的邮箱,请查看邮将"} abort(400)
def send_mail(to, subject, html): """Send a html content mail""" logger.info("Send Mail To %s:\n" % to + html) msg = Message(subject, recipients=[to]) msg.html = html try: mail.send(msg) except Exception as ex: logger.exception(ex) abort(500, "邮件发送失败: %s" % str(ex))
def post_reset_password(self, token, password): """重新设置密码""" id, exp, modify_date = token with db_session: u = model.User.get(id=id) if u and modify_date == u.modify_date: pwd = u.hashsalt + password u.password = pwd return {"message": url_for("api.user@login")} abort(400)
def _init_db(mddir, user_id): with db_session: u = model.BlogUser.get(user_id=user_id) if not u: abort(404) for content, toc, meta in read_articles(mddir): m_tags = _get_mtags(meta) m_meta = model.ArticleMeta(**dict(meta, tags=m_tags, bloguser=u)) m_article = model.Article( content=content, toc=toc, meta=m_meta)
def delete(self, password): """删除此账号""" id = request.me["id"] with db_session: u = model.User.get(id=id) if u: self.post_login(u.username, password) u.delete() return {"message": "success"} else: abort(404)
def put_password(self, password, new_password): """修改密码""" id = request.me["id"] with db_session: u = model.User.get(id=id) if _login_auth(u, password): pwd = u.hashsalt + new_password u.password = pwd header = {api.auth_header: ""} return {"message": "success"}, header abort(403)
def get_server_address(self): """ 获取服务器地址 $output: url?url: 服务器地址 """ if self.server_address is None: abort(500, "Can't get LAN IP") else: return {"url": self.server_address}
def create_or_update_bloguser(user_id, role, article_repo, website): website = website or "" try: __, git_username, __ = gitutil.parse_giturl(article_repo) except: abort(400, "invalid article_repo: %s" % article_repo) user_id = unicode(user_id) config = dict(role=role, article_repo=article_repo, git_username=git_username, website=website, user_system="kkblog", user_id=user_id) with db_session: u = model.BlogUser.get(user_id=user_id) if not u: u = model.BlogUser(date_create=datetime.now(), **config) else: u.set(**config) return _out_user(u)
def post_register(self, email, password, role="user.normal"): """注册,邮箱作为用户名""" if role != "user.normal": abort_if_not_admin("role can't be %s" % role) with db_session: u = model.User.get(username=email) if u: abort(400, "%s已注册" % email) hashsalt = "hashsalt" date_modify = datetime.now() pwd = hashsalt + password info = model.UserInfo(date_create=date_modify, email=email) u = model.User( username=email, password=pwd, role=role, date_modify=date_modify, hashsalt=hashsalt, info=info ) return _out_user(u)
def update_db(mddir, user_id, old_commit=None): # old_commit='' or None, initdb if not old_commit: _init_db(mddir, user_id) return diff = gitutil.modified_files(mddir, old_commit) def parse_path(path): fdir, fname = os.path.split(path) subdir = os.path.basename(fdir) return subdir, fname # delete files deleted_files = [parse_path(path) for status, path in diff if status == "D"] for subdir, filename in deleted_files: with db_session: meta = model.ArticleMeta.get(subdir=subdir, filename=filename) if meta: meta.article.delete() # update files with db_session: u = model.BlogUser.get(user_id=user_id) if not u: abort(404) for content, toc, meta in read_modified_articles(mddir, diff): m_tags = _get_mtags(meta) m_meta = model.ArticleMeta.get(subdir=subdir, filename=filename) if m_meta: m_meta.set(**dict(meta, tags=m_tags)) m_meta.article.set(content=content, toc=toc) else: m_meta = model.ArticleMeta(**dict(meta, tags=m_tags, bloguser=u)) m_article = model.Article( content=content, toc=toc, meta=m_meta)
def post(self): """ 上传文件 $output: - received files - name?str: filename saved?bool: the file saved or not $error: 400.NoFile: 未收到文件 """ files = list(request.files.values()) if not files: abort(400, "NoFile", "未收到文件") result = [] for f in files: filename = secure_filename(f.filename) try: f.save(join(self.shared_folder, filename)) result.append({"name": filename, "saved": True}) except Exception as ex: current_app.logger.exception(ex) result.append({"name": filename, "saved": False}) return result
def _get_user(user_id): with db_session: u = model.BlogUser.get(user_id=unicode(user_id)) if not u: abort(404) return _out_user(u)
def get_me(self): """获取用户的个人信息""" id = request.me["id"] u = _get_user(id) return u if u else abort(404)
def get(self, id): """获取用户的公开信息""" u = _get_user(id) return u if u else abort(404)
def get(self, id): if id in todos: return dict(todos[id], id=id) else: abort(404, "Not Found")
def put(self, **info): """修改个人信息""" id = request.me["id"] u = _put_user(id, **info) return u if u else abort(404)
def get_by_id(self, id): with db_session: art = model.Article.get(id=id) if not art: abort(404) return _out_article(art)
def abort_if_not_admin(msg): role = request.me["role"] if role != "user.admin": abort(403, msg)
def put(self, id, **todo): if id in todos: todos[id] = todo return dict(todo, id=id) else: abort(404, "Not Found")