Пример #1
0
    def test_simple_fields(self):

        DATA = (
            {
                'field_type': CF_TYPE_TEXT,
                'field_value': 'Foobar!',
                'empty_value': ''
            },
            {
                'field_type': CF_TYPE_INTEGER,
                'field_value': 0,
                'empty_value': None
            },
            {
                'field_type': CF_TYPE_INTEGER,
                'field_value': 42,
                'empty_value': None
            },
            {
                'field_type': CF_TYPE_BOOLEAN,
                'field_value': True,
                'empty_value': None
            },
            {
                'field_type': CF_TYPE_BOOLEAN,
                'field_value': False,
                'empty_value': None
            },
            {
                'field_type': CF_TYPE_DATE,
                'field_value': date(2016, 6, 23),
                'empty_value': None
            },
            {
                'field_type': CF_TYPE_URL,
                'field_value': 'http://example.com/',
                'empty_value': ''
            },
        )

        obj_type = ContentType.objects.get_for_model(Site)

        for data in DATA:

            # Create a custom field
            cf = CustomField(type=data['field_type'],
                             name='my_field',
                             required=False)
            cf.save()
            cf.obj_type.set([obj_type])
            cf.save()

            # Assign a value to the first Site
            site = Site.objects.first()
            cfv = CustomFieldValue(field=cf, obj_type=obj_type, obj_id=site.id)
            cfv.value = data['field_value']
            cfv.save()

            # Retrieve the stored value
            cfv = CustomFieldValue.objects.filter(obj_type=obj_type,
                                                  obj_id=site.pk).first()
            self.assertEqual(cfv.value, data['field_value'])

            # Delete the stored value
            cfv.value = data['empty_value']
            cfv.save()
            self.assertEqual(
                CustomFieldValue.objects.filter(obj_type=obj_type,
                                                obj_id=site.pk).count(), 0)

            # Delete the custom field
            cf.delete()
Пример #2
0
    def test_simple_fields(self):
        DATA = (
            {
                'field_type': CustomFieldTypeChoices.TYPE_TEXT,
                'field_value': 'Foobar!',
                'empty_value': ''
            },
            {
                'field_type': CustomFieldTypeChoices.TYPE_INTEGER,
                'field_value': 0,
                'empty_value': None
            },
            {
                'field_type': CustomFieldTypeChoices.TYPE_INTEGER,
                'field_value': 42,
                'empty_value': None
            },
            {
                'field_type': CustomFieldTypeChoices.TYPE_BOOLEAN,
                'field_value': True,
                'empty_value': None
            },
            {
                'field_type': CustomFieldTypeChoices.TYPE_BOOLEAN,
                'field_value': False,
                'empty_value': None
            },
            {
                'field_type': CustomFieldTypeChoices.TYPE_DATE,
                'field_value': '2016-06-23',
                'empty_value': None
            },
            {
                'field_type': CustomFieldTypeChoices.TYPE_URL,
                'field_value': 'http://example.com/',
                'empty_value': ''
            },
        )

        obj_type = ContentType.objects.get_for_model(Site)

        for data in DATA:

            # Create a custom field
            cf = CustomField(type=data['field_type'],
                             name='my_field',
                             required=False)
            cf.save()
            cf.content_types.set([obj_type])

            # Assign a value to the first Site
            site = Site.objects.first()
            site.custom_field_data[cf.name] = data['field_value']
            site.save()

            # Retrieve the stored value
            site.refresh_from_db()
            self.assertEqual(site.custom_field_data[cf.name],
                             data['field_value'])

            # Delete the stored value
            site.custom_field_data.pop(cf.name)
            site.save()
            site.refresh_from_db()
            self.assertIsNone(site.custom_field_data.get(cf.name))

            # Delete the custom field
            cf.delete()
