def edit_column(id): column = Column.query.get_or_404(id) form = ColumnForm() if form.validate_on_submit(): column.title = form.column.data column.timestamp = form.date.data column.url_name = form.url_name.data column.body = form.body.data password = form.password.data if password: column.password = password db.session.add(column) db.session.commit() flash('Update successfully.') cache_tool.clean('column_' + column.url_name) return redirect(url_for('admin.admin_column', id=column.id)) form.column.data = column.title form.date.data = column.timestamp form.url_name.data = column.url_name form.body.data = column.body return render_template('admin_column/edit_column.html', form=form, title='更新专题', column=column)
def write(): form = AdminWrite() if form.validate_on_submit(): exist = Post.query.filter_by(url_name=form.url_name.data).first() if exist: flash('Duplicate articles.') raise DuplicateEntryException('Duplicate articles.') # 保存草稿 if 'save_draft' in request.form and form.validate(): post = save_post(form, True) db.session.add(post) db.session.commit() flash('Saved successfully.') # 发布文章 elif 'submit' in request.form and form.validate(): post = save_post(form) db.session.add(post) db.session.commit() flash('Posted successfully.') # updata cache cache_tool.clean(cache_tool.GLOBAL_KEY) update_linked_cache(post) return redirect(url_for('admin.write')) return render_template('admin/edit_post.html', form=form, title='写文章')
def edit_page(name): page = Page.query.filter_by(url_name=name).first() start_title = page.title form = AddPageForm() if form.validate_on_submit(): page.title = form.title.data page.body = form.body.data page.able_comment = form.can_comment.data page.show_nav = form.is_nav.data page.url_name = form.url_name.data db.session.add(page) db.session.commit() flash('Update successfully.') # 清除缓存 cache_tool.clean(cache_tool.GLOBAL_KEY) return redirect(url_for('admin.edit_page', name=page.url_name)) form.title.data = start_title form.body.data = page.body form.can_comment.data = page.able_comment form.is_nav.data = page.show_nav form.url_name.data = page.url_name return render_template('admin/edit_page.html', title="编辑页面", form=form, page=page)
def edit_column_article(url, id): column = Column.query.filter_by(url_name=url).first() article = Article.query.filter_by(id=id).first() # _title = article.title form = ColumnArticleForm() if form.validate_on_submit(): article.title = form.title.data article.timestamp = form.date.data article.secrecy = form.secrecy.data article.body = form.body.data db.session.add(article) db.session.commit() flash('Update successfully.') # clear cache cache_tool.clean('column_' + url) return redirect(url_for('admin.admin_column', id=column.id)) form.title.data = article.title form.date.data = article.timestamp form.body.data = article.body form.secrecy.data = article.secrecy return render_template('admin_column/edit_article.html', form=form, title='更新文章', column=column, article=article)
def delete_side_box(id): box = SideBox.query.get_or_404(id) db.session.delete(box) db.session.commit() flash('Deleted successfully.') # 清除缓存 cache_tool.clean(cache_tool.GLOBAL_KEY) return redirect(url_for('admin.admin_side_box'))
def delete_column_article(url, id): column = Column.query.filter_by(url_name=url).first() article = Article.query.filter_by(id=id).first() db.session.delete(article) db.session.commit() flash('Deleted successfully.') # 清除对于缓存 cache_tool.clean('column_' + url) return redirect(url_for('admin.admin_column', id=column.id))
def delete_page(name): page = Page.query.filter_by(title=name).first() db.session.delete(page) db.session.commit() flash('Deleted successfully.') if page.isNav is True: # 清除缓存 cache_tool.clean(cache_tool.GLOBAL_KEY) return redirect(url_for('admin.admin_pages'))
def unable_side_box(id): box = SideBox.query.get_or_404(id) box.unable = not box.unable db.session.add(box) db.session.commit() # 清除缓存 cache_tool.clean(cache_tool.GLOBAL_KEY) return redirect(url_for('admin.admin_side_box'))
def great_link(id): link = SiteLink.query.get_or_404(id) link.is_great = not link.is_great db.session.add(link) db.session.commit() # 清除缓存 cache_tool.clean(cache_tool.GLOBAL_KEY) return redirect(url_for('admin.admin_links'))
def delete_column(id): column = Column.query.get_or_404(id) articles = column.articles.order_by(Article.timestamp.desc()).all() db.session.delete(column) db.session.commit() flash('Deleted successfully.') # clean all of this column cache cache_tool.clean('column_' + column.url_name) return redirect(url_for('admin.admin_columns'))
def update_linked_cache(cur): """在新文章更新后,清掉最近一篇文章的缓存""" posts = Post.query.filter_by(draft=False).order_by(Post.timestamp.desc()).all() idx = posts.index(cur) prev_post = posts[idx-1] if idx > 0 else None next_post = posts[idx+1] if idx < len(posts)-1 else None if prev_post: cache_tool.clean('_'.join(map(str, ['post', prev_post.year, prev_post.month, prev_post.url_name]))) if next_post: cache_tool.clean('_'.join(map(str, ['post', next_post.year, next_post.month, next_post.url_name]))) return True
def delete_link(id): link = SiteLink.query.get_or_404(id) db.session.delete(link) db.session.commit() # update cache if link.is_friend: cache_tool.update_global(global_cache_key.FRIEND_COUNT, 1, cache_tool.ADD) else: cache_tool.clean(cache_tool.GLOBAL_KEY) return redirect(url_for('admin.admin_links'))
def admin_edit(id): post = Post.query.get_or_404(id) form = AdminWrite() if form.validate_on_submit(): category = post.category if form.category.data != post.category.category: category = save_category(post.category.category, form.category.data) if not category.is_show: category.is_show = True post.category = category post.tags = form.tags.data post.url_name = form.url_name.data post.timestamp = form.time.data post.title = form.title.data post.body = form.body.data # 编辑草稿 if post.draft: if 'save_draft' in request.form and form.validate(): db.session.add(post) flash('Saved successfully.') elif 'submit' in request.form and form.validate(): post.draft = False db.session.add(post) db.session.commit() flash('Posted successfully.') # 清除缓存 cache_tool.clean(cache_tool.GLOBAL_KEY) update_linked_cache(post) save_xml(post.timestamp) else: db.session.add(post) db.session.commit() flash('Update successfully.') # 清除对应文章缓存 key = '_'.join( map(str, ['post', post.year, post.month, post.url_name])) cache_tool.clean(key) save_xml(post.timestamp) return redirect(url_for('admin.admin_edit', id=post.id)) form.category.data = post.category.category form.tags.data = post.tags form.url_name.data = post.url_name form.time.data = post.timestamp form.title.data = post.title form.body.data = post.body return render_template('admin/edit_post.html', form=form, post=post, title='编辑文章')
def add_side_box(): form = SideBoxForm() if form.validate_on_submit(): is_advertising = form.is_advertising.data box = SideBox(title=form.title.data, body=form.body.data, is_advertising=is_advertising) db.session.add(box) db.session.commit() flash('Added successfully.') # update cache cache_tool.clean(cache_tool.GLOBAL_KEY) return redirect(url_for('admin.admin_side_box')) return render_template('admin/edit_sidebox.html', form=form, title='添加插件')
def add_page(): form = AddPageForm() if form.validate_on_submit(): page = Page(title=form.title.data, url_name=form.url_name.data, body=form.body.data, able_comment=form.can_comment.data, show_nav=form.is_nav.data) # print(f'form: {form}') db.session.add(page) db.session.commit() flash('Posted successfully.') if page.show_nav is True: # 清除缓存 cache_tool.clean(cache_tool.GLOBAL_KEY) return redirect(url_for('admin.add_page')) return render_template('admin/edit_page.html', form=form, title='添加页面')
def save_category(old_category, new_category=None, is_show=True): # 是否需要删除旧的分类 if new_category: category = Category.query.filter_by(category=old_category).first() if category.posts.count() == 1: db.session.delete(category) db.session.commit() # 更新分类缓存 cache_tool.clean(cache_tool.GLOBAL_KEY) old_category = new_category # 是否新的分类需要添加 category = Category.query.filter_by(category=old_category).first() if not category: category = Category(category=old_category, is_show=is_show) db.session.add(category) db.session.commit() return category
def write_column_article(url): column = Column.query.filter_by(url_name=url).first() form = ColumnArticleForm() if form.validate_on_submit(): article = Article(title=form.title.data, timestamp=form.date.data, body=form.body.data, secrecy=form.secrecy.data, column=column) db.session.add(article) db.session.commit() flash('Added successfully.') # clean cache cache_tool.clean('column_' + url) return redirect(url_for('admin.admin_column', id=column.id)) return render_template('admin_column/edit_article.html', form=form, title='编辑文章', column=column)
def add_link(): form = SocialLinkForm() fr_form = FriendLinkForm() # 社交链接 if form.submit.data and form.validate_on_submit(): exist_link = SiteLink.query.filter_by(link=form.link.data).first() if exist_link: flash('Link already exists...') return redirect(url_for('admin.add_link')) url = form.link.data name = form.name.data link = SiteLink(link=url, name=name, is_friend=False) db.session.add(link) db.session.commit() flash('Added successfully.') # update cache cache_tool.clean(cache_tool.GLOBAL_KEY) return redirect(url_for('admin.add_link')) # 友链 if fr_form.submit2.data and fr_form.validate_on_submit(): exist_link = SiteLink.query.filter_by(link=fr_form.link.data).first() if exist_link: flash('Link already exists...') return redirect(url_for('admin.add_link')) link = SiteLink(link=fr_form.link.data, name=fr_form.name.data, info=fr_form.info.data, is_friend=True) db.session.add(link) db.session.commit() flash('Added successfully.') # update cache cache_tool.update_global(global_cache_key.FRIEND_COUNT, 1, cache_tool.ADD) return redirect(url_for('admin.add_link')) return render_template('admin/edit_link.html', title="站点链接", form=form, fr_form=fr_form)
def edit_side_box(id): box = SideBox.query.get_or_404(id) form = SideBoxForm() if form.validate_on_submit(): box.title = form.title.data box.body = form.body.data box.is_advertising = form.is_advertising.data db.session.add(box) db.session.commit() flash('Update successfully.') # update cache cache_tool.clean(cache_tool.GLOBAL_KEY) return redirect(url_for('admin.admin_side_box')) form.title.data = box.title form.body.data = box.body form.is_advertising.data = box.is_advertising return render_template('admin/edit_sidebox.html', form=form, title='更新插件', box=box)
def set_site(): form = AdminSiteForm() user = Admin.query.all()[0] if form.validate_on_submit(): user.name = form.username.data user.profile = form.profile.data user.site_name = form.site_name.data user.site_title = form.site_title.data user.record_info = form.record_info.data or None db.session.add(user) db.session.commit() flash('Set successfully.') # 清除所有缓存 cache_tool.clean(cache_tool.GLOBAL_KEY) return redirect(url_for('admin.index')) form.username.data = user.name form.profile.data = user.profile form.site_name.data = user.site_name form.site_title.data = user.site_title form.record_info.data = user.record_info or None return render_template('admin/profile.html', title='设置网站信息', form=form)
def clean_all_cache(): cache_tool.clean(cache_tool.ALL_KEY) flash('Clean all cache success!') return redirect(url_for('admin.index'))