Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Structures): historique de modification grâce à django-simple-history #1255

Merged
merged 6 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
"huey.contrib.djhuey", # huey (Async tasks)
"rest_framework", # djangorestframework
"phonenumber_field", # django-phonenumber-field
"simple_history", # django-simple-history
]

LOCAL_APPS = [
Expand Down Expand Up @@ -185,7 +186,8 @@
"django.middleware.clickjacking.XFrameOptionsMiddleware",
# Third-party Middlewares
"whitenoise.middleware.WhiteNoiseMiddleware",
"django_htmx.middleware.HtmxMiddleware",
"django_htmx.middleware.HtmxMiddleware", # django-htmx
"simple_history.middleware.HistoryRequestMiddleware", # django-simple-history
# wagtail
"wagtail.contrib.redirects.middleware.RedirectMiddleware",
# Custom Middlewares
Expand Down Expand Up @@ -748,6 +750,7 @@
# django-ckeditor
# https://django-ckeditor.readthedocs.io/en/latest/#optional-customizing-ckeditor-editor
# ------------------------------------------------------------------------------

DEFAULT_CKEDITOR_CONFIG = {
"toolbar": "Custom",
"toolbar_Custom": [
Expand Down Expand Up @@ -785,6 +788,13 @@
}


# Django Simple History
# https://django-simple-history.readthedocs.io/
# ------------------------------------------------------------------------------

SIMPLE_HISTORY_HISTORY_ID_USE_UUID = True


# Internal & external
# (if you need these settings in the template, add them to settings_context_processor.expose_settings)
# ------------------------------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion lemarche/siaes/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from django.urls import reverse
from django.utils.html import format_html, mark_safe
from fieldsets_with_inlines import FieldsetsInlineMixin
from simple_history.admin import SimpleHistoryAdmin

from lemarche.conversations.models import Conversation
from lemarche.labels.models import Label
Expand Down Expand Up @@ -129,7 +130,7 @@ def nb_message_with_link(self, conversation: Conversation):


@admin.register(Siae, site=admin_site)
class SiaeAdmin(FieldsetsInlineMixin, gis_admin.OSMGeoAdmin):
class SiaeAdmin(FieldsetsInlineMixin, gis_admin.OSMGeoAdmin, SimpleHistoryAdmin):
actions = [export_as_xls]
list_display = [
"id",
Expand Down
1 change: 1 addition & 0 deletions lemarche/siaes/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def siaes(self, create, extracted, **kwargs):
class SiaeFactory(DjangoModelFactory):
class Meta:
model = Siae
skip_postgeneration_save = True

name = factory.Faker("company", locale="fr_FR")
# slug auto-generated
Expand Down
670 changes: 670 additions & 0 deletions lemarche/siaes/migrations/0075_historicalsiae.py

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions lemarche/siaes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from django.utils.functional import cached_property
from django.utils.text import slugify
from phonenumber_field.modelfields import PhoneNumberField
from simple_history.models import HistoricalRecords

from lemarche.perimeters.models import Perimeter
from lemarche.siaes import constants as siae_constants
Expand Down Expand Up @@ -877,6 +878,8 @@ class Siae(models.Model):
extra_data = models.JSONField(verbose_name="Données complémentaires", editable=False, default=dict)
import_raw_object = models.JSONField(verbose_name="Donnée JSON brute", editable=False, null=True)

history = HistoricalRecords()

created_at = models.DateTimeField(verbose_name="Date de création", default=timezone.now)
updated_at = models.DateTimeField(verbose_name="Date de mise à jour", auto_now=True)

Expand Down
25 changes: 25 additions & 0 deletions lemarche/siaes/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
)
from lemarche.siaes.models import Siae, SiaeGroup, SiaeLabel, SiaeUser
from lemarche.users.factories import UserFactory
from lemarche.utils.history import HISTORY_TYPE_CREATE, HISTORY_TYPE_UPDATE


class SiaeGroupModelTest(TestCase):
Expand Down Expand Up @@ -567,6 +568,30 @@ def test_geo_range_in_perimeter_list(self):
)


class SiaeHistoryTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.siae_1 = SiaeFactory(name="ZZZ", brand="ABC")
cls.siae_2 = SiaeFactory(name="Test", brand="")

def test_history_object_on_create(self):
self.assertEqual(self.siae_1.history.count(), 1)
siae_1_create_history_item = self.siae_1.history.last()
self.assertEqual(siae_1_create_history_item.history_type, HISTORY_TYPE_CREATE)
self.assertEqual(siae_1_create_history_item.name, self.siae_1.name)

def test_history_object_on_update(self):
self.siae_2.brand = "test"
self.siae_2.save()
self.assertEqual(self.siae_2.history.count(), 1 + 1)
siae_2_create_history_item = self.siae_2.history.last()
self.assertEqual(siae_2_create_history_item.history_type, HISTORY_TYPE_CREATE)
self.assertEqual(siae_2_create_history_item.brand, "")
siae_2_update_history_item = self.siae_2.history.first()
self.assertEqual(siae_2_update_history_item.history_type, HISTORY_TYPE_UPDATE)
self.assertEqual(siae_2_update_history_item.brand, self.siae_2.brand)


class SiaeLabelModelTest(TestCase):
@classmethod
def setUpTestData(cls):
Expand Down
6 changes: 6 additions & 0 deletions lemarche/utils/history.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# https://django-simple-history.readthedocs.io/en/latest/index.html

# https://django-simple-history.readthedocs.io/en/latest/quick_start.html#what-is-django-simple-history-doing-behind-the-scenes
HISTORY_TYPE_CREATE = "+"
HISTORY_TYPE_UPDATE = "~"
HISTORY_TYPE_DELETE = "-"
16 changes: 15 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ wagtail = "^5.1.3"
whitenoise = "^6.6.0"
xlwt = "^1.3.0"
django-phonenumber-field = {extras = ["phonenumbers"], version = "^7.3.0"}
django-simple-history = "^3.7.0"

[tool.poetry.group.dev.dependencies]
black = "^23.12.1"
Expand Down
Loading