Add support for re-importing added dates.

When re-importing, `item.added` and `album.added` will be preserved if
their path match what they are replacing.
This commit is contained in:
Stig Inge Lea Bjørnsen 2014-09-07 16:57:09 +02:00
parent 3b017a6f1e
commit 249748db12
2 changed files with 27 additions and 3 deletions

View file

@ -404,8 +404,10 @@ class Model(object):
associated with a database; you can provide one via the `db`
parameter or use the currently associated database.
The object's `id` and `added` fields are set along with any
The object's `id` field is set along with any
current field values.
The object's `added` field is given a value if it is missing.
"""
if db:
self._db = db
@ -416,7 +418,8 @@ class Model(object):
'INSERT INTO {0} DEFAULT VALUES'.format(self._table)
)
self.id = new_id
self.added = time.time()
if not hasattr(self, 'added') or not self.added:
self.added = time.time()
# Mark every non-null field as dirty and store.
for key in self:

View file

@ -620,14 +620,23 @@ class ImportTask(object):
with lib.transaction():
self.remove_replaced(lib)
self.album = lib.add_album(self.imported_items())
replaced_album = self.replaced_albums.get(self.album.path)
if replaced_album:
self.album.added = replaced_album.added
log.debug('reimported added date %s from album %i for %s',
self.album.added, replaced_album.id,
self.album.path)
def remove_replaced(self, lib):
"""Removes all the items from the library that have the same
path as an item from this task.
Records the replaced items in the `replaced_items` dictionary
Records the replaced items and albums in the `replaced_items`
and `replaced_albums` dictionaries.
"""
self.replaced_items = defaultdict(list)
self.replaced_albums = defaultdict(list)
replaced_album_ids = set()
for item in self.imported_items():
dup_items = list(lib.items(
dbcore.query.BytesQuery('path', item.path)
@ -637,6 +646,18 @@ class ImportTask(object):
log.debug('replacing item %i: %s' %
(dup_item.id, displayable_path(item.path)))
dup_item.remove()
if dup_item.added and not item.added:
item.added = dup_item.added
log.debug('reimported added date %s from item %i for %s',
item.added, dup_item.id, item.path)
if (dup_item.album_id and
not dup_item.album_id in replaced_album_ids):
album_query = dbcore.query.MatchQuery('id',
dup_item.album_id)
replaced_album = lib.albums(album_query).get()
if replaced_album:
replaced_album_ids.add(dup_item.album_id)
self.replaced_albums[replaced_album.path] = replaced_album
log.debug('%i of %i items replaced' % (len(self.replaced_items),
len(self.imported_items())))