Пример #3
0
    def setUpTestData(cls):
        obj_type = ContentType.objects.get_for_model(Site)

        # Integer filtering
        cf = CustomField(name='cf1', type=CustomFieldTypeChoices.TYPE_INTEGER)
        cf.save()
        cf.content_types.set([obj_type])

        # Boolean filtering
        cf = CustomField(name='cf2', type=CustomFieldTypeChoices.TYPE_BOOLEAN)
        cf.save()
        cf.content_types.set([obj_type])

        # Exact text filtering
        cf = CustomField(
            name='cf3',
            type=CustomFieldTypeChoices.TYPE_TEXT,
            filter_logic=CustomFieldFilterLogicChoices.FILTER_EXACT)
        cf.save()
        cf.content_types.set([obj_type])

        # Loose text filtering
        cf = CustomField(
            name='cf4',
            type=CustomFieldTypeChoices.TYPE_TEXT,
            filter_logic=CustomFieldFilterLogicChoices.FILTER_LOOSE)
        cf.save()
        cf.content_types.set([obj_type])

        # Date filtering
        cf = CustomField(name='cf5', type=CustomFieldTypeChoices.TYPE_DATE)
        cf.save()
        cf.content_types.set([obj_type])

        # Exact URL filtering
        cf = CustomField(
            name='cf6',
            type=CustomFieldTypeChoices.TYPE_URL,
            filter_logic=CustomFieldFilterLogicChoices.FILTER_EXACT)
        cf.save()
        cf.content_types.set([obj_type])

        # Loose URL filtering
        cf = CustomField(
            name='cf7',
            type=CustomFieldTypeChoices.TYPE_URL,
            filter_logic=CustomFieldFilterLogicChoices.FILTER_LOOSE)
        cf.save()
        cf.content_types.set([obj_type])

        # Selection filtering
        cf = CustomField(name='cf8',
                         type=CustomFieldTypeChoices.TYPE_URL,
                         choices=['Foo', 'Bar', 'Baz'])
        cf.save()
        cf.content_types.set([obj_type])

        Site.objects.bulk_create([
            Site(name='Site 1',
                 slug='site-1',
                 custom_field_data={
                     'cf1': 100,
                     'cf2': True,
                     'cf3': 'foo',
                     'cf4': 'foo',
                     'cf5': '2016-06-26',
                     'cf6': 'http://foo.example.com/',
                     'cf7': 'http://foo.example.com/',
                     'cf8': 'Foo',
                 }),
            Site(name='Site 2',
                 slug='site-2',
                 custom_field_data={
                     'cf1': 200,
                     'cf2': False,
                     'cf3': 'foobar',
                     'cf4': 'foobar',
                     'cf5': '2016-06-27',
                     'cf6': 'http://bar.example.com/',
                     'cf7': 'http://bar.example.com/',
                     'cf8': 'Bar',
                 }),
            Site(name='Site 3', slug='site-3', custom_field_data={}),
        ])
Пример #4
0
    def setUpTestData(cls):
        content_type = ContentType.objects.get_for_model(Site)

        # Text custom field
        cls.cf_text = CustomField(type=CustomFieldTypeChoices.TYPE_TEXT,
                                  name='text_field',
                                  default='foo')
        cls.cf_text.save()
        cls.cf_text.content_types.set([content_type])

        # Integer custom field
        cls.cf_integer = CustomField(type=CustomFieldTypeChoices.TYPE_INTEGER,
                                     name='number_field',
                                     default=123)
        cls.cf_integer.save()
        cls.cf_integer.content_types.set([content_type])

        # Boolean custom field
        cls.cf_boolean = CustomField(type=CustomFieldTypeChoices.TYPE_BOOLEAN,
                                     name='boolean_field',
                                     default=False)
        cls.cf_boolean.save()
        cls.cf_boolean.content_types.set([content_type])

        # Date custom field
        cls.cf_date = CustomField(type=CustomFieldTypeChoices.TYPE_DATE,
                                  name='date_field',
                                  default='2020-01-01')
        cls.cf_date.save()
        cls.cf_date.content_types.set([content_type])

        # URL custom field
        cls.cf_url = CustomField(type=CustomFieldTypeChoices.TYPE_URL,
                                 name='url_field',
                                 default='http://example.com/1')
        cls.cf_url.save()
        cls.cf_url.content_types.set([content_type])

        # Select custom field
        cls.cf_select = CustomField(type=CustomFieldTypeChoices.TYPE_SELECT,
                                    name='choice_field',
                                    choices=['Foo', 'Bar', 'Baz'])
        cls.cf_select.default = 'Foo'
        cls.cf_select.save()
        cls.cf_select.content_types.set([content_type])

        # Create some sites
        cls.sites = (
            Site(name='Site 1', slug='site-1'),
            Site(name='Site 2', slug='site-2'),
        )
        Site.objects.bulk_create(cls.sites)

        # Assign custom field values for site 2
        cls.sites[1].custom_field_data = {
            cls.cf_text.name: 'bar',
            cls.cf_integer.name: 456,
            cls.cf_boolean.name: True,
            cls.cf_date.name: '2020-01-02',
            cls.cf_url.name: 'http://example.com/2',
            cls.cf_select.name: 'Bar',
        }
        cls.sites[1].save()
