示例#1
0
    def test_current_year05(self):
        "closing_date is hidden"
        user = self.login()

        FieldsConfig.create(Opportunity,
                            descriptions=[('closing_date', {
                                FieldsConfig.HIDDEN: True
                            })])

        create_orga = partial(Organisation.objects.create, user=user)
        emitter = create_orga(name='Emitter', is_managed=True)
        target = create_orga(name='Target')

        won_sp = SalesPhase.objects.filter(won=True).first()

        Opportunity.objects.create(
            user=user,
            name='Opp',
            closing_date=now(),
            sales_phase=won_sp,
            emitter=emitter,
            target=target,
        )

        self.assertEqual([
            _('The field «Actual closing date» is hidden ; these statistics are not available.'
              )
        ],
                         CurrentYearStatistics(Opportunity, Organisation)())
    def test_get_4_models(self):
        model1 = FakeContact
        model2 = FakeOrganisation

        h_field1 = 'phone'
        h_field2 = 'url_site'

        create_fc = FieldsConfig.create
        create_fc(model1,
                  descriptions=[(h_field1, {
                      FieldsConfig.HIDDEN: True
                  })])
        create_fc(model2,
                  descriptions=[(h_field2, {
                      FieldsConfig.HIDDEN: True
                  })])

        with self.assertNumQueries(1):
            fconfigs = FieldsConfig.get_4_models([model1, model2])

        self.assertIsInstance(fconfigs, dict)
        self.assertEqual(2, len(fconfigs))

        fc1 = fconfigs.get(model1)
        self.assertIsInstance(fc1, FieldsConfig)
        self.assertEqual(model1, fc1.content_type.model_class())
        self.assertTrue(fc1.is_fieldname_hidden(h_field1))

        self.assertTrue(fconfigs.get(model2).is_fieldname_hidden(h_field2))

        with self.assertNumQueries(0):
            FieldsConfig.get_4_models([model1, model2])

        with self.assertNumQueries(0):
            FieldsConfig.get_4_model(model1)
示例#3
0
    def test_related_opportunity03(self):
        """Opportunity.description is hidden"""
        user = self.login()

        FieldsConfig.create(Opportunity,
                            descriptions=[('description', {FieldsConfig.HIDDEN: True})],
                           )

        casca = Contact.objects.create(user=user, first_name='Casca', last_name='Miura')
        event = Event.objects.create(user=user, name='Eclipse',
                                     type=EventType.objects.first(),
                                     start_date=now(),
                                    )

        emitter = Organisation.objects.create(user=user, name='My society', is_managed=True)

        name = 'Opp01'
        response = self.client.post(self._build_related_opp_url(event, casca), follow=True,
                                    data={'user':        user.id,
                                          'name':        name,
                                          'sales_phase': SalesPhase.objects.first().id,
                                          'target':      self.formfield_value_generic_entity(casca),
                                          'emitter':     emitter.id,
                                          'currency':    DEFAULT_CURRENCY_PK,
                                         }
                                   )
        self.assertNoFormError(response)

        opp = self.get_object_or_fail(Opportunity, name=name)
        self.assertFalse(opp.description)
 def test_create_errors_03(self):
     "Invalid attribute name"
     with self.assertRaises(FieldsConfig.InvalidAttribute):
         FieldsConfig.create(FakeContact,
                             descriptions=[('phone', {
                                 'invalid': True
                             })])
