2023-12-08 14:53:17 +00:00
|
|
|
from django.apps import AppConfig
|
|
|
|
from django.db import connections
|
|
|
|
from django.db.models.signals import post_save
|
2023-12-12 18:04:30 +00:00
|
|
|
import warnings
|
2024-03-31 14:49:57 +01:00
|
|
|
from django.utils.html import strip_tags
|
2023-12-08 14:53:17 +00:00
|
|
|
|
2024-01-02 23:15:37 +00:00
|
|
|
|
2023-12-08 14:53:17 +00:00
|
|
|
class CoreConfig(AppConfig):
|
|
|
|
name = "yamdl_playground.core"
|
|
|
|
|
|
|
|
loaded = False
|
|
|
|
|
|
|
|
def ready(self):
|
2024-01-02 23:15:37 +00:00
|
|
|
from .models import Page
|
|
|
|
|
2023-12-08 14:53:17 +00:00
|
|
|
if not self.loaded:
|
2024-10-05 14:02:40 +01:00
|
|
|
connection = connections["yamdl"]
|
2023-12-08 14:53:17 +00:00
|
|
|
|
2023-12-12 18:04:30 +00:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
# Django doesn't like running DB queries during app initialization
|
|
|
|
warnings.filterwarnings("ignore", module="django.db", category=RuntimeWarning)
|
2024-05-24 21:37:51 +01:00
|
|
|
|
2023-12-12 18:04:30 +00:00
|
|
|
with connection.cursor() as cursor:
|
2024-03-31 15:08:01 +01:00
|
|
|
cursor.execute("CREATE VIRTUAL TABLE search_index USING fts5(body, content='');")
|
2023-12-08 14:53:17 +00:00
|
|
|
|
2024-01-02 23:15:37 +00:00
|
|
|
post_save.connect(self.post_save, sender=Page)
|
2023-12-08 14:53:17 +00:00
|
|
|
self.loaded = True
|
|
|
|
|
|
|
|
def post_save(self, sender, instance, created, **kwargs):
|
2024-01-02 23:15:37 +00:00
|
|
|
if not created:
|
|
|
|
return
|
2024-08-24 16:42:03 +01:00
|
|
|
|
2024-10-05 14:02:40 +01:00
|
|
|
with connections["yamdl"].cursor() as cursor:
|
2024-03-31 14:49:57 +01:00
|
|
|
cursor.execute("INSERT INTO search_index(body, rowid) VALUES (%s, %s)", [strip_tags(instance.content), instance.pk])
|
2024-08-24 16:42:03 +01:00
|
|
|
|
2024-10-05 14:02:40 +01:00
|
|
|
with connections["yamdl"].cursor() as cursor:
|
2024-08-24 16:42:03 +01:00
|
|
|
cursor.execute("PRAGMA optimise;")
|