Пример #1
0
    def test_customfield_text(self):
        customfield = CustomField.objects.create(
            name='Plot',
            field_type=CustomField.TEXT,
            content_type=self.contact_ct,
        )

        cell = EntityCellCustomField(customfield)

        # Render ---
        user = self.create_user()
        yoko = FakeContact.objects.create(user=user,
                                          first_name='Yoko',
                                          last_name='Littner')

        value = 'Yoko is a young woman from <i>Littner</i>, a village neighboring Giha.\n' \
                'She helps introduce Simon and Kamina to the surface world.'
        customfield.value_class.objects.create(
            entity=yoko,
            custom_field=customfield,
            value=value,
        )
        self.assertHTMLEqual(
            '<p>'
            'Yoko is a young woman from &lt;i&gt;Littner&lt;/i&gt;, a village neighboring Giha.'
            '<br>'
            'She helps introduce Simon and Kamina to the surface world.'
            '</p>', cell.render_html(entity=yoko, user=user))
        self.assertEqual(value, cell.render_csv(entity=yoko, user=user))
Пример #2
0
    def test_customfield_str(self):
        customfield = CustomField.objects.create(
            name='Nickname',
            field_type=CustomField.STR,
            content_type=self.contact_ct,
        )

        cell = EntityCellCustomField(customfield)
        self.assertEqual(settings.CSS_DEFAULT_LISTVIEW,
                         cell.listview_css_class)
        self.assertEqual(settings.CSS_DEFAULT_HEADER_LISTVIEW,
                         cell.header_listview_css_class)

        # Render ---
        user = self.create_user()
        yoko = FakeContact.objects.create(user=user,
                                          first_name='Yoko',
                                          last_name='Littner')

        value = '<i>Sniper</i>'
        customfield.value_class.objects.create(
            entity=yoko,
            custom_field=customfield,
            value=value,
        )
        self.assertEqual('&lt;i&gt;Sniper&lt;/i&gt;',
                         cell.render_html(entity=yoko, user=user))
        self.assertEqual(value, cell.render_csv(entity=yoko, user=user))
Пример #3
0
    def test_customfield_bool(self):
        customfield = CustomField.objects.create(
            name='Is fun ?',
            field_type=CustomField.BOOL,
            content_type=self.contact_ct,
        )

        cell = EntityCellCustomField(customfield)
        self.assertEqual(settings.CSS_DEFAULT_LISTVIEW,
                         cell.listview_css_class)
        self.assertEqual(settings.CSS_DEFAULT_HEADER_LISTVIEW,
                         cell.header_listview_css_class)

        # Render ---
        user = self.create_user()
        yoko = FakeContact.objects.create(user=user,
                                          first_name='Yoko',
                                          last_name='Littner')
        customfield.value_class.objects.create(entity=yoko,
                                               custom_field=customfield,
                                               value=True)
        self.assertEqual(
            f'<input type="checkbox" checked disabled/>{_("Yes")}',
            cell.render_html(entity=yoko, user=user))
        self.assertEqual(_('Yes'), cell.render_csv(entity=yoko, user=user))
Пример #4
0
    def test_customfield_date(self):
        customfield = CustomField.objects.create(
            name='Day',
            field_type=CustomField.DATE,
            content_type=self.contact_ct,
        )

        cell = EntityCellCustomField(customfield)
        self.assertEqual(settings.CSS_DEFAULT_LISTVIEW,
                         cell.listview_css_class)
        self.assertEqual(settings.CSS_DATE_HEADER_LISTVIEW,
                         cell.header_listview_css_class)

        # Render ---
        user = self.create_user()
        yoko = FakeContact.objects.create(user=user,
                                          first_name='Yoko',
                                          last_name='Littner')
        date_obj = date(year=2058, month=3, day=26)
        date_str = date_format(date_obj, 'DATE_FORMAT')
        customfield.value_class.objects.create(
            entity=yoko,
            custom_field=customfield,
            value=date_obj,
        )
        self.assertEqual(date_str, cell.render_html(entity=yoko, user=user))
        self.assertEqual(date_str, cell.render_csv(entity=yoko, user=user))
Пример #5
0
    def test_customfield_int(self):
        self.assertEqual(_('Custom fields'),
                         EntityCellCustomField.verbose_name)

        name = 'Size (cm)'
        customfield = CustomField.objects.create(
            name=name,
            field_type=CustomField.INT,
            content_type=self.contact_ct,
        )

        cell = EntityCellCustomField(customfield)
        self.assertIsInstance(cell, EntityCellCustomField)
        self.assertEqual(str(customfield.id), cell.value)
        self.assertEqual(name, cell.title)
        self.assertEqual(f'custom_field-{customfield.id}', cell.key)
        self.assertIs(cell.is_multiline, False)
        self.assertEqual(settings.CSS_NUMBER_LISTVIEW, cell.listview_css_class)
        self.assertEqual(settings.CSS_DEFAULT_HEADER_LISTVIEW,
                         cell.header_listview_css_class)

        cell = EntityCellCustomField.build(FakeContact, customfield.id)
        self.assertIsInstance(cell, EntityCellCustomField)
        self.assertEqual(str(customfield.id), cell.value)

        self.assertIsNone(EntityCellCustomField.build(FakeContact, 1000))
        self.assertIsNone(EntityCellCustomField.build(FakeContact, 'notanint'))

        # Render ---
        user = self.create_user()
        yoko = FakeContact.objects.create(user=user,
                                          first_name='Yoko',
                                          last_name='Littner')
        self.assertEqual('', cell.render_html(entity=yoko, user=user))

        customfield.value_class.objects.create(entity=yoko,
                                               custom_field=customfield,
                                               value=152)
        yoko = self.refresh(yoko)  # Reset caches
        self.assertEqual('152', cell.render_html(entity=yoko, user=user))
        self.assertEqual('152', cell.render_csv(entity=yoko, user=user))
