示例#1
0
    def test_model_field_checker(self):
        new_res1 = Resource.objects.create(somekey1='someval1', somekey='someval')

        self.assertTrue(ModelFieldChecker.is_field_or_property(new_res1, 'type'))
        self.assertTrue(ModelFieldChecker.is_field_or_property(new_res1, 'parent'))
        self.assertTrue(ModelFieldChecker.is_field_or_property(new_res1, 'parent_id'))
        self.assertTrue(ModelFieldChecker.is_field_or_property(new_res1, 'id'))
        self.assertTrue(ModelFieldChecker.is_field_or_property(new_res1, 'is_free'))
示例#2
0
    def test_model_field_checker(self):
        new_res1 = Resource.objects.create(somekey1='someval1',
                                           somekey='someval')

        self.assertTrue(
            ModelFieldChecker.is_field_or_property(new_res1, 'type'))
        self.assertTrue(
            ModelFieldChecker.is_field_or_property(new_res1, 'parent'))
        self.assertTrue(
            ModelFieldChecker.is_field_or_property(new_res1, 'parent_id'))
        self.assertTrue(ModelFieldChecker.is_field_or_property(new_res1, 'id'))
        self.assertTrue(
            ModelFieldChecker.is_field_or_property(new_res1, 'is_free'))
示例#3
0
文件: cmdbctl.py 项目: itnihao/pytin
    def _handle_command_set(self, *args, **options):
        res_id = options['resource-id']
        resource = Resource.objects.get(pk=res_id)

        update_query = self._parse_reminder_arguments(options['fields'])
        for field_name in update_query:
            field_value = update_query[field_name]

            if field_name in ['parent', 'parent_id']:
                field_name, field_value = self._normalize_parent_field(field_value)

            if field_name == 'type':
                requested_model = apps.get_model(field_value)
                resource = resource.cast_type(requested_model)
            elif ModelFieldChecker.is_field_or_property(resource, field_name):
                setattr(resource, field_name, field_value)
                resource.save()
            else:
                if field_value:
                    resource.set_option(name=field_name,
                                        value=field_value,
                                        format=options['format'] if options['format'] else ResourceOption.FORMAT_STRING)
                elif resource.get_option_value(field_name, default=None):
                    resource.get_option(field_name).delete()  # delete option

        cascade = options['cascade']
        if options['use']:
            resource.use(cascade=cascade)
        elif options['free']:
            resource.free(cascade=cascade)
        elif options['lock']:
            resource.lock(cascade=cascade)

        ConsoleResourceWriter.dump_item(resource)
示例#4
0
    def _handle_command_set(self, *args, **options):
        res_id = options['resource-id']
        resource = Resource.objects.get(pk=res_id)

        update_query = self._parse_reminder_arguments(options['fields'])
        for field_name in update_query:
            field_value = update_query[field_name]

            if field_name in ['parent', 'parent_id']:
                field_name, field_value = self._normalize_parent_field(field_value)

            if field_name == 'type':
                requested_model = apps.get_model(field_value)
                resource = resource.cast_type(requested_model)
            elif ModelFieldChecker.is_field_or_property(resource, field_name):
                setattr(resource, field_name, field_value)
                resource.save()
            else:
                if field_value:
                    resource.set_option(name=field_name,
                                        value=field_value,
                                        format=options['format'] if options['format'] else ResourceOption.FORMAT_STRING)
                elif resource.get_option_value(field_name, default=None):
                    resource.get_option(field_name).delete()  # delete option

        if options['use']:
            resource.use()
        elif options['free']:
            resource.free()
        elif options['lock']:
            resource.lock()

        ConsoleResourceWriter.dump_item(resource)
示例#5
0
    def test_change_asset_type(self):
        new_res1 = Switch.objects.create(label="test switch")

        self.assertTrue(
            ModelFieldChecker.is_field_or_property(new_res1, 'type'))

        self.assertEqual('assets.switch', new_res1.type)
        self.assertEqual(Switch, new_res1.__class__)

        # real change type
        new_res1 = new_res1.cast_type(Server)

        self.assertEqual('assets.server', new_res1.type)
        self.assertEqual(Server, new_res1.__class__)
示例#6
0
    def _get_resource_data_row(resource, fields=None):
        assert resource
        assert fields

        columns = []
        for field in fields:
            if field == 'parent_id':
                field_value = resource.parent_id
            elif field == 'self':
                field_value = unicode(resource)
            else:
                field_value = ModelFieldChecker.get_field_value(resource, field, '%s?' % field)

            columns.append(field_value)

        return columns
示例#7
0
    def test_change_asset_type(self):
        new_res1 = Switch.objects.create(label="test switch")

        self.assertTrue(ModelFieldChecker.is_field_or_property(new_res1, 'type'))

        setattr(new_res1, 'type', 'Server')
        new_res1.save()
        new_res1.refresh_from_db()

        self.assertEqual('Switch', new_res1.type)
        self.assertEqual(Switch, new_res1.__class__)

        # real change type
        new_res1 = new_res1.cast_type(Server)

        self.assertEqual('Server', new_res1.type)
        self.assertEqual(Server, new_res1.__class__)
示例#8
0
文件: cmdbctl.py 项目: itnihao/pytin
    def _handle_command_search(self, *args, **options):
        query = self._parse_reminder_arguments(options['filter'])

        if options['index']:
            updated = 0
            for resource in Resource.active.all():
                resource.name = unicode(resource)
                resource.save()

            logger.debug("Updated resources: %s" % updated)
            return

        # apply status
        if not options['status']:
            resource_set = Resource.active.filter(**query)
        else:
            query['status__in'] = options['status'].split(',')
            resource_set = Resource.objects.filter(**query)

        # order by
        table_sort_by_field = None
        if options['order']:
            fields = options['order'].split(',')
            for field_name in fields:
                if not ModelFieldChecker.is_model_field(Resource, field_name):
                    table_sort_by_field = field_name
                    break

            if not table_sort_by_field:
                resource_set = resource_set.order_by(*fields)

        # apply limits
        limit = options['limit']
        offset = (options['page'] - 1) * limit
        if limit > 0:
            resource_set = resource_set[offset:limit]

        # tabular output with column align
        show_fields = options['show_fields'].split(',')

        console_writer = ConsoleResourceWriter(resource_set)
        console_writer.print_table(show_fields, sort_by=table_sort_by_field)
示例#9
0
    def _handle_command_search(self, *args, **options):
        query = self._parse_reminder_arguments(options['filter'])

        if options['index']:
            updated = 0
            for resource in Resource.active.all():
                resource.name = unicode(resource)
                resource.save()

            logger.debug("Updated resources: %s" % updated)
            return

        # apply status
        if not options['status']:
            resource_set = Resource.active.filter(**query)
        else:
            query['status__in'] = options['status'].split(',')
            resource_set = Resource.objects.filter(**query)

        # order by
        table_sort_by_field = None
        if options['order']:
            fields = options['order'].split(',')
            for field_name in fields:
                if not ModelFieldChecker.is_model_field(Resource, field_name):
                    table_sort_by_field = field_name
                    break

            if not table_sort_by_field:
                resource_set = resource_set.order_by(*fields)

        # apply limits
        limit = options['limit']
        offset = (options['page'] - 1) * limit
        if limit > 0:
            resource_set = resource_set[offset:limit]

        # tabular output with column align
        show_fields = options['show_fields'].split(',')

        console_writer = ConsoleResourceWriter(resource_set)
        console_writer.print_table(show_fields, sort_by=table_sort_by_field)