def handle_delete(self, request, user, *args, **kwargs):
        """Treat requests DELETE to remove Script Type.

        URL: scripttype/<id_script_type>/
        """
        try:

            self.log.info("Remove Script Type")

            # 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)

            id_script_type = kwargs.get('id_script_type')

            # 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)

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

            with distributedlock(LOCK_SCRIPT_TYPE % id_script_type):

                try:

                    if script_type.roteiro_set.count() != 0:
                        raise TipoRoteiroHasRoteiroError(
                            None,
                            u'Existe roteiros associado ao tipo de roteiro %d'
                            % script_type.id)

                    # remove Script Type
                    script_type.delete()

                except TipoRoteiroHasRoteiroError, e:
                    raise e
                except Exception, e:
                    self.log.error(u'Failed to remove the Script Type.')
                    raise RoteiroError(e, u'Failed to remove the Script Type.')
    def handle_post(self, request, user, *args, **kwargs):
        """Treat requests POST to add Script Type.

        URL: scripttype/
        """

        try:

            self.log.info('Add Script Type')

            # 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_type_map = networkapi_map.get('script_type')
            if script_type_map is None:
                return self.response_error(
                    3,
                    u'There is no value to the script_type tag  of XML request.'
                )

            # Get XML data
            type = script_type_map.get('type')
            description = script_type_map.get('description')

            # Valid type
            if not is_valid_string_minsize(
                    type, 3) or not is_valid_string_maxsize(type, 40):
                self.log.error(u'Parameter type is invalid. Value: %s', type)
                raise InvalidValueError(None, 'type', 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)

            try:
                TipoRoteiro.get_by_name(type)
                raise TipoRoteiroNameDuplicatedError(
                    None,
                    u'Já existe um tipo de roteiro com o tipo %s.' % type)
            except TipoRoteiroNotFoundError:
                pass

            script_type = TipoRoteiro()

            # set variables
            script_type.tipo = type
            script_type.descricao = description

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

            script_map = dict()
            script_map['script_type'] = model_to_dict(
                script_type, exclude=['tipo', 'descricao'])

            return self.response(dumps_networkapi(script_map))
Пример #3
0
    def handle_put(self, request, user, *args, **kwargs):
        """Treat requests PUT to edit Script Type.

        URL: scripttype/<id_script_type>/
        """
        try:

            self.log.info('Edit Script Type')

            # 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)

            id_script_type = kwargs.get('id_script_type')

            # 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_type_map = networkapi_map.get('script_type')
            if script_type_map is None:
                return self.response_error(3, u'There is no value to the script_type tag  of XML request.')

            # Get XML data
            type = script_type_map.get('type')
            description = script_type_map.get('description')

            # 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 type
            if not is_valid_string_minsize(type, 3) or not is_valid_string_maxsize(type, 40):
                self.log.error(u'Parameter type is invalid. Value: %s', type)
                raise InvalidValueError(None, 'type', 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)

            with distributedlock(LOCK_SCRIPT_TYPE % id_script_type):

                try:
                    if script_type.tipo.lower() != type.lower():
                        TipoRoteiro.get_by_name(type)
                        raise TipoRoteiroNameDuplicatedError(
                            None, u'Já existe um tipo de roteiro com o tipo %s.' % type)
                except TipoRoteiroNotFoundError:
                    pass

                # set variables
                script_type.tipo = type
                script_type.descricao = description

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

                return self.response(dumps_networkapi({}))

        except InvalidValueError, e:
            return self.response_error(269, e.param, e.value)
Пример #4
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.")
Пример #5
0
    def handle_put(self, request, user, *args, **kwargs):
        """Treat requests PUT to edit Script.

        URL: script/<id_script>/
        """
        try:

            self.log.info("Edit 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)

            id_script = kwargs.get('id_script')

            # 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')
            models = script_map.get('model')
            description = script_map.get('description')

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

            # 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 by ID to check if it exist
            scr = Roteiro.get_by_pk(id_script)

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

            models_old = []
            scr_models = ModeloRoteiro.objects.all().filter(roteiro__id=scr.id)
            for i in scr_models:
                models_old.append(int(i.modelo.id))

            if models is not None and type(models) is not list:
                var = int(models)
                models = []
                models.append(var)
            else:
                models = [int(x) for x in models]

            desassociar = set(models_old) - set(models)
            for i in desassociar:
                scr_model = ModeloRoteiro()
                scr_model.remover(user, int(i), int(scr.id))
            associar = set(models) - set(models_old)
            for i in associar:
                scr_models = ModeloRoteiro()
                scr_models.roteiro = scr
                scr_models.modelo = Modelo.get_by_pk(i)
                scr_models.create(user)

            #verificar se há equipamento daquele modelo que não está associado a um roteiro
            for ids in models:
                equipamentos = Equipamento.objects.filter(modelo__id=int(ids))
                for equip in equipamentos:
                    try:
                        equip_roteiro = EquipamentoRoteiro.objects.filter(
                            equipamento__id=equip.id,
                            roteiro__tipo_roteiro__id=scr.tipo_roteiro.id
                        ).uniqueResult()
                        equip_roteiro.id
                    except:
                        equip_rot = EquipamentoRoteiro()
                        equip_rot.equipamento = equip
                        equip_rot.roteiro = scr
                        equip_rot.create(user)
                        pass

            with distributedlock(LOCK_SCRIPT % id_script):

                try:
                    if not scr.roteiro.lower() == script.lower(
                    ) and not scr.tipo_roteiro.id == id_script_type:
                        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

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

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

                return self.response(dumps_networkapi({}))

        except InvalidValueError, e:
            return self.response_error(269, e.param, e.value)