本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明.
具有新建设备权限的是管理员和生产部。其中管理员的新建表单可以填写每个参数,生产部只能填写一些参数。
@login_required
@main.route('/create_device', methods=['GET', 'POST'])
def create_device():
# 判断是否是管理员
if current_user.is_administrator():
form = DeviceFormAdminCreate()
# 判断是否提交
if form.validate_on_submit():
# id格式化
id_format = '0x%04x' % int(form.device_id.data, base=16)
device = Device(device_type=form.device_type.data,
device_id=id_format,
device_simid=form.device_simid.data,
hard_version=form.hard_version.data,
soft_version=form.soft_version.data,
warehouse=form.warehouse.data,
shipment_time=form.shipment_time.data,
agent=form.agent.data,
prison=form.prison.data,
shutdown=form.shutdown.data)
# 判断是否id重复
flag = True
if Device.query.filter_by(device_id=device.device_id).count() > 0:
flash('失败:设备ID已存在')
flag = False
# 判断simid是否重复
elif Device.query.filter_by(device_simid=device.device_simid).count() > 0:
flash('失败:设备SIMID已存在')
flag = False
if flag:
db.session.add(device)
return redirect(url_for('.index'))
# 默认显示
return render_template('create_device.html', form=form)
# 判断是否是生产部
if current_user.can(Permission.PRODUCTION):
form = DeviceFormProductionCreate()
# 判断是否提交
if form.validate_on_submit():
# id格式化
id_format = '0x%04x' % int(form.device_id.data, base=16)
device = Device(device_type=form.device_type.data,
device_id=id_format,
device_simid=form.device_simid.data,
hard_version=form.hard_version.data,
soft_version=form.soft_version.data,
warehouse=False,
shipment_time='无',
agent='无',
prison='无',
shutdown=form.shutdown.data)
# 判断是否id重复
flag = True
if Device.query.filter_by(device_id=device.device_id).count() > 0:
flash('失败:设备ID已存在')
flag = False
# 判断simid是否重复
elif Device.query.filter_by(device_simid=device.device_simid).count() > 0:
flash('失败:设备SIMID已存在')
flag = False
if flag:
db.session.add(device)
return redirect(url_for('.index'))
# 默认显示
return render_template('create_device.html', form=form)
####表单文件修改 forms.py
新增表单
***
class DeviceFormAdminCreate(Form):
)
{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% block title %}外出押解设备管理系统 by jdh{% endblock %}
{% block page_content %}
<div class="page-header">
<h1>新建设备信息h1>
div>
<div class="col-md-4">
{{ wtf.quick_form(form) }}
div>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
{% endblock %}
}