Пример #1
0
    def test_relations_brick01(self):
        user = self.login()

        create_contact = partial(FakeContact.objects.create, user=user)
        atom = create_contact(first_name='Atom', last_name='Tenma')
        tenma = create_contact(first_name='Dr', last_name='Tenma')
        uran = create_contact(first_name='Uran', last_name='Ochanomizu')

        rtype1 = RelationType.create(
            ('test-subject_son', 'is the son of'),
            ('test-object_father', 'is the father of'))[0]
        Relation.objects.create(subject_entity=atom,
                                type=rtype1,
                                object_entity=tenma,
                                user=user)

        rtype2 = RelationType.create(
            ('test-subject_brother', 'is the brother of'),
            ('test-object_sister', 'is the sister of'))[0]
        Relation.objects.create(subject_entity=atom,
                                type=rtype2,
                                object_entity=uran,
                                user=user)

        response = self.assertGET200(atom.get_absolute_url())
        self.assertTemplateUsed(response, 'creme_core/bricks/relations.html')

        document = self.get_html_tree(response.content)
        brick_node = self.get_brick_node(document, RelationsBrick.id_)
        self.assertInstanceLink(brick_node, tenma)
        self.assertInstanceLink(brick_node, uran)
        self.assertEqual('{}',
                         brick_node.attrib.get('data-brick-reloading-info'))
    def test_converted_relations(self):
        self.login()
        from ..registry import relationtype_converter

        self.assertEqual(0, Relation.objects.count())
        quote, source, target = self.create_quote_n_orgas('My Quote')
        rtype1, rtype2 = RelationType.create(
            ('test-CONVERT-subject_foobar', 'is loving', [Quote]),
            ('test-CONVERT-object_foobar', 'is loved by', [Organisation]),
        )
        rtype3, rtype4 = RelationType.create(
            ('test-CONVERT-subject_foobar_not_copiable', 'is loving',
             [Invoice]),
            ('test-CONVERT-object_foobar_not_copiable', 'is loved by',
             [Organisation]),
        )
        relationtype_converter.register(Quote, rtype1, Invoice, rtype3)

        Relation.objects.create(user=self.user,
                                type=rtype1,
                                subject_entity=quote,
                                object_entity=source)
        self.assertEqual(1, Relation.objects.filter(type=rtype1).count())
        self.assertEqual(1, Relation.objects.filter(type=rtype2).count())
        self.assertEqual(0, Relation.objects.filter(type=rtype3).count())
        self.assertEqual(0, Relation.objects.filter(type=rtype4).count())

        self._convert(200, quote, 'invoice')
        self.assertEqual(1, Relation.objects.filter(type=rtype1).count())
        self.assertEqual(1, Relation.objects.filter(type=rtype2).count())
        self.assertEqual(1, Relation.objects.filter(type=rtype3).count())
        self.assertEqual(1, Relation.objects.filter(type=rtype4).count())
Пример #3
0
    def test_relationship(self):
        constraint = GHCCRelation(model=FakeContact)

        rtype1 = RelationType.create(
            ('test-subject_likes', 'likes'),
            ('test-object_likes', 'is liked by'),
        )[0]
        self.assertTrue(
            constraint.check_cell(EntityCellRelation(FakeContact, rtype1)))

        rtype2 = RelationType.create(
            ('test-subject_loves', 'is loving', [FakeContact]),
            ('test-object_loves', 'is loved by', [FakeContact]),
        )[0]
        self.assertTrue(
            constraint.check_cell(EntityCellRelation(FakeContact, rtype2)))

        rtype3 = RelationType.create(
            ('test-subject_branch', 'has branch', [FakeOrganisation]),
            ('test-object_branch', 'is a branch of', [FakeOrganisation]),
        )[0]
        self.assertFalse(
            constraint.check_cell(EntityCellRelation(FakeContact, rtype3)))

        # ---
        cell1 = constraint.get_cell(cell_key=f'relation-{rtype2.id}')
        self.assertIsInstance(cell1, EntityCellRelation)
        self.assertEqual(rtype2, cell1.relation_type)

        self.assertIsNone(
            constraint.get_cell(cell_key=f'relation-{rtype3.id}'))

        # ---
        cells = [*constraint.cells()]
        self.assertGreaterEqual(len(cells), 2)
        self.assertIsInstance(cells[0], EntityCellRelation)

        def find_cell(rtype):
            for cell in cells:
                if cell.relation_type == rtype:
                    return

            self.fail(f'{rtype} not found in cells.')

        find_cell(rtype1)
        find_cell(rtype2)

        for cell in cells:
            if cell.relation_type == rtype3:
                self.fail(f'{rtype3} should not be found in cells.')
Пример #4
0
    def test_edit02(self):
        "Edit a custom type"
        rt = RelationType.create(('test-subfoo', 'subject_predicate'),
                                 ('test-objfoo', 'object_predicate'),
                                 is_custom=True)[0]
        url = self._build_edit_url(rt)
        response = self.assertGET200(url)
        # self.assertTemplateUsed(response, 'creme_core/generics/blockform/edit_popup.html')
        self.assertTemplateUsed(
            response, 'creme_core/generics/blockform/edit-popup.html')

        context = response.context
        # self.assertEqual(_('Edit the type «{predicate}»').format(predicate=rt),
        self.assertEqual(
            pgettext('creme_config-relationship',
                     'Edit the type «{object}»').format(object=rt),
            context.get('title'))
        self.assertEqual(_('Save the modifications'),
                         context.get('submit_label'))

        # ---
        subject_pred = 'loves'
        object_pred = 'is loved by'
        response = self.client.post(url,
                                    data={
                                        'subject_predicate': subject_pred,
                                        'object_predicate': object_pred,
                                    })
        self.assertNoFormError(response)

        rel_type = RelationType.objects.get(pk=rt.id)
        self.assertEqual(subject_pred, rel_type.predicate)
        self.assertEqual(object_pred, rel_type.symmetric_type.predicate)
Пример #5
0
    def test_manager_safe_multi_save02(self):
        "De-duplicates arguments"
        rtype = RelationType.create(
            ('test-subject_foobar', 'challenges'),
            ('test-object_foobar', 'is challenged by'))[0]

        user = self.user
        create_contact = partial(FakeContact.objects.create, user=user)
        ryuko = create_contact(first_name='Ryuko', last_name='Matoi')
        satsuki = create_contact(first_name='Satsuki', last_name='Kiryuin')

        def build_rel():
            return Relation(user=user,
                            subject_entity=ryuko,
                            type=rtype,
                            object_entity=satsuki)

        with self.assertNoException():
            count = Relation.objects.safe_multi_save(
                [build_rel(), build_rel()])

        rel = self.get_object_or_fail(Relation, type=rtype)
        self.assertEqual(ryuko.id, rel.subject_entity_id)
        self.assertEqual(satsuki.id, rel.object_entity_id)
        self.assertEqual(user.id, rel.user_id)

        self.assertEqual(1, count)
Пример #6
0
    def test_relation01(self):
        subject_pred = 'is loving'
        object_pred = 'is loved by'

        with self.assertNoException():
            rtype1, rtype2 = RelationType.create(
                ('test-subject_foobar', subject_pred),
                ('test-object_foobar', object_pred))

        self.assertEqual(rtype1.symmetric_type, rtype2)
        self.assertEqual(rtype2.symmetric_type, rtype1)
        self.assertEqual(rtype1.predicate, subject_pred)
        self.assertEqual(rtype2.predicate, object_pred)

        with self.assertNoException():
            entity1 = CremeEntity.objects.create(user=self.user)
            entity2 = CremeEntity.objects.create(user=self.user)
            relation = Relation.objects.create(user=self.user,
                                               type=rtype1,
                                               subject_entity=entity1,
                                               object_entity=entity2)

        sym = relation.symmetric_relation
        self.assertEqual(sym.type, rtype2)
        self.assertEqual(sym.subject_entity, entity2)
        self.assertEqual(sym.object_entity, entity1)
Пример #7
0
    def test_manager_safe_get_or_create02(self):
        "Give user ID (not user instance)"
        rtype, srtype = RelationType.create(
            ('test-subject_challenge', 'challenges'),
            ('test-object_challenge', 'is challenged by'))

        user = self.user
        create_contact = partial(FakeContact.objects.create, user=user)
        ryuko = create_contact(first_name='Ryuko', last_name='Matoi')
        satsuki = create_contact(first_name='Satsuki', last_name='Kiryuin')

        rel1 = Relation.objects.safe_get_or_create(
            user_id=user.id,
            subject_entity=ryuko,
            type=rtype,
            object_entity=satsuki,
        )
        self.assertIsInstance(rel1, Relation)
        self.assertTrue(rel1.pk)
        self.assertEqual(rtype.id, rel1.type_id)
        self.assertEqual(ryuko.id, rel1.subject_entity_id)
        self.assertEqual(satsuki.id, rel1.object_entity_id)
        self.assertEqual(user.id, rel1.user_id)
        self.assertEqual(srtype, rel1.symmetric_relation.type)

        # ---
        with self.assertNoException():
            rel2 = Relation.objects.safe_get_or_create(
                user_id=user.id,
                subject_entity=ryuko,
                type=rtype,
                object_entity=satsuki,
            )

        self.assertEqual(rel1, rel2)
Пример #8
0
    def test_manager_safe_create(self):
        rtype, srtype = RelationType.create(
            ('test-subject_challenge', 'challenges'),
            ('test-object_challenge', 'is challenged by'))

        user = self.user
        create_contact = partial(FakeContact.objects.create, user=user)
        ryuko = create_contact(first_name='Ryuko', last_name='Matoi')
        satsuki = create_contact(first_name='Satsuki', last_name='Kiryuin')

        res = Relation.objects.safe_create(
            user=user,
            subject_entity=ryuko,
            type=rtype,
            object_entity=satsuki,
        )
        self.assertIsNone(res)

        rel = self.get_object_or_fail(Relation, type=rtype)
        self.assertEqual(rtype.id, rel.type_id)
        self.assertEqual(ryuko.id, rel.subject_entity_id)
        self.assertEqual(satsuki.id, rel.object_entity_id)
        self.assertEqual(user.id, rel.user_id)
        self.assertEqual(srtype, rel.symmetric_relation.type)

        # ---
        with self.assertNoException():
            Relation.objects.safe_create(
                user=user,
                subject_entity=ryuko,
                type=rtype,
                object_entity=satsuki,
            )
Пример #9
0
    def test_disable02(self):
        "Relationship creation & deletion"
        user = self.user
        hayao = FakeContact.objects.create(user=user,
                                           first_name='Hayao',
                                           last_name='Miyazaki')
        ghibli = FakeOrganisation.objects.create(user=user, name='Ghibli')
        rtype = RelationType.create(('test-subject_employed', 'is employed'),
                                    ('test-object_employed', 'employs'))[0]

        old_count = HistoryLine.objects.count()
        rel = Relation(user=user,
                       subject_entity=hayao,
                       object_entity=ghibli,
                       type=rtype)

        HistoryLine.disable(rel)
        rel.save()
        self.assertEqual(old_count, HistoryLine.objects.count())

        # -----------------------
        rel = self.refresh(rel)
        HistoryLine.disable(rel)

        rel.delete()
        self.assertEqual(old_count, HistoryLine.objects.count())
Пример #10
0
    def test_delete02(self):
        "Can't be deleted"
        user = self.user
        invoice, source, target = self.create_invoice_n_orgas('Invoice001')
        service_line = ServiceLine.objects.create(user=user, related_document=invoice,
                                                  on_the_fly_item='Flyyyyy',
                                                 )
        rel1 = Relation.objects.get(subject_entity=invoice.id, object_entity=service_line.id)

        # This relation prohibits the deletion of the invoice
        ce = CremeEntity.objects.create(user=user)
        rtype = RelationType.create(('test-subject_linked', 'is linked to'),
                                    ('test-object_linked',  'is linked to'),
                                    is_internal=True,
                                   )[0]
        rel2 = Relation.objects.create(subject_entity=invoice, object_entity=ce, type=rtype, user=user)

        self.assertRaises(ProtectedError, invoice.delete)

        try:
            Invoice.objects.get(pk=invoice.pk)
            Organisation.objects.get(pk=source.pk)
            Organisation.objects.get(pk=target.pk)

            CremeEntity.objects.get(pk=ce.id)
            Relation.objects.get(pk=rel2.id)

            ServiceLine.objects.get(pk=service_line.pk)
            Relation.objects.get(pk=rel1.id)
        except Exception as e:
            self.fail("Exception: ({}). Maybe the db doesn't support transaction ?".format(e))
    def test_delete07(self):
        "We replace with the segment with property_type=NULL"
        user = self.login()

        strategy = Strategy.objects.create(user=user, name='Producers')
        desc = self._create_segment_desc(strategy, 'Producer')
        segment1 = desc.segment
        old_ptype = segment1.property_type

        orga = Organisation.objects.create(user=user, name='NHK')
        prop = CremeProperty.objects.create(creme_entity=orga, type=old_ptype)

        rtype = RelationType.create(
            ('commercial-subject_test_segment_delete7', 'has produced',
             [Organisation], [old_ptype]),
            ('commercial-object_test_segment_delete7', 'has been produced by',
             [Organisation]),
        )[0]

        segment2 = self.get_object_or_fail(MarketSegment, property_type=None)
        self.assertPOST200(self._build_delete_url(segment1),
                           data={'to_segment': segment2.id})
        self.assertDoesNotExist(segment1)
        self.assertDoesNotExist(old_ptype)

        desc = self.assertStillExists(desc)
        self.assertEqual(segment2, desc.segment)

        self.assertDoesNotExist(prop)
        self.assertFalse(rtype.subject_properties.all())
Пример #12
0
 def test_delete02(self):
     rt, srt = RelationType.create(('test-subfoo', 'subject_predicate'),
                                   ('test-subfoo', 'object_predicate'),
                                   is_custom=True)
     self.assertPOST200(self.DEL_URL, data={'id': rt.id})
     self.assertDoesNotExist(rt)
     self.assertDoesNotExist(srt)
Пример #13
0
    def test_create08(self):
        "Internal relationships are forbidden."
        user = self.login()
        orga = Organisation.objects.create(user=user,
                                           name='Olympus',
                                           is_managed=True)
        rtype = RelationType.create(
            ('creme_config-subject_test_badrtype', 'Bad RType', [Contact]),
            ('creme_config-object_test_badrtype', 'Bad RType sym',
             [Organisation]),
            is_internal=True,  # <==
        )[0]

        password = '******'
        response = self.assertPOST200(
            self.ADD_URL,
            follow=True,
            data={
                'username': '******',
                'password_1': password,
                'password_2': password,
                'first_name': 'Deunan',
                'last_name': 'Knut',
                'email': '*****@*****.**',
                'organisation': orga.id,
                'relation': rtype.id,
            },
        )
        self.assertFormError(
            response,
            'form',
            'relation',
            _('Select a valid choice. That choice is not one of the available choices.'
              ),
        )
Пример #14
0
    def test_relation_block_errors(self):
        rtype = RelationType.create(('test-subject_rented', 'is rented by'),
                                    ('test-object_rented',  'rents'),
                                   )[0]
        ct_contact = ContentType.objects.get_for_model(FakeContact)
        rbi = RelationBrickItem.create(rtype.id)

        build = partial(EntityCellRegularField.build, model=FakeContact)
        rbi.set_cells(ct_contact,
                      [build(name='last_name'), build(name='description')]
                     )
        rbi.save()

        # Inject error by bypassing checkings
        RelationBrickItem.objects.filter(id=rbi.id) \
            .update(json_cells_map=rbi.json_cells_map.replace('description', 'invalid'))

        rbi = self.refresh(rbi)
        cells_contact = rbi.get_cells(ct_contact)
        self.assertEqual(1, len(cells_contact))
        self.assertEqual('last_name', cells_contact[0].value)

        with self.assertNoException():
            deserialized = jsonloads(rbi.json_cells_map)

        self.assertEqual({str(ct_contact.id): [{'type': 'regular_field', 'value': 'last_name'}]},
                         deserialized
                        )
Пример #15
0
    def test_ok02(self):
        "All types of columns"
        loves = RelationType.create(('test-subject_love', 'Is loving'),
                                    ('test-object_love', 'Is loved by'))[0]
        customfield = CustomField.objects.create(
            name='Size (cm)',
            field_type=CustomField.INT,
            content_type=self.ct_contact,
        )
        funcfield = function_field_registry.get(FakeContact,
                                                'get_pretty_properties')

        field = EntityCellsField(content_type=self.ct_contact)
        cells = field.clean('relation-{},'
                            'regular_field-last_name,'
                            'function_field-{},'
                            'custom_field-{},'
                            'regular_field-first_name'.format(
                                loves.id,
                                funcfield.name,
                                customfield.id,
                            ))

        self.assertEqual(5, len(cells))
        self.assertCellOK(cells[0], EntityCellRelation, loves.id)
        self.assertCellOK(cells[1], EntityCellRegularField, 'last_name')
        self.assertCellOK(cells[2], EntityCellFunctionField, funcfield.name)
        self.assertCellOK(cells[3], EntityCellCustomField, str(customfield.id))
        self.assertCellOK(cells[4], EntityCellRegularField, 'first_name')