示例#5
0
    def test_str02(self):
        FieldsConfig.create(
            Address,
            descriptions=[
                ('zipcode', {
                    FieldsConfig.HIDDEN: True
                }),
                ('department', {
                    FieldsConfig.HIDDEN: True
                }),
                ('state', {
                    FieldsConfig.HIDDEN: True
                }),
            ],
        )

        address_value = '21 jump street'
        po_box = 'Popop'
        city = 'Atlantis'
        state = '??'
        address = Address(
            name='Address#1',
            address=address_value,
            po_box=po_box,
            zipcode='424242',
            city=city,
            department='rucrazy',
            state=state,
            country='wtf',
        )
        self.assertEqual('{} {}'.format(address_value, city), str(address))

        self.assertEqual(po_box, str(Address(po_box=po_box, state=state)))
    def test_get_total_won_quote_this_year04(self):
        "'acceptation_date' is hidden + populate_entities()"
        user = self.login()
        quote1, source1, target1 = self.create_quote_n_orgas('Quote1')
        quote2, source2, target2 = self.create_quote_n_orgas('Quote2')

        FieldsConfig.create(Quote,
                            descriptions=[('acceptation_date', {
                                FieldsConfig.HIDDEN: True
                            })])

        # funf = target1.function_fields.get('total_won_quote_this_year')
        funf = function_field_registry.get(Organisation,
                                           'total_won_quote_this_year')

        FieldsConfig.get_4_model(Quote)  # Fill cache

        with self.assertNumQueries(0):
            funf.populate_entities([target1, target2], user)

        with self.assertNumQueries(0):
            total1 = get_total_won_quote_this_year(target1, user)
            total2 = get_total_won_quote_this_year(target2, user)

        msg = _('Error: «Acceptation date» is hidden')
        self.assertEqual(msg, total1)
        self.assertEqual(msg, total2)
示例#7
0
    def test_edit06(self):
        "With FieldsConfig"
        model = FakeContact
        hidden_fname1 = 'description'
        hidden_fname2 = 'position'
        FieldsConfig.create(model,
                            descriptions=[
                                (hidden_fname1, {
                                    FieldsConfig.HIDDEN: True
                                }),
                                (hidden_fname2, {
                                    FieldsConfig.HIDDEN: True
                                }),
                            ])
        sci = SearchConfigItem.create_if_needed(model, fields=['first_name'])

        response = self.assertGET200(self._build_edit_url(sci))

        with self.assertNoException():
            fields_f = response.context['form'].fields['fields']

        self.assertEqual(['first_name'], fields_f.initial)

        self._find_field_index(fields_f, 'first_name')
        self._find_field_index(fields_f, 'civility__title')

        self._assertNotInChoices(fields_f, hidden_fname1)
        self._assertNotInChoices(fields_f, 'position__title')
示例#8
0
    def test_edit07(self):
        "With FieldsConfig + selected hidden fields"
        model = FakeContact
        hidden_fname1 = 'description'
        hidden_fname2 = 'position'
        hidden_sub_fname2 = hidden_fname2 + '__title'
        sci = SearchConfigItem.create_if_needed(model,
                                                fields=[
                                                    'first_name',
                                                    hidden_fname1,
                                                    hidden_sub_fname2,
                                                ])

        FieldsConfig.create(model,
                            descriptions=[
                                (hidden_fname1, {
                                    FieldsConfig.HIDDEN: True
                                }),
                                (hidden_fname2, {
                                    FieldsConfig.HIDDEN: True
                                }),
                            ])

        response = self.assertGET200(self._build_edit_url(sci))

        with self.assertNoException():
            fields_f = response.context['form'].fields['fields']

        self.assertEqual(['first_name', hidden_fname1, hidden_sub_fname2],
                         fields_f.initial)

        self._find_field_index(fields_f, 'first_name')
        self._find_field_index(fields_f, hidden_fname1)
        self._find_field_index(fields_f, hidden_sub_fname2)
示例#9
0
    def test_editcomment01(self):
        self.login()
        FieldsConfig.create(
            CreditNote,
            descriptions=[('issuing_date', {
                FieldsConfig.HIDDEN: True
            })],
        )

        credit_note = self.create_credit_note_n_orgas('Credit Note 001')[0]

        url = self._build_editcomment_url(credit_note)
        response = self.assertGET200(url)
        self.assertTemplateUsed(
            response, 'creme_core/generics/blockform/edit-popup.html')
        # self.assertEqual(_('Edit «%s»') % credit_note, response.context.get('title'))
        self.assertEqual(
            _('Edit «{object}»').format(object=credit_note),
            response.context.get('title'))

        # ---
        comment = 'Special gift'
        self.assertNoFormError(self.client.post(url, data={'comment':
                                                           comment}))
        self.assertEqual(comment, self.refresh(credit_note).comment)
