Esempio n. 1
0
    def handle_get(self, request, user, *args, **kwargs):
        """Treat requests GET to list all the Model by Brand.

        URL: model/brand/<id_brand>/
        """
        try:

            self.log.info("GET to list all the Model by Brand")

            # User permission
            if not has_perm(user, AdminPermission.BRAND_MANAGEMENT, AdminPermission.READ_OPERATION):
                self.log.error(
                    u'User does not have permission to perform the operation.')
                raise UserNotAuthorizedError(None)

            id_brand = kwargs.get('id_brand')

            # Valid ID Brand
            if not is_valid_int_greater_zero_param(id_brand):
                self.log.error(
                    u'The id_brand parameter is not a valid value: %s.', id_brand)
                raise InvalidValueError(None, 'id_groupl3', id_brand)

            # Find Brand by ID to check if it exist
            Marca.get_by_pk(id_brand)

            model_list = []
            for model in Modelo.get_by_brand(id_brand):
                model_map = dict()
                model_map['id'] = model.id
                model_map['nome'] = model.nome
                model_map['id_marca'] = model.marca.id
                model_map['nome_marca'] = model.marca.nome
                model_list.append(model_map)

            return self.response(dumps_networkapi({'model': model_list}))

        except InvalidValueError, e:
            return self.response_error(269, e.param, e.value)
    def handle_get(self, request, user, *args, **kwargs):
        """Treat requests GET to list all the Model by Brand.

        URL: model/brand/<id_brand>/
        """
        try:

            self.log.info('GET to list all the Model by Brand')

            # User permission
            if not has_perm(user, AdminPermission.BRAND_MANAGEMENT, AdminPermission.READ_OPERATION):
                self.log.error(
                    u'User does not have permission to perform the operation.')
                raise UserNotAuthorizedError(None)

            id_brand = kwargs.get('id_brand')

            # Valid ID Brand
            if not is_valid_int_greater_zero_param(id_brand):
                self.log.error(
                    u'The id_brand parameter is not a valid value: %s.', id_brand)
                raise InvalidValueError(None, 'id_groupl3', id_brand)

            # Find Brand by ID to check if it exist
            Marca.get_by_pk(id_brand)

            model_list = []
            for model in Modelo.get_by_brand(id_brand):
                model_map = dict()
                model_map['id'] = model.id
                model_map['nome'] = model.nome
                model_map['id_marca'] = model.marca.id
                model_map['nome_marca'] = model.marca.nome
                model_list.append(model_map)

            return self.response(dumps_networkapi({'model': model_list}))

        except InvalidValueError, e:
            return self.response_error(269, e.param, e.value)
    def handle_post(self, request, user, *args, **kwargs):
        """Treat requests POST to add Brand.

        URL: brand/
        """

        try:

            self.log.info("Add Brand")

            # User permission
            if not has_perm(user, AdminPermission.BRAND_MANAGEMENT, AdminPermission.WRITE_OPERATION):
                self.log.error(
                    u'User does not have permission to perform the operation.')
                raise UserNotAuthorizedError(None)

            # Load XML data
            xml_map, attrs_map = loads(request.raw_post_data)

            # XML data format
            networkapi_map = xml_map.get('networkapi')
            if networkapi_map is None:
                return self.response_error(3, u'There is no value to the networkapi tag  of XML request.')

            brand_map = networkapi_map.get('brand')
            if brand_map is None:
                return self.response_error(3, u'There is no value to the brand tag  of XML request.')

            # Get XML data
            name = brand_map.get('name')

            # Valid name
            if not is_valid_string_minsize(name, 3) or not is_valid_string_maxsize(name, 100):
                self.log.error(u'Parameter name is invalid. Value: %s', name)
                raise InvalidValueError(None, 'name', name)

            try:
                Marca.get_by_name(name)
                raise MarcaNameDuplicatedError(
                    None, u'Marca com o nome %s já cadastrada.' % name)
            except MarcaNotFoundError:
                pass

            brand = Marca()

            # set variables
            brand.nome = name

            try:
                # save Brand
                brand.save(user)
            except Exception, e:
                self.log.error(u'Failed to save the Brand.')
                raise EquipamentoError(e, u'Failed to save the Brand.')

            brand_map = dict()
            brand_map['brand'] = model_to_dict(brand, exclude=["nome"])

            return self.response(dumps_networkapi(brand_map))
    def handle_delete(self, request, user, *args, **kwargs):
        """Treat requests DELETE to remove Brand.

        URL: brand/<id_brand>/
        """
        try:

            self.log.info("Remove Brand")

            # User permission
            if not has_perm(user, AdminPermission.BRAND_MANAGEMENT,
                            AdminPermission.WRITE_OPERATION):
                self.log.error(
                    u'User does not have permission to perform the operation.')
                raise UserNotAuthorizedError(None)

            id_brand = kwargs.get('id_brand')

            # Valid ID Brand
            if not is_valid_int_greater_zero_param(id_brand):
                self.log.error(
                    u'The id_brand parameter is not a valid value: %s.',
                    id_brand)
                raise InvalidValueError(None, 'id_brand', id_brand)

            # Find Brand by ID to check if it exist
            brand = Marca.get_by_pk(id_brand)

            with distributedlock(LOCK_BRAND % id_brand):

                try:

                    if brand.modelo_set.count() > 0:
                        raise MarcaUsedByModeloError(
                            None,
                            u"A marca %d tem modelo associado." % brand.id)

                    # remove Brand
                    brand.delete()

                except MarcaUsedByModeloError, e:
                    raise e
                except Exception, e:
                    self.log.error(u'Failed to remove the Brand.')
                    raise EquipamentoError(e, u'Failed to remove the Brand.')
    def handle_delete(self, request, user, *args, **kwargs):
        """Treat requests DELETE to remove Brand.

        URL: brand/<id_brand>/
        """
        try:

            self.log.info("Remove Brand")

            # User permission
            if not has_perm(user, AdminPermission.BRAND_MANAGEMENT, AdminPermission.WRITE_OPERATION):
                self.log.error(
                    u'User does not have permission to perform the operation.')
                raise UserNotAuthorizedError(None)

            id_brand = kwargs.get('id_brand')

            # Valid ID Brand
            if not is_valid_int_greater_zero_param(id_brand):
                self.log.error(
                    u'The id_brand parameter is not a valid value: %s.', id_brand)
                raise InvalidValueError(None, 'id_brand', id_brand)

            # Find Brand by ID to check if it exist
            brand = Marca.get_by_pk(id_brand)

            with distributedlock(LOCK_BRAND % id_brand):

                try:

                    if brand.modelo_set.count() > 0:
                        raise MarcaUsedByModeloError(
                            None, u"A marca %d tem modelo associado." % brand.id)

                    # remove Brand
                    brand.delete(user)

                except MarcaUsedByModeloError, e:
                    raise e
                except Exception, e:
                    self.log.error(u'Failed to remove the Brand.')
                    raise EquipamentoError(e, u'Failed to remove the Brand.')
    def handle_post(self, request, user, *args, **kwargs):
        """Treat requests POST to add Model.

        URL: model/
        """

        try:

            self.log.info("Add Model")

            # User permission
            if not has_perm(user, AdminPermission.BRAND_MANAGEMENT, AdminPermission.WRITE_OPERATION):
                self.log.error(
                    u'User does not have permission to perform the operation.')
                raise UserNotAuthorizedError(None)

            # Load XML data
            xml_map, attrs_map = loads(request.raw_post_data)

            # XML data format
            networkapi_map = xml_map.get('networkapi')
            if networkapi_map is None:
                return self.response_error(3, u'There is no value to the networkapi tag  of XML request.')

            model_map = networkapi_map.get('model')
            if model_map is None:
                return self.response_error(3, u'There is no value to the model tag  of XML request.')

            # Get XML data
            name = model_map.get('name')
            id_brand = model_map.get('id_brand')

            # Valid name
            if not is_valid_string_minsize(name, 3) or not is_valid_string_maxsize(name, 100):
                self.log.error(u'Parameter name is invalid. Value: %s', name)
                raise InvalidValueError(None, 'name', name)

            # Valid ID Brand
            if not is_valid_int_greater_zero_param(id_brand):
                self.log.error(
                    u'The id_brand parameter is not a valid value: %s.', id_brand)
                raise InvalidValueError(None, 'id_brand', id_brand)

            # Find Brand by ID to check if it exist
            brand = Marca.get_by_pk(id_brand)

            try:
                Modelo.get_by_name_brand(name, id_brand)
                raise MarcaModeloNameDuplicatedError(
                    None, u'Já existe um modelo com o nome %s com marca %s.' % (name, brand.nome))
            except ModeloNotFoundError:
                pass

            model = Modelo()

            # set variables
            model.nome = name
            model.marca = brand

            try:
                # save Model
                model.save()
            except Exception, e:
                self.log.error(u'Failed to save the Model.')
                raise EquipamentoError(e, u'Failed to save the Model.')

            model_map = dict()
            model_map['model'] = model_to_dict(
                model, exclude=["nome", "marca"])

            return self.response(dumps_networkapi(model_map))
