This commit is contained in:
Joshua Laymon
2025-08-13 00:05:43 -05:00
parent 22a4f8c09a
commit a266dd9ea2
13 changed files with 100 additions and 114 deletions
+13 -17
View File
@@ -1,13 +1,13 @@
import csv, io, re
from dateutil import parser as dateparser
from datetime import date
from .models import Entry, ScriptureRef
SCR_REF_RE = re.compile(r"""^\s*([1-3]?\s*[A-Za-z\.]+)\s+(\d+)(?::(\d+))?(?:\s*[-–—]\s*(\d+)(?::(\d+))?)?\s*$""", re.VERBOSE)
BOOK_ALIASES={'matt':'Matthew','mt':'Matthew','jn':'John','john':'John','lk':'Luke','luke':'Luke','ps':'Psalms'}
def normalize_book(s):
b = re.sub(r"[\.\s]","", s).lower()
import re as _re
b = _re.sub(r"[.\s]","", s).lower()
return BOOK_ALIASES.get(b, s.strip())
def parse_scripture(s):
@@ -35,10 +35,11 @@ def import_csv_bytes(b: bytes, dry_run=True):
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)
from core.models import Entry, ScriptureRef
for row in rows:
try:
code=(row.get("code") or "").strip()
talk=row.get("talk number");
talk=row.get("talk number")
try: talk=int(talk) if str(talk).strip() else None
except: talk=None
data=dict(
@@ -75,17 +76,12 @@ def import_csv_bytes(b: bytes, dry_run=True):
report["skipped"]+=1; report["errors"].append(str(e))
return report
def wildcard_to_like(q:str)->str:
return q.replace("%","\%").replace("_","\_").replace("*","%").replace("?","_")
def terms(q:str): return [t for t in q.split() if t.strip()]
def month_buckets_last_12(today: date):
months=[]; y=today.year; m=today.month
for i in range(12):
mm=m-i; yy=y
while mm<=0: mm+=12; yy-=1
start=date(yy,mm,1)
end=date(yy+1,1,1) if mm==12 else date(yy,mm+1,1)
months.append((f"{yy}-{mm:02d}", start, end))
return list(reversed(months))
# Tokenization with quoted phrases; wildcards tolerated but removed for icontains
_QUOTED_OR_WORD = re.compile(r'"([^"]+)"|(\S+)')
def terms(q: str):
out = []
for m in _QUOTED_OR_WORD.finditer(q or ""):
token = (m.group(1) or m.group(2) or "").replace("*","").replace("?","").strip()
if token:
out.append(token)
return out