python学习
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from flask import Blueprint
|
||||
from flask import Blueprint,request,jsonify
|
||||
from common.libs.Helper import ops_render
|
||||
from common.models.food.FoodCat import FoodCat
|
||||
from application import app,db
|
||||
from common.libs.Helper import getCurrentDate
|
||||
|
||||
route_food = Blueprint( 'food_page',__name__ )
|
||||
|
||||
@@ -20,8 +23,89 @@ def set():
|
||||
|
||||
@route_food.route( "/cat" )
|
||||
def cat():
|
||||
return ops_render( "food/cat.html" )
|
||||
resp_data = {}
|
||||
req = request.values
|
||||
query = FoodCat.query
|
||||
|
||||
@route_food.route( "/cat-set" )
|
||||
if 'status' in req and int( req['status'] ) > -1:
|
||||
query = query.filter( FoodCat.status == int( req['status'] ) )
|
||||
|
||||
list = query.order_by( FoodCat.weight.desc(),FoodCat.id.desc() ).all()
|
||||
resp_data['list'] = list
|
||||
resp_data['search_con'] = req
|
||||
resp_data['status_mapping'] = app.config['STATUS_MAPPING']
|
||||
resp_data['current'] = 'cat'
|
||||
return ops_render( "food/cat.html",resp_data )
|
||||
|
||||
|
||||
@route_food.route( "/cat-set",methods = [ "GET","POST" ] )
|
||||
def catSet():
|
||||
return ops_render( "food/cat_set.html" )
|
||||
if request.method == "GET":
|
||||
resp_data = {}
|
||||
req = request.args
|
||||
id = int(req.get("id", 0))
|
||||
info = None
|
||||
if id:
|
||||
info = FoodCat.query.filter_by( id = id ).first()
|
||||
resp_data['info'] = info
|
||||
resp_data['current'] = 'cat'
|
||||
return ops_render( "food/cat_set.html" ,resp_data )
|
||||
|
||||
resp = {'code': 200, 'msg': '操作成功~~', 'data': {}}
|
||||
req = request.values
|
||||
|
||||
id = req['id'] if 'id' in req else 0
|
||||
name = req['name'] if 'name' in req else ''
|
||||
weight = int( req['weight'] ) if ( 'weight' in req and int( req['weight']) > 0 ) else 1
|
||||
|
||||
if name is None or len( name ) < 1:
|
||||
resp['code'] = -1
|
||||
resp['msg'] = "请输入符合规范的分类名称~~"
|
||||
return jsonify( resp )
|
||||
|
||||
food_cat_info = FoodCat.query.filter_by( id = id ).first()
|
||||
if food_cat_info:
|
||||
model_food_cat = food_cat_info
|
||||
else:
|
||||
model_food_cat = FoodCat()
|
||||
model_food_cat.created_time = getCurrentDate()
|
||||
model_food_cat.name = name
|
||||
model_food_cat.weight = weight
|
||||
model_food_cat.updated_time = getCurrentDate()
|
||||
db.session.add(model_food_cat)
|
||||
db.session.commit()
|
||||
return jsonify( resp )
|
||||
|
||||
|
||||
@route_food.route("/cat-ops",methods = [ "POST" ])
|
||||
def catOps():
|
||||
resp = {'code': 200, 'msg': '操作成功~~', 'data': {}}
|
||||
req = request.values
|
||||
|
||||
id = req['id'] if 'id' in req else 0
|
||||
act = req['act'] if 'act' in req else ''
|
||||
if not id :
|
||||
resp['code'] = -1
|
||||
resp['msg'] = "请选择要操作的账号~~"
|
||||
return jsonify(resp)
|
||||
|
||||
if act not in [ 'remove','recover' ] :
|
||||
resp['code'] = -1
|
||||
resp['msg'] = "操作有误,请重试~~"
|
||||
return jsonify(resp)
|
||||
|
||||
food_cat_info = FoodCat.query.filter_by( id= id ).first()
|
||||
if not food_cat_info:
|
||||
resp['code'] = -1
|
||||
resp['msg'] = "指定分类不存在~~"
|
||||
return jsonify(resp)
|
||||
|
||||
if act == "remove":
|
||||
food_cat_info.status = 0
|
||||
elif act == "recover":
|
||||
food_cat_info.status = 1
|
||||
|
||||
food_cat_info.update_time = getCurrentDate()
|
||||
db.session.add( food_cat_info )
|
||||
db.session.commit()
|
||||
return jsonify(resp)
|
||||
|
||||
52
web/static/js/food/cat.js
Normal file
52
web/static/js/food/cat.js
Normal file
@@ -0,0 +1,52 @@
|
||||
;
|
||||
var food_cat_ops = {
|
||||
init:function(){
|
||||
this.eventBind();
|
||||
},
|
||||
eventBind:function(){
|
||||
var that = this;
|
||||
|
||||
$(".wrap_search select[name=status]").change(function(){
|
||||
$(".wrap_search").submit();
|
||||
});
|
||||
|
||||
$(".remove").click( function(){
|
||||
that.ops( "remove",$(this).attr("data") );
|
||||
} );
|
||||
|
||||
$(".recover").click( function(){
|
||||
that.ops( "recover",$(this).attr("data") );
|
||||
} );
|
||||
},
|
||||
ops:function( act,id ){
|
||||
var callback = {
|
||||
'ok':function(){
|
||||
$.ajax({
|
||||
url:common_ops.buildUrl( "/food/cat-ops" ),
|
||||
type:'POST',
|
||||
data:{
|
||||
act:act,
|
||||
id:id
|
||||
},
|
||||
dataType:'json',
|
||||
success:function( res ){
|
||||
var callback = null;
|
||||
if( res.code == 200 ){
|
||||
callback = function(){
|
||||
window.location.href = window.location.href;
|
||||
}
|
||||
}
|
||||
common_ops.alert( res.msg,callback );
|
||||
}
|
||||
});
|
||||
},
|
||||
'cancel':null
|
||||
};
|
||||
common_ops.confirm( ( act == "remove" ? "确定删除?":"确定恢复?" ), callback );
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
$(document).ready( function(){
|
||||
food_cat_ops.init();
|
||||
} );
|
||||
62
web/static/js/food/cat_set.js
Normal file
62
web/static/js/food/cat_set.js
Normal file
@@ -0,0 +1,62 @@
|
||||
;
|
||||
var food_cat_set_ops = {
|
||||
init:function(){
|
||||
this.eventBind();
|
||||
},
|
||||
eventBind:function(){
|
||||
$(".wrap_cat_set .save").click(function(){
|
||||
var btn_target = $(this);
|
||||
if( btn_target.hasClass("disabled") ){
|
||||
common_ops.alert("正在处理!!请不要重复提交~~");
|
||||
return;
|
||||
}
|
||||
|
||||
var name_target = $(".wrap_cat_set input[name=name]");
|
||||
var name = name_target.val();
|
||||
|
||||
var weight_target = $(".wrap_cat_set input[name=weight]");
|
||||
var weight = weight_target.val();
|
||||
|
||||
if( name.length < 1 ){
|
||||
common_ops.tip( "请输入符合规范的分类名称~~",name_target );
|
||||
return false;
|
||||
}
|
||||
|
||||
if( parseInt( weight ) < 1 ){
|
||||
common_ops.tip( "请输入符合规范的权重,并且至少要大于1~~",weight_target );
|
||||
return false;
|
||||
}
|
||||
|
||||
btn_target.addClass("disabled");
|
||||
|
||||
var data = {
|
||||
name: name,
|
||||
weight: weight,
|
||||
id:$(".wrap_cat_set input[name=id]").val()
|
||||
};
|
||||
|
||||
$.ajax({
|
||||
url:common_ops.buildUrl( "/food/cat-set" ),
|
||||
type:'POST',
|
||||
data:data,
|
||||
dataType:'json',
|
||||
success:function( res ){
|
||||
btn_target.removeClass("disabled");
|
||||
var callback = null;
|
||||
if( res.code == 200 ){
|
||||
callback = function(){
|
||||
window.location.href = common_ops.buildUrl("/food/cat");
|
||||
}
|
||||
}
|
||||
common_ops.alert( res.msg,callback );
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
$(document).ready( function(){
|
||||
food_cat_set_ops.init();
|
||||
} );
|
||||
50
web/static/js/food/index.js
Normal file
50
web/static/js/food/index.js
Normal file
@@ -0,0 +1,50 @@
|
||||
;
|
||||
var food_index_ops = {
|
||||
init:function(){
|
||||
this.eventBind();
|
||||
},
|
||||
eventBind:function(){
|
||||
var that = this;
|
||||
$(".remove").click( function(){
|
||||
that.ops( "remove",$(this).attr("data") )
|
||||
});
|
||||
|
||||
$(".recover").click( function(){
|
||||
that.ops( "recover",$(this).attr("data") )
|
||||
});
|
||||
|
||||
$(".wrap_search .search").click( function(){
|
||||
$(".wrap_search").submit();
|
||||
});
|
||||
},
|
||||
ops:function( act,id ){
|
||||
var callback = {
|
||||
'ok':function(){
|
||||
$.ajax({
|
||||
url:common_ops.buildUrl("/food/ops"),
|
||||
type:'POST',
|
||||
data:{
|
||||
act:act,
|
||||
id:id
|
||||
},
|
||||
dataType:'json',
|
||||
success:function( res ){
|
||||
var callback = null;
|
||||
if( res.code == 200 ){
|
||||
callback = function(){
|
||||
window.location.href = window.location.href;
|
||||
}
|
||||
}
|
||||
common_ops.alert( res.msg,callback );
|
||||
}
|
||||
});
|
||||
},
|
||||
'cancel':null
|
||||
};
|
||||
common_ops.confirm( ( act=="remove" )?"确定删除?":"确定恢复?",callback );
|
||||
}
|
||||
};
|
||||
|
||||
$(document).ready( function(){
|
||||
food_index_ops.init();
|
||||
});
|
||||
173
web/static/js/food/set.js
Normal file
173
web/static/js/food/set.js
Normal file
@@ -0,0 +1,173 @@
|
||||
;
|
||||
var upload = {
|
||||
error: function (msg) {
|
||||
common_ops.alert(msg);
|
||||
},
|
||||
success: function (file_key) {
|
||||
if (!file_key) {
|
||||
return;
|
||||
}
|
||||
var html = '<img src="' + common_ops.buildPicUrl(file_key) + '"/>'
|
||||
+ '<span class="fa fa-times-circle del del_image" data="' + file_key + '"></span>';
|
||||
|
||||
if ($(".upload_pic_wrap .pic-each").size() > 0) {
|
||||
$(".upload_pic_wrap .pic-each").html(html);
|
||||
} else {
|
||||
$(".upload_pic_wrap").append('<span class="pic-each">' + html + '</span>');
|
||||
}
|
||||
food_set_ops.delete_img();
|
||||
}
|
||||
};
|
||||
var food_set_ops = {
|
||||
init: function () {
|
||||
this.ue = null;
|
||||
this.eventBind();
|
||||
this.initEditor();
|
||||
this.delete_img();
|
||||
},
|
||||
eventBind: function () {
|
||||
var that = this;
|
||||
|
||||
$(".wrap_food_set .upload_pic_wrap input[name=pic]").change(function () {
|
||||
$(".wrap_food_set .upload_pic_wrap").submit();
|
||||
});
|
||||
|
||||
$(".wrap_food_set select[name=cat_id]").select2({
|
||||
language: "zh-CN",
|
||||
width: '100%'
|
||||
});
|
||||
|
||||
$(".wrap_food_set input[name=tags]").tagsInput({
|
||||
width: 'auto',
|
||||
height: 40,
|
||||
onAddTag: function (tag) {
|
||||
},
|
||||
onRemoveTag: function (tag) {
|
||||
}
|
||||
});
|
||||
|
||||
$(".wrap_food_set .save").click(function () {
|
||||
var btn_target = $(this);
|
||||
if (btn_target.hasClass("disabled")) {
|
||||
common_ops.alert("正在处理!!请不要重复提交~~");
|
||||
return;
|
||||
}
|
||||
|
||||
var cat_id_target = $(".wrap_food_set select[name=cat_id]");
|
||||
var cat_id = cat_id_target.val();
|
||||
|
||||
var name_target = $(".wrap_food_set input[name=name]");
|
||||
var name = name_target.val();
|
||||
|
||||
var price_target = $(".wrap_food_set input[name=price]");
|
||||
var price = price_target.val();
|
||||
|
||||
var summary = $.trim(that.ue.getContent());
|
||||
|
||||
var stock_target = $(".wrap_food_set input[name=stock]");
|
||||
var stock = stock_target.val();
|
||||
|
||||
var tags_target = $(".wrap_food_set input[name=tags]");
|
||||
var tags = $.trim(tags_target.val());
|
||||
|
||||
if (parseInt(cat_id) < 1) {
|
||||
common_ops.tip("请选择分类~~", cat_id_target);
|
||||
return;
|
||||
}
|
||||
|
||||
if (name.length < 1) {
|
||||
common_ops.alert("请输入符合规范的名称~~");
|
||||
return;
|
||||
}
|
||||
|
||||
if (parseFloat(price) <= 0) {
|
||||
common_ops.tip("请输入符合规范的售卖价格~~", price_target);
|
||||
return;
|
||||
}
|
||||
|
||||
if ($(".wrap_food_set .pic-each").size() < 1) {
|
||||
common_ops.alert("请上传封面图~~");
|
||||
return;
|
||||
}
|
||||
|
||||
if (summary.length < 10) {
|
||||
common_ops.tip("请输入描述,并不能少于10个字符~~", price_target);
|
||||
return;
|
||||
}
|
||||
|
||||
if (parseInt(stock) < 1) {
|
||||
common_ops.tip("请输入符合规范的库存量~~", stock_target);
|
||||
return;
|
||||
}
|
||||
|
||||
if (tags.length < 1) {
|
||||
common_ops.alert("请输入标签,便于搜索~~");
|
||||
return;
|
||||
}
|
||||
|
||||
btn_target.addClass("disabled");
|
||||
|
||||
var data = {
|
||||
cat_id: cat_id,
|
||||
name: name,
|
||||
price: price,
|
||||
main_image: $(".wrap_food_set .pic-each .del_image").attr("data"),
|
||||
summary: summary,
|
||||
stock: stock,
|
||||
tags: tags,
|
||||
id: $(".wrap_food_set input[name=id]").val()
|
||||
};
|
||||
|
||||
$.ajax({
|
||||
url: common_ops.buildUrl("/food/set"),
|
||||
type: 'POST',
|
||||
data: data,
|
||||
dataType: 'json',
|
||||
success: function (res) {
|
||||
btn_target.removeClass("disabled");
|
||||
var callback = null;
|
||||
if (res.code == 200) {
|
||||
callback = function () {
|
||||
window.location.href = common_ops.buildUrl("/food/index");
|
||||
}
|
||||
}
|
||||
common_ops.alert(res.msg, callback);
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
},
|
||||
initEditor: function () {
|
||||
var that = this;
|
||||
that.ue = UE.getEditor('editor', {
|
||||
toolbars: [
|
||||
['undo', 'redo', '|',
|
||||
'bold', 'italic', 'underline', 'strikethrough', 'removeformat', 'formatmatch', 'autotypeset', 'blockquote', 'pasteplain', '|', 'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist', 'selectall', '|', 'rowspacingtop', 'rowspacingbottom', 'lineheight'],
|
||||
['customstyle', 'paragraph', 'fontfamily', 'fontsize', '|',
|
||||
'directionalityltr', 'directionalityrtl', 'indent', '|',
|
||||
'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|', 'touppercase', 'tolowercase', '|',
|
||||
'link', 'unlink'],
|
||||
['imagenone', 'imageleft', 'imageright', 'imagecenter', '|',
|
||||
'insertimage', 'insertvideo', '|',
|
||||
'horizontal', 'spechars', '|', 'inserttable', 'deletetable', 'insertparagraphbeforetable', 'insertrow', 'deleterow', 'insertcol', 'deletecol', 'mergecells', 'mergeright', 'mergedown', 'splittocells', 'splittorows', 'splittocols']
|
||||
|
||||
],
|
||||
enableAutoSave: true,
|
||||
saveInterval: 60000,
|
||||
elementPathEnabled: false,
|
||||
zIndex: 4,
|
||||
serverUrl: common_ops.buildUrl('/upload/ueditor')
|
||||
});
|
||||
},
|
||||
delete_img: function () {
|
||||
$(".wrap_food_set .del_image").unbind().click(function () {
|
||||
$(this).parent().remove();
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
$(document).ready(function () {
|
||||
food_set_ops.init();
|
||||
});
|
||||
14
web/templates/common/tab_food.html
Normal file
14
web/templates/common/tab_food.html
Normal file
@@ -0,0 +1,14 @@
|
||||
<div class="row border-bottom">
|
||||
<div class="col-lg-12">
|
||||
<div class="tab_title">
|
||||
<ul class="nav nav-pills">
|
||||
<li {% if current == "index" %} class="current" {% endif %}>
|
||||
<a href="{{ buildUrl('/food/index') }}">美食列表</a>
|
||||
</li>
|
||||
<li {% if current == "cat" %} class="current" {% endif %}>
|
||||
<a href="{{ buildUrl('/food/cat') }}">分类列表</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -1,19 +1,6 @@
|
||||
{% extends "common/layout_main.html" %}
|
||||
{% block content %}
|
||||
<div class="row border-bottom">
|
||||
<div class="col-lg-12">
|
||||
<div class="tab_title">
|
||||
<ul class="nav nav-pills">
|
||||
<li>
|
||||
<a href="{{ buildUrl('/food/index') }}">美食列表</a>
|
||||
</li>
|
||||
<li class="current">
|
||||
<a href="{{ buildUrl('/food/cat') }}">分类列表</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% include "common/tab_food.html" %}
|
||||
<div class="row">
|
||||
<div class="col-lg-12">
|
||||
<form class="form-inline wrap_search">
|
||||
@@ -21,8 +8,9 @@
|
||||
<div class="form-group">
|
||||
<select name="status" class="form-control inline">
|
||||
<option value="-1">请选择状态</option>
|
||||
<option value="1">正常</option>
|
||||
<option value="0">已删除</option>
|
||||
{% for tmp_key in status_mapping %}
|
||||
<option value="{{ tmp_key }}" {% if tmp_key == search_con['status'] %} selected {% endif %}>{{ status_mapping[ tmp_key ] }}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
@@ -48,23 +36,40 @@
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% if list %}
|
||||
{% for item in list %}
|
||||
<tr>
|
||||
<td>8</td>
|
||||
<td>111</td>
|
||||
<td>正常</td>
|
||||
<td>11</td>
|
||||
<td>{{ item.id }}</td>
|
||||
<td>{{ item.name }}</td>
|
||||
<td>{{ item.status_desc }}</td>
|
||||
<td>{{ item.weight }}</td>
|
||||
<td>
|
||||
<a class="m-l" href="{{ buildUrl('/food/cat-set') }}">
|
||||
|
||||
{% if item.status == 1 %}
|
||||
<a class="m-l" href="{{ buildUrl('/food/cat-set') }}?id={{ item.id }}">
|
||||
<i class="fa fa-edit fa-lg"></i>
|
||||
</a>
|
||||
|
||||
<a class="m-l remove" href="javascript:void(0);" data="8">
|
||||
<a class="m-l remove" href="javascript:void(0);" data="{{ item.id }}">
|
||||
<i class="fa fa-trash fa-lg"></i>
|
||||
</a>
|
||||
{% else %}
|
||||
<a class="m-l recover" href="javascript:void(0);" data="{{ item.id }}">
|
||||
<i class="fa fa-rotate-left fa-lg"></i>
|
||||
</a>
|
||||
{% endif %}
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
{% else %}
|
||||
<tr><td colspan="5">暂无数据</td></tr>
|
||||
{% endif %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
{% block js %}
|
||||
<script src="{{ buildStaticUrl('/js/food/cat.js') }}"></script>
|
||||
{% endblock %}
|
||||
|
||||
@@ -21,19 +21,20 @@
|
||||
<div class="form-group">
|
||||
<label class="col-lg-2 control-label">分类名称:</label>
|
||||
<div class="col-lg-10">
|
||||
<input type="text" name="name" class="form-control" placeholder="请输入分类名称~~" value="111">
|
||||
<input type="text" name="name" class="form-control" placeholder="请输入分类名称~~" value="info.name">
|
||||
</div>
|
||||
</div>
|
||||
<div class="hr-line-dashed"></div>
|
||||
<div class="form-group">
|
||||
<label class="col-lg-2 control-label">权重:</label>
|
||||
<div class="col-lg-10">
|
||||
<input type="text" name="weight" class="form-control" placeholder="请输入分类名称~~" value="11">
|
||||
<input type="text" name="weight" class="form-control" placeholder="请输入分类名称~~" value="{% if info and info.weight > 0 %}{{ info.weight }}{% else %}1{% endif%}">
|
||||
</div>
|
||||
</div>
|
||||
<div class="hr-line-dashed"></div>
|
||||
<div class="form-group">
|
||||
<div class="col-lg-4 col-lg-offset-2">
|
||||
<input type="hidden" name="id" value="{{ info.id }}">
|
||||
<button class="btn btn-w-m btn-outline btn-primary save">保存</button>
|
||||
</div>
|
||||
</div>
|
||||
@@ -41,3 +42,6 @@
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
{% block js %}
|
||||
<script src="{{ buildStaticUrl('/js/food/cat_set.js') }}"></script>
|
||||
{% endblock %}
|
||||
|
||||
@@ -86,3 +86,4 @@
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user