Update web/core/views.py
This commit is contained in:
parent
6da1e92dbe
commit
e78af67684
@ -87,7 +87,6 @@ def search_page(request):
|
|||||||
"entry_code": False,
|
"entry_code": False,
|
||||||
}
|
}
|
||||||
|
|
||||||
# robust checkbox parsing: present only if checked
|
|
||||||
form_submitted = ("q" in request.GET) or any(k in request.GET for k in default_fields)
|
form_submitted = ("q" in request.GET) or any(k in request.GET for k in default_fields)
|
||||||
if form_submitted:
|
if form_submitted:
|
||||||
selected = {k: (k in request.GET) for k in default_fields}
|
selected = {k: (k in request.GET) for k in default_fields}
|
||||||
@ -101,17 +100,16 @@ def search_page(request):
|
|||||||
|
|
||||||
q = (request.GET.get("q") or "").strip()
|
q = (request.GET.get("q") or "").strip()
|
||||||
if q:
|
if q:
|
||||||
tokens = terms(q) # supports quoted phrases
|
tokens = terms(q)
|
||||||
fields = [f for f, sel in selected.items() if sel] or ["subject"]
|
fields = [f for f, sel in selected.items() if sel] or ["subject"]
|
||||||
|
|
||||||
# ---------- Pass A: regex for wildcard tokens ----------
|
|
||||||
qs = Entry.objects.all()
|
qs = Entry.objects.all()
|
||||||
used_regex = False
|
used_regex = False
|
||||||
for tok in tokens:
|
for tok in tokens:
|
||||||
clause = Q()
|
clause = Q()
|
||||||
if has_wildcards(tok):
|
if has_wildcards(tok):
|
||||||
used_regex = True
|
used_regex = True
|
||||||
pattern = wildcard_to_regex(tok) # e.g. "*love*" -> ".*love.*"
|
pattern = wildcard_to_regex(tok)
|
||||||
for f in fields:
|
for f in fields:
|
||||||
clause |= Q(**{f + "__iregex": pattern})
|
clause |= Q(**{f + "__iregex": pattern})
|
||||||
else:
|
else:
|
||||||
@ -121,18 +119,16 @@ def search_page(request):
|
|||||||
|
|
||||||
ids = list(qs.order_by("-date_added", "-id").values_list("id", flat=True))
|
ids = list(qs.order_by("-date_added", "-id").values_list("id", flat=True))
|
||||||
|
|
||||||
# ---------- Pass B (fallback): plain icontains if regex yielded 0 ----------
|
|
||||||
if used_regex and not ids:
|
if used_regex and not ids:
|
||||||
qs = Entry.objects.all()
|
qs = Entry.objects.all()
|
||||||
for tok in tokens:
|
for tok in tokens:
|
||||||
clause = Q()
|
clause = Q()
|
||||||
tok_stripped = tok.replace("*", "").replace("?", "") # fallback normalize
|
tok_stripped = tok.replace("*", "").replace("?", "")
|
||||||
for f in fields:
|
for f in fields:
|
||||||
clause |= Q(**{f + "__icontains": tok_stripped})
|
clause |= Q(**{f + "__icontains": tok_stripped})
|
||||||
qs = qs.filter(clause)
|
qs = qs.filter(clause)
|
||||||
ids = list(qs.order_by("-date_added", "-id").values_list("id", flat=True))
|
ids = list(qs.order_by("-date_added", "-id").values_list("id", flat=True))
|
||||||
|
|
||||||
# Debug to container logs
|
|
||||||
try:
|
try:
|
||||||
print(f"[search] q={q!r} tokens={tokens} fields={fields} count={len(ids)}")
|
print(f"[search] q={q!r} tokens={tokens} fields={fields} count={len(ids)}")
|
||||||
except Exception:
|
except Exception:
|
||||||
@ -204,6 +200,24 @@ def entry_view(request, entry_id):
|
|||||||
return render(request, "entry_view.html", entry_context(entry, ids))
|
return render(request, "entry_view.html", entry_context(entry, ids))
|
||||||
|
|
||||||
|
|
||||||
|
@login_required
|
||||||
|
def entry_add(request):
|
||||||
|
"""
|
||||||
|
Create a brand new Entry.
|
||||||
|
"""
|
||||||
|
if request.method == "POST":
|
||||||
|
form = EntryForm(request.POST)
|
||||||
|
if form.is_valid():
|
||||||
|
entry = form.save(commit=False)
|
||||||
|
entry.save()
|
||||||
|
messages.success(request, "New entry added.")
|
||||||
|
return redirect("entry_view", entry_id=entry.id)
|
||||||
|
else:
|
||||||
|
form = EntryForm()
|
||||||
|
|
||||||
|
return render(request, "entry_add.html", {"form": form})
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
def entry_edit(request, entry_id):
|
def entry_edit(request, entry_id):
|
||||||
ids = request.session.get("result_ids", [])
|
ids = request.session.get("result_ids", [])
|
||||||
@ -319,7 +333,8 @@ def stats_page(request):
|
|||||||
last30 = Entry.objects.filter(date_added__gte=today - timedelta(days=30)).count()
|
last30 = Entry.objects.filter(date_added__gte=today - timedelta(days=30)).count()
|
||||||
last365 = Entry.objects.filter(date_added__gte=today - timedelta(days=365)).count()
|
last365 = Entry.objects.filter(date_added__gte=today - timedelta(days=365)).count()
|
||||||
|
|
||||||
# last 12 months
|
from collections import Counter
|
||||||
|
|
||||||
months = []
|
months = []
|
||||||
y = today.year
|
y = today.year
|
||||||
m = today.month
|
m = today.month
|
||||||
@ -330,7 +345,6 @@ def stats_page(request):
|
|||||||
mm += 12
|
mm += 12
|
||||||
yy -= 1
|
yy -= 1
|
||||||
from datetime import date as _d
|
from datetime import date as _d
|
||||||
|
|
||||||
start = _d(yy, mm, 1)
|
start = _d(yy, mm, 1)
|
||||||
end = _d(yy + 1, 1, 1) if mm == 12 else _d(yy, mm + 1, 1)
|
end = _d(yy + 1, 1, 1) if mm == 12 else _d(yy, mm + 1, 1)
|
||||||
label = f"{yy}-{mm:02d}"
|
label = f"{yy}-{mm:02d}"
|
||||||
@ -347,9 +361,6 @@ def stats_page(request):
|
|||||||
for label, value in series
|
for label, value in series
|
||||||
]
|
]
|
||||||
|
|
||||||
# top subjects
|
|
||||||
from collections import Counter
|
|
||||||
|
|
||||||
counts = Counter()
|
counts = Counter()
|
||||||
for subj in Entry.objects.exclude(subject="").values_list("subject", flat=True):
|
for subj in Entry.objects.exclude(subject="").values_list("subject", flat=True):
|
||||||
for tag in [t.strip() for t in subj.split(",") if t.strip()]:
|
for tag in [t.strip() for t in subj.split(",") if t.strip()]:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user