def post(self, user_in_session):
        """
       Create category
       ---
       tags:
         - Categories Endpoints
       parameters:
         - in: body
           name: Category details
           description: Create category by providing category name
           type: string
           required: true
           schema:
             id: create_categories
             properties:
               category_name:
                 default: Lunch
       responses:
         201:
           description: Category created successfully
         409:
           description: Category exists!
         400:
           description: Invalid category name given
         422:
           description: Please fill all the fields
        """
        category_name = str(request.data.get('category_name', '')).strip()
        print(category_name)
        check_category_exist = Categories.query.filter_by(users_id=user_in_session).all()

        if not category_name:
            return make_response(jsonify({'message': 'Please fill all the fields'})), 400

        if not re.search(self.regex_category_name, category_name):
            # This checks whether the category name matches the pattern specified.
            return make_response(jsonify({'message': 'Invalid category name given'})), 400

        for category_in_list in check_category_exist:
            category_name_in_list = category_in_list.category_name
            if category_name.upper() == category_name_in_list.upper():
                return make_response(jsonify({'message': 'Category exists!'})), 409

        categories = Categories(category_name=category_name, users_id=user_in_session)
        categories.save()
        # This saves the new categories after it passes all the conditions.
        return make_response(jsonify({'message': 'Category created successfully'})), 201
예제 #2
0
    def post(self, current_user):
        """Method to add a new category to the endpoint
        ---
        tags:
            - Categories
        produces:
            - application/json
        security:
          - TokenHeader: []
        parameters:
            - in: body
              name: Category Name
              descrption: Name of the category
              required: true
              type: string
              schema:
                id: categories create
                properties:
                  category_name:
                    type: string
                    default: Breakfast


        responses:
          200:
            schema:
              id: categories
              properties:
                category_name:
                  type: string
                  default: Breakfast
                created_by:
                 type: integer
                 default: 2
                date_created:
                 type: string
                 default: Wed 20 Dec
                date_modified:
                 type: string
                 default: Wed 20 Dec
                id:
                 type: integer
                 default: 1
          400:
            description: category name not valid
          400:
            description: category name not provided
          400:
            description: category name exists
          401:
            description: category not created because user is unauthenticated
          201:
            description: category created
        """

        try:
            category_name = str(request.data.get('category_name', ''))
            category_details = Categories.query.filter_by(
                category_name=category_name,
                created_by=current_user.id).first()
            category_validation(category_name)
            if category_details:
                response = {'message': 'Category name exists'}
                return make_response(jsonify(response)), 400

            category = Categories(category_name=category_name,
                                  created_by=current_user.id)
            category.save()
            response = jsonify({
                'id': category.id,
                'category_name': category.category_name,
                'created_by': category.created_by,
                'date_created': category.date_created,
                'date_modified': category.date_modified
            })
            response.status_code = 201
            return response
        except Exception as e:
            response = {'message': str(e)}
            return make_response(jsonify(response)), 400