Пример #16
0
    def test_relation_brick02(self):
        "All ctypes configured + Relation instance."
        rtype = RelationType.create(
            ('test-subject_rented', 'is rented by'),
            ('test-object_rented', 'rents', [FakeContact, FakeOrganisation]),
        )[0]

        # rbi = RelationBrickItem.create(rtype.id)
        rbi = RelationBrickItem.objects.create_if_needed(rtype)
        self.assertIsInstance(rbi, RelationBrickItem)
        self.assertIsNotNone(rbi.pk)
        self.assertEqual(rtype.id, rbi.relation_type_id)

        get_ct = ContentType.objects.get_for_model

        rbi.set_cells(get_ct(FakeContact),
                      [EntityCellRegularField.build(FakeContact, 'last_name')])
        rbi.save()
        self.assertFalse(self.refresh(rbi).all_ctypes_configured)

        rbi.set_cells(get_ct(FakeOrganisation),
                      [EntityCellRegularField.build(FakeOrganisation, 'name')])
        rbi.save()
        self.assertTrue(self.refresh(rbi).all_ctypes_configured)

        # ---
        self.assertEqual(rbi,
                         RelationBrickItem.objects.create_if_needed(rtype))
Пример #17
0
    def test_create_linked_contact07(self):
        "Avoid internal RelationType."
        user = self.login()

        orga = Organisation.objects.create(user=user, name='Acme')
        rtype = RelationType.create(
            ('persons-subject_test_linked', 'is the employee of the month at', [Contact]),
            ('persons-object_test_linked',  'has the employee of the month',   [Organisation]),
            is_internal=True,
        )[0]

        response = self.assertPOST200(
            self._build_addrelated_url(orga.id),
            follow=True,
            data={
                'user': user.pk,

                'first_name': 'Bugs',
                'last_name':  'Bunny',

                'rtype_for_organisation': rtype.id,
            },
        )
        self.assertFormError(
            response, 'form', 'rtype_for_organisation',
            _('Select a valid choice. That choice is not one of the available choices.'),
        )
Пример #18
0
    def save(self,
             pk_subject='creme_config-subject_userrelationtype',
             pk_object='creme_config-object_userrelationtype',
             generate_pk=True,
             *args,
             **kwargs):
        get_data = self.cleaned_data.get

        subject_ctypes = [
            ct.model_class() for ct in get_data('subject_ctypes')
        ]
        object_ctypes = [ct.model_class() for ct in get_data('object_ctypes')]

        return RelationType.create(
            (
                pk_subject,
                get_data('subject_predicate'),
                subject_ctypes,
                get_data('subject_properties'),
            ),
            (
                pk_object,
                get_data('object_predicate'),
                object_ctypes,
                get_data('object_properties'),
            ),
            is_custom=True,
            generate_pk=generate_pk,
            is_copiable=(get_data('subject_is_copiable'),
                         get_data('object_is_copiable')),
            minimal_display=(get_data('subject_min_display'),
                             get_data('object_min_display')),
        )
Пример #19
0
    def test_relations_n_properties(self):
        "The properties needed by the relation types are added."
        user = self.login()

        orga = FakeOrganisation.objects.create(user=user, name='Bebop')

        create_ptype = CremePropertyType.create
        ptype1 = create_ptype(str_pk='test-prop_captain', text='Is a captain')
        ptype2 = create_ptype(str_pk='test-prop_strong', text='Is strong')

        rtype = RelationType.create(
            ('test-subject_leads', 'leads', [FakeContact], [ptype1, ptype2]),
            ('test-object_leads', 'is lead', [FakeOrganisation]),
        )[0]

        form = FakeContactForm(
            user=user,
            data={
                'user':
                user.id,
                'first_name':
                'Spike',
                'last_name':
                'Spiegel',
                'property_types': [ptype1.id, ptype2.id],
                'relation_types':
                self.formfield_value_multi_relation_entity((rtype.id, orga), ),
            },
        )
        self.assertFalse(form.errors)

        subject = form.save()
        self.assertRelationCount(1, subject, rtype, orga)
Пример #20
0
 def test_delete01(self):
     rt = RelationType.create(
         ('test-subfoo', 'subject_predicate'),
         ('test-subfoo', 'object_predicate'),
         is_custom=False,
     )[0]
     self.assertGET405(self.DEL_URL, data={'id': rt.id})
