github.com/jincm/wesharechain@v0.0.0-20210122032815-1537409ce26a/server/block/api/commodity/commodity.py (about) 1 #coding:utf-8 2 3 from tornado.web import RequestHandler 4 from util.exception import ParamExist 5 import logging 6 import json 7 import os 8 import time 9 from PIL import Image 10 from util import common_util 11 from operation import commodity 12 from util.ini_client import ini_load 13 14 _conf = ini_load('config/service.ini') 15 _dic_con = _conf.get_fields('token') 16 timeout = _dic_con.get('timeout') 17 18 LOG = logging.getLogger(__name__) 19 20 21 class CommodityHandler(RequestHandler): 22 def initialize(self, static_path, commodity_path, **kwds): 23 self.static_path = static_path 24 self.commodity_path = commodity_path 25 26 def get(self): 27 try: 28 id = self.get_argument('id', '') 29 name = self.get_argument('name', '') 30 token = self.get_argument('token', '') 31 limit = self.get_argument('limit', 0) 32 offset = self.get_argument('offset', 0) 33 is_timeout = common_util.validate_token_time(token) 34 if is_timeout: 35 self.finish({'state': 1, 36 'message': 'Token expired' 37 }) 38 op = commodity.CommodityOp() 39 commodity_list = op.lists(limit, offset, id, name) 40 self.finish({ 41 'state': 0, 42 'message': 'ok', 43 'count': len(commodity_list), 44 'data': commodity_list 45 }) 46 except Exception as ex: 47 LOG.error("Get order info error:%s" % ex) 48 self.finish(json.dumps({'state': 10, 'message': 'Get order info error'})) 49 50 def post(self): 51 try: 52 name = self.get_argument('name', '') 53 describe = self.get_argument('describe', '') 54 original_price = self.get_argument('original_price', '') 55 real_price = self.get_argument('real_price', '') 56 commodity_type = self.get_argument('commodity_type', '') 57 token = self.get_argument('token', '') 58 # 获取商品图片 59 imgs = self.request.files.get('image', '') 60 if not imgs: 61 LOG.error("image is none") 62 self.finish(json.dumps({'state': 3, 'message': 'image is none'})) 63 return 64 img = imgs[0] 65 filename = img['filename'] 66 filename = str(int(time.time())) + "." + filename.rpartition(".")[-1] 67 file_path = self.static_path + self.commodity_path + filename 68 with open(file_path, 'wb') as up: 69 up.write(img['body']) 70 71 self._img_resize(file_path) 72 73 is_timeout = common_util.validate_token_time(token, timeout) 74 if is_timeout: 75 self.finish({'state': 1, 76 'message': 'Token expired' 77 }) 78 op = commodity.CommodityOp() 79 op.create(name, describe, original_price, real_price, commodity_type, file_path) 80 self.finish({ 81 'state': 0, 82 'message': 'ok', 83 }) 84 85 except ParamExist as ex: 86 LOG.error("commodity create error:%s" % ex) 87 self.finish(json.dumps({'state': 9, 'message': 'params exit'})) 88 except Exception as ex: 89 LOG.error("commodity create error:%s" % ex) 90 self.finish(json.dumps({'state': 10, 'message': 'commodity create error'})) 91 92 def put(self): 93 try: 94 id = self.get_argument('id', '') 95 name = self.get_argument('name', '') 96 describe = self.get_argument('describe', '') 97 original_price = self.get_argument('original_price', '') 98 real_price = self.get_argument('real_price', '') 99 commodity_type = self.get_argument('commodity_type', '') 100 token = self.get_argument('token', '') 101 # 获取商品图片 102 imgs = self.request.files.get('image', '') 103 if not imgs: 104 LOG.error("image is none") 105 self.finish(json.dumps({'state': 3, 'message': 'image is none'})) 106 return 107 img = imgs[0] 108 filename = img['filename'] 109 filename = str(int(time.time())) + "." + filename.rpartition(".")[-1] 110 file_path = self.static_path + self.commodity_path + filename 111 with open(file_path, 'wb') as up: 112 up.write(img['body']) 113 114 self._img_resize(file_path) 115 116 is_timeout = common_util.validate_token_time(token, timeout) 117 if is_timeout: 118 self.finish({'state': 1, 119 'message': 'Token expired' 120 }) 121 op = commodity.CommodityOp() 122 op.update(id, name, describe, original_price, real_price, commodity_type, file_path) 123 self.finish({ 124 'state': 0, 125 'message': 'ok', 126 }) 127 128 except ParamExist as ex: 129 LOG.error("commodity create error:%s" % ex) 130 self.finish(json.dumps({'state': 9, 'message': 'params exit'})) 131 except Exception as ex: 132 LOG.error("commodity create error:%s" % ex) 133 self.finish(json.dumps({'state': 10, 'message': 'commodity create error'})) 134 135 def _img_resize(self, path): 136 """ 137 对大于2M的图片进行缩放 138 :param path: 139 :return: 140 """ 141 fsize = os.path.getsize(path) 142 if fsize <= 2097152: 143 return 144 ims = Image.open(path) 145 s = 1920 146 w = ims.width / s 147 h = ims.height / s 148 if w > h: 149 width = s 150 height = int(s * (h / w)) 151 else: 152 height = s 153 width = int(s * (w / h)) 154 155 ims = ims.resize((width, height)) 156 ims.save(path)