fix: on_delete_Content

This commit is contained in:
relikd
2024-06-27 23:03:29 +02:00
parent 70d605747d
commit 981d636d90
3 changed files with 13 additions and 10 deletions

View File

@@ -1,5 +1,7 @@
from django.conf import settings
from django.db import models
from django.db.models.signals import post_delete
from django.dispatch import receiver
import json
from tinymce.models import HTMLField
@@ -20,17 +22,13 @@ class Content(models.Model):
def save(self, *args, **kwargs):
rv = super().save(*args, **kwargs)
self.update_json()
Content.update_json()
return rv
def delete(self, *args, **kwargs):
rv = super().delete(*args, **kwargs)
self.update_json()
return rv
def update_json(self):
@staticmethod
def update_json():
with open(settings.MEDIA_ROOT / 'text.json', 'w') as fp:
json.dump(self.asJson(), fp)
json.dump(Content.asJson(), fp)
@staticmethod
def asJson() -> 'dict[str, str]':
@@ -38,3 +36,8 @@ class Content(models.Model):
for x in Content.objects.all():
rv[x.pk] = {'title': x.title, 'body': x.body, 'wide': x.wide}
return rv
@receiver(post_delete, sender=Content)
def on_delete_Content(sender, instance: 'Content', using, **kwargs):
Content.update_json()