Add Album.add_photos for batch adding with less savepoint waste.
Because each call to add_photo opens a savepoint, it is wasteful to use it for a large number of photos that belong together.
This commit is contained in:
parent
8fcaf15fbe
commit
94e518068f
2 changed files with 25 additions and 9 deletions
|
@ -282,6 +282,15 @@ class Album(ObjectBase, GroupableMixin):
|
||||||
self.photodb.log.debug('Committing - add associated directory')
|
self.photodb.log.debug('Committing - add associated directory')
|
||||||
self.photodb.commit()
|
self.photodb.commit()
|
||||||
|
|
||||||
|
def _add_photo(self, photo):
|
||||||
|
self.photodb.log.debug('Adding photo %s to %s', photo, self)
|
||||||
|
data = {
|
||||||
|
'albumid': self.id,
|
||||||
|
'photoid': photo.id,
|
||||||
|
}
|
||||||
|
self.photodb.sql_insert(table='album_photo_rel', data=data)
|
||||||
|
self._uncache_sums()
|
||||||
|
|
||||||
@decorators.required_feature('album.edit')
|
@decorators.required_feature('album.edit')
|
||||||
@decorators.transaction
|
@decorators.transaction
|
||||||
def add_photo(self, photo, *, commit=True):
|
def add_photo(self, photo, *, commit=True):
|
||||||
|
@ -290,18 +299,26 @@ class Album(ObjectBase, GroupableMixin):
|
||||||
if self.has_photo(photo):
|
if self.has_photo(photo):
|
||||||
return
|
return
|
||||||
|
|
||||||
self.photodb.log.debug('Adding photo %s to %s', photo, self)
|
self._add_photo(photo)
|
||||||
data = {
|
|
||||||
'albumid': self.id,
|
|
||||||
'photoid': photo.id,
|
|
||||||
}
|
|
||||||
self.photodb.sql_insert(table='album_photo_rel', data=data)
|
|
||||||
|
|
||||||
self._uncache_sums()
|
|
||||||
if commit:
|
if commit:
|
||||||
self.photodb.log.debug('Committing - add photo to album')
|
self.photodb.log.debug('Committing - add photo to album')
|
||||||
self.photodb.commit()
|
self.photodb.commit()
|
||||||
|
|
||||||
|
@decorators.required_feature('album.edit')
|
||||||
|
@decorators.transaction
|
||||||
|
def add_photos(self, photos, *, commit=True):
|
||||||
|
existing_photos = set(self.get_photos())
|
||||||
|
photos = set(photos)
|
||||||
|
photos = photos.difference(existing_photos)
|
||||||
|
|
||||||
|
for photo in photos:
|
||||||
|
self._add_photo(photo)
|
||||||
|
|
||||||
|
if commit:
|
||||||
|
self.photodb.log.debug('Committing - add photos to album')
|
||||||
|
self.photodb.commit()
|
||||||
|
|
||||||
# Photo.add_tag already has @required_feature
|
# Photo.add_tag already has @required_feature
|
||||||
@decorators.transaction
|
@decorators.transaction
|
||||||
def add_tag_to_all(self, tag, *, nested_children=True, commit=True):
|
def add_tag_to_all(self, tag, *, nested_children=True, commit=True):
|
||||||
|
|
|
@ -1294,8 +1294,7 @@ class PhotoDB(
|
||||||
current_album = create_or_fetch_current_album(albums_by_path, current_directory)
|
current_album = create_or_fetch_current_album(albums_by_path, current_directory)
|
||||||
orphan_join_parent_album(albums_by_path, current_album, current_directory)
|
orphan_join_parent_album(albums_by_path, current_album, current_directory)
|
||||||
|
|
||||||
for photo in photos:
|
current_album.add_photos(photos, commit=False)
|
||||||
current_album.add_photo(photo, commit=False)
|
|
||||||
|
|
||||||
if commit:
|
if commit:
|
||||||
self.log.debug('Committing - digest_directory')
|
self.log.debug('Committing - digest_directory')
|
||||||
|
|
Loading…
Reference in a new issue