Skip to content

Commit

Permalink
AB#108238 Add id_field to Datasettable
Browse files Browse the repository at this point in the history
Is needed for geosearch. In geosearch the `id_field` is now hardcoded on
`id`. But, for singular PK table, a different id for the PK should be
possible.
  • Loading branch information
jjmurre committed Feb 29, 2024
1 parent 485645f commit bf9fa87
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 2024-02-29 (5.26.0)

* Added extra `id_field` to Datasettable. Needs to be configurable for geosearch.

# 2024-01-07 (5.25.0)

* Added temporal indexes
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = amsterdam-schema-tools
version = 5.25.0
version = 5.26.0
url = https://github.com/amsterdam/schema-tools
license = Mozilla Public 2.0
author = Team Data Diensten, van het Dataplatform onder de Directie Digitale Voorzieningen (Gemeente Amsterdam)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.1.7 on 2024-02-29 11:34

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("datasets", "0017_dataset_enable_export"),
]

operations = [
migrations.AddField(
model_name="datasettable",
name="id_field",
field=models.CharField(default="id", max_length=50),
),
]
11 changes: 11 additions & 0 deletions src/schematools/contrib/django/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ class DatasetTable(models.Model):
geometry_field = models.CharField(max_length=50, null=True, blank=True)
geometry_field_type = models.CharField(max_length=50, null=True, blank=True)
is_temporal = models.BooleanField(null=False, blank=False, default=False)
id_field = models.CharField(max_length=50, blank=False, default="id")

class Meta:
ordering = ("name",)
Expand All @@ -389,6 +390,15 @@ def _get_geometry_field(cls, table_schema):
geo_type = match.group("schema") if match is not None else None
return field.db_name, geo_type

@classmethod
def _get_id_field(cls, table_schema):
"""Gets the id_field, this is the PK that is needed by Django and geosearch.
An `id` field will be added to the schema for tables with a compound key.
"""
identifier = table_schema.identifier
return identifier[0] if len(identifier) == 1 else "id"

@classmethod
def create_for_schema(cls, dataset: Dataset, table_schema: DatasetTableSchema) -> DatasetTable:
"""Create a DatasetTable object based on the Amsterdam Schema table spec.
Expand All @@ -411,6 +421,7 @@ def save_for_schema(self, table_schema: DatasetTableSchema):
self.enable_geosearch = (
table_schema.dataset.id not in settings.AMSTERDAM_SCHEMA["geosearch_disabled_datasets"]
)
self.id_field = self._get_id_field(table_schema)

is_creation = not self._state.adding
self.save()
Expand Down
15 changes: 15 additions & 0 deletions tests/django/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from schematools.contrib.django.fields import UnlimitedCharField
from schematools.contrib.django.models import (
Dataset,
DatasetTable,
LooseRelationField,
LooseRelationManyToManyField,
)
Expand Down Expand Up @@ -424,3 +425,17 @@ def test_model_factory_sub_object_is_json(kadastraleobjecten_dataset):
}
model_fields = {f.name: f for f in model_dict["kadastraleobjecten"]._meta.fields}
assert isinstance(model_fields["soort_grootte"], models.JSONField)


@pytest.mark.django_db
def test_dataset_with_singular_pk_has_correct_id_field(meetbouten_dataset):
"""Prove that Datasettable has correct id_field for table with single PK."""
meetbouten_dst = DatasetTable.objects.get(name="meetbouten")
assert meetbouten_dst.id_field == "identificatie"


@pytest.mark.django_db
def test_dataset_with_compound_pk_has_correct_id_field(gebieden_dataset):
"""Prove that Datasettable has correct id_field for table with compound PK."""
gebieden_dst = DatasetTable.objects.get(name="bouwblokken")
assert gebieden_dst.id_field == "id"

0 comments on commit bf9fa87

Please sign in to comment.