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

        URL: filter/<id_filter>/
        """

        try:

            self.log.info('Remove Filter')
            # Commons Validations

            # User permission
            if not has_perm(user, AdminPermission.ENVIRONMENT_MANAGEMENT,
                            AdminPermission.WRITE_OPERATION):
                self.log.error(
                    u'User does not have permission to perform the operation.')
                return self.not_authorized()

            if not is_valid_int_greater_zero_param(kwargs['id_filter']):
                self.log.error(u'Parameter id_filter is invalid. Value: %s.',
                               kwargs['id_filter'])
                raise InvalidValueError(None, 'id_filter', kwargs['id_filter'])
            else:
                # Check existence
                fil = Filter().get_by_pk(kwargs['id_filter'])

            try:
                # Remove filter and its relationships
                fil.delete()
            except Exception, e:
                self.log.error(u'Failed to remove the filter.')
                raise e

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

        URL: filter/<id_filter>/
        """

        try:

            self.log.info('Remove Filter')
            # Commons Validations

            # User permission
            if not has_perm(user, AdminPermission.ENVIRONMENT_MANAGEMENT, AdminPermission.WRITE_OPERATION):
                self.log.error(
                    u'User does not have permission to perform the operation.')
                return self.not_authorized()

            if not is_valid_int_greater_zero_param(kwargs['id_filter']):
                self.log.error(
                    u'Parameter id_filter is invalid. Value: %s.', kwargs['id_filter'])
                raise InvalidValueError(None, 'id_filter', kwargs['id_filter'])
            else:
                # Check existence
                fil = Filter().get_by_pk(kwargs['id_filter'])

            try:
                # Remove filter and its relationships
                fil.delete()
            except Exception, e:
                self.log.error(u'Failed to remove the filter.')
                raise e

            return self.response(dumps_networkapi({}))