示例#10
0
    def test_mass_import03(self):
        "FieldsConfig on Address sub-field."
        user = self.login()
        FieldsConfig.create(Address,
                            descriptions=[('po_box', {FieldsConfig.HIDDEN: True})],
                           )

        name = 'Nerv'
        city = 'Tokyo'
        po_box = 'ABC123'
        doc = self._build_csv_doc([(name, city, po_box)])
        response = self.client.post(
            self._build_import_url(Organisation), follow=True,
            data={**self.IMPORT_DATA,
                  'document': doc.id,
                  'user': user.id,

                  'billaddr_city_colselect':   2,
                  'billaddr_po_box_colselect': 3,  # Should not be used
                 },
        )
        self.assertNoFormError(response)

        self._execute_job(response)
        billing_address = self.get_object_or_fail(Organisation, name=name).billing_address

        self.assertIsNotNone(billing_address)
        self.assertEqual(city, billing_address.city)
        self.assertFalse(billing_address.po_box)
    def test_get_4_model01(self):
        model = FakeContact
        h_field1 = 'phone'
        h_field2 = 'mobile'
        FieldsConfig.create(
            model,
            descriptions=[
                (h_field1, {
                    FieldsConfig.HIDDEN: True
                }),
                (h_field2, {
                    FieldsConfig.HIDDEN: True
                }),
            ],
        )

        with self.assertNumQueries(1):
            fc = FieldsConfig.get_4_model(model)

        is_hidden = fc.is_fieldname_hidden
        self.assertTrue(is_hidden(h_field1))
        self.assertTrue(is_hidden(h_field2))
        self.assertFalse(is_hidden('description'))

        with self.assertNumQueries(0):  # cache
            FieldsConfig.get_4_model(model)
示例#12
0
    def test_mass_import04(self):
        "FieldsConfig on 'billing_address' FK field."
        user = self.login()
        FieldsConfig.create(Organisation,
                            descriptions=[('billing_address', {FieldsConfig.HIDDEN: True})],
                           )

        name = 'Nerv'
        doc = self._build_csv_doc([(name, 'Tokyo', 'ABC123')])
        response = self.client.post(
            self._build_import_url(Organisation), follow=True,
            data={**self.IMPORT_DATA,
                  'document': doc.id,
                  'user': user.id,

                  'billaddr_city_colselect': 2,  # Should not be used
                  'billaddr_po_box_colselect': 3,  # Should not be used
                 },
        )
        self.assertNoFormError(response)

        job = self._execute_job(response)

        orga = self.get_object_or_fail(Organisation, name=name)
        self.assertIsNone(orga.billing_address)

        self._assertNoResultError(self._get_job_results(job))
 def test_create_errors_04(self):
     "Invalid attribute value"
     with self.assertRaises(FieldsConfig.InvalidAttribute):
         FieldsConfig.create(
             FakeContact,
             descriptions=[('phone', {
                 FieldsConfig.HIDDEN: 5
             })],
         )
