Label editing
This commit is contained in:
@@ -172,3 +172,65 @@ def list_tags():
|
||||
tags.append(row['name'])
|
||||
seen.add(row['name'].casefold())
|
||||
return tags
|
||||
|
||||
|
||||
def list_user_labels(user_id: str):
|
||||
with get_connection() as conn:
|
||||
rows = conn.execute(
|
||||
'SELECT id, name, created_by FROM tags WHERE created_by = ? ORDER BY name',
|
||||
(user_id,),
|
||||
).fetchall()
|
||||
return [dict(row) for row in rows]
|
||||
|
||||
|
||||
def create_label(user_id: str, name: str):
|
||||
normalized = normalize_tags([name])
|
||||
label = normalized[0] if normalized else ''
|
||||
if not label:
|
||||
raise ValueError('Label cannot be empty')
|
||||
with get_connection() as conn:
|
||||
existing = conn.execute(
|
||||
'SELECT id FROM tags WHERE lower(name) = lower(?)', (label,)
|
||||
).fetchone()
|
||||
if existing:
|
||||
raise ValueError('Label already exists')
|
||||
label_id = str(uuid4())
|
||||
conn.execute(
|
||||
'INSERT INTO tags (id, name, created_by) VALUES (?, ?, ?)',
|
||||
(label_id, label, user_id),
|
||||
)
|
||||
conn.commit()
|
||||
return {'id': label_id, 'name': label, 'created_by': user_id}
|
||||
|
||||
|
||||
def update_label(label_id: str, user_id: str, name: str):
|
||||
normalized = normalize_tags([name])
|
||||
if not normalized:
|
||||
raise ValueError('Label cannot be empty')
|
||||
with get_connection() as conn:
|
||||
current = conn.execute(
|
||||
'SELECT id, name, created_by FROM tags WHERE id = ?', (label_id,)
|
||||
).fetchone()
|
||||
if current is None or current['created_by'] != user_id:
|
||||
return None
|
||||
duplicate = conn.execute(
|
||||
'SELECT id FROM tags WHERE lower(name) = lower(?) AND id != ?',
|
||||
(normalized[0], label_id),
|
||||
).fetchone()
|
||||
if duplicate:
|
||||
raise ValueError('Label already exists')
|
||||
conn.execute('UPDATE tags SET name = ? WHERE id = ?', (normalized[0], label_id))
|
||||
conn.commit()
|
||||
return {'id': label_id, 'name': normalized[0], 'created_by': user_id}
|
||||
|
||||
|
||||
def delete_label(label_id: str, user_id: str | None = None, is_admin: bool = False):
|
||||
with get_connection() as conn:
|
||||
current = conn.execute(
|
||||
'SELECT id, created_by FROM tags WHERE id = ?', (label_id,)
|
||||
).fetchone()
|
||||
if current is None or (not is_admin and current['created_by'] != user_id):
|
||||
return False
|
||||
conn.execute('DELETE FROM tags WHERE id = ?', (label_id,))
|
||||
conn.commit()
|
||||
return True
|
||||
|
||||
Reference in New Issue
Block a user