Skip to content

Commit

Permalink
PB-1158: Add package distribution model
Browse files Browse the repository at this point in the history
  • Loading branch information
msom committed Dec 10, 2024
1 parent b87599c commit a2b08ed
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 0 deletions.
9 changes: 9 additions & 0 deletions app/distributions/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from .models import Attribution
from .models import Dataset
from .models import PackageDistribution


@admin.register(Attribution)
Expand All @@ -18,3 +19,11 @@ class DatasetAdmin(admin.ModelAdmin): # type:ignore[type-arg]

list_display = ('slug', 'provider')
list_filter = (('provider', admin.RelatedOnlyFieldListFilter),)


@admin.register(PackageDistribution)
class PackageDistributionAdmin(admin.ModelAdmin): # type:ignore[type-arg]
'''Admin View for Package Distribution'''

list_display = ('slug', 'managed_by_stac', 'dataset')
list_filter = ('managed_by_stac',)
39 changes: 39 additions & 0 deletions app/distributions/migrations/0007_packagedistribution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Generated by Django 5.0.9 on 2024-11-27 08:27

import utils.fields

import django.db.models.deletion
from django.db import migrations
from django.db import models


class Migration(migrations.Migration):

dependencies = [
('distributions', '0006_alter_dataset_slug'),
]

operations = [
migrations.CreateModel(
name='PackageDistribution',
fields=[
(
'id',
models.BigAutoField(
auto_created=True, primary_key=True, serialize=False, verbose_name='ID'
)
),
('slug', utils.fields.CustomSlugField(max_length=100, verbose_name='Slug')),
(
'managed_by_stac',
models.BooleanField(max_length=100, verbose_name='Managed by STAC')
),
(
'dataset',
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to='distributions.dataset'
)
),
],
),
]
29 changes: 29 additions & 0 deletions app/distributions/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,32 @@ def save(
using=using,
update_fields=update_fields
)


class PackageDistribution(models.Model):

_context = "Package Distribution Model"

def __str__(self) -> str:
return str(self.slug)

slug = CustomSlugField(_(_context, "Slug"), max_length=100)
managed_by_stac = models.BooleanField(_(_context, "Managed by STAC"), max_length=100)

dataset = models.ForeignKey(Dataset, on_delete=models.CASCADE)

def save(
self,
force_insert: bool | tuple[ModelBase, ...] = False,
force_update: bool = False,
using: str | None = None,
update_fields: Iterable[str] | None = None
) -> None:

self.full_clean()
super().save(
force_insert=force_insert,
force_update=force_update,
using=using,
update_fields=update_fields
)
69 changes: 69 additions & 0 deletions app/tests/distributions/test_package_distribution_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from distributions.models import Attribution
from distributions.models import Dataset
from distributions.models import PackageDistribution
from provider.models import Provider
from pytest import fixture
from pytest import raises

from django.core.exceptions import ValidationError


@fixture(name='provider')
def fixture_provider(db):
yield Provider.objects.create(
acronym_de="BAFU",
acronym_fr="OFEV",
acronym_en="FOEN",
name_de="Bundesamt für Umwelt",
name_fr="Office fédéral de l'environnement",
name_en="Federal Office for the Environment"
)


@fixture(name='attribution')
def fixture_attribution(provider):
yield Attribution.objects.create(
name_de="BAFU",
name_fr="OFEV",
name_en="FOEN",
name_it="UFAM",
name_rm="UFAM",
description_de="Bundesamt für Umwelt",
description_fr="Office fédéral de l'environnement",
description_en="Federal Office for the Environment",
description_it="Ufficio federale dell'ambiente",
description_rm="Uffizi federal per l'ambient",
provider=provider
)


@fixture(name='dataset')
def fixture_dataset(provider, attribution):
yield Dataset.objects.create(
slug="ch.agroscope.feuchtflaechenpotential-kulturlandschaft",
provider=provider,
attribution=attribution,
)


def test_create_package_distribution_in_database(dataset):
PackageDistribution.objects.create(
slug="ch.agroscope.feuchtflaechenpotential-kulturlandschaft",
managed_by_stac=True,
dataset=dataset
)

distributions = PackageDistribution.objects.all()

assert len(distributions) == 1

distribution = distributions[0]

assert distribution.slug == "ch.agroscope.feuchtflaechenpotential-kulturlandschaft"
assert distribution.managed_by_stac is True
assert distribution.dataset == dataset


def test_raises_exception_when_creating_db_object_with_mandatory_field_null(dataset):
with raises(ValidationError) as e:
PackageDistribution.objects.create(dataset=dataset)

0 comments on commit a2b08ed

Please sign in to comment.