Skip to content

Commit

Permalink
add ! set deduplicationset state to Dirty in case changing related co…
Browse files Browse the repository at this point in the history
…nfig
  • Loading branch information
vitali-yanushchyk-valor committed Dec 4, 2024
1 parent 322f2b5 commit ee097d3
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/hope_dedup_engine/apps/api/admin/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.contrib import messages
from django.contrib.admin import ModelAdmin, register, site
from django.core.exceptions import ValidationError
from django.db import models
from django.db import models, transaction
from django.http import HttpRequest, HttpResponse
from django.shortcuts import redirect, render
from django.urls import path, reverse
Expand All @@ -14,7 +14,8 @@
from django_svelte_jsoneditor.widgets import SvelteJSONEditorWidget

from hope_dedup_engine.apps.api.forms import EditSchemaForm
from hope_dedup_engine.apps.api.models import Config
from hope_dedup_engine.apps.api.models import Config, DeduplicationSet
from hope_dedup_engine.apps.api.utils.notification import send_notification
from hope_dedup_engine.apps.api.utils.shema_manager import SchemaManager
from hope_dedup_engine.apps.api.validators import DefaultValidatingValidator
from hope_dedup_engine.utils.security import is_root
Expand Down Expand Up @@ -64,7 +65,8 @@ def response_change(self, request: HttpRequest, obj: Any) -> HttpResponse:
confirm_url = reverse("admin:confirm_save_config", args=[obj.pk])
self.message_user(
request,
f"Related deduplication sets {dd_set} was found. Please confirm saving.",
f"Related deduplication sets {dd_set} was found. "
f"Confirming your save will mark them as '{DeduplicationSet.State.DIRTY.label}'.",
level=messages.WARNING,
)
return redirect(confirm_url)
Expand All @@ -75,11 +77,18 @@ def confirm_save(self, request, object_id) -> HttpResponse: # pragma: no cover
if request.method == "POST":
form_data = request.session.get("unsaved_data", None)
if form_data:
for field, value in form_data.items():
if field == "settings":
value = json.loads(value)
setattr(obj, field, value)
obj.save()

with transaction.atomic():
deduplication_sets = obj.deduplicationset_set.select_for_update()
deduplication_sets.update(state=DeduplicationSet.State.DIRTY)
for field, value in form_data.items():
if field == "settings":
value = json.loads(value)
setattr(obj, field, value)
obj.save()

for ds in deduplication_sets:
send_notification(ds.notification_url)

return redirect(reverse("admin:api_config_changelist"))

Expand Down

0 comments on commit ee097d3

Please sign in to comment.