feat: projekte und suche im backend
Projekte pro Benutzer mit Name und Farbe, Clips optional einem Projekt zugeordnet. Löschen eines Projekts verschiebt seine Clips nach "ohne Projekt". Clip-Liste filtert nach Projekt und sucht Teiltexte in Titel und Inhalt, unabhängig von Groß-/Kleinschreibung (auch Umlaute). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
848cff35b3
commit
ff2860f7be
13 files changed
+711
-9
No files matched your search
@@ -1,15 +1,20 @@
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import delete, func, select, update
|
||||
from sqlalchemy import delete, func, or_, select, update
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.config import get_settings
|
||||
from app.languages import LANGUAGES
|
||||
from app.models import Clip, ClipKind, utcnow
|
||||
from app.services.projects import require_project
|
||||
from app.services.users import UserError
|
||||
|
||||
# Changing only these fields does not count as an edit (no new updated_at)
|
||||
_META_FIELDS = frozenset({"pinned"})
|
||||
_META_FIELDS = frozenset({"pinned", "project_id"})
|
||||
|
||||
# Filter value for clips without a project
|
||||
NO_PROJECT = "none"
|
||||
SEARCH_MAX_LENGTH = 200
|
||||
|
||||
|
||||
class ClipConflict(Exception):
|
||||
@@ -40,15 +45,43 @@ def _normalize(kind: ClipKind, title: str | None, content: str, language: str |
|
||||
return title, language
|
||||
|
||||
|
||||
def list_clips(db: Session, user_id: int, limit: int, offset: int) -> tuple[list[Clip], int]:
|
||||
def _filters(user_id: int, project: int | str | None, query: str | None) -> list:
|
||||
conditions = [Clip.user_id == user_id]
|
||||
if project == NO_PROJECT:
|
||||
conditions.append(Clip.project_id.is_(None))
|
||||
elif project is not None:
|
||||
conditions.append(Clip.project_id == project)
|
||||
needle = (query or "").strip().casefold()
|
||||
if needle:
|
||||
# Substring match via instr(): no LIKE wildcards to escape, finds "config" in "getConfig"
|
||||
conditions.append(
|
||||
or_(
|
||||
func.instr(func.lc_casefold(Clip.title), needle) > 0,
|
||||
func.instr(func.lc_casefold(Clip.content), needle) > 0,
|
||||
)
|
||||
)
|
||||
return conditions
|
||||
|
||||
|
||||
def list_clips(
|
||||
db: Session,
|
||||
user_id: int,
|
||||
limit: int,
|
||||
offset: int,
|
||||
*,
|
||||
project: int | str | None = None,
|
||||
query: str | None = None,
|
||||
) -> tuple[list[Clip], int]:
|
||||
"""Clips of the user, optionally only those of a project (or NO_PROJECT) matching a query."""
|
||||
conditions = _filters(user_id, project, query)
|
||||
items = db.scalars(
|
||||
select(Clip)
|
||||
.where(Clip.user_id == user_id)
|
||||
.where(*conditions)
|
||||
.order_by(Clip.pinned.desc(), Clip.updated_at.desc(), Clip.id.desc())
|
||||
.limit(limit)
|
||||
.offset(offset)
|
||||
)
|
||||
total = db.scalar(select(func.count()).select_from(Clip).where(Clip.user_id == user_id))
|
||||
total = db.scalar(select(func.count()).select_from(Clip).where(*conditions))
|
||||
return list(items), total or 0
|
||||
|
||||
|
||||
@@ -65,10 +98,14 @@ def create_clip(
|
||||
title: str | None = None,
|
||||
language: str | None = None,
|
||||
pinned: bool = False,
|
||||
project_id: int | None = None,
|
||||
) -> Clip:
|
||||
title, language = _normalize(kind, title, content, language)
|
||||
if project_id is not None:
|
||||
require_project(db, user_id, project_id)
|
||||
clip = Clip(
|
||||
user_id=user_id,
|
||||
project_id=project_id,
|
||||
kind=kind,
|
||||
title=title,
|
||||
content=content,
|
||||
@@ -93,8 +130,12 @@ def update_clip(
|
||||
content = changes.get("content", clip.content)
|
||||
language = changes.get("language", clip.language)
|
||||
title, language = _normalize(kind, title, content, language)
|
||||
project_id = changes.get("project_id", clip.project_id)
|
||||
if "project_id" in changes and project_id is not None:
|
||||
require_project(db, user_id, project_id)
|
||||
|
||||
values: dict[str, Any] = {
|
||||
"project_id": project_id,
|
||||
"kind": kind,
|
||||
"title": title,
|
||||
"content": content,
|
||||
|
||||
Reference in new issue
Block a user