python学习
This commit is contained in:
@@ -65,3 +65,63 @@ def ops_render( template,context = {} ):
|
||||
def getCurrentDate( format = "%Y-%m-%d %H:%M:%S"):
|
||||
#return datetime.datetime.now().strftime( format )
|
||||
return datetime.datetime.now()
|
||||
|
||||
'''
|
||||
获取格式化的时间
|
||||
'''
|
||||
def getFormatDate( date = None ,format = "%Y-%m-%d %H:%M:%S" ):
|
||||
if date is None:
|
||||
date = datetime.datetime.now()
|
||||
|
||||
return date.strftime( format )
|
||||
|
||||
|
||||
'''
|
||||
根据某个字段获取一个dic出来
|
||||
'''
|
||||
def getDictFilterField( db_model,select_filed,key_field,id_list ):
|
||||
ret = {}
|
||||
query = db_model.query
|
||||
if id_list and len( id_list ) > 0:
|
||||
query = query.filter( select_filed.in_( id_list ) )
|
||||
|
||||
list = query.all()
|
||||
if not list:
|
||||
return ret
|
||||
for item in list:
|
||||
if not hasattr( item,key_field ):
|
||||
break
|
||||
|
||||
ret[ getattr( item,key_field ) ] = item
|
||||
return ret
|
||||
|
||||
|
||||
|
||||
def selectFilterObj( obj,field ):
|
||||
ret = []
|
||||
for item in obj:
|
||||
if not hasattr(item, field ):
|
||||
break
|
||||
if getattr( item,field ) in ret:
|
||||
continue
|
||||
ret.append( getattr( item,field ) )
|
||||
return ret
|
||||
|
||||
|
||||
def getDictFilterField( db_model,select_filed,key_field,id_list ):
|
||||
ret = {}
|
||||
query = db_model.query
|
||||
if id_list and len( id_list ) > 0:
|
||||
query = query.filter( select_filed.in_( id_list ) )
|
||||
|
||||
list = query.all()
|
||||
if not list:
|
||||
return ret
|
||||
for item in list:
|
||||
if not hasattr( item,key_field ):
|
||||
break
|
||||
if getattr( item,key_field ) not in ret:
|
||||
ret[getattr(item, key_field)] = []
|
||||
|
||||
ret[ getattr( item,key_field ) ].append(item )
|
||||
return ret
|
||||
41
common/libs/UploadService.py
Normal file
41
common/libs/UploadService.py
Normal file
@@ -0,0 +1,41 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from werkzeug.utils import secure_filename
|
||||
from application import app,db
|
||||
from common.libs.Helper import getCurrentDate
|
||||
import datetime
|
||||
import os,stat,uuid
|
||||
from common.models.Image import Image
|
||||
class UploadService():
|
||||
@staticmethod
|
||||
def uploadByFile( file ):
|
||||
config_upload = app.config['UPLOAD']
|
||||
resp = { 'code':200,'msg':'操作成功~~','data':{} }
|
||||
filename = secure_filename( file.filename )
|
||||
ext = filename.rsplit(".",1)[1]
|
||||
if ext not in config_upload['ext']:
|
||||
resp['code'] = -1
|
||||
resp['msg'] = "不允许的扩展类型文件"
|
||||
return resp
|
||||
|
||||
|
||||
root_path = app.root_path + config_upload['prefix_path']
|
||||
#不使用getCurrentDate创建目录,为了保证其他写的可以用,这里改掉,服务器上好像对时间不兼容
|
||||
file_dir = datetime.datetime.now().strftime("%Y%m%d")
|
||||
save_dir = root_path + file_dir
|
||||
if not os.path.exists( save_dir ):
|
||||
os.mkdir( save_dir )
|
||||
os.chmod( save_dir,stat.S_IRWXU | stat.S_IRGRP | stat.S_IRWXO )
|
||||
|
||||
file_name = str( uuid.uuid4() ).replace("-","") + "." + ext
|
||||
file.save( "{0}/{1}".format( save_dir,file_name ) )
|
||||
|
||||
model_image = Image()
|
||||
model_image.file_key = file_dir + "/" + file_name
|
||||
model_image.created_time = getCurrentDate()
|
||||
db.session.add( model_image)
|
||||
db.session.commit()
|
||||
|
||||
resp['data'] = {
|
||||
'file_key': model_image.file_key
|
||||
}
|
||||
return resp
|
||||
@@ -14,4 +14,10 @@ class UrlManager(object):
|
||||
release_version = app.config.get('RELEASE_VERSION')
|
||||
ver = "%s" % (int(time.time())) if not release_version else release_version
|
||||
path = "/static" + path + "?ver=" + ver
|
||||
return UrlManager.buildUrl( path )
|
||||
return UrlManager.buildUrl( path )
|
||||
|
||||
@staticmethod
|
||||
def buildImageUrl( path ):
|
||||
app_config = app.config['APP']
|
||||
url = app_config['domain'] + app.config['UPLOAD']['prefix_url'] + path
|
||||
return url
|
||||
28
common/libs/food/FoodService.py
Normal file
28
common/libs/food/FoodService.py
Normal file
@@ -0,0 +1,28 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from application import app,db
|
||||
from common.models.food.FoodStockChangeLog import FoodStockChangeLog
|
||||
from common.models.food.Food import Food
|
||||
from common.libs.Helper import getCurrentDate
|
||||
class FoodService():
|
||||
|
||||
@staticmethod
|
||||
def setStockChangeLog( food_id = 0,quantity = 0,note = '' ):
|
||||
|
||||
if food_id < 1:
|
||||
return False
|
||||
|
||||
food_info = Food.query.filter_by( id = food_id ).first()
|
||||
if not food_info:
|
||||
return False
|
||||
|
||||
model_stock_change = FoodStockChangeLog()
|
||||
model_stock_change.food_id = food_id
|
||||
model_stock_change.unit = quantity
|
||||
model_stock_change.total_stock = food_info.stock
|
||||
model_stock_change.note = note
|
||||
model_stock_change.created_time = getCurrentDate()
|
||||
db.session.add(model_stock_change)
|
||||
db.session.commit()
|
||||
return True
|
||||
|
||||
|
||||
12
common/models/Image.py
Normal file
12
common/models/Image.py
Normal file
@@ -0,0 +1,12 @@
|
||||
# coding: utf-8
|
||||
from sqlalchemy import Column, DateTime, Integer, String
|
||||
from sqlalchemy.schema import FetchedValue
|
||||
|
||||
from application import db
|
||||
|
||||
class Image(db.Model):
|
||||
__tablename__ = 'images'
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
file_key = db.Column(db.String(60), nullable=False, server_default=db.FetchedValue())
|
||||
created_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
|
||||
Reference in New Issue
Block a user