Пример #1
0
    def post(self):
        """
        Process POST request
        ---
        tags:
          - Auth
        parameters:
          - in: body
            name: body
            required: true
            description: Existing user's account details
            type: string
            schema:
              properties:
                username:
                  type: string
                  default: newuser
                password:
                  type: string
                  default: Bootcamp17
        responses:
          200:
            description: User logged in to account successfully
          400:
            description: Data validation failed
          401:
            description: User authentication failed
          500:
            description: Database could not be accessed
        """

        args = self.parser.parse_args()

        messages = {}
        messages['username_message'] = validate_username(args.username.strip())
        messages['password_message'] = validate_password(args.password)

        if not data_validator(messages):
            return jsonify(messages), 400

        try:
            user = User.query.filter_by(username=args.username).first()
            if user and user.check_password(args.password):
                access_token = user.encode_token(user.id)
                if access_token:
                    response = jsonify({
                        'message':
                        'You are now logged in.',
                        'access_token':
                        access_token.decode('utf-8')
                    })
                    response.status_code = 200
            else:
                response = jsonify(
                    {'message': 'Sorry, your username/password is invalid.'})
                response.status_code = 401
        except exc.SQLAlchemyError as error:
            return jsonify({'message': str(error)}), 500
        return response
Пример #2
0
    def post(self):
        """
        Process POST request
        ---
        tags:
          - Auth
        parameters:
          - in: body
            name: body
            required: true
            description: New user's account details
            type: string
            schema:
              properties:
                username:
                  type: string
                  default: newuser
                email:
                  type: string
                  default: [email protected]
                password:
                  type: string
                  default: Bootcamp17
                confirm_password:
                  type: string
                  default: Bootcamp17
        responses:
          201:
            description: A new user account created successfully
          400:
            description: Data validation failed
          500:
            description: Database could not be accessed
        """

        args = self.parser.parse_args()

        messages = {}
        messages['username_message'] = validate_username(args.username.strip(),
                                                         register=True)
        messages['email_message'] = validate_user_email(args.email.strip(),
                                                        register=True)
        messages['password_message'] = validate_password(args.password)
        messages['confirm_password_message'] = validate_confirm_password(args.confirm_password, \
                  args.password)

        if not data_validator(messages):
            return jsonify(messages), 400

        try:
            user = User(username=args.username,
                        email=args.email,
                        password=args.password)
            user.save()
            response = jsonify({'message': 'Your account has been created.'})
            response.status_code = 201
        except exc.SQLAlchemyError as error:
            return jsonify({'message': str(error)}), 500
        return response
Пример #3
0
    def post(self):
        """
        Process POST request
        ---
        tags:
          - Auth
        parameters:
          - in: body
            name: body
            required: true
            description: User's email address
            type: string
            schema:
              properties:
                email:
                  type: string
                  default: [email protected]
        responses:
          200:
            description: Password reset successfully
          400:
            description: Email validation failed or email address could not be found
          500:
            description: Database could not be accessed or email could not be sent
        """

        args = self.parser.parse_args()

        messages = {}
        messages['email_message'] = validate_user_email(args.email.strip())

        if not data_validator(messages):
            return jsonify(messages), 400

        try:
            user = User.query.filter_by(email=args.email).first()
            if user:
                chars = string.ascii_uppercase + string.ascii_lowercase + \
                        string.digits
                new_password = ''.join(random.choice(chars) for i in range(8))
                user.password = user.hash_password(password=new_password)
                user.save()
                mail_content = 'Hi %s,\n\nYour password has been reset to %s. \
Please change it after login.\n\nBest regards,\nYummy Recipes Inc.' \
%(user.username, new_password)
                send_mail(user, "Yummy Recipes Password Reset", mail_content)
                response = jsonify(
                    {'message': 'Your password has been reset.'})
                response.status_code = 200
            else:
                response = jsonify({
                    'message':
                    'User with this email address does not exist.'
                })
                response.status_code = 400
        except exc.SQLAlchemyError as error:
            return jsonify({'message': str(error)}), 500
        return response
