Skip to content

Commit 67f102a

Browse files
committed
feat: add date_created and date_modified fields to table
1 parent 79d3b76 commit 67f102a

File tree

3 files changed

+10
-62
lines changed

3 files changed

+10
-62
lines changed

src/tagstudio/core/library/alchemy/enums.py

-4
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,11 @@ class ItemType(enum.Enum):
6767

6868
class SortingModeEnum(enum.Enum):
6969
DATE_ADDED = "file.date_added"
70-
<<<<<<< HEAD:src/tagstudio/core/library/alchemy/enums.py
7170
FILE_NAME = "generic.filename"
7271
PATH = "file.path"
73-
=======
74-
>>>>>>> 8f17c362203a368c5860599b75c2e89e6c8c1fc5:tagstudio/src/core/library/alchemy/enums.py
7572
DATE_CREATED = "file.date_created"
7673
DATE_MODIFIED = "file.date_modified"
7774

78-
7975
@dataclass
8076
class FilterState:
8177
"""Represent a state of the Library grid view."""

src/tagstudio/core/library/alchemy/library.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -299,12 +299,12 @@ def migrate_json_to_sqlite(self, json_lib: JsonLibrary):
299299
fields=[],
300300
id=entry.id + 1, # JSON IDs start at 0 instead of 1
301301
date_added=datetime.now(),
302-
# date_modified=date_modified,
303-
# date_created=date_created,
302+
date_modified=date_modified,
303+
date_created=date_created,
304304
)
305305
for entry in json_lib.entries
306-
# if (date_created := get_file_time(entry.path / entry.filename)[0]) is not None
307-
# and (date_modified := get_file_time(entry.path / entry.filename)[1]) is not None
306+
if (date_created := self.get_file_time(entry.path / entry.filename)[0]) is not None
307+
and (date_modified := self.get_file_time(entry.path / entry.filename)[1]) is not None
308308
]
309309
)
310310
for entry in json_lib.entries:
@@ -375,7 +375,7 @@ def open_sqlite_library(self, library_dir: Path, is_new: bool) -> LibraryStatus:
375375
drivername="sqlite",
376376
database=str(self.storage_path),
377377
)
378-
# NOTE: File-based databases should use NullPool ito create new DB connection in order to
378+
# NOTE: File-based databases should use NullPool to create new DB connection in order to
379379
# keep connections on separate threads, which prevents the DB files from being locked
380380
# even after a connection has been closed.
381381
# SingletonThreadPool (the default for :memory:) should still be used for in-memory DBs.

src/tagstudio/core/utils/refresh_dir.py

+5-53
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from datetime import datetime as dt
1+
import datetime as dt
22
from collections.abc import Iterator
33
from dataclasses import dataclass, field
44
from pathlib import Path
@@ -26,7 +26,6 @@
2626
]
2727
)
2828

29-
3029
@dataclass
3130
class RefreshDirTracker:
3231
library: Library
@@ -36,42 +35,24 @@ class RefreshDirTracker:
3635
def files_count(self) -> int:
3736
return len(self.files_not_in_library)
3837

39-
<<<<<<< HEAD:src/tagstudio/core/utils/refresh_dir.py
40-
# moving get_file_times to library to avoid circular import
41-
=======
42-
>>>>>>> 8f17c362203a368c5860599b75c2e89e6c8c1fc5:tagstudio/src/core/utils/refresh_dir.py
38+
# Created get_file_time() (basically same thing) in library
4339
def get_file_times(self, file_path: Path):
4440
"""Get the creation and modification times of a file."""
4541
stat = file_path.stat()
4642
system = platform.system()
47-
<<<<<<< HEAD:src/tagstudio/core/utils/refresh_dir.py
4843