示例#14
0
    def test_createview03(self):
        "FieldsConfig on Address sub-fields"
        user = self.login()
        FieldsConfig.create(
            Address,
            descriptions=[('po_box', {
                FieldsConfig.HIDDEN: True
            })],
        )

        response = self.assertGET200(reverse('persons__create_organisation'))

        with self.assertNoException():
            fields = response.context['form'].fields

        self.assertIn('name', fields)
        self.assertIn('billing_address-address', fields)
        self.assertNotIn('billing_address-po_box', fields)

        name = 'Bebop'

        b_address = 'Mars gate'
        b_po_box = 'Mars1233546'
        b_zipcode = '9874541'
        b_city = 'Redsand'
        b_department = 'Great crater'
        b_state = 'State#3'
        b_country = 'Terran federation'

        response = self.client.post(
            reverse('persons__create_organisation'),
            follow=True,
            data={
                'user': user.pk,
                'name': name,
                'billing_address-address': b_address,
                'billing_address-po_box': b_po_box,  # <== should not be used
                'billing_address-zipcode': b_zipcode,
                'billing_address-city': b_city,
                'billing_address-department': b_department,
                'billing_address-state': b_state,
                'billing_address-country': b_country,
            })
        self.assertNoFormError(response)

        orga = self.get_object_or_fail(Organisation, name=name)
        billing_address = orga.billing_address
        self.assertIsNotNone(billing_address)
        self.assertEqual(b_address, billing_address.address)
        self.assertEqual(b_zipcode, billing_address.zipcode)
        self.assertEqual(b_city, billing_address.city)
        self.assertEqual(b_department, billing_address.department)
        self.assertEqual(b_state, billing_address.state)
        self.assertEqual(b_country, billing_address.country)

        self.assertFalse(billing_address.po_box)
示例#15
0
 def test_ml_contacts_filter03(self):
     "'email' is hidden"
     mlist = MailingList.objects.create(user=self.user, name='ml01')
     FieldsConfig.create(
         Contact,
         descriptions=[('email', {
             FieldsConfig.HIDDEN: True
         })],
     )
     self.assertGET409(self._build_addcontactfilter_url(mlist))
    def test_get_total_won_quote_last_year03(self):
        "'populate_entities()"
        user = self.login()
        previous_year = self.today_date - timedelta(days=365)

        def set_date(quote):
            quote.acceptation_date = previous_year
            quote.save()

        quote01, source01, target01 = self.create_quote_n_orgas(
            'Quote01', status=self.won_status)
        quote02, source02, target02 = self.create_quote_n_orgas(
            'Quote02', status=self.won_status)

        # Not won status => not used
        quote03 = self.create_quote('Quote #3', source01, target01)
        self.assertFalse(quote03.status.won)

        # Current year => not used
        quote04 = self.create_quote('Quote #4',
                                    source01,
                                    target02,
                                    status=self.won_status)
        quote04.acceptation_date = self.today_date
        quote04.save()

        set_date(quote01)
        set_date(quote02)
        set_date(quote03)

        self._set_managed(source01)
        self._set_managed(source02)

        self.create_line(quote01, 5000, 1)
        self.create_line(quote02, 4000, 1)
        self.create_line(quote03, 500, 1)  # Not used
        self.create_line(quote04, 300, 1)  # Not used

        # funf = target01.function_fields.get('total_won_quote_last_year')
        funf = function_field_registry.get(Organisation,
                                           'total_won_quote_last_year')
        self.assertIsNotNone(funf)

        FieldsConfig.get_4_model(Quote)  # Fill cache
        bool(Organisation.get_all_managed_by_creme())  # Fill cache

        with self.assertNumQueries(2):
            funf.populate_entities([target01, target02], user)

        with self.assertNumQueries(0):
            total1 = funf(target01, user).for_csv()
            total2 = funf(target02, user).for_csv()

        self.assertEqual(number_format('5000.00', use_l10n=True), total1)
        self.assertEqual(number_format('4000.00', use_l10n=True), total2)
示例#17
0
    def test_ml_orgas02(self):
        "'email' is hidden"
        mlist = MailingList.objects.create(user=self.user, name='ml01')

        FieldsConfig.create(
            Organisation,
            descriptions=[('email', {
                FieldsConfig.HIDDEN: True
            })],
        )
        self.assertGET409(self._build_addorga_url(mlist))
 def _create_contact_conf(self):
     FieldsConfig.create(
         FakeContact,
         descriptions=[
             ('phone', {
                 FieldsConfig.HIDDEN: True
             }),
             ('mobile', {
                 FieldsConfig.HIDDEN: True
             }),
         ],
     )