Пример #4
0
    def post(self, access_token, user):
        """
        Process POST request
        ---
        tags:
          - Category
        security:
          - Bearer: []
        parameters:
          - in: body
            name: body
            required: true
            description: Category's category name
            type: string
            schema:
              properties:
                category_name:
                  type: string
                  default: Breakfast
        responses:
          201:
            description: A new category created successfully
          400:
            description: Data validation failed
          500:
            description: Database could not be accessed
        """

        args = self.parser.parse_args()

        messages = {}
        messages['category_name_message'] = validate_category_name(
            args.category_name.strip(), user.id)

        if not data_validator(messages):
            return jsonify(messages), 400

        try:
            category = Category(category_name=args.category_name,
                                user_id=user.id)
            category.save()
            response = jsonify({
                'id': category.id,
                'category_name': category.category_name,
                'user_id': category.user_id,
                'date_created': category.date_created,
                'date_modified': category.date_modified
            })
            response.status_code = 201
        except exc.SQLAlchemyError as error:
            return jsonify({'message': str(error)}), 500
        return response
Пример #5
0
    def post(self, access_token, user):
        """
        Process POST request
        ---
        tags:
          - Auth
        parameters:
          - in: body
            name: body
            required: true
            description: User's new password
            type: string
            schema:
              properties:
                new_password:
                  type: string
                  default: Bootcamp17
                confirm_new_password:
                  type: string
                  default: Bootcamp17
        responses:
          200:
            description: Password changed successfully
          400:
            description: Data validation failed
          500:
            description: Database could not be accessed or email could not be sent
        """

        args = self.parser.parse_args()

        messages = {}
        messages['new_password_message'] = validate_password(args.new_password)
        messages['confirm_new_password_message'] = validate_confirm_password(args.confirm_new_password, \
                  args.new_password)

        if not data_validator(messages):
            return jsonify(messages), 400

        try:
            user.password = user.hash_password(password=args.new_password)
            user.save()
            response = jsonify({'message': 'Your password has been changed.'})
            response.status_code = 200
        except exc.SQLAlchemyError as error:
            return jsonify({'message': str(error)}), 500
        return response
Пример #6
0
    def post(self, access_token, user, category_id):
        """
        Process POST request
        ---
        tags:
          - Recipe
        security:
          - Bearer: []
        parameters:
          - in: path
            name: category_id
            required: true
            description: The id of recipe category
            type: int
          - in: body
            name: body
            required: true
            description: Recipe's name, ingredients, directions and category_id
            type: string
            schema:
              properties:
                recipe_name:
                  type: string
                  default: Espresso Esiri
                ingredients:
                  type: string
                  default: 1) 1 tbsp plus 1 or 2 tsp (20-25 ml) Espresso, 2) 2 \
tbsp (30 ml) Benedictine, 3) Approx. 3 tbsp (40 ml) fresh heavy cream, 4) Unsweetened \
cocoa powder, 5) Ice cubes
                directions:
                  type: string
                  default: 1) Prepare the Espresso in a small cup. 2) Fill the mixing \
glass 3/4 full with ice cubes. Add the Benedictine and the Espresso. Cool, mixing the \
ingredients with the mixing spoon. 3) Pour into the glass, filtering the ice with a strainer. \
4) Shake the cream, which should be very cold, in the mini shaker until it becomes quite thick. \
5) Rest the cream on the surface of the cocktail, making it run down the back of the mixing spoon. \
6) Garnish with a light dusting of cocoa, and serve.
                category_id:
                  type: int
                  default: 1
        responses:
          201:
            description: A new recipe created successfully
          400:
            description: Data validation failed
          404:
            description: Invalid recipe category id
          500:
            description: Database could not be accessed
        """

        args = self.parser.parse_args()

        messages = {}
        messages['recipe_name_message'] = validate_recipe_name(
            args.recipe_name.strip(), category_id)
        messages['ingredients_message'] = validate_ingredients(
            args.ingredients)
        messages['directions_message'] = validate_directions(args.directions)

        if not data_validator(messages):
            return jsonify(messages), 400

        try:
            category = Category.query.filter_by(id=category_id,
                                                user_id=user.id).first()
            if category:
                recipe = Recipe(recipe_name=args.recipe_name, ingredients=args.ingredients, \
                        directions=args.directions, category_id=category_id)
                recipe.save()
                response = jsonify({
                    'id': recipe.id,
                    'recipe_name': recipe.recipe_name,
                    'ingredients': recipe.ingredients,
                    'directions': recipe.directions,
                    'category_id': recipe.category_id,
                    'date_created': recipe.date_created,
                    'date_modified': recipe.date_modified
                })
                response.status_code = 201
            else:
                response = jsonify(
                    {'message': 'Sorry, recipe category could not be found.'})
                response.status_code = 404
        except exc.SQLAlchemyError as error:
            return jsonify({'message': str(error)}), 500
        return response
