From 0431944a5d9973f4cf7656a1c4b61794b3295bef Mon Sep 17 00:00:00 2001 From: LePetitTim Date: Fri, 22 Jan 2021 16:50:56 +0100 Subject: [PATCH 1/2] Fix constraint clean different crud view routing setting --- .../migrations/0061_auto_20210122_1546.py | 29 +++++++++++++++++++ terra_geocrud/models.py | 10 ++++--- terra_geocrud/tests/test_models.py | 27 +++++++++++++++++ 3 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 terra_geocrud/migrations/0061_auto_20210122_1546.py diff --git a/terra_geocrud/migrations/0061_auto_20210122_1546.py b/terra_geocrud/migrations/0061_auto_20210122_1546.py new file mode 100644 index 00000000..d2942dd4 --- /dev/null +++ b/terra_geocrud/migrations/0061_auto_20210122_1546.py @@ -0,0 +1,29 @@ +# Generated by Django 3.1.5 on 2021-01-22 15:46 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('terra_geocrud', '0060_auto_20210122_1132'), + ] + + operations = [ + migrations.RemoveConstraint( + model_name='routingsettings', + name='check_provider_layer', + ), + migrations.RemoveConstraint( + model_name='routingsettings', + name='check_provider_mapbox_transit', + ), + migrations.AddConstraint( + model_name='routingsettings', + constraint=models.UniqueConstraint(condition=models.Q(layer__isnull=False), fields=('provider', 'layer', 'crud_view'), name='check_provider_layer'), + ), + migrations.AddConstraint( + model_name='routingsettings', + constraint=models.UniqueConstraint(condition=models.Q(_negated=True, mapbox_transit=''), fields=('provider', 'mapbox_transit', 'crud_view'), name='check_provider_mapbox_transit'), + ), + ] diff --git a/terra_geocrud/models.py b/terra_geocrud/models.py index 6a6f82c9..c84232e1 100644 --- a/terra_geocrud/models.py +++ b/terra_geocrud/models.py @@ -371,10 +371,10 @@ class Meta: ('layer', 'crud_view'), ) constraints = [ - UniqueConstraint(fields=['provider', 'layer'], condition=Q(layer__isnull=False), + UniqueConstraint(fields=['provider', 'layer', 'crud_view'], condition=Q(layer__isnull=False), name='check_provider_layer' ), - UniqueConstraint(fields=['provider', 'mapbox_transit'], condition=~Q(mapbox_transit=''), + UniqueConstraint(fields=['provider', 'mapbox_transit', 'crud_view'], condition=~Q(mapbox_transit=''), name='check_provider_mapbox_transit' ), ] @@ -400,11 +400,13 @@ def clean(self): raise ValidationError( _("You should define a layer with this provider") ) - if RoutingSettings.objects.filter(Q(mapbox_transit=self.mapbox_transit) & ~Q(mapbox_transit='')).exclude(label=self.label): + if RoutingSettings.objects.filter(Q(mapbox_transit=self.mapbox_transit) & ~Q(mapbox_transit=''), + crud_view=self.crud_view).exclude(label=self.label): raise ValidationError( _("This transit is already used") ) - if RoutingSettings.objects.filter(Q(layer=self.layer) & Q(layer__isnull=False)).exclude(label=self.label): + if RoutingSettings.objects.filter(Q(layer=self.layer) & Q(layer__isnull=False), + crud_view=self.crud_view).exclude(label=self.label): raise ValidationError( _("This layer is already used") ) diff --git a/terra_geocrud/tests/test_models.py b/terra_geocrud/tests/test_models.py index a4281617..dc1b27c1 100644 --- a/terra_geocrud/tests/test_models.py +++ b/terra_geocrud/tests/test_models.py @@ -255,6 +255,7 @@ class RoutingSettingsTestCase(TestCase): def setUpTestData(cls): cls.layer = LayerFactory(routable=True) cls.crud_view = factories.CrudViewFactory() + cls.other_crud_view = factories.CrudViewFactory() def test_str(self): setting = RoutingSettingsFactory.create(provider="mapbox", mapbox_transit="driving", @@ -317,3 +318,29 @@ def test_same_layer(self): RoutingSettingsFactory.create(provider="geostore", layer=self.layer, crud_view=self.crud_view) with self.assertRaises(IntegrityError): RoutingSettingsFactory.create(provider="geostore", layer=self.layer, crud_view=self.crud_view) + + def test_same_transit_different_crud_view(self): + RoutingSettingsFactory.create(provider="mapbox", mapbox_transit='cycling', crud_view=self.crud_view) + setting = RoutingSettingsFactory.create(provider="mapbox", mapbox_transit='cycling', + crud_view=self.other_crud_view, label="Other") + self.assertEqual(str(setting), 'Other') + + def test_same_layer_different_crud_view(self): + RoutingSettingsFactory.create(provider="mapbox", mapbox_transit='cycling', crud_view=self.crud_view) + setting = RoutingSettingsFactory.create(provider="mapbox", mapbox_transit='cycling', + crud_view=self.other_crud_view, label="Other") + self.assertEqual(str(setting), 'Other') + + def test_same_transit_clean_different_crud_view(self): + RoutingSettingsFactory.create(provider="mapbox", mapbox_transit='cycling', crud_view=self.crud_view) + setting = RoutingSettingsFactory.build(provider="mapbox", mapbox_transit='cycling', + crud_view=self.other_crud_view, label="Other") + setting.clean() + self.assertEqual(str(setting), 'Other') + + def test_same_layer_clean_different_crud_view(self): + RoutingSettingsFactory.create(provider="geostore", layer=self.layer, crud_view=self.crud_view) + setting = RoutingSettingsFactory.build(provider="geostore", layer=self.layer, crud_view=self.other_crud_view, + label="Other") + setting.clean() + self.assertEqual(str(setting), 'Other') From 9fc12fc4b1053bf4f40bdba88730410a31e99d34 Mon Sep 17 00:00:00 2001 From: LePetitTim Date: Fri, 22 Jan 2021 17:05:00 +0100 Subject: [PATCH 2/2] Change changelog --- CHANGES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 60d8d3ab..c851a60a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -4,7 +4,7 @@ CHANGELOG 1.0.5.dev0 (XXXX-XX-XX) ---------------------------- -* +* Fix constraints different crudviews for routing settings 1.0.4 (2021-01-22)