示例#19
0
    def test_editcomment02(self):
        "'comment' is hidden"
        self.login()
        FieldsConfig.create(
            CreditNote,
            descriptions=[('comment', {
                FieldsConfig.HIDDEN: True
            })],
        )

        credit_note = self.create_credit_note_n_orgas('Credit Note 001')[0]
        self.assertGET409(self._build_editcomment_url(credit_note))
    def test_get_total_won_quote_this_year01(self):
        user = self.login()

        def set_date(quote):
            quote.acceptation_date = self.today_date
            quote.save()

        quote01, source, target = self.create_quote_n_orgas(
            'Quote #1', status=self.won_status)
        set_date(quote01)
        self._set_managed(source)

        quote02 = self.create_quote('Quote #2',
                                    source,
                                    target,
                                    status=self.won_status)
        set_date(quote02)

        # Not won status => not used
        quote03 = self.create_quote('Quote #3', source, target)
        self.assertFalse(quote03.status.won)
        set_date(quote03)
        self.create_line(quote03, 500, 1)

        # Previous year => not used
        quote04 = self.create_quote('Quote #4',
                                    source,
                                    target,
                                    status=self.won_status)
        quote04.acceptation_date = self.today_date - timedelta(days=366)
        quote04.save()
        self.create_line(quote04, 300, 1)

        FieldsConfig.get_4_model(Quote)  # Fill cache
        bool(Organisation.get_all_managed_by_creme())  # Fill cache

        with self.assertNumQueries(2):
            total = get_total_won_quote_this_year(target, user)

        self.assertEqual(0, total)

        self.create_line(quote01, 5000, 1)
        self.create_line(quote02, 1000, 1)
        self.assertEqual(6000, get_total_won_quote_this_year(target, user))

        # funf = target.function_fields.get('total_won_quote_this_year')
        funf = function_field_registry.get(Organisation,
                                           'total_won_quote_this_year')
        self.assertIsNotNone(funf)

        val = number_format('6000.00', use_l10n=True)
        self.assertEqual(val, funf(target, user).for_html())
        self.assertEqual(val, funf(target, user).for_csv())
示例#21
0
    def test_create_billing02(self):
        "FK is hidden"
        orga = self.login()

        FieldsConfig.create(
            Organisation,
            descriptions=[('billing_address', {
                FieldsConfig.HIDDEN: True
            })],
        )
        self.assertGET409(
            reverse('persons__create_billing_address', args=(orga.id, )))
示例#22
0
    def test_respond_to_a_call05(self):
        """FieldsConfig: some fields are hidden"""
        user = self.login()

        FieldsConfig.create(Contact,
                            descriptions=[('phone', {FieldsConfig.HIDDEN: True})]
                           )

        phone = '558899'
        contact = Contact.objects.create(user=user, first_name='Bean', last_name='Bandit', phone=phone)
        response = self.assertGET200(self.RESPOND_URL, data={'number': phone})
        self.assertNotContains(response, str(contact))
示例#23
0
    def test_set_default_in_invoice04(self):
        "'payment_info' is hidden."
        user = self.login()

        invoice, sony_source = self.create_invoice_n_orgas('Playstations')[:2]
        pi_sony = PaymentInformation.objects.create(organisation=sony_source, name='RIB sony')

        FieldsConfig.create(Invoice,
                            descriptions=[('payment_info', {FieldsConfig.HIDDEN: True})],
                           )

        self.assertPOST409(self._build_setdefault_url(pi_sony, invoice))