Exemple #3
0
    def handle_get(self, request, user, *args, **kwargs):
        """Treat GET requests to get a Filter by id.

        URL: filter/get/<id_filter>/
        """

        try:

            self.log.info("Get Filter by id")
            # Commons Validations

            # User permission
            if not has_perm(user, AdminPermission.ENVIRONMENT_MANAGEMENT, AdminPermission.READ_OPERATION):
                self.log.error(
                    u'User does not have permission to perform the operation.')
                return self.not_authorized()

            if not is_valid_int_greater_zero_param(kwargs['id_filter']):
                self.log.error(
                    u'Parameter id_filter is invalid. Value: %s.', kwargs['id_filter'])
                raise InvalidValueError(None, 'id_filter', kwargs['id_filter'])
            else:
                # Check existence
                fil = Filter().get_by_pk(kwargs['id_filter'])

            filter_dict = model_to_dict(fil)
            filter_dict['equip_types'] = list()
            for fil_equip_type in fil.filterequiptype_set.all():
                filter_dict['equip_types'].append(
                    model_to_dict(fil_equip_type.equiptype))

            return self.response(dumps_networkapi({'filter': filter_dict}))

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

        URL: filter/<id_filter>/
        """

        try:

            self.log.info('Alter Filter')
            # Commons Validations

            # User permission
            if not has_perm(user, AdminPermission.ENVIRONMENT_MANAGEMENT, AdminPermission.WRITE_OPERATION):
                self.log.error(
                    u'User does not have permission to perform the operation.')
                return self.not_authorized()

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

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

            if not is_valid_int_greater_zero_param(kwargs['id_filter']):
                self.log.error(
                    u'Parameter id_filter is invalid. Value: %s.', kwargs['id_filter'])
                raise InvalidValueError(None, 'id_filter', kwargs['id_filter'])
            else:
                # Check existence
                fil = Filter().get_by_pk(kwargs['id_filter'])

            fil.validate_filter(filter_map)

            try:
                # Save filter
                fil.save()
            except Exception, e:
                self.log.error(u'Failed to edit the filter.')
                raise e

            return self.response(dumps_networkapi({}))
    def handle_post(self, request, user, *args, **kwargs):
        """Treat POST requests to add new Filter.

        URL: filter/
        """

        try:

            self.log.info("Add Filter")
            # Commons Validations

            # User permission
            if not has_perm(user, AdminPermission.ENVIRONMENT_MANAGEMENT, AdminPermission.WRITE_OPERATION):
                self.log.error(
                    u'User does not have permission to perform the operation.')
                return self.not_authorized()

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

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

            # New Filter
            filter_ = Filter()

            # Validates
            filter_.validate_filter(filter_map)

            try:
                # Save filter
                filter_.save()
            except Exception, e:
                self.log.error(u'Failed to save the filter.')
                raise FilterError(e, u'Failed to save the filter')

            filter_map = dict()
            filter_map['id'] = filter_.id

            return self.response(dumps_networkapi({'filter': filter_map}))
    def handle_put(self, request, user, *args, **kwargs):
        """Treat PUT requests to associate Filter and TipoEquipamento.

        URL: filter/<filter_id>/equiptype/<equiptype_id>
        """

        try:
            self.log.info('')
            # Commons Validations

            # User permission
            if not has_perm(user, AdminPermission.ENVIRONMENT_MANAGEMENT,
                            AdminPermission.WRITE_OPERATION):
                self.log.error(
                    u'User does not have permission to perform the operation.')
                return self.not_authorized()

            if not is_valid_int_greater_zero_param(kwargs['id_filter']):
                self.log.error(u'Parameter id_filter is invalid. Value: %s.',
                               kwargs['id_filter'])
                raise InvalidValueError(None, 'id_filter', kwargs['id_filter'])
            else:
                # Check existence
                fil = Filter().get_by_pk(kwargs['id_filter'])

            if not is_valid_int_greater_zero_param(kwargs['id_equiptype']):
                self.log.error(
                    u'Parameter id_equiptype is invalid. Value: %s.',
                    kwargs['id_equiptype'])
                raise InvalidValueError(None, 'id_equiptype',
                                        kwargs['id_equiptype'])
            else:
                # Check existence
                tp_equip = TipoEquipamento().get_by_pk(kwargs['id_equiptype'])

            association = FilterEquipType()
            association.filter = fil
            association.equiptype = tp_equip

            # Check existence
            association.validate()

            # Save association
            association.save()

            fil_et_map = dict()
            fil_et_map['id'] = association.id

            return self.response(
                dumps_networkapi({'equiptype_filter_xref': fil_et_map}))

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

        URL: filter/<id_filter>/dissociate/<id_equip_type>
        """

        try:
            self.log.info("")
            # Commons Validations

            # User permission
            if not has_perm(user, AdminPermission.ENVIRONMENT_MANAGEMENT,
                            AdminPermission.WRITE_OPERATION):
                self.log.error(
                    u'User does not have permission to perform the operation.')
                return self.not_authorized()

            if not is_valid_int_greater_zero_param(kwargs['id_filter']):
                self.log.error(u'Parameter id_filter is invalid. Value: %s.',
                               kwargs['id_filter'])
                raise InvalidValueError(None, 'id_filter', kwargs['id_filter'])
            else:
                # Check existence
                fil = Filter().get_by_pk(kwargs['id_filter'])

            if not is_valid_int_greater_zero_param(kwargs['id_equip_type']):
                self.log.error(
                    u'Parameter id_equip_type is invalid. Value: %s.',
                    kwargs['id_equip_type'])
                raise InvalidValueError(None, 'id_equip_type',
                                        kwargs['id_equip_type'])
            else:
                # Check existence
                eq_tp = TipoEquipamento().get_by_pk(kwargs['id_equip_type'])

            # Delete association
            try:
                association = FilterEquipType.objects.get(filter=fil.id,
                                                          equiptype=eq_tp.id)

                ## Only delete if there's no conflicts ##
                association.delete()

            except ObjectDoesNotExist, e:
                # Association doesn't exist, ok
                self.log.error(e)
                pass

            return self.response(dumps_networkapi({}))
    def handle_put(self, request, user, *args, **kwargs):
        """Treat PUT requests to edit Filters.

        URL: filter/<id_filter>/
        """

        try:

            self.log.info('Alter Filter')
            # Commons Validations

            # User permission
            if not has_perm(user, AdminPermission.ENVIRONMENT_MANAGEMENT,
                            AdminPermission.WRITE_OPERATION):
                self.log.error(
                    u'User does not have permission to perform the operation.')
                return self.not_authorized()

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

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

            if not is_valid_int_greater_zero_param(kwargs['id_filter']):
                self.log.error(u'Parameter id_filter is invalid. Value: %s.',
                               kwargs['id_filter'])
                raise InvalidValueError(None, 'id_filter', kwargs['id_filter'])
            else:
                # Check existence
                fil = Filter().get_by_pk(kwargs['id_filter'])

            fil.validate_filter(filter_map)

            try:
                # Save filter
                fil.save()
            except Exception, e:
                self.log.error(u'Failed to edit the filter.')
                raise e

            return self.response(dumps_networkapi({}))
    def handle_post(self, request, user, *args, **kwargs):
        """Treat POST requests to add new Filter.

        URL: filter/
        """

        try:

            self.log.info('Add Filter')
            # Commons Validations

            # User permission
            if not has_perm(user, AdminPermission.ENVIRONMENT_MANAGEMENT,
                            AdminPermission.WRITE_OPERATION):
                self.log.error(
                    u'User does not have permission to perform the operation.')
                return self.not_authorized()

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

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

            # New Filter
            filter_ = Filter()

            # Validates
            filter_.validate_filter(filter_map)

            try:
                # Save filter
                filter_.save()
            except Exception, e:
                self.log.error(u'Failed to save the filter.')
                raise FilterError(e, u'Failed to save the filter')

            filter_map = dict()
            filter_map['id'] = filter_.id

            return self.response(dumps_networkapi({'filter': filter_map}))