mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-07-22 17:54:40 -05:00
Handle quotes
This commit is contained in:
parent
9f55626ba6
commit
c8ed0453a6
@ -459,43 +459,33 @@ def get_permissions_criterias(user: User | None = None) -> list:
|
|||||||
|
|
||||||
def rewrite_natural_date_keywords(query_string: str) -> str:
|
def rewrite_natural_date_keywords(query_string: str) -> str:
|
||||||
"""
|
"""
|
||||||
Rewrites `added:today`, `created:yesterday` into whoosh datetime ranges.
|
Rewrites natural date keywords (e.g. added:today or added:"yesterday") to UTC range syntax for Whoosh.
|
||||||
This prevents UTC confusion when searching with natural language date keywords.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
replacements = {}
|
|
||||||
patterns = [
|
|
||||||
("added:today", "added"),
|
|
||||||
("added:yesterday", "added"),
|
|
||||||
("created:today", "created"),
|
|
||||||
("created:yesterday", "created"),
|
|
||||||
]
|
|
||||||
|
|
||||||
tz = get_current_timezone()
|
tz = get_current_timezone()
|
||||||
local_now = now().astimezone(tz)
|
local_now = now().astimezone(tz)
|
||||||
|
|
||||||
today_start_local = datetime.combine(local_now.date(), time.min).replace(tzinfo=tz)
|
today = local_now.date()
|
||||||
today_end_local = datetime.combine(local_now.date(), time.max).replace(tzinfo=tz)
|
yesterday = today - timedelta(days=1)
|
||||||
yesterday_start_local = today_start_local - timedelta(days=1)
|
|
||||||
yesterday_end_local = today_end_local - timedelta(days=1)
|
|
||||||
|
|
||||||
for pattern, field in patterns:
|
ranges = {
|
||||||
if pattern in query_string:
|
"today": (
|
||||||
if pattern.endswith("today"):
|
datetime.combine(today, time.min, tzinfo=tz),
|
||||||
start = today_start_local
|
datetime.combine(today, time.max, tzinfo=tz),
|
||||||
end = today_end_local
|
),
|
||||||
else:
|
"yesterday": (
|
||||||
start = yesterday_start_local
|
datetime.combine(yesterday, time.min, tzinfo=tz),
|
||||||
end = yesterday_end_local
|
datetime.combine(yesterday, time.max, tzinfo=tz),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
|
||||||
|
pattern = r"(\b(?:added|created))\s*:\s*[\"']?(today|yesterday)[\"']?"
|
||||||
|
|
||||||
|
def repl(m):
|
||||||
|
field, keyword = m.group(1), m.group(2)
|
||||||
|
start, end = ranges[keyword]
|
||||||
start_str = start.astimezone(timezone.utc).strftime("%Y%m%d%H%M%S")
|
start_str = start.astimezone(timezone.utc).strftime("%Y%m%d%H%M%S")
|
||||||
end_str = end.astimezone(timezone.utc).strftime("%Y%m%d%H%M%S")
|
end_str = end.astimezone(timezone.utc).strftime("%Y%m%d%H%M%S")
|
||||||
|
return f"{field}:[{start_str} TO {end_str}]"
|
||||||
|
|
||||||
range_expr = f"{field}:[{start_str} TO {end_str}]"
|
return re.sub(pattern, repl, query_string)
|
||||||
logger.warning(f"RANGE: {range_expr}")
|
|
||||||
replacements[pattern] = range_expr
|
|
||||||
|
|
||||||
for match, replacement in replacements.items():
|
|
||||||
query_string = re.sub(rf"\b{re.escape(match)}\b", replacement, query_string)
|
|
||||||
|
|
||||||
return query_string
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user