示例#24
0
    def test_display_custombrick03(self):
        "With FieldsConfig on sub-fields"
        user = self.login()

        hidden_fname = 'zipcode'
        FieldsConfig.create(
            FakeAddress,
            descriptions=[(hidden_fname, {
                FieldsConfig.HIDDEN: True
            })],
        )
        build_cell = EntityCellRegularField.build
        cbc_item = CustomBrickConfigItem.objects.create(
            id='tests-contacts1',
            name='Contact info',
            content_type=ContentType.objects.get_for_model(FakeContact),
            cells=[
                build_cell(FakeContact, 'last_name'),
                build_cell(FakeContact, 'address__' + hidden_fname),
                build_cell(FakeContact, 'address__city'),
            ],
        )
        bdl = BrickDetailviewLocation.create_if_needed(
            brick_id=cbc_item.generate_id(),
            order=1000,  # Should be the last block
            model=FakeContact,
            zone=BrickDetailviewLocation.BOTTOM,
        )
        naru = FakeContact.objects.create(
            user=user,
            last_name='Narusegawa',
            first_name='Naru',
            phone='1122334455',
        )
        naru.address = FakeAddress.objects.create(
            value='Hinata Inn',
            city='Tokyo',
            zipcode='112233',
            entity=naru,
        )
        naru.save()

        content_node = self._get_contact_brick_content(naru,
                                                       brick_id=bdl.brick_id)
        self.assertEqual(
            naru.last_name,
            self.get_brick_tile(content_node, 'regular_field-last_name').text)
        self.assertEqual(
            naru.address.city,
            self.get_brick_tile(content_node,
                                'regular_field-address__city').text)
        self._assertNoBrickTile(content_node, 'regular_field-address__zipcode')
示例#25
0
    def test_search10(self):
        "With FieldsConfig"
        user = self.login()

        hidden_fname1 = 'description'
        hidden_fname2 = 'sector'
        SearchConfigItem.create_if_needed(
            FakeContact,
            [
                'first_name',
                'last_name',
                hidden_fname1,
                hidden_fname2 + '__title',
            ],
        )

        sector = FakeSector.objects.create(title='Linux dev')

        create_contact = partial(FakeContact.objects.create, user=user)
        linus = create_contact(first_name='Linus',
                               last_name='Torvalds',
                               description="Alan's friend")
        alan = create_contact(first_name='Alan',
                              last_name='Cox',
                              description="Linus' friend")
        andrew = create_contact(first_name='Andrew',
                                last_name='Morton',
                                sector=sector)

        FieldsConfig.create(FakeContact,
                            descriptions=[
                                (hidden_fname1, {
                                    FieldsConfig.HIDDEN: True
                                }),
                                (hidden_fname2, {
                                    FieldsConfig.HIDDEN: True
                                }),
                            ])

        response = self._search('Linu', self.contact_ct_id)
        self.assertEqual(200, response.status_code)

        self.assertContains(response, linus.get_absolute_url())
        self.assertNotContains(response, alan.get_absolute_url())
        self.assertNotContains(response, andrew.get_absolute_url())

        self.assertContains(response, _('First name'))
        self.assertContains(response, _('Last name'))
        self.assertNotContains(response, _('Description'))
        self.assertNotContains(response, _('Sector'))
示例#26
0
    def test_event_createview04(self):
        "FieldsConfig: end is hidden"
        self.login()

        FieldsConfig.create(Event,
                            descriptions=[('end_date', {FieldsConfig.HIDDEN: True})],
                           )

        event = self._create_event('Comiket',
                                   start_date='2016-7-25 8:00',
                                   end_date='2016-7-29 18:30',
                                  )
        self.assertEqual(self.create_datetime(year=2016, month=7, day=25, hour=8), event.start_date)
        self.assertIsNone(event.end_date)
示例#27
0
    def test_addresses_brick07(self):
        "With field config on 'shipping_address' FK field"
        FieldsConfig.create(
            Contact,
            descriptions=[('shipping_address', {
                FieldsConfig.HIDDEN: True
            })],
        )

        c = self._create_contact_n_addresses()

        brick_node = self._get_address_brick_node(c)
        self._assertAddressIn(brick_node, c.billing_address,
                              _('Billing address'))
        self._assertAddressNotIn(brick_node, c.shipping_address)
