diff options
Diffstat (limited to 'archaeological_context_records')
5 files changed, 120 insertions, 1 deletions
diff --git a/archaeological_context_records/forms.py b/archaeological_context_records/forms.py index e5c244fde..c310e98fa 100644 --- a/archaeological_context_records/forms.py +++ b/archaeological_context_records/forms.py @@ -56,6 +56,7 @@ class OperationFormSelection(forms.Form): class RecordSelect(TableSelect): + search_vector = forms.CharField(label=_(u"Full text search")) label = forms.CharField(label=_(u"ID"), max_length=100) parcel__town = get_town_field() if settings.COUNTRY == 'fr': diff --git a/archaeological_context_records/migrations/0010_auto_20171011_1644.py b/archaeological_context_records/migrations/0010_auto_20171011_1644.py new file mode 100644 index 000000000..379110e44 --- /dev/null +++ b/archaeological_context_records/migrations/0010_auto_20171011_1644.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11 on 2017-10-11 16:44 +from __future__ import unicode_literals + +import django.contrib.postgres.search +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('archaeological_context_records', '0009_auto_20170829_1639'), + ] + + operations = [ + migrations.AddField( + model_name='contextrecord', + name='search_vector', + field=django.contrib.postgres.search.SearchVectorField(blank=True, help_text='Auto filled at save', null=True, verbose_name='Search vector'), + ), + migrations.AddField( + model_name='historicalcontextrecord', + name='search_vector', + field=django.contrib.postgres.search.SearchVectorField(blank=True, help_text='Auto filled at save', null=True, verbose_name='Search vector'), + ), + ] diff --git a/archaeological_context_records/migrations/0011_auto_20171012_1316.py b/archaeological_context_records/migrations/0011_auto_20171012_1316.py new file mode 100644 index 000000000..95b042c43 --- /dev/null +++ b/archaeological_context_records/migrations/0011_auto_20171012_1316.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11 on 2017-10-12 13:16 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('archaeological_context_records', '0010_auto_20171011_1644'), + ] + + operations = [ + migrations.AlterField( + model_name='contextrecord', + name='cached_label', + field=models.TextField(blank=True, db_index=True, null=True, verbose_name='Cached name'), + ), + migrations.AlterField( + model_name='historicalcontextrecord', + name='cached_label', + field=models.TextField(blank=True, db_index=True, null=True, verbose_name='Cached name'), + ), + ] diff --git a/archaeological_context_records/models.py b/archaeological_context_records/models.py index 2f02ed9df..a8517ed26 100644 --- a/archaeological_context_records/models.py +++ b/archaeological_context_records/models.py @@ -302,7 +302,14 @@ class ContextRecord(BulkUpdatedItem, BaseHistorizedItem, point_2d = models.PointField(_(u"Point (2D)"), blank=True, null=True) point = models.PointField(_(u"Point (3D)"), blank=True, null=True, dim=3) polygon = models.PolygonField(_(u"Polygon"), blank=True, null=True) - cached_label = models.TextField(_(u"Cached name"), null=True, blank=True) + cached_label = models.TextField(_(u"Cached name"), null=True, blank=True, + db_index=True) + PARENT_SEARCH_VECTORS = ['operation'] + BASE_SEARCH_VECTORS = ["cached_label", "label", "location", + "interpretation", "filling", "datings_comment", + "identification__label", "activity__label", + "excavation_technic__label"] + M2M_SEARCH_VECTORS = ["datings__period__label"] history = HistoricalRecords() class Meta: diff --git a/archaeological_context_records/tests.py b/archaeological_context_records/tests.py index 89b15fbbf..b0f4b8f9e 100644 --- a/archaeological_context_records/tests.py +++ b/archaeological_context_records/tests.py @@ -273,6 +273,20 @@ class ContextRecordTest(ContextRecordInit, TestCase): cr.operation ) + def test_search_vector_update(self): + cr = self.create_context_record(force=True)[0] + cr = models.ContextRecord.objects.get(pk=cr.pk) + cr.label = "Label label" + cr.location = "I am heeere" + cr.save() + for key in ('label', 'heeer'): + self.assertIn(key, cr.search_vector) + cr.operation.code_patriarche = "PATRIARCHE" + cr.operation.save() + cr = models.ContextRecord.objects.get(pk=cr.pk) + self.assertIn(settings.ISHTAR_OPE_PREFIX.lower() + "patriarch", + cr.search_vector) + def test_upstream_cache_update(self): cr = self.create_context_record()[0] cr_pk = cr.pk @@ -399,6 +413,44 @@ class ContextRecordSearchTest(ContextRecordInit, TestCase): models.RecordRelations.objects.create( left_record=cr_1, right_record=cr_2, relation_type=sym_rel_type) + def test_town_search(self): + c = Client() + c.login(username=self.username, password=self.password) + + data = {'numero_insee': '98989', 'name': 'base_town'} + base_town = self.create_towns(datas=data)[-1] + + parcel = self.create_parcel(data={'town': base_town, + 'section': 'A', 'parcel_number': '1'})[-1] + self.context_records[0].parcel = parcel + self.context_records[0].save() + + data = {'numero_insee': '56789', 'name': 'parent_town'} + parent_town = self.create_towns(datas=data)[-1] + parent_town.children.add(base_town) + + data = {'numero_insee': '01234', 'name': 'child_town'} + child_town = self.create_towns(datas=data)[-1] + base_town.children.add(child_town) + + # simple search + search = {'parcel__town': base_town.pk} + response = c.get(reverse('get-contextrecord'), search) + self.assertEqual(response.status_code, 200) + self.assertEqual(json.loads(response.content)['total'], 1) + + # parent search + search = {'parcel__town': parent_town.pk} + response = c.get(reverse('get-contextrecord'), search) + self.assertEqual(response.status_code, 200) + self.assertEqual(json.loads(response.content)['total'], 1) + + # child search + search = {'parcel__town': child_town.pk} + response = c.get(reverse('get-contextrecord'), search) + self.assertEqual(response.status_code, 200) + self.assertEqual(json.loads(response.content)['total'], 1) + def testSearchExport(self): c = Client() response = c.get(reverse('get-contextrecord')) @@ -416,6 +468,14 @@ class ContextRecordSearchTest(ContextRecordInit, TestCase): {'label': 'cr 1', 'cr_relation_types_0': self.cr_rel_type.pk}) self.assertEqual(json.loads(response.content)['total'], 2) + # test search vector + response = c.get(reverse('get-contextrecord'), + {'search_vector': 'CR'}) + self.assertEqual(json.loads(response.content)['total'], 2) + # the 2 context records have the same operation + response = c.get(reverse('get-contextrecord'), + {'search_vector': 'op2010'}) + self.assertEqual(json.loads(response.content)['total'], 2) # test search between related operations first_ope = self.operations[0] first_ope.year = 2010 |
