示例#1
0
    def post(self):
        """
        Creates a Pet

        This endpoint will create a Pet based the data in the body that is posted
        or data that is sent via an html form post.
        """
        app.logger.info('Request to Create a Pet')
        content_type = request.headers.get('Content-Type')
        if not content_type:
            abort(status.HTTP_400_BAD_REQUEST, "No Content-Type set")

        data = {}
        # Check for form submission data
        if content_type == 'application/x-www-form-urlencoded':
            app.logger.info('Processing FORM data')
            app.logger.info(type(request.form))
            app.logger.info(request.form)
            data = {
                'name':
                request.form['name'],
                'category':
                request.form['category'],
                'available':
                request.form['available'].lower()
                in ['yes', 'y', 'true', 't', '1']
            }
        elif content_type == 'application/json':
            app.logger.info('Processing JSON data')
            data = request.get_json()
        else:
            message = 'Unsupported Content-Type: {}'.format(content_type)
            app.logger.info(message)
            abort(status.HTTP_400_BAD_REQUEST, message)

        pet = Pet()
        try:
            pet.deserialize(data)
        except DataValidationError as error:
            raise BadRequest(str(error))
        pet.save()
        app.logger.info('Pet with new id [%s] saved!', pet.id)
        location_url = api.url_for(PetResource, pet_id=pet.id, _external=True)
        return pet.serialize(), status.HTTP_201_CREATED, {
            'Location': location_url
        }
示例#2
0
    def post(self):
        """Create a customer in the database"""
        app.logger.info('Creating a new customer')

        content_type = request.headers.get('Content-Type')

        data = {}

        if content_type == 'application/x-www-form-urlencoded':
            app.logger.info('Processing FORM data')
            app.logger.info(request.form)
            app.logger.info(type(request.form))

            data = {
                'username': request.form['username'],
                'password': request.form['password'],
                'first_name': request.form['first_name'],
                'last_name': request.form['last_name'],
                'address': request.form['address'],
                'phone_number': request.form['phone_number'],
                'active': request.form['active'],
                'email': request.form['email'],
                'id': request.form['id']
            }
        elif content_type == 'application/json':
            app.logger.info('Processing JSON data')
            data = request.get_json()
            data.pop("_id", None)
        else:
            message = 'Unsupported Content-Type: {}'.format(content_type)
            app.logger.info(message)
            abort(status.HTTP_400_BAD_REQUEST, message)
        customer = Customer()
        try:
            customer.deserialize(data)
        except DataValidationError as error:
            raise BadRequest(str(error))
        customer.save()
        app.logger.info('Customer with new id [%s] saved!', customer.id)
        location_url = api.url_for(CustomerResource,
                                   customer_id=customer.id,
                                   _external=True)
        return customer.serialize(), status.HTTP_201_CREATED, {
            'Location': location_url
        }
    def post(self):
        """
        Creates a Recommendation
        This endpoint will create a Recommendation based the data in the body that is posted
        or data that is sent via an html form post.
        """
        #app.logger.info('Request to Create a Recommendation')
        content_type = request.headers.get('Content-Type')
        if not content_type:
            abort(status.HTTP_400_BAD_REQUEST, "No Content-Type set")

        data = {}
        # Check for form submission data
        if content_type == 'application/x-www-form-urlencoded':
            #app.logger.info('Processing FORM data')
            #app.logger.info(type(request.form))
            #app.logger.info(request.form)
            data = {
                'productId': request.form['productId'],
                'suggestionId': request.form['suggestionId'],
                'categoryId': request.form['categoryId'],
            }
        elif content_type == 'application/json':
            #app.logger.info('Processing JSON data')
            data = request.get_json()
        else:
            message = 'Unsupported Content-Type: {}'.format(content_type)
            abort(status.HTTP_400_BAD_REQUEST, message)

        recommendation = Recommendation(data["id"])
        try:
            recommendation.deserialize(data)
        except DataValidationError as error:
            raise BadRequest(str(error))
        recommendation.save()
        #app.logger.info('Recommendation with new id [%s] saved!', recommendation.id)
        location_url = api.url_for(RecommendationResource,
                                   recommendation_id=recommendation.id,
                                   _external=True)
        #app.logger.info('Location url [%s]', location_url)
        return recommendation.serialize(), status.HTTP_201_CREATED, {
            'Location': location_url
        }