示例#28
0
    def test_search_field02(self):
        "Ignore hidden fields"
        self.login(create_orga=False)

        FieldsConfig.create(
            Address,
            descriptions=[('city', {
                FieldsConfig.HIDDEN: True
            })],
        )

        field = AddressFKField(
            cell=EntityCellRegularField.build(model=Organisation,
                                              name='billing_address'),
            user=self.user,
        )

        create_orga = partial(Organisation.objects.create, user=self.user)
        orga1 = create_orga(name='Orga without address')
        orga2 = create_orga(name='Orga with empty address')
        orga3 = create_orga(name='Orga with address #1')
        orga4 = create_orga(name='Orga with address #2 (hidden field)')
        orga5 = create_orga(name='Orga with address #3 (not OK)')
        orga6 = create_orga(name='Orga with named address')
        orga_ids = [orga1.id, orga2.id, orga3.id, orga4.id, orga5.id, orga6.id]

        create_address = Address.objects.create
        addr2 = create_address(address='', owner=orga2)
        addr3 = create_address(address='42 Towel street', owner=orga3)
        addr4 = create_address(city='TowelCity', owner=orga4)
        addr5 = create_address(address='42 Fish street', owner=orga5)
        addr6 = create_address(name='Towel', owner=orga6)

        orga2.billing_address = addr2
        orga2.save()
        orga3.billing_address = addr3
        orga3.save()
        orga4.billing_address = addr4
        orga4.save()
        orga5.billing_address = addr5
        orga5.save()
        orga6.billing_address = addr6
        orga6.save()

        self.assertListEqual([orga3], [
            *Organisation.objects.filter(id__in=orga_ids).filter(
                field.to_python(value='towel'))
        ])
示例#29
0
    def _hide_fields(self):
        fields = self.fields
        address_mapping = self.address_mapping
        fconfigs = FieldsConfig.get_4_models((Contact, Organisation, Address))

        # TODO: use shipping address if not hidden ?
        if fconfigs[Contact].is_fieldname_hidden('billing_address'):
            prefix = HOME_ADDR_PREFIX

            for form_fname, __ in address_mapping:
                del fields[prefix + form_fname]

        is_orga_field_hidden = fconfigs[Organisation].is_fieldname_hidden
        for fname in [*fields
                      ]:  # NB: Cannot mutate the OrderedDict during iteration.
            # NB: 5 == len('work_')
            if fname.startswith('work_') and is_orga_field_hidden(fname[5:]):
                del fields[fname]
                del fields['update_' + fname]

        if is_orga_field_hidden('billing_address'):
            prefix = WORK_ADDR_PREFIX

            for form_fname, __ in address_mapping:
                del fields[prefix + form_fname]

            del fields['update_work_address']

        is_addr_field_hidden = fconfigs[Address].is_fieldname_hidden
        addr_prefixes = self.address_prefixes.values()
        for form_fname, model_fname in address_mapping:
            if is_addr_field_hidden(model_fname):
                for prefix in addr_prefixes:
                    fields.pop(prefix + form_fname, None)
示例#30
0
def get_merge_form_builder(model, base_form_class=_PersonMergeForm):
    address_field_names = [*Address.info_field_names()]
    # TODO: factorise with lv_import.py
    try:
        address_field_names.remove('name')
    except ValueError:
       pass

    attrs = {'_address_field_names': address_field_names}

    get_field = Address._meta.get_field
    fields = [get_field(field_name) for field_name in address_field_names]

    is_hidden = FieldsConfig.get_4_model(model).is_fieldname_hidden

    def add_fields(attr_name, prefix):
        fnames = []

        if not is_hidden(attr_name):
            for field in fields:
                form_fieldname = prefix + field.name
                attrs[form_fieldname] = mergefield_factory(field)
                fnames.append(form_fieldname)

        return fnames

    billing_address_fnames  = add_fields('billing_address', _BILL_PREFIX)
    shipping_address_fnames = add_fields('shipping_address', _SHIP_PREFIX)

    attrs['blocks'] = MergeEntitiesBaseForm.blocks.new(
        ('billing_address',  _('Billing address'),  billing_address_fnames),
        ('shipping_address', _('Shipping address'), shipping_address_fnames),
    )

    return type('PersonMergeForm', (base_form_class,), attrs)