Skip to content

Commit

Permalink
Do not search in model with no search configuration anymore.
Browse files Browse the repository at this point in the history
  • Loading branch information
genglert committed Apr 18, 2023
1 parent bef99c6 commit 580b7a5
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 82 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@
- In 'field_printers', 'print_date()' & print_datetime() have been removed.
- In 'creme_core.models' :
- The method 'bricks.RelationBrickItemManager.create_if_needed()' has been removed.
- The method 'Relation.populate_real_object_entities()'' has been removed.
- The method 'relation.Relation.populate_real_object_entities()'' has been removed.
- The method 'search.SearchConfigItemManager.iter_for_models()' does not return unsaved
instances for models without configuration (these models are just ignored).
- In 'creme_core.utils' :
- The function 'find_first()' & 'split_filter()' have been removed.
- The function 'imports.import_object()' has been removed.
Expand Down
6 changes: 3 additions & 3 deletions creme/creme_core/models/search.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
################################################################################
# Creme is a free/open-source Customer Relationship Management software
# Copyright (C) 2009-2022 Hybird
# Copyright (C) 2009-2023 Hybird
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
Expand Down Expand Up @@ -122,8 +122,8 @@ def filter_func(sci):

if sc_items:
yield next((item for item in sc_items if filter_func(item)), sc_items[0])
else:
yield self.model(content_type=ctype)
# else:
# yield self.model(content_type=ctype)


class SearchConfigItem(CremeModel):
Expand Down
27 changes: 17 additions & 10 deletions creme/creme_core/tests/models/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,16 +260,19 @@ def test_manager_get_for_models02(self):
"One model, no config in BD."
user = self.create_user()

configs = [*SearchConfigItem.objects.iter_for_models([FakeContact], user)]
self.assertEqual(1, len(configs))

sc_item = configs[0]
self.assertIsInstance(sc_item, SearchConfigItem)
self.assertEqual(FakeContact, sc_item.content_type.model_class())
self.assertIsNone(sc_item.role)
self.assertFalse(sc_item.superuser)
self.assertTrue(sc_item.all_fields)
self.assertIsNone(sc_item.pk)
# configs = [*SearchConfigItem.objects.iter_for_models([FakeContact], user)]
# self.assertEqual(1, len(configs))
#
# sc_item = configs[0]
# self.assertIsInstance(sc_item, SearchConfigItem)
# self.assertEqual(FakeContact, sc_item.content_type.model_class())
# self.assertIsNone(sc_item.role)
# self.assertFalse(sc_item.superuser)
# self.assertTrue(sc_item.all_fields)
# self.assertIsNone(sc_item.pk)
self.assertFalse(
[*SearchConfigItem.objects.iter_for_models([FakeContact], user)],
)

def test_manager_get_for_models03(self):
"One model, 1 config in DB."
Expand Down Expand Up @@ -352,6 +355,10 @@ def test_manager_get_for_models08(self):
"2 models."
user = self.create_user()

create = SearchConfigItem.objects.create_if_needed
create(FakeContact, ['last_name'])
create(FakeOrganisation, ['name'])

configs = [
*SearchConfigItem.objects
.iter_for_models([FakeContact, FakeOrganisation], user)
Expand Down
23 changes: 21 additions & 2 deletions creme/creme_core/tests/templatetags/test_creme_search.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
from django.contrib.contenttypes.models import ContentType
from django.template import Context, Template

from creme.creme_core.models import FakeContact, SearchConfigItem
from creme.creme_core.models import (
FakeContact,
FakeDocument,
FakeOrganisation,
FakeSector,
SearchConfigItem,
)
from creme.creme_core.tests.base import CremeTestCase
from creme.creme_core.tests.fake_models import FakeOrganisation, FakeSector


class CremeSearchTagsTestCase(CremeTestCase):
Expand All @@ -12,6 +17,10 @@ def test_search_form01(self):
get_ct = ContentType.objects.get_for_model
contact_ct_id = get_ct(FakeContact).id

create_sci = SearchConfigItem.objects.create_if_needed
create_sci(model=FakeContact, fields=['last_name', 'first_name'])
create_sci(model=FakeOrganisation, fields=['name'])

with self.assertNoException():
render = Template(
r'{% load creme_search %}'
Expand Down Expand Up @@ -43,6 +52,10 @@ def test_search_form01(self):
label='Test Organisation',
choices=choices,
)
self.assertNotInChoices(
value=str(get_ct(FakeDocument).id),
choices=choices,
)
self.assertNotInChoices(
value=str(get_ct(FakeSector).id),
choices=choices,
Expand All @@ -65,6 +78,12 @@ def test_search_form02(self):
role='superuser',
disabled=True,
)
SearchConfigItem.objects.create_if_needed(
model=FakeOrganisation,
fields=(),
role='superuser',
disabled=False,
)

get_ct = ContentType.objects.get_for_model
contact_ct_id = get_ct(FakeContact).id
Expand Down
Loading

0 comments on commit 580b7a5

Please sign in to comment.