Skip to content

Commit

Permalink
Merge pull request #334 from betagouv/mixin-cleaning
Browse files Browse the repository at this point in the history
Nettoyage (partiel) des mixins / behaviours
  • Loading branch information
ddahan authored Mar 28, 2024
2 parents 64e4afb + cf6eeb2 commit 9e215d7
Show file tree
Hide file tree
Showing 21 changed files with 102 additions and 82 deletions.
10 changes: 5 additions & 5 deletions data/admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,26 @@
from .condition import Condition # noqa

from data.models import Ingredient, PlantPart, PlantFamily, Microorganism
from data.admin.abstract_admin import IngredientAdminWithHistoryChangedFields
from data.admin.abstract_admin import IngredientAdminHistorisableChangedFields


@admin.register(Ingredient)
class IngredientAdmin(IngredientAdminWithHistoryChangedFields):
class IngredientAdmin(IngredientAdminHistorisableChangedFields):
pass


@admin.register(PlantPart)
class PlantPartAdmin(IngredientAdminWithHistoryChangedFields):
class PlantPartAdmin(IngredientAdminHistorisableChangedFields):
pass


@admin.register(PlantFamily)
class PlantFamilyAdmin(IngredientAdminWithHistoryChangedFields):
class PlantFamilyAdmin(IngredientAdminHistorisableChangedFields):
pass


@admin.register(Microorganism)
class MicroorganismAdmin(IngredientAdminWithHistoryChangedFields):
class MicroorganismAdmin(IngredientAdminHistorisableChangedFields):
pass


Expand Down
6 changes: 3 additions & 3 deletions data/admin/abstract_admin.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from simple_history.admin import SimpleHistoryAdmin


class IngredientAdminWithHistoryChangedFields(SimpleHistoryAdmin):
history_list_display = ['changed_fields']
class IngredientAdminHistorisableChangedFields(SimpleHistoryAdmin):
history_list_display = ["changed_fields"]

def changed_fields(self, obj):
if obj.prev_record:
delta = obj.diff_against(obj.prev_record)
return delta.changed_fields
return None
return None
4 changes: 2 additions & 2 deletions data/admin/plant.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.db import models

from data.models import Plant, PlantSynonym
from data.admin.abstract_admin import IngredientAdminWithHistoryChangedFields
from data.admin.abstract_admin import IngredientAdminHistorisableChangedFields


class PlantSynonymInline(admin.TabularInline):
Expand Down Expand Up @@ -36,7 +36,7 @@ class Meta:


@admin.register(Plant)
class PlantAdmin(IngredientAdminWithHistoryChangedFields):
class PlantAdmin(IngredientAdminHistorisableChangedFields):
form = PlantForm
fieldsets = [
(
Expand Down
4 changes: 2 additions & 2 deletions data/admin/substance.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.urls import reverse

from data.models import Substance
from data.admin.abstract_admin import IngredientAdminWithHistoryChangedFields
from data.admin.abstract_admin import IngredientAdminHistorisableChangedFields


class SubstanceForm(forms.ModelForm):
Expand All @@ -19,7 +19,7 @@ class Meta:


@admin.register(Substance)
class SubstanceAdmin(IngredientAdminWithHistoryChangedFields):
class SubstanceAdmin(IngredientAdminHistorisableChangedFields):
@classmethod
def links_to_objects(cls, object_name, objects):
rel_list = "<ul>"
Expand Down
2 changes: 2 additions & 0 deletions data/mixins/__init__.py → data/behaviours/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from .auto_validable import AutoValidable # noqa: F401
from .time_stampable import TimeStampable # noqa: F401
from .historisable import Historisable # noqa: F401
from .deactivable import Deactivable, DeactivableQuerySet # noqa: F401
from .expirable import Expirable, ExpirableQuerySet # noqa: F401
File renamed without changes.
File renamed without changes.
File renamed without changes.
9 changes: 9 additions & 0 deletions data/behaviours/historisable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.db import models
from simple_history.models import HistoricalRecords


class Historisable(models.Model):
class Meta:
abstract = True

history = HistoricalRecords(inherit=True)
22 changes: 22 additions & 0 deletions data/behaviours/time_stampable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from django.db import models


class TimeStampableQueryset(models.query.QuerySet):
class Meta:
abstract = True

def created_between(self, i_start, i_end):
"""
Returns all objects created between the provided interval
"""

assert i_start <= i_end
return self.filter(creation_date__lte=i_end, creation_date__gte=i_start)


class TimeStampable(models.Model):
class Meta:
abstract = True

creation_date = models.DateTimeField(auto_now_add=True)
modification_date = models.DateTimeField(auto_now=True)
35 changes: 0 additions & 35 deletions data/mixins/time_stampable.py

This file was deleted.

5 changes: 3 additions & 2 deletions data/models/abstract_models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from .mixins import WithCreationAndModificationDate, WithMissingImportBoolean, WithDefaultFields
from data.behaviours import TimeStampable
from .mixins import WithMissingImportBoolean, WithDefaultFields


class CommonModel(WithCreationAndModificationDate, WithMissingImportBoolean, WithDefaultFields):
class CommonModel(TimeStampable, WithMissingImportBoolean, WithDefaultFields):
"""
Les modèles ingrédients et les synonymes héritent de ce modèle
"""
Expand Down
7 changes: 4 additions & 3 deletions data/models/ingredient.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from django.db import models
from simple_history.models import HistoricalRecords

from .mixins import WithCreationAndModificationDate, WithHistory, WithMissingImportBoolean, WithComments
from data.behaviours import TimeStampable, Historisable
from .mixins import WithMissingImportBoolean, WithComments
from .abstract_models import CommonModel
from .substance import Substance

Expand All @@ -25,7 +26,7 @@ def description(self):
return self.siccrf_description


class IngredientSubstanceRelation(WithCreationAndModificationDate, WithHistory):
class IngredientSubstanceRelation(TimeStampable, Historisable):
ingredient = models.ForeignKey(Ingredient, on_delete=models.CASCADE)
substance = models.ForeignKey(Substance, on_delete=models.CASCADE)
siccrf_is_related = models.BooleanField(
Expand All @@ -34,7 +35,7 @@ class IngredientSubstanceRelation(WithCreationAndModificationDate, WithHistory):
ca_is_related = models.BooleanField(null=True, default=None, verbose_name="substance associée à l'ingrédient")


class IngredientSynonym(WithCreationAndModificationDate, WithHistory, WithMissingImportBoolean):
class IngredientSynonym(TimeStampable, Historisable, WithMissingImportBoolean):
class Meta:
verbose_name = "synonyme d'ingrédient"

Expand Down
7 changes: 4 additions & 3 deletions data/models/microorganism.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

from simple_history.models import HistoricalRecords

from .mixins import WithCreationAndModificationDate, WithHistory, WithMissingImportBoolean, WithComments
from data.behaviours import TimeStampable, Historisable
from .mixins import WithMissingImportBoolean, WithComments
from .abstract_models import CommonModel
from .substance import Substance

Expand All @@ -29,7 +30,7 @@ def name_en(self):
return self.siccrf_name_en


class MicroorganismSubstanceRelation(WithCreationAndModificationDate, WithHistory):
class MicroorganismSubstanceRelation(TimeStampable, Historisable):
microorganism = models.ForeignKey(Microorganism, on_delete=models.CASCADE)
substance = models.ForeignKey(Substance, on_delete=models.CASCADE)
siccrf_is_related = models.BooleanField(
Expand All @@ -38,7 +39,7 @@ class MicroorganismSubstanceRelation(WithCreationAndModificationDate, WithHistor
ca_is_related = models.BooleanField(null=True, default=None, verbose_name="substance associée au micro-organisme")


class MicroorganismSynonym(WithCreationAndModificationDate, WithHistory, WithMissingImportBoolean):
class MicroorganismSynonym(TimeStampable, Historisable, WithMissingImportBoolean):
class Meta:
verbose_name = "synonyme de micro-organisme"

Expand Down
16 changes: 0 additions & 16 deletions data/models/mixins.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,6 @@
from django.db import models
from django.db.models.functions import Coalesce, NullIf
from django.db.models import F, Value
from simple_history.models import HistoricalRecords


class WithCreationAndModificationDate(models.Model):
class Meta:
abstract = True

creation_date = models.DateTimeField(auto_now_add=True)
modification_date = models.DateTimeField(auto_now=True)


class WithHistory(models.Model):
class Meta:
abstract = True

history = HistoricalRecords(inherit=True)


class WithMissingImportBoolean(models.Model):
Expand Down
9 changes: 5 additions & 4 deletions data/models/plant.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

from simple_history.models import HistoricalRecords

from .mixins import WithCreationAndModificationDate, WithHistory, WithMissingImportBoolean, WithComments
from data.behaviours import TimeStampable, Historisable
from .mixins import WithMissingImportBoolean, WithComments
from .abstract_models import CommonModel
from .substance import Substance

Expand Down Expand Up @@ -63,7 +64,7 @@ class Meta:
history = HistoricalRecords(inherit=True, excluded_fields=["name", "is_obsolete", "family"])


class Part(WithCreationAndModificationDate):
class Part(TimeStampable):
"""
Ce modèle permet d'associer des données supplémentaires à la relation ManyToMany
plant_parts
Expand Down Expand Up @@ -92,7 +93,7 @@ class Part(WithCreationAndModificationDate):
)


class PlantSubstanceRelation(WithCreationAndModificationDate, WithHistory):
class PlantSubstanceRelation(TimeStampable, Historisable):
plant = models.ForeignKey(Plant, on_delete=models.CASCADE)
substance = models.ForeignKey(Substance, on_delete=models.CASCADE)
siccrf_is_related = models.BooleanField(
Expand All @@ -101,7 +102,7 @@ class PlantSubstanceRelation(WithCreationAndModificationDate, WithHistory):
ca_is_related = models.BooleanField(null=True, default=None, verbose_name="substance associée à la plante")


class PlantSynonym(WithCreationAndModificationDate, WithHistory, WithMissingImportBoolean):
class PlantSynonym(TimeStampable, Historisable, WithMissingImportBoolean):
class Meta:
verbose_name = "synonyme de plante"

Expand Down
2 changes: 1 addition & 1 deletion data/models/roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from django.conf import settings
from django.contrib.auth import get_user_model
from data.mixins import Deactivable, DeactivableQuerySet
from data.behaviours import Deactivable, DeactivableQuerySet
from .company import Company


Expand Down
5 changes: 3 additions & 2 deletions data/models/substance.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from django.db.models import F, Value
from simple_history.models import HistoricalRecords

from .mixins import WithCreationAndModificationDate, WithHistory, WithMissingImportBoolean, WithComments
from data.behaviours import TimeStampable, Historisable
from .mixins import WithMissingImportBoolean, WithComments
from .abstract_models import CommonModel
from .unit import SubstanceUnit

Expand Down Expand Up @@ -114,7 +115,7 @@ def name_en(self):
return self.siccrf_name_en


class SubstanceSynonym(WithCreationAndModificationDate, WithHistory, WithMissingImportBoolean):
class SubstanceSynonym(TimeStampable, Historisable, WithMissingImportBoolean):
class Meta:
verbose_name = "synonyme substance active"

Expand Down
2 changes: 1 addition & 1 deletion data/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
BaseUserManager,
PermissionsMixin,
)
from data.mixins import AutoValidable, Deactivable, DeactivableQuerySet
from data.behaviours import AutoValidable, Deactivable, DeactivableQuerySet
from django.db.models import OneToOneRel
from django.utils import timezone
from django.core.exceptions import ObjectDoesNotExist
Expand Down
35 changes: 35 additions & 0 deletions tokens/migrations/0003_remove_magiclinktoken_created_and_more.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Generated by Django 5.0.3 on 2024-03-26 13:39

import django.utils.timezone
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("tokens", "0002_alter_magiclinktoken_usage"),
]

operations = [
migrations.RemoveField(
model_name="magiclinktoken",
name="created",
),
migrations.RemoveField(
model_name="magiclinktoken",
name="modified",
),
migrations.AddField(
model_name="magiclinktoken",
name="creation_date",
field=models.DateTimeField(
auto_now_add=True, default=django.utils.timezone.now
),
preserve_default=False,
),
migrations.AddField(
model_name="magiclinktoken",
name="modification_date",
field=models.DateTimeField(auto_now=True),
),
]
4 changes: 1 addition & 3 deletions tokens/models/magic_link_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
from django.db.models.deletion import CASCADE
from django.db.models.manager import Manager

from data.mixins import AutoValidable, TimeStampable

from ..mixins.expirable import Expirable, ExpirableQuerySet
from data.behaviours import AutoValidable, TimeStampable, Expirable, ExpirableQuerySet
from ..mixins.unique_secret_key import UniqueSecretKeyMixin


Expand Down

0 comments on commit 9e215d7

Please sign in to comment.