Esempio n. 7
0
    def handle_put(self, request, user, *args, **kwargs):
        """Treat requests PUT to edit Model.

        URL: model/<id_model>/
        """
        try:

            self.log.info('Edit Model')

            # User permission
            if not has_perm(user, AdminPermission.BRAND_MANAGEMENT,
                            AdminPermission.WRITE_OPERATION):
                self.log.error(
                    u'User does not have permission to perform the operation.')
                raise UserNotAuthorizedError(None)

            id_model = kwargs.get('id_model')

            # Load XML data
            xml_map, attrs_map = loads(request.raw_post_data)

            # XML data format

            networkapi_map = xml_map.get('networkapi')
            if networkapi_map is None:
                return self.response_error(
                    3,
                    u'There is no value to the networkapi tag  of XML request.'
                )

            model_map = networkapi_map.get('model')
            if model_map is None:
                return self.response_error(
                    3, u'There is no value to the model tag  of XML request.')

            # Get XML data
            name = model_map.get('name')
            id_brand = model_map.get('id_brand')

            # Valid ID Model
            if not is_valid_int_greater_zero_param(id_model):
                self.log.error(
                    u'The id_model parameter is not a valid value: %s.',
                    id_model)
                raise InvalidValueError(None, 'id_model', id_model)

            # Valid name
            if not is_valid_string_minsize(
                    name, 3) or not is_valid_string_maxsize(name, 100):
                self.log.error(u'Parameter name is invalid. Value: %s', name)
                raise InvalidValueError(None, 'name', name)

            # Valid ID Brand
            if not is_valid_int_greater_zero_param(id_brand):
                self.log.error(
                    u'The id_brand parameter is not a valid value: %s.',
                    id_brand)
                raise InvalidValueError(None, 'id_brand', id_brand)

            # Find Brand by ID to check if it exist
            brand = Marca.get_by_pk(id_brand)

            # Find Model by ID to check if it exist
            model = Modelo.get_by_pk(id_model)

            with distributedlock(LOCK_MODEL % id_model):

                try:

                    if not (model.nome.lower() == name.lower()
                            and model.marca.id == id_brand):
                        Modelo.get_by_name_brand(name, id_brand)
                        raise MarcaModeloNameDuplicatedError(
                            None,
                            u'Já existe um modelo com o nome %s com marca %s.'
                            % (name, brand.nome))
                except ModeloNotFoundError:
                    pass

                # set variables
                model.nome = name
                model.marca = brand

                try:
                    # update Model
                    model.save()
                except Exception, e:
                    self.log.error(u'Failed to update the Model.')
                    raise EquipamentoError(e, u'Failed to update the Model.')

                return self.response(dumps_networkapi({}))

        except InvalidValueError, e:
            return self.response_error(269, e.param, e.value)
    def handle_put(self, request, user, *args, **kwargs):
        """Treat requests PUT to edit Brand.

        URL: brand/<id_brand>/
        """
        try:

            self.log.info("Edit Brand")

            # User permission
            if not has_perm(user, AdminPermission.BRAND_MANAGEMENT,
                            AdminPermission.WRITE_OPERATION):
                self.log.error(
                    u'User does not have permission to perform the operation.')
                raise UserNotAuthorizedError(None)

            id_brand = kwargs.get('id_brand')

            # Load XML data
            xml_map, attrs_map = loads(request.raw_post_data)

            # XML data format

            networkapi_map = xml_map.get('networkapi')
            if networkapi_map is None:
                return self.response_error(
                    3,
                    u'There is no value to the networkapi tag  of XML request.'
                )

            brand_map = networkapi_map.get('brand')
            if brand_map is None:
                return self.response_error(
                    3, u'There is no value to the brand tag  of XML request.')

            # Get XML data
            name = brand_map.get('name')

            # Valid ID Brand
            if not is_valid_int_greater_zero_param(id_brand):
                self.log.error(
                    u'The id_brand parameter is not a valid value: %s.',
                    id_brand)
                raise InvalidValueError(None, 'id_brand', id_brand)

            # Valid name
            if not is_valid_string_minsize(
                    name, 3) or not is_valid_string_maxsize(name, 100):
                self.log.error(u'Parameter name is invalid. Value: %s', name)
                raise InvalidValueError(None, 'name', name)

            # Find Brand by ID to check if it exist
            brand = Marca.get_by_pk(id_brand)

            with distributedlock(LOCK_BRAND % id_brand):

                try:
                    if brand.nome.lower() != name.lower():
                        Marca.get_by_name(name)
                        raise MarcaNameDuplicatedError(
                            None, u'Marca com o nome %s já cadastrada.' % name)
                except MarcaNotFoundError:
                    pass

                # set variables
                brand.nome = name

                try:
                    # update Brand
                    brand.save()
                except Exception, e:
                    self.log.error(u'Failed to update the Brand.')
                    raise EquipamentoError(e, u'Failed to update the Brand.')

                return self.response(dumps_networkapi({}))

        except InvalidValueError, e:
            return self.response_error(269, e.param, e.value)
    def handle_put(self, request, user, *args, **kwargs):
        """Treat requests PUT to edit Brand.

        URL: brand/<id_brand>/
        """
        try:

            self.log.info("Edit Brand")

            # User permission
            if not has_perm(user, AdminPermission.BRAND_MANAGEMENT, AdminPermission.WRITE_OPERATION):
                self.log.error(
                    u'User does not have permission to perform the operation.')
                raise UserNotAuthorizedError(None)

            id_brand = kwargs.get('id_brand')

            # Load XML data
            xml_map, attrs_map = loads(request.raw_post_data)

            # XML data format

            networkapi_map = xml_map.get('networkapi')
            if networkapi_map is None:
                return self.response_error(3, u'There is no value to the networkapi tag  of XML request.')

            brand_map = networkapi_map.get('brand')
            if brand_map is None:
                return self.response_error(3, u'There is no value to the brand tag  of XML request.')

            # Get XML data
            name = brand_map.get('name')

            # Valid ID Brand
            if not is_valid_int_greater_zero_param(id_brand):
                self.log.error(
                    u'The id_brand parameter is not a valid value: %s.', id_brand)
                raise InvalidValueError(None, 'id_brand', id_brand)

            # Valid name
            if not is_valid_string_minsize(name, 3) or not is_valid_string_maxsize(name, 100):
                self.log.error(u'Parameter name is invalid. Value: %s', name)
                raise InvalidValueError(None, 'name', name)

            # Find Brand by ID to check if it exist
            brand = Marca.get_by_pk(id_brand)

            with distributedlock(LOCK_BRAND % id_brand):

                try:
                    if brand.nome.lower() != name.lower():
                        Marca.get_by_name(name)
                        raise MarcaNameDuplicatedError(
                            None, u'Marca com o nome %s já cadastrada.' % name)
                except MarcaNotFoundError:
                    pass

                # set variables
                brand.nome = name

                try:
                    # update Brand
                    brand.save(user)
                except Exception, e:
                    self.log.error(u'Failed to update the Brand.')
                    raise EquipamentoError(e, u'Failed to update the Brand.')

                return self.response(dumps_networkapi({}))

        except InvalidValueError, e:
            return self.response_error(269, e.param, e.value)
    def handle_put(self, request, user, *args, **kwargs):
        """Treat requests PUT to edit Model.

        URL: model/<id_model>/
        """
        try:

            self.log.info("Edit Model")

            # User permission
            if not has_perm(user, AdminPermission.BRAND_MANAGEMENT, AdminPermission.WRITE_OPERATION):
                self.log.error(
                    u'User does not have permission to perform the operation.')
                raise UserNotAuthorizedError(None)

            id_model = kwargs.get('id_model')

            # Load XML data
            xml_map, attrs_map = loads(request.raw_post_data)

            # XML data format

            networkapi_map = xml_map.get('networkapi')
            if networkapi_map is None:
                return self.response_error(3, u'There is no value to the networkapi tag  of XML request.')

            model_map = networkapi_map.get('model')
            if model_map is None:
                return self.response_error(3, u'There is no value to the model tag  of XML request.')

            # Get XML data
            name = model_map.get('name')
            id_brand = model_map.get('id_brand')

            # Valid ID Model
            if not is_valid_int_greater_zero_param(id_model):
                self.log.error(
                    u'The id_model parameter is not a valid value: %s.', id_model)
                raise InvalidValueError(None, 'id_model', id_model)

            # Valid name
            if not is_valid_string_minsize(name, 3) or not is_valid_string_maxsize(name, 100):
                self.log.error(u'Parameter name is invalid. Value: %s', name)
                raise InvalidValueError(None, 'name', name)

            # Valid ID Brand
            if not is_valid_int_greater_zero_param(id_brand):
                self.log.error(
                    u'The id_brand parameter is not a valid value: %s.', id_brand)
                raise InvalidValueError(None, 'id_brand', id_brand)

            # Find Brand by ID to check if it exist
            brand = Marca.get_by_pk(id_brand)

            # Find Model by ID to check if it exist
            model = Modelo.get_by_pk(id_model)

            with distributedlock(LOCK_MODEL % id_model):

                try:

                    if not (model.nome.lower() == name.lower() and model.marca.id == id_brand):
                        Modelo.get_by_name_brand(name, id_brand)
                        raise MarcaModeloNameDuplicatedError(
                            None, u'Já existe um modelo com o nome %s com marca %s.' % (name, brand.nome))
                except ModeloNotFoundError:
                    pass

                # set variables
                model.nome = name
                model.marca = brand

                try:
                    # update Model
                    model.save()
                except Exception, e:
                    self.log.error(u'Failed to update the Model.')
                    raise EquipamentoError(e, u'Failed to update the Model.')

                return self.response(dumps_networkapi({}))

        except InvalidValueError, e:
            return self.response_error(269, e.param, e.value)
    def handle_post(self, request, user, *args, **kwargs):
        """Treat requests POST to add Model.

        URL: model/
        """

        try:

            self.log.info("Add Model")

            # User permission
            if not has_perm(user, AdminPermission.BRAND_MANAGEMENT,
                            AdminPermission.WRITE_OPERATION):
                self.log.error(
                    u'User does not have permission to perform the operation.')
                raise UserNotAuthorizedError(None)

            # Load XML data
            xml_map, attrs_map = loads(request.raw_post_data)

            # XML data format
            networkapi_map = xml_map.get('networkapi')
            if networkapi_map is None:
                return self.response_error(
                    3,
                    u'There is no value to the networkapi tag  of XML request.'
                )

            model_map = networkapi_map.get('model')
            if model_map is None:
                return self.response_error(
                    3, u'There is no value to the model tag  of XML request.')

            # Get XML data
            name = model_map.get('name')
            id_brand = model_map.get('id_brand')

            # Valid name
            if not is_valid_string_minsize(
                    name, 3) or not is_valid_string_maxsize(name, 100):
                self.log.error(u'Parameter name is invalid. Value: %s', name)
                raise InvalidValueError(None, 'name', name)

            # Valid ID Brand
            if not is_valid_int_greater_zero_param(id_brand):
                self.log.error(
                    u'The id_brand parameter is not a valid value: %s.',
                    id_brand)
                raise InvalidValueError(None, 'id_brand', id_brand)

            # Find Brand by ID to check if it exist
            brand = Marca.get_by_pk(id_brand)

            try:
                Modelo.get_by_name_brand(name, id_brand)
                raise MarcaModeloNameDuplicatedError(
                    None, u'Já existe um modelo com o nome %s com marca %s.' %
                    (name, brand.nome))
            except ModeloNotFoundError:
                pass

            model = Modelo()

            # set variables
            model.nome = name
            model.marca = brand

            try:
                # save Model
                model.save()
            except Exception, e:
                self.log.error(u'Failed to save the Model.')
                raise EquipamentoError(e, u'Failed to save the Model.')

            model_map = dict()
            model_map['model'] = model_to_dict(model,
                                               exclude=["nome", "marca"])

            return self.response(dumps_networkapi(model_map))
