示例#1
0
    def post(self):

        json_data = request.get_json()
        schema = UserSchema()
        try:
            data = schema.load(json_data)
        except ValidationError as err:
            return err.messages, HTTPStatus.UNPROCESSABLE_ENTITY

        if UserModel.find_by_username(data.get("username")):
            return {
                'message': "User already exists. Please Log in.",
                'status': 'fail'
            }, HTTPStatus.BAD_REQUEST

        try:
            new_user = UserModel(username=data.get('username'),
                                 password=UserModel.generate_hash(
                                     data['password']),
                                 access=0)

            new_user.save_to_db()
            clear_cache('/users')
            return UserSchema().dump(new_user), HTTPStatus.CREATED

        except Exception as e:
            print(e)
            return {
                'message': 'Something went wrong'
            }, HTTPStatus.INTERNAL_SERVER_ERROR
示例#2
0
    def post(self):
        json_data = request.get_json()
        schema = BrandSchema(exclude=("hyperlink", ))
        try:
            data = schema.load(json_data)
        except ValidationError as err:
            return err.messages

        if BrandModel.find_by_name(data.get("name")):
            return {
                'message':
                "A brand with name '{}' already exists. Please choose other refence name."
                .format(data.get("name")),
                'status':
                'fail'
            }, HTTPStatus.BAD_REQUEST

        try:
            new_brand = BrandModel(**data)
            new_brand.createdBy_id = current_user.id
            new_brand.save_to_db()
            clear_cache('/brands')
            return schema.dump(new_brand), HTTPStatus.CREATED

        except Exception as e:
            print(e)
            return {
                'message': 'Something went wrong'
            }, HTTPStatus.INTERNAL_SERVER_ERROR
示例#3
0
    def put(self, id):

        json_data = request.get_json()
        try:
            data = product_schema.load(data=json_data, partial=('name', ))
        except ValidationError as err:
            return err.messages

        product = ProductModel.find_by_id(id)

        if product is None:
            return {'message': 'Product not found'}, HTTPStatus.NOT_FOUND

        product.name = data.get('name') or product.name
        product.description = data.get('description') or product.description
        product.partNumber = data.get('partNumber') or product.partNumber
        if data.get('brand_id') and BrandModel.find_by_id(
                data.get('brand_id')):
            product.brand_id = data.get('brand_id') or product.brand_id

        product.updatedBy_id = current_user.id

        product.save_to_db()

        clear_cache('/products')

        return product_schema.dump(product), HTTPStatus.OK
示例#4
0
    def delete(self, id):

        brand = BrandModel.find_by_id(id=id)

        if brand is None:
            return {'message': 'Brand not found'}, HTTPStatus.NOT_FOUND

        brand.delete_from_db()

        clear_cache('/brands')

        return {}, HTTPStatus.NO_CONTENT
示例#5
0
    def delete(self, id):

        user = UserModel.find_by_id(id=id)

        if user is None:
            return {'message': 'User not found'}, HTTPStatus.NOT_FOUND

        user.delete_from_db()

        clear_cache('/users')

        return {}, HTTPStatus.NO_CONTENT
示例#6
0
    def delete(self, id):

        product = ProductModel.find_by_id(id=id)

        if product is None:
            return {'message': 'Product not found'}, HTTPStatus.NOT_FOUND

        product.delete_from_db()

        clear_cache('/products')

        return {}, HTTPStatus.NO_CONTENT
示例#7
0
    def put(self, id):

        json_data = request.get_json()
        try:
            data = brand_schema.load(data=json_data, partial=('name', ))
        except ValidationError as err:
            return err.messages

        brand = BrandModel.find_by_id(id)

        if brand is None:
            return {'message': 'Brand not found'}, HTTPStatus.NOT_FOUND

        brand.name = data.get('name') or brand.name
        brand.website = data.get('website') or brand.website

        brand.updatedBy_id = current_user.id

        brand.save_to_db()

        clear_cache('/brands')

        return brand_schema.dump(brand), HTTPStatus.OK
示例#8
0
    def put(self, id):

        json_data = request.get_json()
        try:
            data = user_schema.load(data=json_data, partial=('username', ))
        except ValidationError as err:
            return err.messages

        user = UserModel.find_by_id(id)

        if user is None:
            return {'message': 'User not found'}, HTTPStatus.NOT_FOUND

        user.password = UserModel.generate_hash(
            data.get('password')) or user.password

        if current_user.is_admin():
            user.access = ACCESS[data.get('access')] or user.access

        user.save_to_db()

        clear_cache('/users')

        return user_schema.dump(user), HTTPStatus.OK