Skip to content

Commit

Permalink
Ajout de modèles pour la suite (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
tut-tuuut authored Nov 7, 2024
2 parents 746c164 + 203f83c commit db58545
Show file tree
Hide file tree
Showing 22 changed files with 615 additions and 1 deletion.
1 change: 1 addition & 0 deletions gsl/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
# gsl apps:
"gsl_core",
"gsl_demarches_simplifiees",
"gsl_projet",
"gsl_pages",
"gsl_oidc",
]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
# Generated by Django 5.1.1 on 2024-11-06 14:22

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


class Migration(migrations.Migration):
dependencies = [
("gsl_core", "0002_collegue_proconnect_chorusdt_and_more"),
]

operations = [
migrations.CreateModel(
name="Commune",
fields=[
("created_at", models.DateTimeField(auto_now_add=True)),
("updated_at", models.DateTimeField(auto_now=True)),
(
"insee_code",
models.CharField(
primary_key=True,
serialize=False,
unique=True,
verbose_name="Code INSEE",
),
),
("name", models.CharField(verbose_name="Nom")),
],
options={
"abstract": False,
},
),
migrations.CreateModel(
name="Departement",
fields=[
("created_at", models.DateTimeField(auto_now_add=True)),
("updated_at", models.DateTimeField(auto_now=True)),
(
"insee_code",
models.CharField(
primary_key=True,
serialize=False,
unique=True,
verbose_name="Code INSEE",
),
),
("name", models.CharField(verbose_name="Nom")),
],
options={
"verbose_name": "Département",
},
),
migrations.CreateModel(
name="Region",
fields=[
("created_at", models.DateTimeField(auto_now_add=True)),
("updated_at", models.DateTimeField(auto_now=True)),
(
"insee_code",
models.CharField(
primary_key=True,
serialize=False,
unique=True,
verbose_name="Code INSEE",
),
),
("name", models.CharField(verbose_name="Nom")),
],
options={
"verbose_name": "Région",
},
),
migrations.CreateModel(
name="Adresse",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("created_at", models.DateTimeField(auto_now_add=True)),
("updated_at", models.DateTimeField(auto_now=True)),
("label", models.TextField(verbose_name="Adresse complète")),
("postal_code", models.CharField(verbose_name="Code postal")),
("street_address", models.CharField(verbose_name="Adresse")),
(
"commune",
models.ForeignKey(
on_delete=django.db.models.deletion.PROTECT,
to="gsl_core.commune",
),
),
],
options={
"abstract": False,
},
),
migrations.AddField(
model_name="commune",
name="departement",
field=models.ForeignKey(
on_delete=django.db.models.deletion.PROTECT, to="gsl_core.departement"
),
),
migrations.CreateModel(
name="Arrondissement",
fields=[
("created_at", models.DateTimeField(auto_now_add=True)),
("updated_at", models.DateTimeField(auto_now=True)),
(
"insee_code",
models.CharField(
primary_key=True,
serialize=False,
unique=True,
verbose_name="Code INSEE",
),
),
("name", models.CharField(verbose_name="Nom")),
(
"departement",
models.ForeignKey(
on_delete=django.db.models.deletion.PROTECT,
to="gsl_core.departement",
),
),
],
options={
"abstract": False,
},
),
migrations.AddField(
model_name="departement",
name="region",
field=models.ForeignKey(
on_delete=django.db.models.deletion.PROTECT, to="gsl_core.region"
),
),
]
59 changes: 59 additions & 0 deletions gsl_core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@
from django.db import models


