https://wizardforcel.gitbooks.io/flask-extension-docs/content
http://cabeza.cn/blog/2016/02/28/datatable-learning-note-1/
页面
......
<table class="pure-table">
<tr>
<td><i class="fa fa-cog fa-lg"></i> 切换</td>
<td>
<input name="bandwidth" type="radio" id="radio_5" value="5"> 关闭
<input name="bandwidth" type="radio" id="radio_100" value="100"> 开启
</td>
<td>
<button id="submit" class="pure-button button-small pure-button-primary" disabled="disabled">提交</button>
</td>
</tr>
</table>
......
$(function() {
var old_qos;
// gpn当前状态
$.ajax({
type: 'GET',
url: $SCRIPT_ROOT + "{{ url_for('get_gpn_bandwidth') }}",
dataType: 'json',
contentType: 'application/json;charset=utf-8',
success: function(data){
old_qos = data.qos
$('#radio_' + old_qos).attr('checked', 'checked');
},
});
// 提交按钮激活
$('input[name=bandwidth]').change(function(){
if ( $('input[name=bandwidth]:checked').val() == old_qos ) {
$('#submit').attr('disabled', 'disabled');
} else {
$('#submit').removeAttr('disabled');
}
});
// 提交gpn设置
$('#submit').click(function(){
var new_qos = $('input[name=bandwidth]:checked').val();
data = {'old_qos': old_qos, 'new_qos': new_qos};
$.ajax({
type: 'POST',
url: $SCRIPT_ROOT + "{{ url_for('set_gpn_bandwidth') }}",
dataType: 'json',
contentType: 'application/json;charset=utf-8',
data: JSON.stringify(data),
success: function(data){
console.log(data);
window.location.href = "{{ url_for('gpn') }}";
}
});
})
});
后台
@app.route('/gpn/', methods=['GET'])
@login_required
def gpn():
gpns = GPN.query.order_by(GPN.id.desc()).limit(20)
return render_template('gpn.html', gpns=gpns)
@app.route('/json/gpn/bandwidth', methods=['GET'])
@login_required
def get_gpn_bandwidth():
'''
获取gpn当前状态
'''
gpn = GPN.query.order_by(GPN.id.desc()).first()
return json.dumps({'qos': gpn.bandwidth})
@app.route('/json/gpn/bandwidth', methods=['POST'])
@login_required
def set_gpn_bandwidth():
'''
设置gpn带宽
'''
old_qos, new_qos = int(request.json['old_qos']), int(request.json['new_qos'])
url_token = 'http://xxx.com/get_token/'
url_gpn = 'http://xxx.com/gpn/update/'
username = 'xxxx'
password = 'xxxx'
# get token
headers = {'username': username, 'password': password}
req = urllib2.Request(url_token, headers=headers)
resp = urllib2.urlopen(req).read()
token = json.loads(resp)['Access-Token']
# set qos
data = {'qos': new_qos, 'area_id': 'cn'}
headers = {'token': token, 'Content-Type': 'application/json'}
req = urllib2.Request(url_gpn, headers=headers, data=json.dumps(data))
try:
resp = urllib2.urlopen(req).read()
bandwidth = new_qos
status = json.loads(resp)['status']
message = json.loads(resp)['messsage']
except urllib2.HTTPError, e:
bandwidth = old_qos
status = 'failure'
message = e.code
except urllib2.URLError, e:
bandwidth = old_qos
status = 'failure'
message = e.reason
# submit data
gpn = GPN()
gpn.bandwidth = bandwidth
gpn.updated_user = current_user.name
gpn.updated_time = datetime.datetime.now()
gpn.status = status
gpn.message = message
db.session.add(gpn)
db.session.commit()
return json.dumps({'current_qos': bandwidth})