Пример #7
0
    def put(self, access_token, user, category_id, recipe_id):
        """
        Process PUT request
        ---
        tags:
          - Recipe
        security:
          - Bearer: []
        parameters:
          - in: path
            name: category_id
            required: true
            description: The id of recipe category
            type: int
          - in: path
            name: recipe_id
            required: true
            description: The id of recipe requested
            type: int
          - in: body
            name: body
            required: true
            description: Recipe's name, ingredients and directions
            type: string
            schema:
              properties:
                recipe_name:
                  type: string
                  default: Apple Cinnamon White Cake
                ingredients:
                  type: string
                  default: 1) 1 teaspoon ground cinnamon 2) 2/3 cup white sugar \
3) 1/2 cup butter, softened 4) 2 eggs 5) 1 1/2 teaspoons vanilla extract 6) 1 1/2 \
cups all-purpose flour 7) 1 3/4 teaspoons baking powder 8) 1/2 cup milk 9) 1 apple, \
peeled and chopped
                directions:
                  type: string
                  default: 1) Prepare the Espresso in a small cup. 2) Fill the mixing \
glass 3/4 full with ice cubes. Add the Benedictine and the Espresso. Cool, mixing the \
ingredients with the mixing spoon. 3) Pour into the glass, filtering the ice with a strainer. \
4) Shake the cream, which should be very cold, in the mini shaker until it becomes quite thick. \
5) Rest the cream on the surface of the cocktail, making it run down the back of the mixing spoon. \
6) Garnish with a light dusting of cocoa, and serve.
        responses:
          200:
            description: Recipe updated successfully
          404:
            description: Category/recipe with id could not be found
          500:
            description: Database could not be accessed
        """
        args = self.parser.parse_args()

        messages = {}
        messages['recipe_name_message'] = validate_recipe_name(args.recipe_name.strip(), \
                category_id=category_id, recipe_id=recipe_id)
        messages['ingredients_message'] = validate_ingredients(
            args.ingredients)
        messages['directions_message'] = validate_directions(args.directions)

        if not data_validator(messages):
            return jsonify(messages), 400

        try:
            category = Category.query.filter_by(id=category_id,
                                                user_id=user.id).first()
            if category:
                recipe = Recipe.query.filter_by(
                    id=recipe_id, category_id=category.id).first()
                if recipe:
                    recipe.recipe_name = args.recipe_name
                    recipe.ingredients = args.ingredients
                    recipe.directions = args.directions
                    recipe.save()
                    response = jsonify({
                        'id': recipe.id,
                        'recipe_name': recipe.recipe_name,
                        'ingredients': recipe.ingredients,
                        'directions': recipe.directions,
                        'category_id': recipe.category_id,
                        'date_created': recipe.date_created,
                        'date_modified': recipe.date_modified
                    })
                    response.status_code = 200
                else:
                    response = jsonify(
                        {'message': 'Sorry, recipe could not be found.'})
                    response.status_code = 404
            else:
                response = jsonify(
                    {'message': 'Sorry, recipe category could not be found.'})
                response.status_code = 404
        except exc.SQLAlchemyError as error:
            return jsonify({'message': str(error)}), 500
        return response
Пример #8
0
    def put(self, access_token, user, category_id):
        """
        Process PUT request
        ---
        tags:
          - Category
        security:
          - Bearer: []
        parameters:
          - in: path
            name: category_id
            required: true
            description: The id of category requested
            type: int
          - in: body
            name: body
            required: true
            description: Category's category name
            type: string
            schema:
              properties:
                category_name:
                  type: string
                  default: Snacks
        responses:
          200:
            description: Category updates successfully
          404:
            description: Category with category id could not be found
          500:
            description: Database could not be accessed
        """

        args = self.parser.parse_args()

        messages = {}
        messages['category_name_message'] = validate_category_name(args.category_name.strip(), user.id, \
                category_id=category_id)

        if not data_validator(messages):
            return jsonify(messages), 400

        try:
            category = Category.query.filter_by(id=category_id,
                                                user_id=user.id).first()
            if category:
                category.category_name = args.category_name
                category.save()
                response = jsonify({
                    'id': category.id,
                    'category_name': category.category_name,
                    'user_id': category.user_id,
                    'date_created': category.date_created,
                    'date_modified': category.date_modified
                })
                response.status_code = 200
            else:
                response = jsonify({
                    'message':
                    'Category with category id could not be found.'
                })
                response.status_code = 404
        except exc.SQLAlchemyError as error:
            return jsonify({'message': str(error)}), 500
        return response