Пример #5
0
    def setUpTestData(cls):
        content_type = ContentType.objects.get_for_model(Site)

        # Text custom field
        cls.cf_text = CustomField(type=CustomFieldTypeChoices.TYPE_TEXT,
                                  name='text_field',
                                  default='foo')
        cls.cf_text.save()
        cls.cf_text.obj_type.set([content_type])

        # Integer custom field
        cls.cf_integer = CustomField(type=CustomFieldTypeChoices.TYPE_INTEGER,
                                     name='number_field',
                                     default=123)
        cls.cf_integer.save()
        cls.cf_integer.obj_type.set([content_type])

        # Boolean custom field
        cls.cf_boolean = CustomField(type=CustomFieldTypeChoices.TYPE_BOOLEAN,
                                     name='boolean_field',
                                     default=False)
        cls.cf_boolean.save()
        cls.cf_boolean.obj_type.set([content_type])

        # Date custom field
        cls.cf_date = CustomField(type=CustomFieldTypeChoices.TYPE_DATE,
                                  name='date_field',
                                  default='2020-01-01')
        cls.cf_date.save()
        cls.cf_date.obj_type.set([content_type])

        # URL custom field
        cls.cf_url = CustomField(type=CustomFieldTypeChoices.TYPE_URL,
                                 name='url_field',
                                 default='http://example.com/1')
        cls.cf_url.save()
        cls.cf_url.obj_type.set([content_type])

        # Select custom field
        cls.cf_select = CustomField(type=CustomFieldTypeChoices.TYPE_SELECT,
                                    name='choice_field')
        cls.cf_select.save()
        cls.cf_select.obj_type.set([content_type])
        cls.cf_select_choice1 = CustomFieldChoice(field=cls.cf_select,
                                                  value='Foo')
        cls.cf_select_choice1.save()
        cls.cf_select_choice2 = CustomFieldChoice(field=cls.cf_select,
                                                  value='Bar')
        cls.cf_select_choice2.save()
        cls.cf_select_choice3 = CustomFieldChoice(field=cls.cf_select,
                                                  value='Baz')
        cls.cf_select_choice3.save()

        cls.cf_select.default = cls.cf_select_choice1.value
        cls.cf_select.save()

        # Create some sites
        cls.sites = (
            Site(name='Site 1', slug='site-1'),
            Site(name='Site 2', slug='site-2'),
        )
        Site.objects.bulk_create(cls.sites)

        # Assign custom field values for site 2
        site2_cfvs = {
            cls.cf_text: 'bar',
            cls.cf_integer: 456,
            cls.cf_boolean: True,
            cls.cf_date: '2020-01-02',
            cls.cf_url: 'http://example.com/2',
            cls.cf_select: cls.cf_select_choice2.pk,
        }
        for field, value in site2_cfvs.items():
            cfv = CustomFieldValue(field=field, obj=cls.sites[1])
            cfv.value = value
            cfv.save()
Пример #6
0
 def setUp(self):
     content_type = ContentType.objects.get_for_model(Site)
     custom_field = CustomField(type=CustomFieldTypeChoices.TYPE_TEXT, name='text_field', default='foo')
     custom_field.save()
     custom_field.obj_type.set([content_type])
