Exemple #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'))
Exemple #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'))
Exemple #3
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

        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)
Exemple #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)
Exemple #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__)
Exemple #6
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__)