Skip to content

Commit

Permalink
Merge pull request #85 from Terralego/fix_routing_setting
Browse files Browse the repository at this point in the history
Fix constraint clean different crud view routing setting
  • Loading branch information
LePetitTim authored Jan 22, 2021
2 parents c952a87 + 9fc12fc commit d1ff36e
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 29 additions & 0 deletions terra_geocrud/migrations/0061_auto_20210122_1546.py
Original file line number Diff line number Diff line change
@@ -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'),
),
]
10 changes: 6 additions & 4 deletions terra_geocrud/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
),
]
Expand All @@ -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")
)
27 changes: 27 additions & 0 deletions terra_geocrud/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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')

0 comments on commit d1ff36e

Please sign in to comment.