def save(): try: data = json.loads(str(request.data, encoding="utf-8")) except JSONDecodeError: return jsonify(Result().fail(code="error.json")) area = data.get('area') year = data.get('year') unit = data.get('unit') print1 = data.get('print') num_start = int(data.get('num_start')) num_end = int(data.get('num_end')) if not area or not year or not unit or not num_start or not print1: return jsonify(Result().fail(code="param.null", msg="Invalid param")) if len(area) != 2 or len(year) != 4 or len(unit) != 2 or len(print1) != 2: return jsonify(Result().fail(code="param.length", msg="param length error")) if num_end - num_start > 1000: return jsonify(Result().fail(code="param.range", msg="end - start over 1000")) for num in range(int(num_start), int(num_end)): code = GiftCardCode() code.area = area code.year = year code.unit = unit code.print = print1 code.num = num if gift_card_service.find_by_code(code.code()) is None: gift_card_service.save(code) return jsonify(Result().success())
def get_gift_card(self): r = GiftCardCode() r.area = "01" r.print = "01" r.year = "2019" r.unit = "01" r.num = "000001" gift_card = gift_card_service.find_by_code(r.code()) gift_card.update(status=1, product_id=str(self.product.id)) self.gift_card = gift_card
def find_by_code(code): gift = gift_card_service.find_by_code(code) if not gift: current_app.logger.warn("can not find the gift code: %s" % code) return jsonify(Result().fail(code="gift_card.not.found")) pro = product_service.find_by_id(gift.product_id) if not pro: return jsonify(Result().fail(code="product.not.bind")) return jsonify(Result().success({ "gift_card": gift.to_dict(), "product": pro.to_dict() }))
def setUp(self): r = GiftCardCode() r.area = "01" r.print = "01" r.year = "2019" r.unit = "01" r.num = "000011" self.code = r self.assertEqual(self.code.code(), "0120190101000011") gift_card = gift_card_service.find_by_code(self.code.code()) if not gift_card: gift_card = gift_card_service.save(self.code) self.assertEqual(gift_card.status, const.GIFT_NOT_BIND)
def test_save_gift_card(self): jsonstr = json.dumps({ "address": "xianggggggggggggg", "areas": ["010_北京", "01011_海淀区", "02112_中山路"], "code": self.gift_card.code, "mobile": 18555555555, "name": "xxx", "password": self.gift_card.password }) response = self.client.post('/ord/gift_card', data=jsonstr) json_data = response.data json_dict = json.loads(json_data) self.assertEqual(json_dict['code'], "success") self.ord_id = json_dict['data']['id'] gc1 = gift_card_service.find_by_code(self.gift_card.code) self.assertIsNotNone(gc1.use_time)
def create_gift_card(): user_id = session.get("user_id") content = request.data try: data = json.loads(str(content, encoding="utf-8")) except JSONDecodeError: return jsonify(Result().fail(code="error.json")) code = data.get('code') password = data.get('password') areas = data.get('areas') name = data.get('name') mobile = data.get('mobile') address = data.get('address') save = OrdSave() gift = gift_card_service.find_by_code(code) if not gift: current_app.logger.warn("can not find the gift code: %s" % code) return jsonify(Result().fail(code="gift.not.found")) if password != gift.password: return jsonify(Result().fail(code="gift.password.error")) if not gift.product_id: return jsonify(Result().fail(code="gift.product.relation", msg="gift card not bind to the product")) if gift.status != 1: return jsonify(Result().fail(code="gift.card.expire", msg="gift card expire or used")) save.gift_card_id = str(gift.id) save.gift_card_code = gift.code product = product_service.find_by_id(gift.product_id) if not product: return jsonify(Result().fail(code="product.not.exist", msg="product not exist")) gift_card_service.update_used(code, password) ord_product = OrdProduct() ord_product.product_id = str(product.id) ord_product.num = 1 ord_product.title = product.title ord_product.main_pic = product.main_pic save.add_product(ord_product) save.areas = areas save.name = name save.mobile = mobile save.address = address save.user_id = user_id save.status = const.ORD_WAIT_SEND id = ord_service.save(save) return jsonify(Result().success({"id": id}))
def test_update_used(self): gift_card = gift_card_service.find_by_code(self.code.code()) gift_card_service.update_used(gift_card.code, gift_card.password) gift_card2 = gift_card_service.find_by_code(self.code.code()) self.assertEqual(gift_card2.status, const.GIFT_CARD_USED)
def test_find_by_code(self): return gift_card_service.find_by_code(self.code.code())
def test_bind_product(self): gift_card_service.update_bind_product([self.code.code()], "test_product_id") gift_card = gift_card_service.find_by_code(self.code.code()) self.assertEqual(gift_card.status, const.GIFT_VALID)
def tearDown(self): gift_card = gift_card_service.find_by_code(self.code.code()) gift_card_service.delete(gift_card.id) gift_card = gift_card_service.find_by_code(self.code.code()) self.assertIsNone(gift_card)