def post(self): ''' 创建一个新文章''' args = parse_article_args() article = article_factory(args['path']) article.meta = args['meta'] article.text = args['text'] article.save() app.logger.debug("写入文件 {} 成功!".format(article.path)) return {'msg': '写入成功'}, 201, {'Location': args['location']}
def patch(self, path): ''' 部分更新''' args = request.json config = Config() path = urllib.parse.unquote(path) # full_path = os.path.join(config.path or '', path) article = article_factory(path) meta = args.get('meta', {}) for k, v in meta.items(): article.update_meta(k, v) if args.get('text', None): article.text = text article.save() return {'msg': 'update success.'}
def get(self): config = Config() paths = [config.path] if config.article_paths: for path in config.article_paths: paths = list(map(lambda p: os.path.join(p, path), paths)) articles = [] for path in paths: for full_path in iterdir(path): article = article_factory(full_path) if article and article.meta.get('title', None): data = { 'meta': article.meta, 'path': article.path, 'text': article.text, } # print(data) yield data
def edit(): form = ArticleForm() path = request.args.get('path') if path is None: abort(405) article = article_factory(path) if request.method == 'POST': if form.validate_on_submit(): data = copy.deepcopy(form.data) if 'csrf_token' in data: data.pop('csrf_token') for k, v in data.items(): article.update_meta(k, v) article.save() flash("保存成功!") else: flash("保存失败。") # return redirect(url_for('admin.index')) else: return render_template('article/edit.html', form=form, article=article)
def put(self, path): ''' 更新指定的文章 目录不存在就抛出异常 ''' args = request.json config = Config() text = args.get('text', '') path = urllib.parse.unquote(path) article = article_factory(path) if article.exists() is not True: return {'error_msg': '文章不存在'} meta = args.get('meta', {}) # 更新修改时间 if 'modified' not in meta: meta['modified'] = datetime.datetime.now().strftime( config.date_format) article.meta = meta article.text = args.get('text', '') print(article.meta) article.save() return {'msg': '更新成功!'}
def get(self, path): config = Config() # print(path) # full_path = os.path.join(config.path or '', path) # article = article_factory(full_path) article = article_factory(path) if article is None: return {'err_code': 101, 'msg': '此格式暂不支持'} if os.path.exists(article.full_path): data = { 'meta': article.meta, 'path': article.path, 'text': article.text } return data else: return { 'err_code': 100, 'msg': '此文章不存在', 'full_path': article.full_path }
def article(request): path = request.param article = article_factory(path) if article: return article