Initial import
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
|
||||
from django.contrib import admin
|
||||
from .models import Entry, ScriptureRef
|
||||
|
||||
class ScriptureInline(admin.TabularInline):
|
||||
model = ScriptureRef
|
||||
extra = 0
|
||||
|
||||
@admin.register(Entry)
|
||||
class EntryAdmin(admin.ModelAdmin):
|
||||
list_display = ("talk_title","entry_code","source","date_added","date_edited")
|
||||
search_fields = ("talk_title","entry_code","source","subject","illustration","application","scripture_raw")
|
||||
inlines = [ScriptureInline]
|
||||
@@ -0,0 +1,5 @@
|
||||
|
||||
from django.apps import AppConfig
|
||||
class CoreConfig(AppConfig):
|
||||
default_auto_field = "django.db.models.BigAutoField"
|
||||
name = "core"
|
||||
@@ -0,0 +1,6 @@
|
||||
|
||||
from django import forms
|
||||
|
||||
class ImportForm(forms.Form):
|
||||
file = forms.FileField(allow_empty_file=False)
|
||||
dry_run = forms.BooleanField(initial=True, required=False, help_text="Preview changes without saving")
|
||||
@@ -0,0 +1,27 @@
|
||||
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.contrib.auth.models import User
|
||||
import os
|
||||
|
||||
class Command(BaseCommand):
|
||||
def handle(self, *args, **kwargs):
|
||||
admin_user = os.getenv("INIT_ADMIN_USERNAME")
|
||||
admin_pass = os.getenv("INIT_ADMIN_PASSWORD")
|
||||
editor_user = os.getenv("INIT_EDITOR_USERNAME")
|
||||
editor_pass = os.getenv("INIT_EDITOR_PASSWORD")
|
||||
if admin_user and admin_pass:
|
||||
u, created = User.objects.get_or_create(username=admin_user)
|
||||
u.is_staff = True
|
||||
u.is_superuser = True
|
||||
if admin_pass:
|
||||
u.set_password(admin_pass)
|
||||
u.save()
|
||||
self.stdout.write(self.style.SUCCESS(f"Admin ready: {admin_user}"))
|
||||
if editor_user and editor_pass:
|
||||
u, created = User.objects.get_or_create(username=editor_user)
|
||||
u.is_staff = False
|
||||
u.is_superuser = False
|
||||
if editor_pass:
|
||||
u.set_password(editor_pass)
|
||||
u.save()
|
||||
self.stdout.write(self.style.SUCCESS(f"Editor ready: {editor_user}"))
|
||||
@@ -0,0 +1,39 @@
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
initial = True
|
||||
dependencies = []
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Entry',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('subject', models.TextField(blank=True)),
|
||||
('illustration', models.TextField(blank=True)),
|
||||
('application', models.TextField(blank=True)),
|
||||
('scripture_raw', models.TextField(blank=True)),
|
||||
('source', models.CharField(blank=True, max_length=255)),
|
||||
('talk_title', models.CharField(blank=True, max_length=255)),
|
||||
('talk_number', models.IntegerField(blank=True, null=True)),
|
||||
('entry_code', models.CharField(blank=True, db_index=True, max_length=64)),
|
||||
('date_added', models.DateField(blank=True, null=True)),
|
||||
('date_edited', models.DateField(blank=True, null=True)),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='ScriptureRef',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('book', models.CharField(max_length=32)),
|
||||
('chapter_from', models.IntegerField(blank=True, null=True)),
|
||||
('verse_from', models.IntegerField(blank=True, null=True)),
|
||||
('chapter_to', models.IntegerField(blank=True, null=True)),
|
||||
('verse_to', models.IntegerField(blank=True, null=True)),
|
||||
('entry', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='scripture_refs', to='core.entry')),
|
||||
],
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,32 @@
|
||||
|
||||
from django.db import models
|
||||
|
||||
class Entry(models.Model):
|
||||
# Field names aligned to CSV headers (case-insensitive mapping in importer)
|
||||
subject = models.TextField(blank=True)
|
||||
illustration = models.TextField(blank=True)
|
||||
application = models.TextField(blank=True)
|
||||
scripture_raw = models.TextField(blank=True) # from CSV 'Scripture'
|
||||
source = models.CharField(max_length=255, blank=True)
|
||||
talk_title = models.CharField(max_length=255, blank=True) # 'Talk Title'
|
||||
talk_number = models.IntegerField(null=True, blank=True) # 'Talk Number'
|
||||
entry_code = models.CharField(max_length=64, blank=True, db_index=True) # 'Code'
|
||||
date_added = models.DateField(null=True, blank=True) # 'Date'
|
||||
date_edited = models.DateField(null=True, blank=True) # 'Date Edited'
|
||||
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.talk_title or '(untitled)'} [{self.entry_code}]"
|
||||
|
||||
class ScriptureRef(models.Model):
|
||||
entry = models.ForeignKey(Entry, on_delete=models.CASCADE, related_name="scripture_refs")
|
||||
book = models.CharField(max_length=32)
|
||||
chapter_from = models.IntegerField(null=True, blank=True)
|
||||
verse_from = models.IntegerField(null=True, blank=True)
|
||||
chapter_to = models.IntegerField(null=True, blank=True)
|
||||
verse_to = models.IntegerField(null=True, blank=True)
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.book} {self.chapter_from}:{self.verse_from}"
|
||||
@@ -0,0 +1,163 @@
|
||||
|
||||
import csv, io, re
|
||||
from datetime import datetime
|
||||
from dateutil import parser as dateparser
|
||||
from django.db.models import Q
|
||||
from .models import Entry, ScriptureRef
|
||||
|
||||
# Scripture parsing --------------------------------------------------
|
||||
BOOK_ALIASES = {
|
||||
"gen":"Genesis","ge":"Genesis","ex":"Exodus","lev":"Leviticus","num":"Numbers","deut":"Deuteronomy",
|
||||
"josh":"Joshua","judg":"Judges","rut":"Ruth","1sam":"1 Samuel","2sam":"2 Samuel",
|
||||
"1kings":"1 Kings","2kings":"2 Kings","1chron":"1 Chronicles","2chron":"2 Chronicles",
|
||||
"ezra":"Ezra","neh":"Nehemiah","esth":"Esther","job":"Job","ps":"Psalms","psa":"Psalms","prov":"Proverbs",
|
||||
"eccl":"Ecclesiastes","song":"Song of Solomon","isa":"Isaiah","jer":"Jeremiah","lam":"Lamentations",
|
||||
"ezek":"Ezekiel","dan":"Daniel","hos":"Hosea","joel":"Joel","amos":"Amos","obad":"Obadiah",
|
||||
"jon":"Jonah","mic":"Micah","nah":"Nahum","hab":"Habakkuk","zeph":"Zephaniah","hag":"Haggai",
|
||||
"zech":"Zechariah","mal":"Malachi","matt":"Matthew","mt":"Matthew","mark":"Mark","mk":"Mark","lk":"Luke",
|
||||
"luke":"Luke","jn":"John","john":"John","acts":"Acts","rom":"Romans","1cor":"1 Corinthians",
|
||||
"2cor":"2 Corinthians","gal":"Galatians","eph":"Ephesians","phil":"Philippians","col":"Colossians",
|
||||
"1thess":"1 Thessalonians","2thess":"2 Thessalonians","1tim":"1 Timothy","2tim":"2 Timothy",
|
||||
"titus":"Titus","phlm":"Philemon","heb":"Hebrews","jas":"James","jam":"James","1pet":"1 Peter","2pet":"2 Peter",
|
||||
"1john":"1 John","2john":"2 John","3john":"3 John","jude":"Jude","rev":"Revelation","re":"Revelation",
|
||||
}
|
||||
|
||||
SCR_REF_RE = re.compile(r"""
|
||||
^\s*([1-3]?\s*[A-Za-z\.]+)\s+ # book
|
||||
(\d+) # chapter start
|
||||
(?::(\d+))? # verse start
|
||||
(?:\s*[-–—]\s*(\d+)(?::(\d+))?)? # optional range
|
||||
\s*$
|
||||
""", re.VERBOSE)
|
||||
|
||||
def normalize_book(book_raw:str) -> str:
|
||||
b = re.sub(r"[\.\s]","", book_raw).lower()
|
||||
return BOOK_ALIASES.get(b, book_raw.strip())
|
||||
|
||||
def parse_scripture(s: str):
|
||||
parts = [p.strip() for p in (s or "").split(";") if p.strip()]
|
||||
parsed = []
|
||||
for p in parts:
|
||||
m = SCR_REF_RE.match(p)
|
||||
if not m:
|
||||
parsed.append(None); continue
|
||||
book_raw, ch1, v1, ch2, v2 = m.groups()
|
||||
parsed.append({
|
||||
"book": normalize_book(book_raw),
|
||||
"chapter_from": int(ch1),
|
||||
"verse_from": int(v1) if v1 else None,
|
||||
"chapter_to": int(ch2) if ch2 else None,
|
||||
"verse_to": int(v2) if v2 else None,
|
||||
})
|
||||
return parsed
|
||||
|
||||
# CSV import ---------------------------------------------------------
|
||||
EXPECTED_HEADERS = ["Subject","Illustration","Application","Scripture","Source","Talk Title","Talk Number","Code","Date","Date Edited"]
|
||||
|
||||
def parse_date(value):
|
||||
if not value or not str(value).strip(): return None
|
||||
try: return dateparser.parse(str(value)).date()
|
||||
except Exception: return None
|
||||
|
||||
def import_csv(file_bytes: bytes, dry_run: bool=True):
|
||||
text = file_bytes.decode("utf-8-sig")
|
||||
reader = csv.DictReader(io.StringIO(text))
|
||||
headers = reader.fieldnames or []
|
||||
# normalize
|
||||
lower_map = {h.lower():h for h in headers}
|
||||
required_lower = [h.lower() for h in EXPECTED_HEADERS]
|
||||
missing = [orig for orig in EXPECTED_HEADERS if orig.lower() not in lower_map]
|
||||
if missing:
|
||||
raise ValueError(f"Missing required headers: {missing}")
|
||||
report = {"rows":0,"inserted":0,"updated":0,"skipped":0,"errors":[],"scripture_parsed":0,"scripture_failed":0}
|
||||
rows = list(reader); report["rows"] = len(rows)
|
||||
for r in rows:
|
||||
try:
|
||||
def get(name):
|
||||
return r[ lower_map[name.lower()] ].strip() if r.get(lower_map[name.lower()]) is not None else ""
|
||||
|
||||
entry_code = get("Code")
|
||||
data = dict(
|
||||
subject=get("Subject"),
|
||||
illustration=get("Illustration"),
|
||||
application=get("Application"),
|
||||
scripture_raw=get("Scripture"),
|
||||
source=get("Source"),
|
||||
talk_title=get("Talk Title"),
|
||||
talk_number=int(get("Talk Number")) if get("Talk Number") else None,
|
||||
entry_code=entry_code,
|
||||
date_added=parse_date(get("Date")),
|
||||
date_edited=parse_date(get("Date Edited")),
|
||||
)
|
||||
obj = None
|
||||
if entry_code:
|
||||
try: obj = Entry.objects.get(entry_code=entry_code)
|
||||
except Entry.DoesNotExist: obj = None
|
||||
|
||||
if not dry_run:
|
||||
if obj:
|
||||
for k,v in data.items(): setattr(obj,k,v)
|
||||
obj.save()
|
||||
obj.scripture_refs.all().delete()
|
||||
report["updated"] += 1
|
||||
else:
|
||||
obj = Entry.objects.create(**data)
|
||||
report["inserted"] += 1
|
||||
for pr in parse_scripture(data["scripture_raw"]):
|
||||
if pr: ScriptureRef.objects.create(entry=obj, **pr); report["scripture_parsed"] += 1
|
||||
else: report["scripture_failed"] += 1
|
||||
else:
|
||||
for pr in parse_scripture(data["scripture_raw"]):
|
||||
if pr: report["scripture_parsed"] += 1
|
||||
else: report["scripture_failed"] += 1
|
||||
|
||||
except Exception as e:
|
||||
report["skipped"] += 1
|
||||
report["errors"].append(str(e))
|
||||
return report
|
||||
|
||||
# Search helpers -----------------------------------------------------
|
||||
SEARCHABLE_FIELDS = {
|
||||
"Subject": "subject",
|
||||
"Illustration": "illustration",
|
||||
"Application": "application",
|
||||
"Scripture": "scripture_raw",
|
||||
"Source": "source",
|
||||
"Talk Title": "talk_title",
|
||||
"Talk Number": "talk_number",
|
||||
"Code": "entry_code",
|
||||
}
|
||||
|
||||
def wildcard_to_ilike(term:str)->str:
|
||||
# Convert * ? to SQL ILIKE pattern
|
||||
return term.replace('%','\%').replace('_','\_').replace('*','%').replace('?','_')
|
||||
|
||||
def build_query(selected_fields, query_text):
|
||||
# Split on spaces unless inside quotes
|
||||
tokens = []
|
||||
buf = ''
|
||||
in_quotes = False
|
||||
for ch in query_text:
|
||||
if ch == '"': in_quotes = not in_quotes; continue
|
||||
if ch.isspace() and not in_quotes:
|
||||
if buf: tokens.append(buf); buf=''
|
||||
else:
|
||||
buf += ch
|
||||
if buf: tokens.append(buf)
|
||||
|
||||
# Build Q objects: AND across tokens, OR across fields for each token
|
||||
q = Q()
|
||||
for t in tokens:
|
||||
pat = wildcard_to_ilike(t)
|
||||
token_q = Q()
|
||||
# OR across fields
|
||||
for label in selected_fields:
|
||||
col = SEARCHABLE_FIELDS[label]
|
||||
if col == "talk_number" and pat.replace('%','').replace('_','').isdigit():
|
||||
try:
|
||||
token_q |= Q(**{col: int(pat.replace('%','').replace('_',''))})
|
||||
except: pass
|
||||
else:
|
||||
token_q |= Q(**{f"{col}__icontains": t.replace('*','').replace('?','')}) | Q(**{f"{col}__iregex": pat.replace('%','.*').replace('_','.')})
|
||||
q &= token_q
|
||||
return q
|
||||
@@ -0,0 +1,176 @@
|
||||
|
||||
from django.shortcuts import render, redirect, get_object_or_404
|
||||
from django.contrib.auth import authenticate, login
|
||||
from django.contrib.auth.decorators import login_required, user_passes_test
|
||||
from django.http import HttpResponse, HttpResponseForbidden
|
||||
from django.contrib import messages
|
||||
from django.db.models import Q
|
||||
import csv
|
||||
from django.utils.timezone import now
|
||||
from .forms import ImportForm
|
||||
from .models import Entry
|
||||
from .utils import import_csv, SEARCHABLE_FIELDS, build_query
|
||||
|
||||
def is_admin(user):
|
||||
return user.is_superuser or user.is_staff
|
||||
|
||||
def login_view(request):
|
||||
if request.user.is_authenticated:
|
||||
return redirect("search")
|
||||
ctx = {}
|
||||
if request.method == "POST":
|
||||
username = request.POST.get("username")
|
||||
password = request.POST.get("password")
|
||||
user = authenticate(request, username=username, password=password)
|
||||
if user:
|
||||
login(request, user)
|
||||
return redirect("search")
|
||||
ctx["error"] = "Invalid credentials"
|
||||
return render(request, "login.html", ctx)
|
||||
|
||||
@login_required
|
||||
def redirect_to_search(request):
|
||||
return redirect("search")
|
||||
|
||||
@login_required
|
||||
def search_view(request):
|
||||
total = Entry.objects.count()
|
||||
query = request.GET.get("q", "").strip()
|
||||
selected = request.GET.getlist("fields") or list(SEARCHABLE_FIELDS.keys())
|
||||
entries = []
|
||||
results_count = 0
|
||||
current_id = None
|
||||
|
||||
if query:
|
||||
q = build_query(selected, query)
|
||||
entries = list(Entry.objects.filter(q).order_by("-date_added","-id").values_list("id", flat=True))
|
||||
results_count = len(entries)
|
||||
request.session["search_ids"] = entries
|
||||
request.session["search_index"] = 0
|
||||
if entries:
|
||||
current_id = entries[0]
|
||||
return redirect("record_view", entry_id=current_id)
|
||||
|
||||
return render(request, "search.html", {
|
||||
"total": total,
|
||||
"q": query,
|
||||
"selected": selected,
|
||||
"fields": list(SEARCHABLE_FIELDS.keys()),
|
||||
"results_count": results_count,
|
||||
})
|
||||
|
||||
@login_required
|
||||
def record_view(request, entry_id):
|
||||
ids = request.session.get("search_ids", [])
|
||||
if entry_id in ids:
|
||||
request.session["search_index"] = ids.index(entry_id)
|
||||
idx = request.session.get("search_index", 0)
|
||||
total = Entry.objects.count()
|
||||
results_count = len(ids)
|
||||
pos = (idx+1) if ids else 1
|
||||
entry = get_object_or_404(Entry, id=entry_id)
|
||||
return render(request, "record.html", {
|
||||
"entry": entry,
|
||||
"locked": True,
|
||||
"total": total,
|
||||
"results_count": results_count,
|
||||
"position": pos,
|
||||
})
|
||||
|
||||
@login_required
|
||||
def nav_prev(request):
|
||||
ids = request.session.get("search_ids", [])
|
||||
idx = request.session.get("search_index", 0)
|
||||
if ids:
|
||||
idx = max(0, idx-1)
|
||||
request.session["search_index"] = idx
|
||||
return redirect("record_view", entry_id=ids[idx])
|
||||
messages.info(request, "No search results loaded.")
|
||||
return redirect("search")
|
||||
|
||||
@login_required
|
||||
def nav_next(request):
|
||||
ids = request.session.get("search_ids", [])
|
||||
idx = request.session.get("search_index", 0)
|
||||
if ids:
|
||||
idx = min(len(ids)-1, idx+1)
|
||||
request.session["search_index"] = idx
|
||||
return redirect("record_view", entry_id=ids[idx])
|
||||
messages.info(request, "No search results loaded.")
|
||||
return redirect("search")
|
||||
|
||||
@login_required
|
||||
def record_save(request, entry_id):
|
||||
if request.method != "POST":
|
||||
return redirect("record_view", entry_id=entry_id)
|
||||
e = get_object_or_404(Entry, id=entry_id)
|
||||
# Save edited fields
|
||||
e.subject = request.POST.get("subject","")
|
||||
e.illustration = request.POST.get("illustration","")
|
||||
e.application = request.POST.get("application","")
|
||||
e.scripture_raw = request.POST.get("scripture_raw","")
|
||||
e.source = request.POST.get("source","")
|
||||
e.talk_title = request.POST.get("talk_title","")
|
||||
tn = request.POST.get("talk_number","").strip()
|
||||
e.talk_number = int(tn) if tn.isdigit() else None
|
||||
e.entry_code = request.POST.get("entry_code","")
|
||||
e.date_added = request.POST.get("date_added") or None
|
||||
e.date_edited = request.POST.get("date_edited") or None
|
||||
e.save()
|
||||
messages.success(request, "Saved changes.")
|
||||
return redirect("record_view", entry_id=entry_id)
|
||||
|
||||
@login_required
|
||||
def record_delete(request, entry_id):
|
||||
if request.method == "POST":
|
||||
e = get_object_or_404(Entry, id=entry_id)
|
||||
e.delete()
|
||||
messages.success(request, "Entry deleted.")
|
||||
# After delete, move to previous or search page
|
||||
ids = request.session.get("search_ids", [])
|
||||
idx = request.session.get("search_index", 0)
|
||||
if ids:
|
||||
ids = [i for i in ids if i != entry_id]
|
||||
request.session["search_ids"] = ids
|
||||
if not ids:
|
||||
return redirect("search")
|
||||
idx = max(0, min(idx, len(ids)-1))
|
||||
request.session["search_index"] = idx
|
||||
return redirect("record_view", entry_id=ids[idx])
|
||||
return redirect("search")
|
||||
return HttpResponseForbidden("Use POST to delete.")
|
||||
|
||||
@login_required
|
||||
@user_passes_test(is_admin)
|
||||
def import_wizard(request):
|
||||
from .forms import ImportForm
|
||||
if request.method == "POST":
|
||||
form = ImportForm(request.POST, request.FILES)
|
||||
if form.is_valid():
|
||||
fbytes = form.cleaned_data["file"].read()
|
||||
dry = form.cleaned_data["dry_run"]
|
||||
try:
|
||||
report = import_csv(fbytes, dry_run=dry)
|
||||
return render(request, "import_result.html", {"report": report, "dry_run": dry})
|
||||
except Exception as e:
|
||||
messages.error(request, f"Import failed: {e}")
|
||||
else:
|
||||
form = ImportForm()
|
||||
return render(request, "import_wizard.html", {"form": form})
|
||||
|
||||
@login_required
|
||||
@user_passes_test(is_admin)
|
||||
def export_csv(request):
|
||||
response = HttpResponse(content_type='text/csv')
|
||||
ts = now().strftime("%Y-%m-%d_%H-%M-%S")
|
||||
response['Content-Disposition'] = f'attachment; filename="illustrations_backup_{ts}.csv"'
|
||||
writer = csv.writer(response)
|
||||
writer.writerow(["Subject","Illustration","Application","Scripture","Source","Talk Title","Talk Number","Code","Date","Date Edited"])
|
||||
for e in Entry.objects.all().order_by("id"):
|
||||
writer.writerow([
|
||||
e.subject, e.illustration, e.application, e.scripture_raw, e.source,
|
||||
e.talk_title, e.talk_number if e.talk_number is not None else "", e.entry_code,
|
||||
e.date_added.isoformat() if e.date_added else "",
|
||||
e.date_edited.isoformat() if e.date_edited else "",
|
||||
])
|
||||
return response
|
||||
Reference in New Issue
Block a user