コード例 #1
0
    def handle_post(self, request, user, *args, **kwargs):
        """Treat requests POST to add Script.

        URL: script/
        """

        try:

            self.log.info("Add Script")

            # User permission
            if not has_perm(user, AdminPermission.SCRIPT_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.'
                )

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

            # Get XML data
            script = script_map.get('script')
            id_script_type = script_map.get('id_script_type')
            model = script_map.get('model')
            description = script_map.get('description')

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

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

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

            # Find Script Type by ID to check if it exist
            script_type = TipoRoteiro.get_by_pk(id_script_type)

            try:
                Roteiro.get_by_name_script(script, id_script_type)
                raise RoteiroNameDuplicatedError(
                    None,
                    u'Já existe um roteiro com o nome %s com tipo de roteiro %s.'
                    % (script, script_type.tipo))
            except RoteiroNotFoundError:
                pass

            scr = Roteiro()

            # set variables
            scr.roteiro = script
            scr.tipo_roteiro = script_type
            scr.descricao = description

            modelo_list = []

            try:
                # save Script
                scr.save()
            except Exception, e:
                self.log.error(u'Failed to save the Script.')
                raise RoteiroError(e, u'Failed to save the Script.')

            #associar o modelo ao roteiro
            try:
                if type(model) is unicode:
                    item = model
                    model = []
                    model.append(item)
                for ids in model:
                    modelos = ModeloRoteiro()
                    modelos.roteiro = scr
                    modelo = Modelo().get_by_pk(int(ids))
                    modelos.modelo = modelo
                    modelos.create(user)
                    modelo_list.append(modelos.modelo)
            except Exception, e:
                raise RoteiroError(e, u"Failed to save modelo_roteiro.")
コード例 #2
0
    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))