Пример #7
0
    def test_simple_fields(self):
        DATA = (
            {
                'field': {
                    'type': CustomFieldTypeChoices.TYPE_TEXT,
                },
                'value': 'Foobar!',
            },
            {
                'field': {
                    'type': CustomFieldTypeChoices.TYPE_LONGTEXT,
                },
                'value': 'Text with **Markdown**',
            },
            {
                'field': {
                    'type': CustomFieldTypeChoices.TYPE_INTEGER,
                },
                'value': 0,
            },
            {
                'field': {
                    'type': CustomFieldTypeChoices.TYPE_INTEGER,
                    'validation_minimum': 1,
                    'validation_maximum': 100,
                },
                'value': 42,
            },
            {
                'field': {
                    'type': CustomFieldTypeChoices.TYPE_INTEGER,
                    'validation_minimum': -100,
                    'validation_maximum': -1,
                },
                'value': -42,
            },
            {
                'field': {
                    'type': CustomFieldTypeChoices.TYPE_BOOLEAN,
                },
                'value': True,
            },
            {
                'field': {
                    'type': CustomFieldTypeChoices.TYPE_BOOLEAN,
                },
                'value': False,
            },
            {
                'field': {
                    'type': CustomFieldTypeChoices.TYPE_DATE,
                },
                'value': '2016-06-23',
            },
            {
                'field': {
                    'type': CustomFieldTypeChoices.TYPE_URL,
                },
                'value': 'http://example.com/',
            },
            {
                'field': {
                    'type': CustomFieldTypeChoices.TYPE_JSON,
                },
                'value': '{"foo": 1, "bar": 2}',
            },
        )

        obj_type = ContentType.objects.get_for_model(Site)

        for data in DATA:

            # Create a custom field
            cf = CustomField(name='my_field', required=False, **data['field'])
            cf.save()
            cf.content_types.set([obj_type])

            # Check that the field has a null initial value
            site = Site.objects.first()
            self.assertIsNone(site.custom_field_data[cf.name])

            # Assign a value to the first Site
            site.custom_field_data[cf.name] = data['value']
            site.save()

            # Retrieve the stored value
            site.refresh_from_db()
            self.assertEqual(site.custom_field_data[cf.name], data['value'])

            # Delete the stored value
            site.custom_field_data.pop(cf.name)
            site.save()
            site.refresh_from_db()
            self.assertIsNone(site.custom_field_data.get(cf.name))

            # Delete the custom field
            cf.delete()
Пример #8
0
    def handle(self, *args, **options):
        dry_run = options['dry_run']
        quiet = options['quiet']

        tenant_obj_type = ContentType.objects.get_for_model(Tenant)
        device_obj_type = ContentType.objects.get_for_model(Device)

        # Create a new Tenant Group called Members.
        try:
            tg = TenantGroup.objects.get(name="Members")
        except TenantGroup.DoesNotExist:
            tg = TenantGroup(name='Members')
            if dry_run:
                self.stdout.write(
                    'Would have created a Tenant Group called Members')
            else:
                tg.save()
                if not quiet:
                    self.stdout.write('Created Tenant Group called Members')

        # Create a new Custom Field called MemberType
        # and add it to Tenant.
        try:
            member_type = CustomField.objects.get(name="member_type")
        except CustomField.DoesNotExist:
            member_type = CustomField(
                type=CustomFieldTypeChoices.TYPE_SELECT,
                name='member_type',
                required=False,
                choices=MEMBER_TYPES,
            )
            if dry_run:
                self.stdout.write(
                    'Would have created a Custom Field called member_type')
            else:
                member_type.save()
                member_type.content_types.set([tenant_obj_type])
                if not quiet:
                    self.stdout.write(
                        'Created Custom Field called member_type')

        # Create a new Custom Field called crm_id
        # and add it to Tenant.
        try:
            crm_id = CustomField.objects.get(name="crm_id")
        except CustomField.DoesNotExist:
            crm_id = CustomField(
                type=CustomFieldTypeChoices.TYPE_TEXT,
                name='crm_id',
                required=False,
                default='',
            )
            if dry_run:
                self.stdout.write(
                    'Would have created a Custom Field called crm_id')
            else:
                crm_id.save()
                crm_id.content_types.set([tenant_obj_type])
                if not quiet:
                    self.stdout.write('Created Custom Field called crm_id')

        # Create a new Custom Field called active
        # and add it to Tenant.
        try:
            active = CustomField.objects.get(name="active")
        except CustomField.DoesNotExist:
            active = CustomField(
                type=CustomFieldTypeChoices.TYPE_BOOLEAN,
                name='active',
                required=False,
                default=True,
            )
            if dry_run:
                self.stdout.write(
                    'Would have created a Custom Field called active')
            else:
                active.save()
                active.content_types.set([tenant_obj_type])
                if not quiet:
                    self.stdout.write('Created Custom Field called active')

        # Create a new Custom Field called legacy_id
        # and add it to Device
        try:
            legacy_id = CustomField.objects.get(name="legacy_id")
        except CustomField.DoesNotExist:
            legacy_id = CustomField(
                type=CustomFieldTypeChoices.TYPE_TEXT,
                name='legacy_id',
                required=False,
                default='',
            )
            if dry_run:
                self.stdout.write(
                    'Would have created a Custom Field called legacy_id')
            else:
                legacy_id.save()
                legacy_id.content_types.set([device_obj_type])
                if not quiet:
                    self.stdout.write('Created Custom Field called legacy_id')