4944
# st_birthtime on Windows and Mac, st_ctime on Linux.
50-
if system in ['Windows', 'Darwin']: # Windows & macOS
45+
if system in ["Windows", "Darwin"]: # Windows & macOS
5146
date_created = dt.datetime.fromtimestamp(stat.st_birthtime)
5247
else: # Linux
5348
date_created = dt.datetime.fromtimestamp(stat.st_ctime) # Linux lacks st_birthtime
5449

5550
date_modified = dt.datetime.fromtimestamp(stat.st_mtime)
56-
=======
57-
if system == 'Windows': # Windows
58-
date_created = dt.fromtimestamp(stat.st_ctime, dt.timezone.utc)
59-
elif system == 'Darwin': # macOS
60-
date_created = dt.fromtimestamp(stat.st_birthtime, dt.timezone.utc)
61-
else: # Linux and other systems
62-
try:
63-
date_created = dt.fromtimestamp(stat.st_birthtime, dt.timezone.utc)
64-
except AttributeError:
65-
# st_birthtime is not available on some Linux filesystems
66-
date_created = dt.fromtimestamp(stat.st_ctime, dt.timezone.utc)
67-
date_modified = dt.fromtimestamp(stat.st_mtime, dt.timezone.utc)
68-
>>>>>>> 8f17c362203a368c5860599b75c2e89e6c8c1fc5:tagstudio/src/core/utils/refresh_dir.py
6951
return date_created, date_modified
7052

7153
def save_new_files(self):
7254
"""Save the list of files that are not in the library."""
7355
if self.files_not_in_library:
74-
<<<<<<< HEAD:src/tagstudio/core/utils/refresh_dir.py
7556
entries = [
7657
Entry(
7758
path=entry_path,
@@ -82,26 +63,9 @@ def save_new_files(self):
8263
date_modified=date_modified,
8364
)
8465
for entry_path in self.files_not_in_library
85-
if (date_created := self.get_file_times(entry_path)[0]) is not None
86-
and (date_modified := self.get_file_times(entry_path)[1]) is not None
66+
if (date_created := self.get_file_times(entry_path)[0]) is not None
67+
and (date_modified := self.get_file_times(entry_path)[1]) is not None
8768
]
88-
=======
89-
entries = []
90-
for entry_path in self.files_not_in_library:
91-
date_created, date_modified = self.get_file_times(entry_path)
92-
if date_created is None or date_modified is None:
93-
continue # Skip files that could not be processed
94-
entries.append(
95-
Entry(
96-
path=entry_path,
97-
folder=self.library.folder,
98-
fields=[],
99-
date_added=dt.now(),
100-
date_created=dt.now(),
101-
date_modified=dt.now(),
102-
)
103-
)
104-
>>>>>>> 8f17c362203a368c5860599b75c2e89e6c8c1fc5:tagstudio/src/core/utils/refresh_dir.py
10569
self.library.add_entries(entries)
10670

10771
self.files_not_in_library = []
@@ -152,19 +116,7 @@ def refresh_dir(self, lib_path: Path) -> Iterator[int]:
152116
relative_path = f.relative_to(lib_path)
153117
# TODO - load these in batch somehow
154118
if not self.library.has_path_entry(relative_path):
155-
<<<<<<< HEAD:src/tagstudio/core/utils/refresh_dir.py
156119
self.files_not_in_library.append(f)
157-
=======
158-
self.files_not_in_library.append(relative_path)
159-
else:
160-
# Update date_modified for existing entries if it has changed
161-
entry = self.library.get_entry_by_path(relative_path)
162-
if entry:
163-
date_modified = dt.fromtimestamp(f.stat().st_mtime, dt.timezone.utc)
164-
if entry.date_modified != date_modified:
165-
entry.date_modified = date_modified
166-
self.library.update_entry(entry)
167-
>>>>>>> 8f17c362203a368c5860599b75c2e89e6c8c1fc5:tagstudio/src/core/utils/refresh_dir.py
168120

169121
end_time_total = time()
170122
yield dir_file_count

0 commit comments

Comments
 (0)