Improve song data integrity

bzr-revno: 934
This commit is contained in:
Andreas Preikschat 2010-07-12 17:48:54 +01:00 committed by Jon Tibble
commit 13c0496553
1 changed files with 151 additions and 57 deletions

View File

@ -24,6 +24,7 @@
############################################################################### ###############################################################################
from PyQt4 import QtGui, QtCore from PyQt4 import QtGui, QtCore
from sqlalchemy.sql import and_
from openlp.core.lib import translate from openlp.core.lib import translate
from openlp.plugins.songs.forms import AuthorsForm, TopicsForm, SongBookForm from openlp.plugins.songs.forms import AuthorsForm, TopicsForm, SongBookForm
@ -97,6 +98,9 @@ class SongMaintenanceForm(QtGui.QDialog, Ui_SongMaintenanceDialog):
QtGui.QMessageBox.critical(self, dlg_title, sel_text) QtGui.QMessageBox.critical(self, dlg_title, sel_text)
def resetAuthors(self): def resetAuthors(self):
"""
Reloads the Authors list.
"""
self.AuthorsListWidget.clear() self.AuthorsListWidget.clear()
authors = self.songmanager.get_all_objects(Author, Author.display_name) authors = self.songmanager.get_all_objects(Author, Author.display_name)
for author in authors: for author in authors:
@ -109,6 +113,9 @@ class SongMaintenanceForm(QtGui.QDialog, Ui_SongMaintenanceDialog):
self.AuthorsListWidget.addItem(author_name) self.AuthorsListWidget.addItem(author_name)
def resetTopics(self): def resetTopics(self):
"""
Reloads the Topics list.
"""
self.TopicsListWidget.clear() self.TopicsListWidget.clear()
topics = self.songmanager.get_all_objects(Topic, Topic.name) topics = self.songmanager.get_all_objects(Topic, Topic.name)
for topic in topics: for topic in topics:
@ -117,13 +124,88 @@ class SongMaintenanceForm(QtGui.QDialog, Ui_SongMaintenanceDialog):
self.TopicsListWidget.addItem(topic_name) self.TopicsListWidget.addItem(topic_name)
def resetBooks(self): def resetBooks(self):
"""
Reloads the Books list.
"""
self.BooksListWidget.clear() self.BooksListWidget.clear()
books = self.songmanager.get_all_objects(Book, Book.name) books = self.songmanager.get_all_objects(Book, Book.name)
for book in books: for book in books:
book_name = QtGui.QListWidgetItem(book.name) book_name = QtGui.QListWidgetItem(u'%s (%s)' % (book.name,
book.publisher))
book_name.setData(QtCore.Qt.UserRole, QtCore.QVariant(book.id)) book_name.setData(QtCore.Qt.UserRole, QtCore.QVariant(book.id))
self.BooksListWidget.addItem(book_name) self.BooksListWidget.addItem(book_name)
def checkAuthor(self, new_author, edit=False):
"""
Returns False when the given Author is already in the list elsewise
True.
"""
authors = self.songmanager.get_all_objects_filtered(Author,
and_(
Author.first_name == new_author.first_name,
Author.last_name == new_author.last_name,
Author.display_name == new_author.display_name
)
)
if len(authors) > 0:
# If we edit an existing Author, we need to make sure that we do
# not return False when nothing has changed (because this would
# cause an error message later on).
if edit:
if authors[0].id == new_author.id:
return True
else:
return False
else:
return False
else:
return True
def checkTopic(self, new_topic, edit=False):
"""
Returns False when the given Topic is already in the list elsewise True.
"""
topics = self.songmanager.get_all_objects_filtered(Topic,
Topic.name == new_topic.name
)
if len(topics) > 0:
# If we edit an existing Topic, we need to make sure that we do
# not return False when nothing has changed (because this would
# cause an error message later on).
if edit:
if topics[0].id == new_topic.id:
return True
else:
return False
else:
return False
else:
return True
def checkBook(self, new_book, edit=False):
"""
Returns False when the given Book is already in the list elsewise True.
"""
books = self.songmanager.get_all_objects_filtered(Book,
and_(
Book.name == new_book.name,
Book.publisher == new_book.publisher
)
)
if len(books) > 0:
# If we edit an existing Book, we need to make sure that we do
# not return False when nothing has changed (because this would
# cause an error message later on).
if edit:
if books[0].id == new_book.id:
return True
else:
return False
else:
return False
else:
return True
def onAuthorAddButtonClick(self): def onAuthorAddButtonClick(self):
self.authorform.setAutoDisplayName(True) self.authorform.setAutoDisplayName(True)
if self.authorform.exec_(): if self.authorform.exec_():
@ -131,86 +213,93 @@ class SongMaintenanceForm(QtGui.QDialog, Ui_SongMaintenanceDialog):
first_name=unicode(self.authorform.FirstNameEdit.text()), first_name=unicode(self.authorform.FirstNameEdit.text()),
last_name=unicode(self.authorform.LastNameEdit.text()), last_name=unicode(self.authorform.LastNameEdit.text()),
display_name=unicode(self.authorform.DisplayEdit.text())) display_name=unicode(self.authorform.DisplayEdit.text()))
if self.songmanager.save_object(author): if self.checkAuthor(author):
self.resetAuthors() if self.songmanager.save_object(author):
self.resetAuthors()
else: else:
QtGui.QMessageBox.critical( QtGui.QMessageBox.critical(self,
self, translate('SongsPlugin.SongMaintenanceForm', translate('SongsPlugin.SongMaintenanceForm', 'Error'),
'Error'),
translate('SongsPlugin.SongMaintenanceForm', translate('SongsPlugin.SongMaintenanceForm',
'Couldn\'t add your author.')) 'Could not add your author.'))
def onTopicAddButtonClick(self): def onTopicAddButtonClick(self):
if self.topicform.exec_(): if self.topicform.exec_():
topic = Topic.populate(name=unicode(self.topicform.NameEdit.text())) topic = Topic.populate(name=unicode(self.topicform.NameEdit.text()))
if self.songmanager.save_object(topic): if self.checkTopic(topic):
self.resetTopics() if self.songmanager.save_object(topic):
self.resetTopics()
else: else:
QtGui.QMessageBox.critical( QtGui.QMessageBox.critical(self,
self, translate('SongsPlugin.SongMaintenanceForm', translate('SongsPlugin.SongMaintenanceForm', 'Error'),
'Error'),
translate('SongsPlugin.SongMaintenanceForm', translate('SongsPlugin.SongMaintenanceForm',
'Couldn\'t add your topic.')) 'Could not add your topic.'))
def onBookAddButtonClick(self): def onBookAddButtonClick(self):
if self.bookform.exec_(): if self.bookform.exec_():
book = Book.populate( book = Book.populate(
name=unicode(self.bookform.NameEdit.text()), name=unicode(self.bookform.NameEdit.text()),
publisher=unicode(self.bookform.PublisherEdit.text())) publisher=unicode(self.bookform.PublisherEdit.text()))
if self.songmanager.save_object(book): if self.checkBook(book):
self.resetBooks() if self.songmanager.save_object(book):
self.resetBooks()
else: else:
QtGui.QMessageBox.critical( QtGui.QMessageBox.critical(self,
self, translate('SongsPlugin.SongMaintenanceForm', translate('SongsPlugin.SongMaintenanceForm', 'Error'),
'Error'),
translate('SongsPlugin.SongMaintenanceForm', translate('SongsPlugin.SongMaintenanceForm',
'Couldn\'t add your book.')) 'Could not add your book.'))
def onAuthorEditButtonClick(self): def onAuthorEditButtonClick(self):
author_id = self._getCurrentItemId(self.AuthorsListWidget) author_id = self._getCurrentItemId(self.AuthorsListWidget)
if author_id != -1: if author_id != -1:
author = self.songmanager.get_object(Author, author_id) author = self.songmanager.get_object(Author, author_id)
# Just make sure none of the fields is None
if author.first_name is None:
author.first_name = u''
if author.last_name is None:
author.last_name = u''
if author.display_name is None:
author.display_name = u''
self.authorform.setAutoDisplayName(False) self.authorform.setAutoDisplayName(False)
self.authorform.FirstNameEdit.setText(author.first_name) self.authorform.FirstNameEdit.setText(author.first_name)
self.authorform.LastNameEdit.setText(author.last_name) self.authorform.LastNameEdit.setText(author.last_name)
self.authorform.DisplayEdit.setText(author.display_name) self.authorform.DisplayEdit.setText(author.display_name)
# Save the author's first and last name as well as the display name
# for the case that they have to be restored.
temp_first_name = author.first_name
temp_last_name = author.last_name
temp_display_name = author.display_name
if self.authorform.exec_(False): if self.authorform.exec_(False):
author.first_name = unicode( author.first_name = unicode(
self.authorform.FirstNameEdit.text()) self.authorform.FirstNameEdit.text())
author.last_name = unicode(self.authorform.LastNameEdit.text()) author.last_name = unicode(self.authorform.LastNameEdit.text())
author.display_name = unicode( author.display_name = unicode(
self.authorform.DisplayEdit.text()) self.authorform.DisplayEdit.text())
if self.songmanager.save_object(author): if self.checkAuthor(author, True):
self.resetAuthors() if self.songmanager.save_object(author):
self.resetAuthors()
else: else:
QtGui.QMessageBox.critical( # We restore the author's old first and last name as well as
self, translate('SongsPlugin.SongMaintenanceForm', # his display name.
'Error'), author.first_name = temp_first_name
author.last_name = temp_last_name
author.display_name = temp_display_name
QtGui.QMessageBox.critical(self,
translate('SongsPlugin.SongMaintenanceForm', 'Error'),
translate('SongsPlugin.SongMaintenanceForm', translate('SongsPlugin.SongMaintenanceForm',
'Couldn\'t save your author.')) 'Could not save your author.'))
def onTopicEditButtonClick(self): def onTopicEditButtonClick(self):
topic_id = self._getCurrentItemId(self.TopicsListWidget) topic_id = self._getCurrentItemId(self.TopicsListWidget)
if topic_id != -1: if topic_id != -1:
topic = self.songmanager.get_object(Topic, topic_id) topic = self.songmanager.get_object(Topic, topic_id)
self.topicform.NameEdit.setText(topic.name) self.topicform.NameEdit.setText(topic.name)
# Save the topic's name for the case that he has to be restored.
temp_name = topic.name
if self.topicform.exec_(False): if self.topicform.exec_(False):
topic.name = unicode(self.topicform.NameEdit.text()) topic.name = unicode(self.topicform.NameEdit.text())
if self.songmanager.save_object(topic): if self.checkTopic(topic, True):
self.resetTopics() if self.songmanager.save_object(topic):
self.resetTopics()
else: else:
QtGui.QMessageBox.critical( # We restore the topics's old name.
self, translate('SongsPlugin.SongMaintenanceForm', topic.name = temp_name
'Error'), QtGui.QMessageBox.critical(self,
translate('SongsPlugin.SongMaintenanceForm', 'Error'),
translate('SongsPlugin.SongMaintenanceForm', translate('SongsPlugin.SongMaintenanceForm',
'Couldn\'t save your topic.')) 'Could not save your topic.'))
def onBookEditButtonClick(self): def onBookEditButtonClick(self):
book_id = self._getCurrentItemId(self.BooksListWidget) book_id = self._getCurrentItemId(self.BooksListWidget)
@ -218,17 +307,24 @@ class SongMaintenanceForm(QtGui.QDialog, Ui_SongMaintenanceDialog):
book = self.songmanager.get_object(Book, book_id) book = self.songmanager.get_object(Book, book_id)
self.bookform.NameEdit.setText(book.name) self.bookform.NameEdit.setText(book.name)
self.bookform.PublisherEdit.setText(book.publisher) self.bookform.PublisherEdit.setText(book.publisher)
# Save the book's name and publisher for the case that they have to
# be restored.
temp_name = book.name
temp_publisher = book.publisher
if self.bookform.exec_(False): if self.bookform.exec_(False):
book.name = unicode(self.bookform.NameEdit.text()) book.name = unicode(self.bookform.NameEdit.text())
book.publisher = unicode(self.bookform.PublisherEdit.text()) book.publisher = unicode(self.bookform.PublisherEdit.text())
if self.songmanager.save_object(book): if self.checkBook(book, True):
self.resetBooks() if self.songmanager.save_object(book):
self.resetBooks()
else: else:
QtGui.QMessageBox.critical( # We restore the book's old name and publisher.
self, translate('SongsPlugin.SongMaintenanceForm', book.name = temp_name
'Error'), book.publisher = temp_publisher
QtGui.QMessageBox.critical(self,
translate('SongsPlugin.SongMaintenanceForm', 'Error'),
translate('SongsPlugin.SongMaintenanceForm', translate('SongsPlugin.SongMaintenanceForm',
'Couldn\'t save your book.')) 'Could not save your book.'))
def onAuthorDeleteButtonClick(self): def onAuthorDeleteButtonClick(self):
""" """
@ -239,10 +335,9 @@ class SongMaintenanceForm(QtGui.QDialog, Ui_SongMaintenanceDialog):
translate('SongsPlugin.SongMaintenanceForm', translate('SongsPlugin.SongMaintenanceForm',
'Are you sure you want to delete the selected author?'), 'Are you sure you want to delete the selected author?'),
translate('SongsPlugin.SongMaintenanceForm', translate('SongsPlugin.SongMaintenanceForm',
'This author can\'t be deleted, they are currently ' 'This author cannot be deleted, they are currently '
'assigned to at least one song.'), 'assigned to at least one song.'),
translate('SongsPlugin.SongMaintenanceForm', translate('SongsPlugin.SongMaintenanceForm', 'No author selected!'))
'No author selected!'))
def onTopicDeleteButtonClick(self): def onTopicDeleteButtonClick(self):
""" """
@ -253,10 +348,9 @@ class SongMaintenanceForm(QtGui.QDialog, Ui_SongMaintenanceDialog):
translate('SongsPlugin.SongMaintenanceForm', translate('SongsPlugin.SongMaintenanceForm',
'Are you sure you want to delete the selected topic?'), 'Are you sure you want to delete the selected topic?'),
translate('SongsPlugin.SongMaintenanceForm', translate('SongsPlugin.SongMaintenanceForm',
'This topic can\'t be deleted, it is currently ' 'This topic cannot be deleted, it is currently '
'assigned to at least one song.'), 'assigned to at least one song.'),
translate('SongsPlugin.SongMaintenanceForm', translate('SongsPlugin.SongMaintenanceForm', 'No topic selected!'))
'No topic selected!'))
def onBookDeleteButtonClick(self): def onBookDeleteButtonClick(self):
""" """
@ -267,6 +361,6 @@ class SongMaintenanceForm(QtGui.QDialog, Ui_SongMaintenanceDialog):
translate('SongsPlugin.SongMaintenanceForm', translate('SongsPlugin.SongMaintenanceForm',
'Are you sure you want to delete the selected book?'), 'Are you sure you want to delete the selected book?'),
translate('SongsPlugin.SongMaintenanceForm', translate('SongsPlugin.SongMaintenanceForm',
'This book can\'t be deleted, it is currently ' 'This book cannot be deleted, it is currently '
'assigned to at least one song.'), 'assigned to at least one song.'),
translate('SongsPlugin.SongMaintenanceForm', u'No book selected!')) translate('SongsPlugin.SongMaintenanceForm', 'No book selected!'))