def test_edit09(self): "Not custom filter cannot be private + callback URL." self.login() hf = HeaderFilter.create( pk='tests-hf_contact', name='Contact view', model=FakeContact, is_custom=False, ) url = hf.get_edit_absolute_url() self.assertGET200(url) callback = FakeOrganisation.get_lv_absolute_url() response = self.client.post( url, data={ 'name': hf.name, 'user': self.user.id, 'is_private': 'on', # Should not be used 'cells': 'regular_field-last_name', 'cancel_url': callback, }) self.assertNoFormError(response, status=302) self.assertFalse(self.refresh(hf).is_private) self.assertRedirects(response, callback)
def test_registry01(self): user = CremeUser() registry = field_printers._FieldPrintersRegistry() as_html = registry.get_html_field_value as_csv = registry.get_csv_field_value sector = FakeSector.objects.all()[0] o = FakeOrganisation( user=user, name='Mars', url_site='www.mars.info', sector=sector, ) self.assertEqual(o.name, as_html(o, 'name', user)) self.assertEqual(o.name, as_csv(o, 'name', user)) self.assertHTMLEqual( '<a href="{url}" target="_blank">{url}</a>'.format(url=o.url_site), as_html(o, 'url_site', user), ) self.assertEqual(o.url_site, as_csv(o, 'url_site', user)) self.assertEqual(sector.title, as_html(o, 'sector', user)) self.assertEqual(sector.title, as_csv(o, 'sector', user)) self.assertEqual(sector.title, as_html(o, 'sector__title', user)) self.assertEqual(sector.title, as_csv(o, 'sector__title', user))
def test_print_integer01(self): "No choices." user = self.login() create_orga = partial(FakeOrganisation.objects.create, user=user) for name, capital in (('Bebop', 1000), ('Swordfish', 20000), ('Redtail', None)): create_orga(name=name, capital=capital) build = partial(EntityCellRegularField.build, model=FakeOrganisation) hf = HeaderFilter.objects.create_if_needed( pk='test-hf_orga', name='Organisation view', model=FakeOrganisation, cells_desc=[build(name='name'), build(name='capital')], ) lv_url = FakeOrganisation.get_lv_absolute_url() response = self.assertGET200( self._build_dl_url(FakeOrganisation, list_url=lv_url, hfilter_id=hf.id), follow=True, ) lines = {force_str(line) for line in response.content.splitlines()} self.assertIn('"Bebop","1000"', lines) self.assertIn('"Swordfish","20000"', lines) self.assertIn('"Redtail",""', lines)
def test_icon_registry02(self): "get_4_instance()" icon_reg = IconRegistry() icon_reg.register(FakeContact, 'images/contact_%(size)s.png') icon_reg.register(FakeOrganisation, 'images/organisation_%(size)s.png') phone_label = 'Contact with phone' email_label = 'Contact with email' icon_reg.register_4_instance( FakeContact, lambda instance: ('phone', phone_label) if instance.phone else ('email', email_label)) c = FakeContact(first_name='Casca', last_name='Mylove') icon1 = icon_reg.get_4_instance(instance=c, theme='icecream', size_px=22) self.assertIsInstance(icon1, Icon) self.assertIn('icecream/images/email_22', icon1.url) self.assertEqual(email_label, icon1.label) c.phone = '123456' icon2 = icon_reg.get_4_instance(instance=c, theme='icecream', size_px=22) self.assertIn('icecream/images/phone_22', icon2.url) self.assertEqual(phone_label, icon2.label) o = FakeOrganisation(name='Midland') icon3 = icon_reg.get_4_instance(instance=o, theme='icecream', size_px=22) self.assertIn('icecream/images/organisation_22', icon3.url) self.assertEqual('Test Organisation', icon3.label)
def test_print_integer01(self): o = FakeOrganisation() user = CremeUser() field = o._meta.get_field('capital') self.assertEqual('', field_printers.print_integer(o, fval=None, user=user, field=field)) self.assertEqual( '1234', field_printers.print_integer(o, fval=1234, user=user, field=field) )
def _aux_print_integer_html01(self): o = FakeOrganisation() user = CremeUser() field = o._meta.get_field('capital') self.assertEqual( '', print_integer_html(o, fval=None, user=user, field=field)) value = 1234 self.assertEqual( number_format(value, use_l10n=True, force_grouping=True), print_integer_html(o, fval=value, user=user, field=field))
def test_has_attr(self): with self.assertNoException(): template = Template( "{% load creme_core_tags %}" "{% if orga|has_attr:'name' %}OK{% else %}KO{% endif %}#" "{% if orga|has_attr:'invalid' %}OK{% else %}KO{% endif %}") render = template.render( Context({ 'orga': FakeOrganisation(name='Amestris'), })) self.assertEqual('OK#KO', render.strip())
def test_xls_export02(self): "Other CT, other type of fields." user = self.login() create_orga = partial(FakeOrganisation.objects.create, user=user) orga01 = create_orga(name='Bebop') orga02 = create_orga( name='Swordfish', subject_to_vat=False, creation_date=date(year=2016, month=7, day=5), ) build_cell = partial(EntityCellRegularField.build, model=FakeOrganisation) cells = [ build_cell(name='name'), build_cell(name='subject_to_vat'), build_cell(name='creation_date'), ] hf = HeaderFilter.objects.create_if_needed( pk='test-hf_orga', name='Organisation view', model=FakeOrganisation, cells_desc=cells, ) response = self.assertGET200( self._build_dl_url( FakeOrganisation, doc_type='xls', list_url=FakeOrganisation.get_lv_absolute_url(), hfilter_id=hf.id, ), follow=True, ) it = iter( XlrdReader(None, file_contents=b''.join(response.streaming_content))) self.assertListEqual(next(it), [hfi.title for hfi in cells]) self.assertListEqual(next(it), [orga01.name, _('Yes'), '']) self.assertListEqual( next(it), [ orga02.name, _('No'), date_format(orga02.creation_date, 'DATE_FORMAT') ], ) with self.assertRaises(StopIteration): next(it)
def test_print_url_html(self): o = FakeOrganisation() user = CremeUser() field = o._meta.get_field('url_site') self.assertEqual('', print_url_html(o, fval=None, user=user, field=field)) url1 = 'www.wikipedia.org' self.assertEqual(f'<a href="{url1}" target="_blank">{url1}</a>', print_url_html(o, fval=url1, user=user, field=field)) url2 = '</a><script>Muhaha</script>' self.assertEqual( '<a href="{url}" target="_blank">{url}</a>'.format( url=escape(url2)), print_url_html(o, fval=url2, user=user, field=field))
def test_create05(self): "Use cancel_url for redirection." self.login() callback = FakeOrganisation.get_lv_absolute_url() response = self.client.post( self._build_add_url(self.contact_ct), follow=True, data={ 'name': 'DefaultHeaderFilter', 'cells': 'regular_field-first_name', 'cancel_url': callback, }, ) self.assertNoFormError(response) self.assertRedirects(response, callback)
def test_add_entity02(self): "ValidationError + cancel_url" user = self.login() url = reverse('creme_core__create_fake_organisation') lv_url = FakeOrganisation.get_lv_absolute_url() response = self.assertGET200(url, HTTP_REFERER='http://testserver' + lv_url) self.assertEqual(lv_url, response.context.get('cancel_url')) response = self.client.post(url, follow=True, data={'user': user.id, # 'name': name, # NB: Missing 'cancel_url': lv_url, } ) self.assertFormError(response, 'form', 'name', _('This field is required.')) self.assertEqual(lv_url, response.context.get('cancel_url'))
def test_manager_compatible01(self): orig_compat_ids = self.build_compatible_set() orig_internal_compat_ids = self.build_compatible_set( include_internals=True) create_rtype = RelationType.create rtype = create_rtype( ('test-subject_foobar', 'manages', [FakeContact]), ('test-object_foobar', 'is managed by', [FakeOrganisation]))[0] internal_rtype = create_rtype( ('test-subject_foobar_2', 'manages internal', [FakeContact]), ('test-object_foobar_2', 'is managed by internal', [FakeOrganisation]), is_internal=True, )[0] compatibles_ids = self.build_compatible_set() self.assertEqual(len(orig_compat_ids) + 1, len(compatibles_ids)) self.assertIn(rtype.id, compatibles_ids) compatibles_internal_ids = self.build_compatible_set( include_internals=True) self.assertEqual( len(orig_internal_compat_ids) + 2, len(compatibles_internal_ids)) self.assertIn(rtype.id, compatibles_internal_ids) self.assertIn(internal_rtype.id, compatibles_internal_ids) contact_ct = self.contact_ct self.assertTrue(rtype.is_compatible(contact_ct.id)) self.assertTrue(rtype.is_compatible(contact_ct)) self.assertTrue(rtype.is_compatible(str(contact_ct.id))) self.assertTrue(rtype.is_compatible(FakeContact)) self.assertTrue(rtype.is_compatible(FakeContact())) orga_ct = ContentType.objects.get_for_model(FakeOrganisation) self.assertFalse(rtype.is_compatible(orga_ct.id)) self.assertFalse(rtype.is_compatible(orga_ct)) self.assertFalse(rtype.is_compatible(str(orga_ct.id))) self.assertFalse(rtype.is_compatible(FakeOrganisation)) self.assertFalse(rtype.is_compatible(FakeOrganisation())) # Model as argument self.assertSetEqual( compatibles_ids, self.build_compatible_set(FakeContact), )
def test_edit_entity03(self): "ValidationError + cancel_url" user = self.login() orga = FakeOrganisation.objects.create(user=user, name='Nerv') url = orga.get_edit_absolute_url() lv_url = FakeOrganisation.get_lv_absolute_url() response = self.assertGET200(url, HTTP_REFERER='http://testserver' + lv_url) self.assertEqual(lv_url, response.context.get('cancel_url')) response = self.client.post(url, follow=True, data={'user': user.id, # 'name': name, # NB: Missing 'cancel_url': lv_url, } ) self.assertFormError(response, 'form', 'name', _('This field is required.')) self.assertEqual(lv_url, response.context.get('cancel_url'))
def test_as_ctype(self): ctype = ContentType.objects.get_for_model(FakeOrganisation) self.assertIs(ctype, as_ctype(ctype)) self.assertEqual(ctype, as_ctype(FakeOrganisation)) self.assertEqual(ctype, as_ctype(FakeOrganisation()))