-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.py
More file actions
66 lines (54 loc) · 2.29 KB
/
admin.py
File metadata and controls
66 lines (54 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import logging
import threading
from django.contrib import admin
from django.contrib import messages
from django.core.management import call_command
from .models import BibleBook, BibleVerse
logger = logging.getLogger(__name__)
def run_kjv_import():
"""
Helper function to run the KJV Bible import in a background thread.
This function calls the import_kjv management command with the --clear flag
to replace existing data.
"""
try:
logger.info("Starting KJV Bible import...")
call_command("import_kjv", "--clear")
logger.info("KJV Bible import completed successfully")
except Exception as e:
logger.error(f"KJV Bible import failed: {str(e)}", exc_info=True)
@admin.register(BibleBook)
class BibleBookAdmin(admin.ModelAdmin):
list_display = ["order", "name", "slug", "testament", "chapters"]
list_filter = ["testament"]
search_fields = ["name", "slug"]
prepopulated_fields = {"slug": ("name",)}
ordering = ["order"]
actions = ["import_kjv_bible"]
def import_kjv_bible(self, request, queryset):
"""
Admin action to import the KJV Bible.
Runs the import in a background thread to avoid blocking the request.
"""
try:
# Start import in background thread (daemon=True allows clean shutdown)
import_thread = threading.Thread(target=run_kjv_import, daemon=True)
import_thread.start()
messages.success(
request,
"KJV Bible import has been started in the background. "
"Check the server logs for progress and completion status.",
)
except Exception as e:
logger.error(f"Failed to start KJV Bible import: {str(e)}", exc_info=True)
messages.error(request, f"Failed to start KJV Bible import: {str(e)}")
import_kjv_bible.short_description = "Import KJV Bible"
@admin.register(BibleVerse)
class BibleVerseAdmin(admin.ModelAdmin):
list_display = ["book", "chapter", "verse", "text_preview"]
list_filter = ["book", "chapter"]
search_fields = ["text", "book__name"]
ordering = ["book__order", "chapter", "verse"]
def text_preview(self, obj):
return obj.text[:50] + "..." if len(obj.text) > 50 else obj.text
text_preview.short_description = "Text"