Пример #6
0
    def test_customfield_mulitenum(self):
        customfield = CustomField.objects.create(
            name='Eva',
            field_type=CustomField.MULTI_ENUM,
            content_type=self.contact_ct,
        )

        create_enumvalue = partial(
            CustomFieldEnumValue.objects.create,
            custom_field=customfield,
        )
        enum_value1 = create_enumvalue(value='Eva-00')
        create_enumvalue(value='Eva-01')
        enum_value3 = create_enumvalue(value='Eva-02<script>')

        cell = EntityCellCustomField(customfield)
        self.assertEqual(settings.CSS_DEFAULT_LISTVIEW,
                         cell.listview_css_class)
        self.assertEqual(settings.CSS_DEFAULT_HEADER_LISTVIEW,
                         cell.header_listview_css_class)

        # Render ---
        user = self.create_user()
        yoko = FakeContact.objects.create(user=user,
                                          first_name='Yoko',
                                          last_name='Littner')
        self.assertEqual('', cell.render_html(entity=yoko, user=user))
        self.assertEqual('', cell.render_csv(entity=yoko, user=user))

        cf_value = customfield.value_class(entity=yoko,
                                           custom_field=customfield)
        cf_value.set_value_n_save([enum_value1.id, enum_value3.id])
        yoko = self.refresh(yoko)  # Avoid cache
        self.assertHTMLEqual(
            f'<ul><li>{enum_value1.value}</li><li>Eva-02&lt;script&gt;</li></ul>',
            cell.render_html(entity=yoko, user=user))
        self.assertEqual(f'{enum_value1.value} / {enum_value3.value}',
                         cell.render_csv(entity=yoko, user=user))
Пример #7
0
    def test_customfield_enum(self):
        customfield = CustomField.objects.create(
            name='Eva',
            field_type=CustomField.ENUM,
            content_type=self.contact_ct,
        )

        create_enumvalue = partial(
            CustomFieldEnumValue.objects.create,
            custom_field=customfield,
        )
        enum_value1 = create_enumvalue(value='Eva-00<script>')
        create_enumvalue(value='Eva-01')

        cell = EntityCellCustomField(customfield)
        self.assertEqual(settings.CSS_DEFAULT_LISTVIEW,
                         cell.listview_css_class)
        self.assertEqual(settings.CSS_DEFAULT_HEADER_LISTVIEW,
                         cell.header_listview_css_class)

        # Render ---
        user = self.create_user()
        yoko = FakeContact.objects.create(user=user,
                                          first_name='Yoko',
                                          last_name='Littner')
        self.assertEqual('', cell.render_html(entity=yoko, user=user))
        self.assertEqual('', cell.render_csv(entity=yoko, user=user))

        customfield.value_class.objects.create(
            entity=yoko,
            custom_field=customfield,
            value=enum_value1,
        )
        yoko = self.refresh(yoko)  # Avoid cache
        self.assertEqual('Eva-00&lt;script&gt;',
                         cell.render_html(entity=yoko, user=user))
        self.assertEqual(enum_value1.value,
                         cell.render_csv(entity=yoko, user=user))
Пример #8
0
    def test_customfield_url(self):
        customfield = CustomField.objects.create(
            name='Village URL',
            field_type=CustomField.URL,
            content_type=self.contact_ct,
        )

        cell = EntityCellCustomField(customfield)

        # Render ---
        user = self.create_user()
        yoko = FakeContact.objects.create(user=user,
                                          first_name='Yoko',
                                          last_name='Littner')

        value = 'www.littner.org'
        customfield.value_class.objects.create(
            entity=yoko,
            custom_field=customfield,
            value=value,
        )
        self.assertHTMLEqual(f'<a href="{value}" target="_blank">{value}</a>',
                             cell.render_html(entity=yoko, user=user))
        self.assertEqual(value, cell.render_csv(entity=yoko, user=user))
Пример #9
0
    def test_customfield_decimal(self):
        customfield = CustomField.objects.create(
            name='Weight',
            field_type=CustomField.FLOAT,
            content_type=self.contact_ct,
        )

        cell = EntityCellCustomField(customfield)
        self.assertEqual(settings.CSS_NUMBER_LISTVIEW, cell.listview_css_class)
        self.assertEqual(settings.CSS_DEFAULT_HEADER_LISTVIEW,
                         cell.header_listview_css_class)

        # Render ---
        user = self.create_user()
        yoko = FakeContact.objects.create(user=user,
                                          first_name='Yoko',
                                          last_name='Littner')
        value = Decimal('1.52')
        value_str = number_format(value, use_l10n=True)
        customfield.value_class.objects.create(entity=yoko,
                                               custom_field=customfield,
                                               value=value)
        self.assertEqual(value_str, cell.render_html(entity=yoko, user=user))
        self.assertEqual(value_str, cell.render_csv(entity=yoko, user=user))