class BaseModel(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

class Meta:
abstract = True

def __str__(self):
return f"{self._meta.verbose_name} {self.pk}"


class Collegue(AbstractUser):
proconnect_sub = models.UUIDField(
"Identifiant unique proconnect", null=True, blank=True
Expand All @@ -18,3 +29,51 @@ class Collegue(AbstractUser):
default="",
blank=True,
)


class Region(BaseModel):
insee_code = models.CharField("Code INSEE", unique=True, primary_key=True)
name = models.CharField("Nom")

class Meta:
verbose_name = "Région"

def __str__(self):
return f"Région {self.name}"


class Departement(BaseModel):
insee_code = models.CharField("Code INSEE", unique=True, primary_key=True)
name = models.CharField("Nom")
region = models.ForeignKey(Region, on_delete=models.PROTECT)

class Meta:
verbose_name = "Département"

def __str__(self):
return f"Département {self.name}"


class Commune(BaseModel):
insee_code = models.CharField("Code INSEE", unique=True, primary_key=True)
name = models.CharField("Nom")
departement = models.ForeignKey(Departement, on_delete=models.PROTECT)

def __str__(self):
return f"Commune {self.insee_code} {self.name}"


class Arrondissement(BaseModel):
insee_code = models.CharField("Code INSEE", unique=True, primary_key=True)
name = models.CharField("Nom")
departement = models.ForeignKey(Departement, on_delete=models.PROTECT)

def __str__(self):
return f"Arrondissement {self.name}"


class Adresse(BaseModel):
label = models.TextField("Adresse complète")
postal_code = models.CharField("Code postal")
commune = models.ForeignKey(Commune, on_delete=models.PROTECT)
street_address = models.CharField("Adresse")
58 changes: 58 additions & 0 deletions gsl_core/tests/factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import factory

from ..models import Adresse, Arrondissement, Collegue, Commune, Departement, Region


class CollegueFactory(factory.django.DjangoModelFactory):
class Meta:
model = Collegue

username = factory.Faker("user_name")
email = factory.Faker("email")
is_staff = False
is_active = True


class RegionFactory(factory.django.DjangoModelFactory):
class Meta:
model = Region

insee_code = factory.Sequence(lambda n: f"{n}")
name = factory.Faker("word", locale="fr_FR")


class DepartementFactory(factory.django.DjangoModelFactory):
class Meta:
model = Departement

insee_code = factory.Sequence(lambda n: f"{n}")
name = factory.Faker("word", locale="fr_FR")
region = factory.SubFactory(RegionFactory)


class ArrondissementFactory(factory.django.DjangoModelFactory):
class Meta:
model = Arrondissement

insee_code = factory.Sequence(lambda n: f"{n}")
name = factory.Faker("city", locale="fr_FR")
departement = factory.SubFactory(DepartementFactory)


class CommuneFactory(factory.django.DjangoModelFactory):
class Meta:
model = Commune

insee_code = factory.Sequence(lambda n: f"{n}")
name = factory.Faker("city", locale="fr_FR")
departement = factory.SubFactory(DepartementFactory)


class AdresseFactory(factory.django.DjangoModelFactory):
class Meta:
model = Adresse

label = factory.Faker("address", locale="fr_FR")
postal_code = factory.Faker("postcode", locale="fr_FR")
commune = factory.SubFactory(CommuneFactory)
street_address = factory.Faker("street_address", locale="fr_FR")
29 changes: 29 additions & 0 deletions gsl_core/tests/test_factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import pytest

from ..models import Adresse, Arrondissement, Collegue, Commune, Departement, Region
from .factories import (
AdresseFactory,
ArrondissementFactory,
CollegueFactory,
CommuneFactory,
DepartementFactory,
RegionFactory,
)

pytestmark = pytest.mark.django_db

test_data = (
(CollegueFactory, Collegue),
(RegionFactory, Region),
(DepartementFactory, Departement),
(ArrondissementFactory, Arrondissement),
(CommuneFactory, Commune),
(AdresseFactory, Adresse),
)


@pytest.mark.parametrize("factory,expected_class", test_data)
def test_every_factory_can_be_called_twice(factory, expected_class):
for _ in range(2):
obj = factory()
assert isinstance(obj, expected_class)
30 changes: 30 additions & 0 deletions gsl_demarches_simplifiees/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,36 @@ class DemarcheAdmin(admin.ModelAdmin):
class DossierAdmin(admin.ModelAdmin):
list_filter = ("ds_demarche__ds_number",)
list_display = ("ds_number", "ds_demarche__ds_number", "ds_state")
fieldsets = (
(
"Informations générales",
{"fields": ("ds_demarche", "ds_id", "ds_number", "ds_state")},
),
(
"Champs DS",
{
"classes": ("collapse", "open"),
"fields": tuple(field.name for field in Dossier.MAPPED_FIELDS),
},
),
(
"Dates",
{
"classes": ("collapse", "open"),
"fields": (
"ds_date_depot",
"ds_date_passage_en_construction",
"ds_date_passage_en_instruction",
"ds_date_derniere_modification",
"ds_date_derniere_modification_champs",
),
},
),
(
"Données brutes",
{"classes": ("collapse", "open"), "fields": ("raw_ds_data",)},
),
)


@admin.register(FieldMappingForHuman)
Expand Down
Empty file.
23 changes: 23 additions & 0 deletions gsl_demarches_simplifiees/tests/factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import factory

from ..models import Demarche, Dossier


class DemarcheFactory(factory.django.DjangoModelFactory):
class Meta:
model = Demarche

ds_id = factory.Sequence(lambda n: f"demarche-{n}")
ds_number = factory.Faker("random_int", min=1000000, max=9999999)
ds_title = "Titre de la démarche"
ds_state = Demarche.STATE_PUBLIEE


class DossierFactory(factory.django.DjangoModelFactory):
class Meta:
model = Dossier

ds_demarche = factory.SubFactory(DemarcheFactory)
ds_id = factory.Sequence(lambda n: f"dossier-{n}")
ds_number = factory.Faker("random_int", min=1000000, max=9999999)
ds_state = Dossier.STATE_ACCEPTE
22 changes: 22 additions & 0 deletions gsl_demarches_simplifiees/tests/test_factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import pytest

from gsl_demarches_simplifiees.models import (
Demarche,
Dossier,
)

from .factories import DemarcheFactory, DossierFactory

pytestmark = pytest.mark.django_db

test_data = (
(DemarcheFactory, Demarche),
(DossierFactory, Dossier),
)


@pytest.mark.parametrize("factory,expected_class", test_data)
def test_every_factory_can_be_called_twice(factory, expected_class):
for _ in range(2):
obj = factory()
assert isinstance(obj, expected_class)
Empty file added gsl_projet/__init__.py
Empty file.
Loading

0 comments on commit db58545

Please sign in to comment.