def cancel_promotions(id): """ Cancel a single Promotion This endpoint will set the status of promotion as Inactive on success or return an error message if promotion is not found. --- tags: - Promotions parameters: - name: id in: path description: ID of promotion to cancel type: integer required: true responses: 200: description: success message, 'Cancelled the promotion with given id' 404: description: error message, 'Promotion with given id was not found' """ promotion = Promotion.find(redis, id) if promotion: promotion = Promotion.cancel_by_id(redis, id) promotion.save(redis) message = {'Success': 'Cancelled the Promotion with id ' + str(id)} rc = HTTP_200_OK else: message = {'error': 'Promotion %s was not found' % id} rc = HTTP_404_NOT_FOUND return make_response(jsonify(message), rc)
def list_promotions(): """ Retrieve a list of all Promotions This endpoint will return all Promotions unless a query parameter on kind is specified --- tags: - Promotions description: The Promotions endpoint allows you to query Promotion schemes parameters: - name: kind in: query description: the kind of Promotion scheme you are looking for required: false type: string responses: 200: description: An array of Promotion schemes schema: type: array items: schema: id: Promotion properties: id: type: integer description: unique id assigned internally by service name: type: string description: the promotion scheme's name kind: type: string description: the kind of Promotion scheme (sales-promotion1, sale-senior-promotion, black-friday-promotion etc.) description: type: string description: the complete detail of the Promotion scheme and the criteria for the promotion. status: type: string description: the status of promotion scheme whether it is currently "Active" or "Inactive" 404: description: No promotion schemes found. """ results = [] kind = request.args.get('kind') if kind: result = Promotion.find_by_kind(redis, kind) else: result = Promotion.all(redis) if len(result) > 0: results = [Promotion.serialize(promotion) for promotion in result] return make_response(jsonify(results), HTTP_200_OK) else: results = {'error': 'No promotions found'} rc = HTTP_404_NOT_FOUND return make_response(jsonify(results), rc)
def get_promotions_kind(kind): """ Retrieve all promotions for one kind This endpoint will return a Promotion based on it's kind --- tags: - Promotions produces: - application/json parameters: - name: kind in: path description: the kind of Promotion scheme you are looking for type: string required: true responses: 200: description: Promotion returned schema: id: Promotion properties: id: type: integer description: unique id assigned internally by service name: type: string description: name for the Promotion scheme kind: type: string description: the kind of Promotion scheme (sales-promotion1, sale-senior-promotion, black-friday-promotion etc.) description: type: string description: the complete detail of the Promotion scheme and the criteria for the promotion. status: type: string description: the status of promotion scheme whether it is currently "Active" or "Inactive" 404: description: Promotion not found """ results = Promotion.find_by_kind(redis, kind.upper()) if len(results) > 0: result = [Promotion.serialize(promotion) for promotion in results] rc = HTTP_200_OK else: result = {'error': 'Promotion with kind: %s was not found' % str(kind)} rc = HTTP_404_NOT_FOUND return make_response(jsonify(result), rc)
def list_all_active_promotions(): """ Retrieve a list of all acitve Promotions This endpoint will return all active Promotions unless no active Promotions can be found --- tags: - Promotions description: The Promotions endpoint allows you to query Promotion schemes produces: - application/json responses: 200: description: An array of Promotion schemes schema: type: array items: schema: id: Promotion properties: id: type: integer description: unique id assigned internally by service name: type: string description: the promotion scheme's name kind: type: string description: the kind of Promotion scheme (sales-promotion1, sale-senior-promotion, black-friday-promotion etc.) description: type: string description: the complete detail of the Promotion scheme and the criteria for the promotion. status: type: string description: the status of promotion scheme. Will always be "Active" 404: description: No promotion schemes found. """ results = Promotion.find_by_status(redis, 'ACTIVE') if len(results) > 0: result = [Promotion.serialize(promotion) for promotion in results] rc = HTTP_200_OK else: result = {'error': 'No active promotions found'} rc = HTTP_404_NOT_FOUND return make_response(jsonify(result), rc)
def test_validate(self): lack_arguments_input = { "name": "Buy one, get two free", "kind": "sales-promotion1" } self.assertFalse(Promotion.validate(lack_arguments_input)) incorrect_type_input = { "name": 1, "description": "empty", "kind": "sales-promotion3" } self.assertFalse(Promotion.validate(incorrect_type_input)) correct_input = { "name": "Buy one, get two free", "description": "Buy an item having a cost of atleast 90$ to get three free.Cost of the higher price product will be taken into account", "kind": "sales-promotion3" } self.assertTrue(Promotion.validate(correct_input))
def get_promotions(id): """ Retrieve a single Promotion This endpoint will return a Promotion based on it's id --- tags: - Promotions produces: - application/json parameters: - name: id in: path description: ID of promotion to retrieve type: integer required: true responses: 200: description: Promotion returned schema: id: Promotion properties: id: type: integer description: unique id assigned internally by service name: type: string description: name for the Promotion scheme kind: type: string description: the kind of Promotion scheme (sales-promotion1, sale-senior-promotion, black-friday-promotion etc.) description: type: string description: the complete detail of the Promotion scheme and the criteria for the promotion. status: type: string description: the status of promotion scheme whether it is currently "Active" or "Inactive" 404: description: Promotion not found """ promotion = Promotion.find(redis, id) if promotion: message = promotion.serialize() rc = HTTP_200_OK else: message = {'error': 'Promotion with id: %s was not found' % str(id)} rc = HTTP_404_NOT_FOUND return make_response(jsonify(message), rc)
def delete_promotions(id): """ Delete a single Promotion This endpoint will return an empty response and delete the promotion in database --- tags: - Promotions parameters: - name: id in: path description: ID of promotion to delete type: integer required: true responses: 204: description: no content """ promotion = Promotion.find(redis, id) if promotion: promotion.delete(redis) return make_response('', HTTP_204_NO_CONTENT)
def data_load(payload): promotion = Promotion(0, payload['name'], payload['description'], payload['kind'], payload['status']) promotion.save(redis)
def update_promotions(id): """ Update a Promotion This endpoint will update a Promotion based the body that is posted --- tags: - Promotions consumes: - application/json produces: - application/json parameters: - name: id in: path description: ID of promotion to retrieve type: integer required: true - in: body name: body schema: id: data required: - name - kind - description properties: name: type: string description: name for the Promotion scheme kind: type: string description: the kind of Promotion scheme (sales-promotion1, sale-senior-promotion, black-friday-promotion etc.) description: type: string description: the complete detail of the Promotion scheme and the criteria for the promotion. responses: 200: description: Promotion Updated schema: id: Promotion properties: id: type: integer description: unique id assigned internally by service name: type: string description: name for the Promotion scheme kind: type: string description: the kind of Promotion scheme (sales-promotion1, sale-senior-promotion, black-friday-promotion etc.) description: type: string description: the complete detail of the Promotion scheme and the criteria for the promotion. status: type: string description: the status of promotion scheme whether it is currently "Active" or "Inactive" 400: description: Bad Request (the posted data was not valid) """ promotion = Promotion.find(redis, id) if promotion: payload = request.get_json() print 'payload is', payload if Promotion.validate(payload): promotion = Promotion(id, payload['name'], payload['description'], payload['kind'], promotion.status) promotion.save(redis) message = promotion.serialize() rc = HTTP_200_OK else: message = {'error': 'Promotion data was not valid'} rc = HTTP_400_BAD_REQUEST else: message = {'error': 'Promotion %s was not found' % id} rc = HTTP_404_NOT_FOUND return make_response(jsonify(message), rc)
def create_promotions(): """ Creates a Promotion This endpoint will create a Promotion scheme based the data in the body that is posted --- tags: - Promotions consumes: - application/json produces: - application/json parameters: - in: body name: body required: true schema: id: data required: - name - kind - description properties: name: type: string description: name for the Promotion scheme kind: type: string description: the kind of Promotion scheme (sales-promotion1, sale-senior-promotion, black-friday-promotion etc.) description: type: string description: the complete detail of the Promotion scheme and the criteria for the promotion. responses: 201: description: Promotion created schema: id: Promotion properties: id: type: integer description: unique id assigned internally by service name: type: string description: name for the Promotion scheme kind: type: string description: the kind of Promotion scheme (sales-promotion1, sale-senior-promotion, black-friday-promotion etc.) description: type: string description: the complete detail of the Promotion scheme and the criteria for the promotion. status: type: string description: the status of promotion scheme with the value "Active" 400: description: Bad Request (the posted data was not valid) """ id = 0 payload = request.get_json() print payload if Promotion.validate(payload): promotion = Promotion(id, payload['name'], payload['description'], payload['kind'], 'Active') promotion.save(redis) id = promotion.id message = promotion.serialize() rc = HTTP_201_CREATED else: message = {'error': 'Data is not valid'} rc = HTTP_400_BAD_REQUEST response = make_response(jsonify(message), rc) if rc == HTTP_201_CREATED: response.headers['Location'] = url_for('get_promotions', id=id) return response
def promotion(self): Promotion(self)