Пример #21
0
    def test_relationtype_registry01(self):
        "Default data + register() method."
        registry = lv_search.RelationSearchRegistry()

        cell1 = EntityCellRelation.build(model=FakeContact,
                                         rtype_id=REL_SUB_HAS)

        field = registry.get_field(cell=cell1, user=self.user)
        self.assertIsInstance(field, lv_form.RelationField)
        self.assertIsNone(registry.builder(REL_SUB_HAS))
        self.assertEqual(lv_form.RelationField, registry.default_builder)

        # ---
        class MyRelationField(lv_form.ListViewSearchField):
            pass

        rtype2 = RelationType.create(
            ('test-subject_loves', 'loves'),
            ('test-object_loved', 'is loved by'),
        )[0]
        cell2 = EntityCellRelation(model=FakeContact, rtype=rtype2)

        registry.register(rtype_id=rtype2.id, sfield_builder=MyRelationField)
        self.assertIsInstance(registry.get_field(cell=cell2, user=self.user),
                              MyRelationField)
        self.assertIsInstance(registry.get_field(cell=cell1, user=self.user),
                              lv_form.RelationField)

        self.assertIsNone(registry.builder(REL_SUB_HAS))
        self.assertEqual(MyRelationField, registry.builder(rtype2.id))
Пример #22
0
    def test_add_relation02(self):
        "Create the relation using the 'object' relation type"
        user = self.user
        nerv = FakeOrganisation.objects.create(user=user, name='Nerv')
        rei  = FakeContact.objects.create(user=user, first_name='Rei', last_name='Ayanami')
        olds_ids = list(HistoryLine.objects.values_list('id', flat=True))

        rtype, srtype = RelationType.create(('test-subject_works5', 'is employed'),
                                            ('test-object_works5',  'employs')
                                           )
        Relation.objects.create(user=user, subject_entity=nerv, object_entity=rei, type=srtype)

        hlines = list(HistoryLine.objects.exclude(id__in=olds_ids).order_by('id'))
        self.assertEqual(2, len(hlines))

        hline = hlines[-2]
        self.assertEqual(rei.id,            hline.entity.id)
        self.assertEqual(TYPE_RELATION,     hline.type)
        self.assertEqual([rtype.id],        hline.modifications)

        hline_sym = hlines[-1]
        self.assertEqual(nerv.id,           hline_sym.entity.id)
        self.assertEqual(TYPE_SYM_RELATION, hline_sym.type)
        self.assertEqual([srtype.id],       hline_sym.modifications)

        self.assertEqual(hline_sym.id, hline.related_line.id)
Пример #23
0
    def test_related_edition01(self):
        user = self.user
        ghibli = self._build_organisation(user=user.id, name='Ghibli')

        first_name = 'Hayao'
        last_name  = 'Miyazaki'
        hayao = self._build_contact(user=user.id, first_name=first_name, last_name=last_name)

        rtype, srtype = RelationType.create(('test-subject_employed', 'is employed'),
                                            ('test-object_employed', 'employs')
                                           )
        Relation.objects.create(user=user, subject_entity=hayao, object_entity=ghibli, type=rtype)

        old_count = HistoryLine.objects.count()
        description = 'A great animation movie maker'
        response = self.client.post(hayao.get_edit_absolute_url(), follow=True,
                                    data={'user':        user.id,
                                          'first_name':  first_name,
                                          'last_name':   last_name,
                                          'description': description,
                                         }
                                   )
        self.assertNoFormError(response)
        self.assertEqual(description, self.refresh(hayao).description)

        hlines = self._get_hlines()
        self.assertEqual(old_count + 1, len(hlines))

        hline = hlines[-1]
        self.assertEqual(TYPE_EDITION, hline.type)
        self.assertIsNone(hline.related_line)
Пример #24
0
    def test_delete_lines(self):
        user = self.user
        hayao = FakeContact.objects.create(user=user, first_name='Hayao', last_name='Miyazaki')
        ghibli = FakeOrganisation.objects.create(user=user, name='Ghibli')

        rtype = RelationType.create(('test-subject_delline_works', 'is employed'),
                                    ('test-object_delline_works',  'employs')
                                   )[0]
        Relation.objects.create(user=user, subject_entity=hayao, object_entity=ghibli, type=rtype)

        HistoryConfigItem.objects.create(relation_type=rtype)
        hayao = self.refresh(hayao)
        hayao.description = 'Dream maker'
        hayao.save()

        hayao_line_qs = HistoryLine.objects.filter(entity=hayao)
        ghibli_line_qs = HistoryLine.objects.filter(entity=ghibli)
        self.assertEqual(3, hayao_line_qs.count())
        self.assertEqual(3, ghibli_line_qs.count())

        HistoryLine.delete_lines(hayao_line_qs)
        self.assertFalse(hayao_line_qs.all())

        ghibli_lines = list(ghibli_line_qs.all())
        self.assertEqual(1, len(ghibli_lines))
        self.assertEqual(TYPE_CREATION, ghibli_lines[0].type)
