def create_promotions(): """ Creates a Promotion This endpoint will create a Promotion based the data in the body that is posted """ app.logger.info('Request to Create a Promotion...') data = {} # Check for form submission data if request.headers.get( 'Content-Type') == 'application/x-www-form-urlencoded': app.logger.info('Getting data from form submit') data = { 'productid': request.form['productid'], 'category': request.form['category'], 'available': True, 'discount': request.form['discount'] } else: check_content_type('application/json') app.logger.info('Getting data from API call') data = request.get_json() app.logger.info(data) promotion = Promotion() promotion.deserialize(data) promotion.save() app.logger.info('Promotion with new id [%s] saved!', promotion.id) message = promotion.serialize() location_url = url_for('get_promotions', promotion_id=promotion.id, _external=True) return make_response(jsonify(message), status.HTTP_201_CREATED, {'Location': location_url})
def test_query_2(self): '''test query by available on''' promo1 = Promotion(1) promo2 = Promotion(2) promo1.deserialize( {'start_date': '2017-01-01 00:00:00', 'end_date': '2019-01-01 00:00:00'}) promo2.deserialize( {'start_date': '2018-01-01 00:00:00', 'end_date': '2020-01-01 00:00:00'}) promo1.save() promo2.save() test = Promotion.find_by_conditions( {'available_on': '2016-01-01 00:00:00'}) self.assertIsInstance(test, list) self.assertEqual(len(test), 0) test2 = Promotion.find_by_conditions( {'available_on': '2017-06-01 00:00:00'}) self.assertIsInstance(test2, list) self.assertEqual(len(test2), 1) self.assertEqual(test2[0].id, 1) test3 = Promotion.find_by_conditions( {'available_on': '2018-06-01 00:00:00'}) self.assertIsInstance(test3, list) self.assertEqual(len(test3), 2) test4 = Promotion.find_by_conditions( {'available_on': '2019-06-01 00:00:00'}) self.assertIsInstance(test4, list) self.assertEqual(len(test4), 1) self.assertEqual(test4[0].id, 2)
def test_deserialize(self): promo = Promotion() data = {'name': 'test', 'promo_type': '%', 'value': 5.0, 'start_date': '2017-10-17 23:59:59', 'end_date': '9999-12-31 23:59:59', 'detail': 'testtest'} promo.deserialize(data) self.assertEqual(promo.serialize(), {'id': promo.id, 'name': 'test', 'promo_type': '%', 'value': 5.0, 'start_date': '2017-10-17 23:59:59', 'end_date': '9999-12-31 23:59:59', 'detail': 'testtest'})
def create_promotions(): """ Creates a Promotion This endpoint will create a Promotion based the data in the body that is posted """ check_content_type('application/json') promotion = Promotion() promotion.deserialize(request.get_json()) promotion.save() message = promotion.serialize() location_url = url_for('get_promotions', promotion_id=promotion.promotion_id, _external=True) return make_response(jsonify(message), status.HTTP_201_CREATED, {'Location': location_url})
def test_deserialize_a_promotion(self): """ Test deserialization of a Promotion """ data = { "promotion_id": 1, "name": "20%OFF", "product_id": 9527, "discount_ratio": 80 } promotion = Promotion() promotion.deserialize(data) self.assertNotEqual(promotion, None) self.assertEqual(promotion.promotion_id, None) self.assertEqual(promotion.name, "20%OFF") self.assertEqual(promotion.product_id, 9527) self.assertEqual(promotion.discount_ratio, 80)
def test_deserialize_a_promotion(self): """ Deserialize a Promotion """ data = { "productid": "B4321", "category": "dollar", "available": True, "discount": "5" } promotion = Promotion() promotion.deserialize(data) self.assertNotEqual(promotion, None) self.assertEqual(promotion.id, None) self.assertEqual(promotion.productid, "B4321") self.assertEqual(promotion.category, "dollar") self.assertEqual(promotion.available, True) self.assertEqual(promotion.discount, "5")
def test_query(self): '''test query by promo type''' promo1 = Promotion(1) promo2 = Promotion(2) promo1.deserialize({'promo_type': '%'}) promo2.deserialize({'promo_type': '$'}) promo1.save() promo2.save() test = Promotion.find_by_conditions({'promo_type': '$'}) self.assertIsInstance(test, list) self.assertEqual(len(test), 1) self.assertEqual(test[0].id, 2) test2 = Promotion.find_by_conditions({'promo_type': '%'}) self.assertIsInstance(test, list) self.assertEqual(len(test), 1) self.assertEqual(test2[0].id, 1)
def post(self): """ Creates a Promotion This endpoint will create a Promotion based the data in the body that is posted """ check_content_type('application/json') try: promotion = Promotion() promotion.deserialize(api.payload) promotion.save() flask_app.logger.info('CREATE promotion Success') location_url = api.url_for(PromotionResource, promo_id=promotion.id, _external=True) return promotion.serialize(), 201, {'Location': location_url} except Exception as e: print("EXCEPTION:", str(e)) return jsonify(error=str(e)), 400
def test_query_3(self): '''test query by multiple conditions''' promo1 = Promotion(1) promo2 = Promotion(2) promo1.deserialize( {'promo_type': '%', 'start_date': '2017-01-01 00:00:00', 'end_date': '2019-01-01 00:00:00'}) promo2.deserialize( {'promo_type': '$', 'start_date': '2018-01-01 00:00:00', 'end_date': '2020-01-01 00:00:00'}) promo1.save() promo2.save() test = Promotion.find_by_conditions( {'promo_type': '$', 'available_on': '2017-06-01 00:00:00'}) self.assertIsInstance(test, list) self.assertEqual(len(test), 0) test2 = Promotion.find_by_conditions( {'promo_type': '%', 'available_on': '2018-06-01 00:00:00'}) self.assertIsInstance(test2, list) self.assertEqual(len(test2), 1) self.assertEqual(test2[0].id, 1)
def test_deserialize_a_promotion(self): """ Test deserialization of a Promotion """ data = { "id": 1, "promo_name": "random", "goods_name": "random_good", "category": "random_category", "price": 20, "discount": 20, "available": True } promotion = Promotion() promotion.deserialize(data) self.assertNotEqual(promotion, None) self.assertEqual(promotion.id, None) self.assertEqual(promotion.promo_name, "random") self.assertEqual(promotion.goods_name, "random_good") self.assertEqual(promotion.category, "random_category") self.assertEqual(promotion.price, 20) self.assertEqual(promotion.discount, 20) self.assertEqual(promotion.available, True)
def create_promotions(): """ Creates a Promotion This endpoint will create a Promotion based the data in the body that is posted --- tags: - promotions consumes: - application/json produces: - application/json parameters: - in: body name: Promotion description: Promotion entry to add required: false schema: $ref: '#/definitions/PromotionObject' responses: 201: description: item created schema: $ref: '#/definitions/ResponsePromotionObject' 400: description: invalid input, object invalid """ check_content_type('application/json') promotion = Promotion() promotion.deserialize(request.get_json()) promotion.save() message = promotion.serialize() location_url = url_for('get_promotions', promotion_id=promotion.promotion_id, _external=True) return make_response(jsonify(message), status.HTTP_201_CREATED, {'Location': location_url})
def data_load(promo_id, data): promo = Promotion(promo_id) promo.deserialize(data) promo.save()
def save_promotion(self, data): """ save a promotion into the db """ promotion = Promotion() promotion.deserialize(data) promotion.save()