Fix bug 1316979 by making the authors relation viewonly

Fixes: https://launchpad.net/bugs/1316979
This commit is contained in:
Samuel Mehrbrodt 2014-05-07 11:51:59 +02:00
parent fe52d50619
commit ef2a03d510
7 changed files with 40 additions and 14 deletions

View File

@ -400,7 +400,7 @@ class SongMaintenanceForm(QtGui.QDialog, Ui_SongMaintenanceDialog, RegistryPrope
"""
Merges two authors into one author.
:param old_author: The object, which was edited, that will be deleted
:param old_author: The object, which was edited, that will be deleted
"""
# Find the duplicate.
existing_author = self.manager.get_object_filtered(
@ -415,11 +415,9 @@ class SongMaintenanceForm(QtGui.QDialog, Ui_SongMaintenanceDialog, RegistryPrope
# Find the songs, which have the old_author as author.
songs = self.manager.get_all_objects(Song, Song.authors.contains(old_author))
for song in songs:
# We check if the song has already existing_author as author. If
# that is not the case we add it.
if existing_author not in song.authors:
song.authors.append(existing_author)
song.authors.remove(old_author)
for author_song in song.authors_songs:
song.add_author(existing_author, author_song.author_type)
song.remove_author(old_author, author_song.author_type)
self.manager.save_object(song)
self.manager.delete_object(Author, old_author.id)

View File

@ -390,12 +390,12 @@ def clean_song(manager, song):
verses = SongXML().get_verses(song.lyrics)
song.search_lyrics = ' '.join([clean_string(verse[1]) for verse in verses])
# The song does not have any author, add one.
if not song.authors and not song.authors_songs: # Need to check both relations
if not song.authors_songs:
name = SongStrings.AuthorUnknown
author = manager.get_object_filtered(Author, Author.display_name == name)
if author is None:
author = Author.populate(display_name=name, last_name='', first_name='')
song.authors.append(author)
song.add_author(author)
if song.copyright:
song.copyright = CONTROL_CHARS.sub('', song.copyright).strip()

View File

@ -114,6 +114,33 @@ class Song(BaseModel):
"""
self.sort_key = get_natural_key(self.title)
def add_author(self, author, author_type=None, ignore_type=False):
"""
Add an author to the song if it not yet exists
:return: True if the author has been added successfully. False if it was already there.
"""
for author_song in self.authors_songs:
if author_song.author == author and (ignore_type or author_song.author_type == author_type):
return False
new_author_song = AuthorSong()
new_author_song.author = author
new_author_song.author_type = author_type
self.authors_songs.append(new_author_song)
return True
def remove_author(self, author, author_type=None, ignore_type=False):
"""
Remove an existing author from the song
:return: True if the author has been removed successfully. False if it could not be found.
"""
for author_song in self.authors_songs:
if author_song.author == author and (ignore_type or author_song.author_type == author_type):
self.authors_songs.remove(author_song)
return True
return False
class Topic(BaseModel):
"""
@ -283,9 +310,10 @@ def init_schema(url):
mapper(Book, song_books_table)
mapper(MediaFile, media_files_table)
mapper(Song, songs_table, properties={
# Use the authors_songs relation when you need access to the 'author_type' attribute.
# Use the authors_songs relation when you need access to the 'author_type' attribute
# or when creating new relations
'authors_songs': relation(AuthorSong, cascade="all, delete-orphan"),
'authors': relation(Author, secondary=authors_songs_table),
'authors': relation(Author, secondary=authors_songs_table, viewonly=True),
'book': relation(Book, backref='songs'),
'media_files': relation(MediaFile, backref='songs', order_by=media_files_table.c.weight),
'topics': relation(Topic, backref='songs', secondary=songs_topics_table)

View File

@ -343,7 +343,7 @@ class FoilPresenter(object):
author = Author.populate(display_name=display_name, last_name=display_name.split(' ')[-1],
first_name=' '.join(display_name.split(' ')[:-1]))
self.manager.save_object(author)
song.authors.append(author)
song.add_author(author)
def _process_cclinumber(self, foilpresenterfolie, song):
"""

View File

@ -187,7 +187,7 @@ class OpenLPSongImport(SongImport):
first_name=author.first_name,
last_name=author.last_name,
display_name=author.display_name)
new_song.authors.append(existing_author)
new_song.authors.add_author(existing_author)
if song.book:
existing_song_book = self.manager.get_object_filtered(Book, Book.name == song.book.name)
if existing_song_book is None:

View File

@ -325,7 +325,7 @@ class SongImport(QtCore.QObject):
author = Author.populate(display_name=author_text,
last_name=author_text.split(' ')[-1],
first_name=' '.join(author_text.split(' ')[:-1]))
song.authors.append(author)
song.authors.add_author(author)
if self.song_book_name:
song_book = self.manager.get_object_filtered(Book, Book.name == self.song_book_name)
if song_book is None:

View File

@ -203,6 +203,6 @@ class SongSelectImport(object):
author = Author.populate(first_name=author_name.rsplit(' ', 1)[0],
last_name=author_name.rsplit(' ', 1)[1],
display_name=author_name)
db_song.authors.append(author)
db_song.authors.add_author(author)
self.db_manager.save_object(db_song)
return db_song