Пример #25
0
    def test_not_copiable_relations(self):
        self.login()
        self.assertEqual(0, Relation.objects.count())

        quote, source, target = self.create_quote_n_orgas('My Quote')
        rtype1, rtype2 = RelationType.create(('test-subject_foobar', 'is loving', (Quote, Invoice)),
                                             ('test-object_foobar',  'is loved by', (Organisation,)))

        self.assertTrue(rtype1.is_copiable)
        self.assertTrue(rtype2.is_copiable)

        Relation.objects.create(user=self.user, type=rtype1,
                                subject_entity=quote,
                                object_entity=source)
        self.assertEqual(1, Relation.objects.filter(type=rtype1).count())
        self.assertEqual(1, Relation.objects.filter(type=rtype2).count())

        rtype3, rtype4 = RelationType.create(('test-subject_foobar_not_copiable', 'is loving', (Quote, Invoice)),
                                             ('test-object_foobar_not_copiable',  'is loved by', (Organisation,)),
                                             is_copiable=False)

        self.assertFalse(rtype3.is_copiable)
        self.assertFalse(rtype4.is_copiable)

        Relation.objects.create(user=self.user, type=rtype3,
                                subject_entity=quote,
                                object_entity=source)
        self.assertEqual(1, Relation.objects.filter(type=rtype3).count())
        self.assertEqual(1, Relation.objects.filter(type=rtype4).count())

        rtype5, rtype6 = RelationType.create(('test-subject_foobar_wrong_ctype', 'is loving', (Quote,)),
                                             ('test-object_foobar_wrong_ctype',  'is loved by', (Organisation,)))

        Relation.objects.create(user=self.user, type=rtype5,
                                subject_entity=quote,
                                object_entity=source)
        self.assertEqual(1, Relation.objects.filter(type=rtype5).count())
        self.assertEqual(1, Relation.objects.filter(type=rtype6).count())

        # Contact2 < ---- > Orga
        self._convert(200, quote, 'invoice')
        self.assertEqual(2, Relation.objects.filter(type=rtype1).count())
        self.assertEqual(2, Relation.objects.filter(type=rtype2).count())
        self.assertEqual(1, Relation.objects.filter(type=rtype3).count())
        self.assertEqual(1, Relation.objects.filter(type=rtype4).count())
        self.assertEqual(1, Relation.objects.filter(type=rtype5).count())
        self.assertEqual(1, Relation.objects.filter(type=rtype6).count())
Пример #26
0
    def test_delete(self):
        rtype = RelationType.create(('test-subject_foo', 'fooes'),
                                    ('test-object_foo', 'fooed'))[0]
        hci = HistoryConfigItem.objects.create(relation_type=rtype)

        self.assertPOST200(reverse('creme_config__remove_history_config'),
                           data={'id': hci.id})
        self.assertDoesNotExist(hci)
Пример #27
0
    def test_relations_credentials02(self):
        "Label if cannot link the future entity."
        user = self.login(is_superuser=False, creatable_models=[FakeContact])

        create_creds = partial(SetCredentials.objects.create, role=self.role)
        create_creds(value=EntityCredentials.VIEW   |
                           EntityCredentials.CHANGE |
                           EntityCredentials.LINK,
                     set_type=SetCredentials.ESET_OWN,
                    )
        create_creds(value=EntityCredentials.LINK,
                     set_type=SetCredentials.ESET_ALL,
                     ctype=FakeContact, forbidden=True,
                    )

        orga = FakeOrganisation.objects.create(user=user, name='Oshino corp.')

        rtype = RelationType.create(('test-subject_heals', 'heals'),
                                    ('test-object_heals',  'is healed by'),
                                   )[1]

        sfrt = SemiFixedRelationType.objects.create(predicate='Healed by Oshino',
                                                    relation_type=rtype,
                                                    object_entity=orga,
                                                   )

        fields = FakeContactForm(user=user).fields

        with self.assertNoException():
            info_field = fields['rtypes_info']

        self.assertNotIn('relation_types', fields)
        self.assertNotIn('semifixed_rtypes', fields)
        self.assertIsInstance(info_field.widget, Label)
        self.assertEqual(
            _('You are not allowed to link this kind of entity.'),
            info_field.initial
        )

        # ---
        form = FakeContactForm(
            user=user,
            data={
                'user':       user.id,
                'first_name': 'Kanbaru',
                'last_name':  'Suruga',

                'relation_types': self.formfield_value_multi_relation_entity(
                    (rtype.id, orga),  # Should not be used
                ),
                'semifixed_rtypes': [sfrt.id],  # Idem
             },
        )
        self.assertFalse(form.errors)

        subject = form.save()
        self.assertFalse(subject.relations.count())
