def add_category(): new_category = request.get_json() new_category = Category ( category_name = new_category.get("category_name"), ) new_category.add() return jsonify(new_category.to_dict())
def post(current_user, self): """ Create a category --- tags: - categories parameters: - in: body name: category required: true description: The name of the category type: string schema: id: category properties: name: type: string default: soup security: - TokenHeader: [] responses: 200: description: Create a category schema: id: category properties: name: type: string default: soup """ request_dict = request.get_json() if not request_dict: return {'message': 'No output data provided'}, 400 errors = category_schema.validate(request_dict) if errors: return {'message': errors}, 400 category_name = request_dict['name'].strip() if not Category.is_unique( id=0, name=category_name, user_id=current_user.id): return { 'message': 'A category with the same name already exists' }, 400 error, validated_name = Category.validate_data(ctx=category_name) if validated_name: category = Category(category_name, user_id=current_user.id) category.add(category) result = {'message': 'Category successfully added!'} return result, status.HTTP_201_CREATED else: return {'message': error}, 400