from flask import render_template, request, redirect, url_for, flash, current_app, jsonify, send_from_directory
from flask_login import login_user, logout_user, login_required, current_user
from . import admin
from app.extensions import db,app_helper
from .forms import AddAdminForm, LoginForm, AddUserForm, DeleteUserForm, EditUserForm, ArticleForm, \
ChangePasswordForm, AddFolderForm, CategoryForm, RecommendForm, InvitcodeForm, OnlineToolForm
from app.models import User, Category, Tag, Article, Recommend, AccessLog, Picture, InvitationCode, \
OnlineTool
import os
from datetime import datetime
from app.util import admin_required, author_required, isAjax, upload_file_qiniu, allowed_file, \
baidu_push_urls, strip_tags, gen_invit_code
from app.settings import config
@admin.route('/', methods=['GET', 'POST'])
@login_required
@admin_required
def index():
current_user.ping()
return render_template('admin/index.html')
@admin.route('/login', methods=['GET', 'POST'])
def login():
login_form = LoginForm(prefix='login')
user = User.query.filter_by(status=True).first()
if not user:
add_admin_form = AddAdminForm(prefix='add_admin')
if add_admin_form.validate_on_submit():
u = User(username=add_admin_form.username.data.strip(),
email=add_admin_form.email.data.strip(),
password=add_admin_form.password.data.strip(),
status=True, role=True
)
db.session.add(u)
db.session.commit()
login_user(user=u)
return redirect(url_for('admin.index'))
return render_template('admin/add_admin.html', addAdminForm=add_admin_form)
else:
if login_form.validate_on_submit():
u = User.query.filter_by(username=login_form.username.data.strip()).first()
if u is None:
flash({'error': '帐号未注册!'})
elif u is not None and u.verify_password(login_form.password.data.strip()) and u.status:
login_user(user=u, remember=login_form.remember_me.data)
return redirect(url_for('admin.index'))
elif not u.status:
flash({'error': '用户已被管理员注销!'})
elif not u.verify_password(login_form.password.data.strip()):
flash({'error': '密码不正确!'})
return render_template('admin/login.html', loginForm=login_form)
@admin.route('/logout')
@login_required
def logout():
"""
退出系统
"""
logout_user()
return redirect(url_for('main.index'))
@admin.route('/articles', methods=['GET'])
@login_required
@admin_required
def articles():
title = request.args.get('title','')
page = request.args.get('page', 1, type=int)
articles = Article.query.filter(
Article.title.like("%" + title + "%") if title is not None else ''
).order_by(Article.timestamp.desc()).paginate(page, per_page=current_app.config['H3BLOG_POST_PER_PAGE'], error_out=False)
return render_template('admin/articles.html', articles=articles,title = title)
@admin.route('/article/edit/<id>', methods=['GET'])
@login_required
@admin_required
def article_edit(id):
article = Article.query.get(int(id))
form = ArticleForm(obj=article)
form.tags.data = ','.join([t.name for t in article.tags.all()])
return render_template('admin/write.html', form=form)
@admin.route('/article/write', methods=['GET','POST'])
@login_required
@admin_required
def write():
form = ArticleForm()
if form.validate_on_submit():
# --------以下功能是增加文章的分类
cty = Category.query.get(int(form.category_id.data))
a = None
if form.id.data:
a = Article.query.get(int(form.id.data))
if a :
a.title = form.title.data.strip()
a.content = form.content.data
a.content_html = a.content_to_html()
a.summary = form.summary.data
a.thumbnail = form.thumbnail.data
a.category = cty
a.name = form.name.data.strip()
a.state = form.state.data
a.timestamp = form.timestamp.data
if not a.name and len(a.name) == 0 :
a.name = a.id
db.session.commit()
else:
# --------以下功能是将文章信息插入数据库
a = Article(title=form.title.data.strip(), content=form.content.data,
thumbnail = form.thumbnail.data,name = form.name.data.strip(),
state = form.state.data,summary = form.summary.data,
category=cty, author=current_user._get_current_object())
a.content_html = a.content_to_html()
db.session.add(a)
db.session.commit()
if not a.name and len(a.name) == 0 :
a.name = a.id
db.session.commit()
# --------以下功能是将文章标识插入数据库
a.tags = []
for tg in form.tags.data.split(','):
if tg.strip() == '':
continue
t = Tag.query.filter_by(name=tg.strip()).first()
if not t:
t = Tag(name=tg.strip())
db.session.add(t)
if t not in a.tags :
a.tags.append(t)
if isAjax() :
msg = '发布成功' if int(form.state.data) == 1 else '保存成功'
return jsonify({'code':1,'msg':msg,'id':a.id})
return render_template('admin/write.html', form=form)
@admin.route('/users', methods=['GET', 'POST'])
@login_required
@admin_required
def users():
add_user_form = AddUserForm(prefix='add_user')
delete_user_form = DeleteUserForm(prefix='delete_user')
if add_user_form.validate_on_submit():
if add_user_form.role.data == 'True':
role = True
else:
role = False
if add_user_form.status.data == 'True':
status = True
else:
status = False
u = User(username=add_user_form.username.data.strip(), email=add_user_form.email.data.strip(),
role=role, status=status, password='123456')
db.session.add(u)
flash({'success': '添加用户<%s>成功!' % add_user_form.username.data.strip()})
if delete_user_form.validate_on_submit():
u = User.query.get_or_404(int(delete_user_form.user_id.data.strip()))
db.session.delete(u)
flash({'success': '删除用户<%s>成功!' % u.username})
users = User.query.all()
return render_template('admin/users.html', users=users, addUserForm=add_user_form,
deleteUserForm=delete_user_form)
@admin.route('/user-edit/<user_id>', methods=['GET', 'POST'])
@login_required
@admin_required
def user_edit(user_id):
user = User.query.get_or_404(user_id)
edit_user_form = EditUserForm(prefix='edit_user', obj=user)
if edit_user_form.validate_on_submit():
user.username = edit_user_form.username.data.strip()
user.email = edit_user_form.email.data.strip()
if edit_user_form.role.data == 'True':
user.role = True
else:
user.role = False
if edit_user_form.status.data == 'True':
user.status = True
else:
user.status = False
flash({'success': '用户资料已修改成功!'})
return render_template('admin/edit_user.html', editUserForm=edit_user_form, user=user)
@admin.route('/password', methods=['GET', 'POST'])
@login_required
@admin_required
def password():
change_password_form = ChangePasswordForm(prefix='change_password')
if change_password_form.validate_on_submit():
if current_user.verify_password(change_password_form.old_password.data.strip()):
current_user.password = change_password_form.password.data.strip()
# db.session.add(current_user)
db.session.commit(
天天501
- 粉丝: 624
- 资源: 5906
最新资源
- LCL单相并网逆变器PI双闭环有源阻尼仿真【附参考文献及说明文档】 1)采用直接电流控制; 2)采用电流双闭环控制; 3)外环采用并网电流,PI控制; 4)内环采用电容电流反馈,PI控制,有效抑制系统
- 基于ssm的物流信息管理系统源码(java毕业设计完整源码+LW).zip
- Anaconda介绍、安装及使用教程PDF
- 880b5debf99dd9c84cd01cc4fc0ffaf9.zip
- 基于ssm的图书管理系统设计与实现源码(java毕业设计完整源码+LW).zip
- 相场法水力压裂,共四个模型,comsol6.0版本及以上,附赠参考文献,标价即卖价,不不 的噢 模型一:对称三簇压裂;模型二:水力裂缝与天然裂缝相交;模型三:单水平裂缝扩展;模型四:水平裂缝偏转
- 10.风格化调色.rar
- Notepad++V8.6.7-全部9个版本.rar
- 基于ssm的文学网的设计与实现源码(java毕业设计完整源码+LW).zip
- Linux下操作USB camera的示例代码, C/C++
- 基于ssm的猎头公司管理系统源码(java毕业设计完整源码+LW).zip
- comsol光学仿真 光子晶体光纤 lunwen复现 基于SPR的光纤传感器 光子晶体光纤偏振分束器 石墨烯-黑磷增强SPR等离子 体谐振传感 光子晶体光纤仿真 模式分析 计算等效折射率,限制损耗,模
- 《javascript》-JavaScript基本语法总结PDF
- 《运筹学与最优化方法 第3版》-(习题解答)
- 基于ssm的企业人事管理系统源码(java毕业设计完整源码+LW).zip
- 基于CNN的自适应量化参数机制视频编码滤波器研究
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