Пример #28
0
    def test_add_relation01(self):
        user = self.user
        nerv = FakeOrganisation.objects.create(user=user, name='Nerv')
        rei = FakeContact.objects.create(user=user,
                                         first_name='Rei',
                                         last_name='Ayanami')
        old_count = HistoryLine.objects.count()

        sleep(1)  # Ensure than relation is younger than entities

        rtype, srtype = RelationType.create(
            ('test-subject_works4', 'is employed'),
            ('test-object_works4', 'employs'))
        relation = Relation.objects.create(user=user,
                                           subject_entity=rei,
                                           object_entity=nerv,
                                           type=rtype)
        relation = self.refresh(
            relation)  # Refresh to get the right modified value

        hlines = self._get_hlines()
        self.assertEqual(old_count + 2, len(hlines))

        hline = hlines[-2]
        self.assertEqual(rei.id, hline.entity.id)
        self.assertEqual(str(rei), hline.entity_repr)
        self.assertEqual(TYPE_RELATION, hline.type)
        self.assertEqual([rtype.id], hline.modifications)
        # self.assertEqual(relation.modified, hline.date)
        self.assertEqual(relation.created, hline.date)
        self.assertEqual(True, hline.line_type.is_about_relation)

        hline_sym = hlines[-1]
        self.assertEqual(nerv.id, hline_sym.entity.id)
        self.assertEqual(str(nerv), hline_sym.entity_repr)
        self.assertEqual(TYPE_SYM_RELATION, hline_sym.type)
        self.assertEqual([srtype.id], hline_sym.modifications)
        # self.assertEqual(relation.modified, hline_sym.date)
        self.assertEqual(relation.created, hline_sym.date)
        self.assertIs(True, hline.line_type.is_about_relation)

        self.assertEqual(hline_sym.id, hline.related_line.id)
        self.assertEqual(hline.id, hline_sym.related_line.id)

        msg = _('Add a relationship “{}”').format
        self.assertEqual([msg(rtype.predicate)],
                         hline.get_verbose_modifications(user))
        self.assertEqual([msg(srtype.predicate)],
                         hline_sym.get_verbose_modifications(user))

        rtype_id = rtype.id
        relation.delete()
        rtype.delete()
        self.assertDoesNotExist(rtype)
        self.assertEqual([msg(rtype_id)],
                         self.refresh(hline).get_verbose_modifications(user))
Пример #29
0
    def setUp(self):
        self.login()

        self.loves = RelationType.create(
            ('test-subject_foobar', 'is loving'),
            ('test-object_foobar', 'is loved by'))[0]

        self.iori = FakeContact.objects.create(user=self.user,
                                               first_name='Iori',
                                               last_name='Yoshizuki')
Пример #30
0
    def test_relations_credentials06(self):
        "Link credentials on the created entity + forced relationships."
        user = self.login(is_superuser=False, creatable_models=[FakeContact])

        create_creds = partial(SetCredentials.objects.create, role=self.role)
        create_creds(
            value=EntityCredentials.VIEW | EntityCredentials.LINK,
            set_type=SetCredentials.ESET_OWN,
        )
        create_creds(value=EntityCredentials.VIEW,
                     set_type=SetCredentials.ESET_ALL)

        orga = FakeOrganisation.objects.create(user=user, name='Oshino corp.')

        rtype = RelationType.create(
            ('test-subject_heals', 'heals'),
            ('test-object_heals', 'is healed by'),
        )[1]

        data = {
            'first_name': 'Kanbaru',
            'last_name': 'Suruga',
        }
        forced_relations = [Relation(type=rtype, object_entity=orga)]

        # KO ---
        form1 = FakeContactForm(
            user=user,
            data={
                **data, 'user': self.other_user.id
            },
            forced_relations=forced_relations,
        )
        self.assertFormInstanceErrors(
            form1,
            (
                'user',
                _('You are not allowed to link with the «{models}» of this user.'
                  ).format(models='Test Contacts', ),
            ),
        )

        # OK ---
        form2 = FakeContactForm(
            user=user,
            data={
                **data, 'user': user.id
            },
            forced_relations=forced_relations,
        )
        self.assertFalse(form2.errors)

        subject = form2.save()
        self.assertRelationCount(1, subject, rtype, orga)