Flask学习-设备管理系统4:修改设备信息
本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明.
环境
- 主机:win10
- python版本:python3.4
- 开发环境:PyCharm5.0.2
说明
可以修改设备信息,不同部门的权限不一样,所以修改的参数不一样
效果图
源代码
视图文件修改 view.py
@login_required
@main.route('/edit_device/<device_id>', methods=['GET', 'POST'])
def edit_device(device_id):
device = Device.query.filter_by(device_id=device_id).first()
# 判断是否是管理员
if current_user.is_administrator():
form = DeviceFormAdminEdit()
# 判断是否提交
if form.validate_on_submit():
temp = list(request.form)
# 判断是否点击的是删除键
if temp.count('delete') > 0:
db.session.delete(device)
else:
device.device_type = form.device_type.data
device.device_simid = form.device_simid.data
device.hard_version = form.hard_version.data
device.soft_version = form.soft_version.data
device.warehouse = form.warehouse.data
device.shipment_time = form.shipment_time.data
device.agent = form.agent.data
device.prison = form.prison.data
device.shutdown = form.shutdown.data
db.session.add(device)
return redirect(url_for('.index'))
# 默认显示
form.device_type.data = device.device_type
form.device_simid.data = device.device_simid
form.hard_version.data = device.hard_version
form.soft_version.data = device.soft_version
form.warehouse.data = device.warehouse
form.shipment_time.data = device.shipment_time
form.agent.data = device.agent
form.prison.data = device.prison
form.shutdown.data = device.shutdown
return render_template('edit_device.html', form=form, device=device)
# 判断是否是生产部
if current_user.can(Permission.PRODUCTION):
form = DeviceFormProductionEdit()
# 判断是否提交
if form.validate_on_submit():
temp = list(request.form)
# 判断是否点击的是删除键
if temp.count('delete') > 0:
db.session.delete(device)
else:
device.device_type = form.device_type.data
device.device_simid = form.device_simid.data
device.hard_version = form.hard_version.data
device.soft_version = form.soft_version.data
device.shutdown = form.shutdown.data
db.session.add(device)
return redirect(url_for('.index'))
# 默认显示
form.device_type.data = device.device_type
form.device_simid.data = device.device_simid
form.hard_version.data = device.hard_version
form.soft_version.data = device.soft_version
form.shutdown.data = device.shutdown
return render_template('edit_device.html', form=form, device=device)
# 判断是否是库房
if current_user.can(Permission.WAREHOUSE):
form = DeviceFormWarehouseEdit()
# 判断是否提交
if form.validate_on_submit():
device.warehouse = form.warehouse.data
device.shipment_time = form.shipment_time.data
db.session.add(device)
return redirect(url_for('.index'))
# 默认显示
form.warehouse.data = device.warehouse
form.shipment_time.data = device.shipment_time
return render_template('edit_device.html', form=form, device=device)
# 判断是否是市场部
if current_user.can(Permission.MARKETING):
form = DeviceFormMarketingEdit()
# 判断是否提交
if form.validate_on_submit():
device.agent = form.agent.data
db.session.add(device)
return redirect(url_for('.index'))
# 默认显示
form.agent.data = device.agent
return render_template('edit_device.html', form=form, device=device)
# 判断是否是技术支持部
if current_user.can(Permission.SUPPORT):
form = DeviceFormSupportEdit()
# 判断是否提交
if form.validate_on_submit():
device.prison = form.prison.data
device.shutdown = form.shutdown.data
db.session.add(device)
return redirect(url_for('.index'))
# 默认显示
form.prison.data = device.prison
form.shutdown.data = device.shutdown
return render_template('edit_device.html', form=form, device=device)
新建网页文件 edit_device.html
{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% block title %}修改设备信息{% endblock %}
{% block page_content %}
<div class="page-header">
<h1>修改设备信息</h1>
</div>
<div class="page-header">
<h5>设备id:{{ device.device_id}}</h5>
{# {% if current_user.can(Permission.ADMINISTER) %}#}
{# <h5>设备id:{{ device.device_id}}</h5>#}
{# {% endif %}#}
</div>
<div class="col-md-4">
{{ wtf.quick_fo>
</div>
{% endblock %}