Extract method to delete a song.

This commit is contained in:
Patrick Zimmermann 2013-06-14 22:20:26 +02:00
parent b172153b15
commit 2f55624977
3 changed files with 37 additions and 32 deletions

View File

@ -38,6 +38,7 @@ from PyQt4 import QtCore, QtGui
from openlp.core.lib import Registry, translate
from openlp.core.ui.wizard import OpenLPWizard, WizardStrings
from openlp.core.utils import AppLocation
from openlp.plugins.songs.lib import delete_song
from openlp.plugins.songs.lib.db import Song, MediaFile
from openlp.plugins.songs.forms.songreviewwidget import SongReviewWidget
from openlp.plugins.songs.lib.songcompare import songs_probably_equal
@ -273,23 +274,7 @@ class DuplicateSongRemovalForm(OpenLPWizard):
# Remove song from duplicate song list.
self.duplicate_song_list[-1].remove(song_review_widget.song)
# Remove song from the database.
item_id = song_review_widget.song.id
media_files = self.plugin.manager.get_all_objects(MediaFile,
MediaFile.song_id == item_id)
for media_file in media_files:
try:
os.remove(media_file.file_name)
except:
log.exception(u'Could not remove file: %s',
media_file.file_name)
try:
save_path = os.path.join(AppLocation.get_section_data_path(
self.plugin.name), u'audio', str(item_id))
if os.path.exists(save_path):
os.rmdir(save_path)
except OSError:
log.exception(u'Could not remove directory: %s', save_path)
self.plugin.manager.delete_object(Song, item_id)
delete_song(song_review_widget.song.id, self.plugin)
# Remove GUI elements for the song.
self.review_scroll_area_layout.removeWidget(song_review_widget)
song_review_widget.setParent(None)

View File

@ -29,15 +29,21 @@
"""
The :mod:`~openlp.plugins.songs.lib` module contains a number of library functions and classes used in the Songs plugin.
"""
import logging
import os
import re
from PyQt4 import QtGui
from openlp.core.lib import translate
from openlp.core.utils import CONTROL_CHARS
from openlp.core.utils import AppLocation, CONTROL_CHARS
from openlp.plugins.songs.lib.db import MediaFile, Song
from db import Author
from ui import SongStrings
log = logging.getLogger(__name__)
WHITESPACE = re.compile(r'[\W_]+', re.UNICODE)
APOSTROPHE = re.compile(u'[\'`ʻ]', re.UNICODE)
PATTERN = re.compile(r"\\([a-z]{1,32})(-?\d{1,10})?[ ]?|\\'([0-9a-f]{2})|\\([^a-z])|([{}])|[\r\n]+|(.)", re.I)
@ -593,3 +599,29 @@ def strip_rtf(text, default_encoding=None):
text = u''.join(out)
return text, default_encoding
def delete_song(song_id, song_plugin):
"""
Deletes a song from the database. Media files associated to the song
are removed prior to the deletion of the song.
``song_id``
The ID of the song to delete.
``song_plugin``
The song plugin instance.
"""
media_files = song_plugin.manager.get_all_objects(MediaFile, MediaFile.song_id == song_id)
for media_file in media_files:
try:
os.remove(media_file.file_name)
except:
log.exception('Could not remove file: %s', media_file.file_name)
try:
save_path = os.path.join(AppLocation.get_section_data_path(song_plugin.name), 'audio', str(song_id))
if os.path.exists(save_path):
os.rmdir(save_path)
except OSError:
log.exception(u'Could not remove directory: %s', save_path)
song_plugin.manager.delete_object(Song, song_id)

View File

@ -43,7 +43,7 @@ from openlp.plugins.songs.forms.editsongform import EditSongForm
from openlp.plugins.songs.forms.songmaintenanceform import SongMaintenanceForm
from openlp.plugins.songs.forms.songimportform import SongImportForm
from openlp.plugins.songs.forms.songexportform import SongExportForm
from openlp.plugins.songs.lib import VerseType, clean_string
from openlp.plugins.songs.lib import VerseType, clean_string, delete_song
from openlp.plugins.songs.lib.db import Author, Song, Book, MediaFile
from openlp.plugins.songs.lib.ui import SongStrings
from openlp.plugins.songs.lib.xml import OpenLyrics, SongXML
@ -368,19 +368,7 @@ class SongMediaItem(MediaManagerItem):
self.main_window.display_progress_bar(len(items))
for item in items:
item_id = item.data(QtCore.Qt.UserRole)
media_files = self.plugin.manager.get_all_objects(MediaFile, MediaFile.song_id == item_id)
for media_file in media_files:
try:
os.remove(media_file.file_name)
except:
log.exception('Could not remove file: %s', media_file.file_name)
try:
save_path = os.path.join(AppLocation.get_section_data_path(self.plugin.name), 'audio', str(item_id))
if os.path.exists(save_path):
os.rmdir(save_path)
except OSError:
log.exception(u'Could not remove directory: %s', save_path)
self.plugin.manager.delete_object(Song, item_id)
delete_song(item_id, self.plugin)
self.main_window.increment_progress_bar()
self.main_window.finished_progress_bar()
self.application.set_normal_cursor()