Esempio n. 12
0
    def handle_post(self, request, user, *args, **kwargs):
        """Treat requests POST to add Brand.

        URL: brand/
        """

        try:

            self.log.info('Add Brand')

            # User permission
            if not has_perm(user, AdminPermission.BRAND_MANAGEMENT,
                            AdminPermission.WRITE_OPERATION):
                self.log.error(
                    u'User does not have permission to perform the operation.')
                raise UserNotAuthorizedError(None)

            # Load XML data
            xml_map, attrs_map = loads(request.raw_post_data)

            # XML data format
            networkapi_map = xml_map.get('networkapi')
            if networkapi_map is None:
                return self.response_error(
                    3,
                    u'There is no value to the networkapi tag  of XML request.'
                )

            brand_map = networkapi_map.get('brand')
            if brand_map is None:
                return self.response_error(
                    3, u'There is no value to the brand tag  of XML request.')

            # Get XML data
            name = brand_map.get('name')

            # Valid name
            if not is_valid_string_minsize(
                    name, 3) or not is_valid_string_maxsize(name, 100):
                self.log.error(u'Parameter name is invalid. Value: %s', name)
                raise InvalidValueError(None, 'name', name)

            try:
                Marca.get_by_name(name)
                raise MarcaNameDuplicatedError(
                    None, u'Marca com o nome %s já cadastrada.' % name)
            except MarcaNotFoundError:
                pass

            brand = Marca()

            # set variables
            brand.nome = name

            try:
                # save Brand
                brand.save()
            except Exception, e:
                self.log.error(u'Failed to save the Brand.')
                raise EquipamentoError(e, u'Failed to save the Brand.')

            brand_map = dict()
            brand_map['brand'] = model_to_dict(brand, exclude=['nome'])

            return self.response(dumps_networkapi(brand_map))