Started adding the reference system to add images

by adding a hash to each photo and also the possibility to add a reference by pressing i whilst the sidebar is on focus
This commit is contained in:
Gustavo Henrique Santos Souza de Miranda
2025-07-02 12:48:12 -03:00
parent 2973620573
commit d80825d3a1
3 changed files with 327 additions and 78 deletions
@@ -5,6 +5,7 @@ from textual.screen import Screen
from textual.widgets import Static, Input, Button
from textual.containers import Horizontal, Container
from .file_picker_modal import FilePickerModal
import hashlib
class AddPhotoModal(Screen):
"""Modal for adding a new photo"""
@@ -12,6 +13,14 @@ class AddPhotoModal(Screen):
super().__init__()
self.diary_id = diary_id
self.result = None
self.created_photo = None
def _generate_photo_hash(self, photo_data: dict) -> str:
"""Generate a short, unique hash for a photo"""
# Use temporary data for hash generation
unique_string = f"{photo_data['name']}_{photo_data.get('photo_id', 0)}_new"
hash_object = hashlib.md5(unique_string.encode())
return hash_object.hexdigest()[:8]
def compose(self) -> ComposeResult:
yield Container(
@@ -72,13 +81,23 @@ class AddPhotoModal(Screen):
)
if new_photo:
self.notify(f"Photo '{new_photo.name}' added successfully!")
self.created_photo = new_photo
# Generate hash for the new photo
photo_hash = self._generate_photo_hash({
"name": new_photo.name,
"photo_id": new_photo.id
})
self.notify(f"Photo '{new_photo.name}' added successfully!\nHash: {photo_hash}\nReference: \\[\\[photo:{new_photo.name}:{photo_hash}\\]\\]",
severity="information", timeout=5)
# Return the created photo data to the calling screen
self.result = {
"filepath": photo_data["filepath"],
"name": photo_data["name"],
"caption": photo_data["caption"],
"photo_id": new_photo.id
"photo_id": new_photo.id,
"hash": photo_hash
}
self.dismiss(self.result)
else: