Exemple #1
0
    def post(self):
        """ Endpoint to create the user """

        request_data = request.get_json()
        UserValidators.validate(request_data)

        request_data = request_data_strip(request_data)

        bytes_password = bytes(request_data['password'], encoding='utf-8')
        hashed = bcrypt.hashpw(bytes_password, bcrypt.gensalt(10))
        request_data['password'] = hashed.decode('utf-8')

        new_user = User(**request_data)
        new_user.save()

        user_schema = UserSchema()
        user_data = user_schema.dump(new_user)

        send_email(user_data, 'Confirmation Email', 'confirmation_email.html')

        return {
            'status':
            'success',
            'message':
            'User successfully created. Please check your email to continue.'
        }, 201
Exemple #2
0
    def post(self):
        """ Endpoint to add items to cart """

        request_data = request.get_json()
        CartValidators.validate_item(request_data)
        request_data = request_data_strip(request_data)

        user_id = request.decoded_token['user']['id']
        cart = Cart.query.filter_by(user_id=user_id).first()
        request_data.update({'cart_id': cart.id})
        cart_item = CartItem.query.filter_by(
            cart_id=cart.id, product_id=request_data['product_id']).first()
        product = Product.find_by_id(request_data['product_id'])

        if cart_item:
            updated_quantity = product.quantity - request_data['quantity']
            request_data['quantity'] += cart_item.quantity
            CartValidators.validate_item(request_data)
            cart_item.update(request_data)
            product.update({'quantity': updated_quantity})

        else:
            new_cart_item = CartItem(**request_data)
            new_cart_item.save()
            new_quantity = product.quantity - request_data['quantity']
            product.update({'quantity': new_quantity})

        cart_schema = CartSchema(exclude=EXCLUDED_FIELDS)
        success_response['message'] = 'Item successfully added to the cart'
        success_response['data'] = {
            'cart': cart_schema.dump(cart)
        }

        return success_response, 200
    def post(self):
        """ Endpoint to create the category """

        request_data = request.get_json()
        ProductValidators.validate(request_data)

        request_data = request_data_strip(request_data)
        request_data['name'] = request_data['name'].lower()
        new_product = Product(**request_data)
        new_product.save()

        product_schema = ProductSchema()
        product_data = product_schema.dump(new_product)

        success_response['message'] = 'Product successfully created'
        success_response['data'] = {'product': product_data}
        return success_response, 201
Exemple #4
0
    def post(self):
        """ Endpoint to create the category """

        request_data = request.get_json()
        CategoryValidators.validate(request_data)

        request_data = request_data_strip(request_data)
        request_data['name'] = request_data['name'].lower()
        new_category = Category(**request_data)
        new_category.save()

        category_schema = CategorySchema()
        category_data = category_schema.dump(new_category)

        success_response['message'] = 'Category successfully created'
        success_response['data'] = {'category': category_data}

        return success_response, 201
Exemple #5
0
    def post(self):
        """ Endpoint to create the brand """

        request_data = request.get_json()
        BrandValidators.validate(request_data)

        request_data = request_data_strip(request_data)
        request_data['name'] = request_data['name'].lower()

        new_brand = Brand(**request_data)
        new_brand.save()

        brand_schema = BrandSchema()
        brand_data = brand_schema.dump(new_brand)

        success_response['message'] = 'Brand successfully created'
        success_response['data'] = {'brand': brand_data}

        return success_response, 201
Exemple #6
0
    def put(self, category_id):
        """" Endpoint to update category """

        category_schema = CategorySchema()
        category = Category.find_by_id(category_id)

        if not category:
            error_response['message'] = 'Category not found'
            return error_response, 404

        request_data = request.get_json()
        CategoryValidators.validate(request_data, category_id=category_id)
        request_data = request_data_strip(request_data)
        request_data['name'] = request_data['name'].lower()

        category.update(request_data)

        success_response['message'] = 'Category successfully updated'
        success_response['data'] = {'category': category_schema.dump(category)}

        return success_response, 200
    def put(self, product_id):
        """" Endpoint to update product """

        product_schema = ProductSchema()
        product = Product.find_by_id(product_id)

        if not product:
            error_response['message'] = 'Product not found'
            return error_response, 404

        request_data = request.get_json()
        ProductValidators.validate(request_data, product_id=product_id)
        request_data = request_data_strip(request_data)
        request_data['name'] = request_data['name'].lower()

        product.update(request_data)

        success_response['message'] = 'Product successfully updated'
        success_response['data'] = {'product': product_schema.dump(product)}

        return success_response, 200
Exemple #8
0
    def put(self, brand_id):
        """" Endpoint to update brand """

        brand_schema = BrandSchema()
        brand = Brand.find_by_id(brand_id)

        if not brand:
            error_response['message'] = 'Brand not found'
            return error_response, 404

        request_data = request.get_json()
        BrandValidators.validate(request_data, brand_id=brand_id)
        request_data = request_data_strip(request_data)
        request_data['name'] = request_data['name'].lower()

        brand.update(request_data)

        success_response['message'] = 'Brand successfully updated'
        success_response['data'] = {'brand': brand_schema.dump(brand)}

        return success_response, 200
Exemple #9
0
    def patch(self, token):
        """ Endpoint to rest user password """

        user = verify_user_token(token)

        if not user:
            error_response['message'] = 'Password reset token is invalid'
            return error_response, 400

        request_data = request.get_json()
        UserValidators.validate_password(request_data['password'])
        request_data = request_data_strip(request_data)
        bytes_password = bytes(request_data['password'], encoding='utf-8')
        hashed = bcrypt.hashpw(bytes_password, bcrypt.gensalt(10))
        password = hashed.decode('utf-8')

        user.update({'password': password})

        return {
            'status': 'success',
            'message': 'User password successfully changed'
        }, 200