diff --git a/beets/library.py b/beets/library.py index 34b8684f7..399048612 100644 --- a/beets/library.py +++ b/beets/library.py @@ -24,7 +24,7 @@ import unicodedata import threading import contextlib import traceback -import datetime +import time from collections import defaultdict from unidecode import unidecode from beets.mediafile import MediaFile @@ -106,10 +106,7 @@ ITEM_FIELDS = [ ('bitdepth', 'int', False, True), ('channels', 'int', False, True), ('mtime', 'int', False, False), - - ('year_added', 'int', False, False), - ('month_added', 'int', False, False), - ('day_added', 'int', False, False), + ('itime', 'int', False, False), ] ITEM_KEYS_WRITABLE = [f[0] for f in ITEM_FIELDS if f[3] and f[2]] ITEM_KEYS_META = [f[0] for f in ITEM_FIELDS if f[3]] @@ -152,9 +149,7 @@ ALBUM_FIELDS = [ ('original_month', 'int', True), ('original_day', 'int', True), - ('year_added', 'int', False), - ('month_added', 'int', False), - ('day_added', 'int', False), + ('itime', 'int', False), ] ALBUM_KEYS = [f[0] for f in ALBUM_FIELDS] ALBUM_KEYS_ITEM = [f[0] for f in ALBUM_FIELDS if f[2]] @@ -174,11 +169,6 @@ if not log.handlers: log.addHandler(logging.StreamHandler()) log.propagate = False # Don't propagate to root handler. -# Return a tuple for the current date (year, month, date) -def _date_tuple(): - date = datetime.datetime.now() - return (date.year, date.month, date.day) - # A little SQL utility. def _orelse(exp1, exp2): """Generates an SQLite expression that evaluates to exp1 if exp1 is @@ -1303,10 +1293,10 @@ class Library(BaseLibrary): # Item manipulation. def add(self, item, copy=False): - item.day_added, item.month_added, item.year_added = _date_tuple() - item.library = self - if copy: - self.move(item, copy=True) + item.itime = time.time() + item.library = self + if copy: + self.move(item, copy=True) # Build essential parts of query. columns = ','.join([key for key in ITEM_KEYS if key != 'id']) @@ -1510,21 +1500,21 @@ class Library(BaseLibrary): def add_album(self, items): """Create a new album in the database with metadata derived - from its items. The items are added to the database if they - don't yet have an ID. Returns an Album object. - """ - album_keys = ALBUM_KEYS_ITEM + ['day_added', 'month_added', 'year_added'] + from its items. The items are added to the database if they + don't yet have an ID. Returns an Album object. + """ + album_keys = ALBUM_KEYS_ITEM + ['itime'] - # Set the metadata from the first item. + # Set the metadata from the first item. album_values = dict( (key, getattr(items[0], key)) for key in ALBUM_KEYS_ITEM) # Manually set the date when the album was added, # because the items don't yet have these - album_values['day_added'], album_values['month_added'], album_values['year_added'] = _date_tuple() + album_values['itime'] = time.time() - with self.transaction() as tx: - sql = 'INSERT INTO albums (%s) VALUES (%s)' % \ + with self.transaction() as tx: + sql = 'INSERT INTO albums (%s) VALUES (%s)' % \ (', '.join(album_keys), ', '.join(['?'] * len(album_keys))) subvals = [album_values[key] for key in album_keys] diff --git a/test/test_db.py b/test/test_db.py index f7e7f4bdb..f6c94bba7 100644 --- a/test/test_db.py +++ b/test/test_db.py @@ -964,26 +964,23 @@ class MtimeTest(unittest.TestCase): def test_mtime_up_to_date_after_read(self): self.i.title = 'something else' - self.i.read() - self.assertGreaterEqual(self.i.mtime, self._mtime()) + self.i.read() + self.assertGreaterEqual(self.i.mtime, self._mtime()) -class AtimeTest(unittest.TestCase): +class ImportTimeTest(unittest.TestCase): def setUp(self): self.lib = beets.library.Library(':memory:') - def test_atime_for_album(self): + def test_itime_for_album(self): self.track = item() self.album = self.lib.add_album((self.track,)) - self.assertGreater(self.album.day_added, 0) - self.assertGreater(self.album.month_added, 0) - self.assertGreater(self.album.year_added, 0) + self.assertGreater(self.album.itime, 0) + self.assertGreater(self.track.itime, 0) def test_atime_for_singleton(self): self.singleton = item() self.lib.add(self.singleton) - self.assertGreater(self.singleton.day_added, 0) - self.assertGreater(self.singleton.month_added, 0) - self.assertGreater(self.singleton.year_added, 0) + self.assertGreater(self.singleton.itime, 0) def suite(): return unittest.TestLoader().loadTestsFromName(__name__)