Exemple #1
0
    def patch(self, user_resource_id):
        user_to_edit = users.get(user_resource_id)

        if user_to_edit is None:
            raise NotFound('User not found.')

        user_id = get_jwt_identity()

        if user_to_edit.id != user_id:
            raise BadRequest('You cannot edit profile of other person.')

        data: Dict = request.get_json()

        if data is None:
            raise InvalidPayload

        attributes = {
            'first_name', 'last_name', 'phone', 'street', 'zip_code', 'city',
            'country', 'date_of_birth'
        }

        if not any(data.get(attribute) for attribute in attributes):
            raise InvalidPayload

        try:
            users.update(user_to_edit, attributes, data)
        except (TypeError, ValueError) as e:
            raise BadRequest(str(e))

        return {'message': 'Profile successfully modified.'}
Exemple #2
0
    def get(self, product_id):
        product = products.get(product_id)

        if product is None:
            raise NotFound('Product not found.')

        return product
Exemple #3
0
    def post(self, product_id):
        data = request.get_json()

        product = products.get(product_id)

        if product is None:
            raise NotFound('Product not found.')

        user_id = get_jwt_identity()
        user = users.get(user_id)

        if products.get_product_rating_by_user(product, user) is not None:
            raise BadRequest('This user already rated this product.')

        rating = data.get('rating')

        if rating is None:
            raise InvalidPayload

        if not isinstance(rating, int):
            raise ProductRatingError

        if not (1 <= rating <= 5):
            raise ProductRatingError

        products.add_rating(product, user, rating)

        return {
            'message': 'Rating was successfully added.'
        }, status.HTTP_201_CREATED
Exemple #4
0
    def delete(self, product_id, image_id):
        if not products.has_image(product_id, image_id):
            raise NotFound('Image not found.')

        products.delete_image(image_id)

        return {'message': 'Image was successfully deleted.'}
Exemple #5
0
    def delete(self, product_id):
        product = products.get(product_id)
        if not product:
            raise NotFound('Product not found.')

        products.delete(product)

        return {'message': 'Product was successfully deleted.'}
Exemple #6
0
    def delete(self, product_id):
        product = products.get(product_id)

        if product is None:
            raise NotFound('Product not found.')

        user_id = get_jwt_identity()
        user = users.get(user_id)

        rating = products.get_product_rating_by_user(product, user)

        if rating is None:
            raise NotFound('Rating not found.')

        products.delete_rating(product, user)

        return {
            'message': 'Rating was successfully deleted.'
        }, status.HTTP_200_OK
Exemple #7
0
    def delete(self, category_id):
        category = categories.get(category_id)
        if not category:
            raise NotFound('Category not found.')

        if len(category.products) != 0:
            raise BadRequest('Category contains products.')

        categories.delete(category_id)

        return {'message': 'Category was successfully deleted.'}
Exemple #8
0
    def get(self, user_resource_id):
        user_to_get = users.get(user_resource_id)

        if user_to_get is None:
            raise NotFound('User not found.')

        user_id = get_jwt_identity()

        if user_to_get.id != user_id:
            raise BadRequest('You cannot get user profile of other person.')

        return user_to_get
Exemple #9
0
    def put(self, category_id):
        data = request.get_json()

        category = categories.get(category_id)
        if not category:
            raise NotFound('Category not found.')

        name = data.get('name')

        if name is None:
            raise InvalidPayload

        categories.change_name(category, name)

        return {'message': 'Category was successfully modified.'}
Exemple #10
0
    def put(self, product_id):
        data = request.get_json()

        product = products.get(product_id)
        if not product:
            raise NotFound('Product not found.')

        name = data.get('name')
        price = data.get('price')
        description = data.get('description')
        category_id = data.get('category_id')

        if name is None or price is None or category_id is None:
            raise InvalidPayload

        product.name = name
        product.price = price
        product.description = description
        product.category_id = category_id

        return {'message': 'Product was successfully modified.'}
Exemple #11
0
    def post(self, product_id):
        product = products.get(product_id)

        if product is None:
            raise NotFound('Product not found.')

        file = request.files.get('file')

        if file is None:
            raise InvalidPayload

        if not is_uploaded_file_allowed(file.filename):
            raise BadRequest('File extension not allowed.')

        # TODO move this shit to util function or something
        basedir = os.path.abspath(os.path.dirname(__file__))

        product_dir = os.path.join(basedir, '..',
                                   current_app.config.get('UPLOAD_FOLDER'),
                                   str(product_id))

        pathlib.Path(product_dir).mkdir(parents=True, exist_ok=True)

        current_timestamp = int(time.time())

        from werkzeug.utils import secure_filename
        secured_filename = secure_filename(ntpath.basename(file.filename))

        filename = f'{current_timestamp}_{secured_filename}'
        file_path = os.path.join(product_dir, filename)
        file.save(file_path)

        products.add_image(product, url=file_path)

        return {
            'message': 'Image was successfully uploaded.'
        }, status.HTTP_201_CREATED
Exemple #12
0
    def post(self, category_id):
        data = request.get_json()

        if not data:
            raise InvalidPayload

        name = data.get('name')
        price = data.get('price')
        description = data.get('description')

        if name is None or price is None:
            raise InvalidPayload

        category = categories.get(category_id)

        if category is None:
            raise NotFound('Category not found.')

        product = Product(name=name, price=price, description=description)
        categories.add_product(category, product)

        return {
            'message': 'Product was successfully added.'
        }, status.HTTP_201_CREATED