def index(): if request.method == 'GET': '''首页获取货物列表''' try: goodslist = Goods.query.all() except Exception as e: current_app.logger.debug(e) return jsonify(code="500", msg="获取货物列表失败") goodss = [goods.to_json() for goods in goodslist] return jsonify(code="200", msg="获取货物列表成功", goodss=goodss) if request.method == 'POST': '''新增货物''' title = request.values.get("title") price= request.values.get("price") stock = request.values.get("stock") storage_location = request.values.get("storage_location") if not all ([title, price, stock, storage_location]): return jsonify(code="403", msg="参数错误") goods = Goods() goods.title = title goods.price = price goods.stock = stock goods.storage_location = storage_location try: db.session.add(goods) db.session.commit() except Exception as e: current_app.logger.debug(e) db.session.rollback() return jsonify(code="500", msg="添加货物失败") return jsonify(code="200", msg="添加货物成功")
def add_goods(): # 获取用户输入信息 title = request.form.get('title') img = request.files.get('img') price = request.form.get('price') detail = request.form.get('detail') if not title: return jsonify({'code': '601', 'msg': '商品名称不能为空!'}) if not price: return jsonify({'code': '602', 'msg': '请输入商品价格!'}) # 保存图片 if img: # 获取项目根路径 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # 获取媒体文件路径 MEDIA_DIR = os.path.join(BASE_DIR, 'static/media') # 随机生成图片名称 filename = str(uuid.uuid4()) i = img.mimetype.split('/')[-1] image = filename + '.' + i # 拼接图片地址 path = os.path.join(MEDIA_DIR, image) img.save(path) img_url = '/static/media/' + image else: img_url = '' # 存储到数据库 goods = Goods() goods.title = title goods.img_url = img_url goods.price = price goods.detail = detail db.session.add(goods) db.session.commit() return jsonify({'code': 200, 'msg': '添加成功'})
def spiders(): # eval(str转dict)需要的参数 true = True false = False null = None qthing = request.values.get('q') api = r'https://s.taobao.com/api?_ksTS=1523179236254_226&callback=jsonp227&ajax=true&m=customized&stats_%27%20\%20%27click=search_radio_all:1&q={}&s=1&imgfile=&initiative_id=staobaoz_20180425&bcoffset=-1%27%20\%20%27&js=1&ie=utf8&rn=d5706a3802513dad625d594a35702a6b'.format(urllib.request.quote(qthing)) current_app.logger.debug(api) rep = urllib.request.urlopen(api).read().decode('utf-8') result = eval(re.findall(r'jsonp227(.*?);', rep)[0][1:-1].strip().replace("\n", "")) for r in result['API.CustomizedApi']['itemlist']['auctions']: #r = result['API.CustomizedApi']['itemlist']['auctions'][0] title = r["raw_title"]#re.sub(r'<[^>]+>', '', r["title"]) price = r["view_price"] stock = r["comment_count"] if " " in r["item_loc"]: storage_location = r["item_loc"].split(" ")[-1] else: storage_location = r["item_loc"] goods = Goods() goods.title = title goods.price = price goods.stock = stock goods.storage_location = storage_location try: db.session.add(goods) db.session.commit() except Exception as e: current_app.logger.debug(e) db.session.rollback() return jsonify(code="500", msg="添加货物失败") return jsonify(code="200", msg="添加货物成功")
def goodsup(request): token = request.session.get('token') userid = cache.get(token) if userid: user = User.objects.get(pk=userid) if request.method == 'GET': return render(request, 'mine/goodsup.html') elif request.method == 'POST': goods = Goods() goodsname = request.POST.get('goodsname') price = request.POST.get('price') title = request.POST.get('title') num = request.POST.get('num') type = request.POST.get('type') file = request.FILES['file'] file.name = str(time.time()) + str(file.name) filepath = os.path.join(settings.GOODSIMG_ROOT, file.name) with open(filepath, 'wb') as fp: for info in file.chunks(): fp.write(info) goods.img = 'img/' + file.name goods.bigimg = 'img/' + file.name goods.name = goodsname goods.price = "¥" + price goods.num = num goods.title = title goods.fatherid = type goods.save() publish = Publish() publish.goods = goods publish.user = user publish.save() return redirect('app:index') else: return redirect('app:login')