Make it possible to add a tag from the Tag Browser
All checks were successful
CI / test (push) Successful in 3m46s
Lint / test (push) Successful in 17s
Trivy / test (push) Successful in 21s

This commit is contained in:
Miguel Jacq 2025-11-15 11:22:40 +11:00
parent b1ae56270a
commit b1ba599e99
Signed by: mig5
GPG key ID: 59B3F0C24135C6A9
7 changed files with 63 additions and 4 deletions

View file

@ -1,3 +1,7 @@
# 0.3.1
* Make it possible to add a tag from the Tag Browser
# 0.3
* Introduce Tags

View file

@ -561,6 +561,30 @@ class DBManager:
).fetchall()
return [(r[0], r[1], r[2]) for r in rows]
def add_tag(self, name: str, color: str) -> None:
"""
Update a tag's name and colour.
"""
name = name.strip()
color = color.strip() or "#CCCCCC"
try:
with self.conn:
cur = self.conn.cursor()
cur.execute(
"""
INSERT INTO tags
(name, color)
VALUES (?, ?);
""",
(name, color),
)
except sqlite.IntegrityError as e:
if "UNIQUE constraint failed: tags.name" in str(e):
raise sqlite.IntegrityError(
strings._("tag_already_exists_with_that_name")
) from e
def update_tag(self, tag_id: int, name: str, color: str) -> None:
"""
Update a tag's name and colour.

View file

@ -127,6 +127,7 @@
"add": "Add",
"remove": "Remove",
"ok": "OK",
"add_a_tag": "Add a tag",
"edit_tag_name": "Edit tag name",
"new_tag_name": "New tag name:",
"change_color": "Change colour",

View file

@ -127,6 +127,7 @@
"add": "Ajouter",
"remove": "Supprimer",
"ok": "OK",
"add_a_tag": "Ajouter une étiquette",
"edit_tag_name": "Modifier le nom de l'étiquette",
"new_tag_name": "Nouveau nom de l'étiquette :",
"change_color": "Changer la couleur",

View file

@ -126,6 +126,7 @@
"add": "Aggiungi",
"remove": "Rimuovi",
"ok": "OK",
"add_a_tag": "Aggiungi un tag",
"edit_tag_name": "Modifica nome tag",
"new_tag_name": "Nuovo nome tag:",
"change_color": "Cambia colore",

View file

@ -1,4 +1,3 @@
# tag_browser.py
from PySide6.QtCore import Qt, Signal
from PySide6.QtGui import QColor
from PySide6.QtWidgets import (
@ -11,6 +10,7 @@ from PySide6.QtWidgets import (
QLabel,
QColorDialog,
QMessageBox,
QInputDialog,
)
from .db import DBManager
@ -52,6 +52,10 @@ class TagBrowserDialog(QDialog):
# Tag management buttons
btn_row = QHBoxLayout()
self.add_tag_btn = QPushButton(strings._("add_a_tag"))
self.add_tag_btn.clicked.connect(self._add_a_tag)
btn_row.addWidget(self.add_tag_btn)
self.edit_name_btn = QPushButton(strings._("edit_tag_name"))
self.edit_name_btn.clicked.connect(self._edit_tag_name)
self.edit_name_btn.setEnabled(False)
@ -155,6 +159,23 @@ class TagBrowserDialog(QDialog):
self.openDateRequested.emit(date_iso)
self.accept()
def _add_a_tag(self):
"""Add a new tag"""
new_name, ok = QInputDialog.getText(
self, strings._("add_a_tag"), strings._("new_tag_name"), text=""
)
if ok and new_name:
color = QColorDialog.getColor(QColor(), self)
if color.isValid():
try:
self._db.add_tag(new_name, color.name())
self._populate(None)
self.tagsModified.emit()
except IntegrityError as e:
QMessageBox.critical(self, strings._("db_database_error"), str(e))
def _edit_tag_name(self):
"""Edit the name of the selected tag"""
item = self.tree.currentItem()
@ -169,9 +190,6 @@ class TagBrowserDialog(QDialog):
old_name = data["name"]
color = data["color"]
# Simple input dialog
from PySide6.QtWidgets import QInputDialog
new_name, ok = QInputDialog.getText(
self, strings._("edit_tag_name"), strings._("new_tag_name"), text=old_name
)

View file

@ -141,6 +141,16 @@ def test_list_all_tags(fresh_db):
assert "tag3" in tag_names
def test_add_tag_name_and_color(fresh_db):
"""Test adding a tag's name and color"""
fresh_db.add_tag("new123", "#FF0000")
updated_tags = fresh_db.list_tags()
assert len(updated_tags) == 1
assert updated_tags[0][1] == "new123"
assert updated_tags[0][2] == "#FF0000"
def test_update_tag_name_and_color(fresh_db):
"""Test updating a tag's name and color"""
date_iso = "2024-01-15"