SongUsage cleanup

This commit is contained in:
Tim Bentley 2011-01-28 18:41:37 +00:00
parent ab092e0124
commit 139cf93541
3 changed files with 34 additions and 14 deletions

View File

@ -315,7 +315,7 @@ def check_directory_exists(dir):
``dir``
Theme directory to make sure exists
"""
log.debug(u'check_directory_exists')
log.debug(u'check_directory_exists %s' % dir)
if not os.path.exists(dir):
os.makedirs(dir)

View File

@ -60,10 +60,11 @@ class Ui_SongUsageDetailDialog(object):
self.horizontalLayout.setObjectName(u'horizontalLayout')
self.fileLineEdit = QtGui.QLineEdit(self.fileGroupBox)
self.fileLineEdit.setObjectName(u'fileLineEdit')
self.fileLineEdit.setReadOnly(True)
self.horizontalLayout.addWidget(self.fileLineEdit)
self.saveFilePushButton = QtGui.QPushButton(self.fileGroupBox)
self.saveFilePushButton.setIcon(
build_icon(u':/general/general_load.png'))
build_icon(u':/general/general_open.png'))
self.saveFilePushButton.setObjectName(u'saveFilePushButton')
self.horizontalLayout.addWidget(self.saveFilePushButton)
self.verticalLayout4.addLayout(self.horizontalLayout)
@ -96,4 +97,4 @@ class Ui_SongUsageDetailDialog(object):
translate('SongUsagePlugin.SongUsageDetailForm', 'to'))
self.fileGroupBox.setTitle(
translate('SongUsagePlugin.SongUsageDetailForm',
'Report Location'))
'Report Location'))

View File

@ -30,7 +30,7 @@ import os
from PyQt4 import QtCore, QtGui
from sqlalchemy.sql import and_
from openlp.core.lib import SettingsManager, translate
from openlp.core.lib import SettingsManager, translate, check_directory_exists
from openlp.plugins.songusage.lib.db import SongUsageItem
from songusagedetaildialog import Ui_SongUsageDetailDialog
@ -51,49 +51,68 @@ class SongUsageDetailForm(QtGui.QDialog, Ui_SongUsageDetailDialog):
self.setupUi(self)
def initialise(self):
"""
We need to set up the screen
"""
year = QtCore.QDate().currentDate().year()
if QtCore.QDate().currentDate().month() < 9:
year -= 1
toDate = QtCore.QDate(year, 8, 31)
fromDate = QtCore.QDate(year - 1, 9, 1)
toDate = QtCore.QSettings().value(
u'songusage/to date',
QtCore.QVariant(QtCore.QDate(year, 8, 31))).toDate()
fromDate = QtCore.QSettings().value(
u'songusage/from date',
QtCore.QVariant(QtCore.QDate(year - 1, 9, 1))).toDate()
self.fromDate.setSelectedDate(fromDate)
self.toDate.setSelectedDate(toDate)
self.fileLineEdit.setText(
SettingsManager.get_last_dir(self.plugin.settingsSection, 1))
def defineOutputLocation(self):
"""
Triggered when the Directory selection button is pressed
"""
path = QtGui.QFileDialog.getExistingDirectory(self,
translate('SongUsagePlugin.SongUsageDetailForm',
'Output File Location'),
SettingsManager.get_last_dir(self.plugin.settingsSection, 1))
path = unicode(path)
if path != u'':
if path:
SettingsManager.set_last_dir(self.plugin.settingsSection, path, 1)
self.fileLineEdit.setText(path)
def accept(self):
log.debug(u'Detailed report generated')
"""
Ok was pressed so lets save the data and run the report
"""
log.debug(u'accept')
path = unicode(self.fileLineEdit.text())
check_directory_exists(path)
filename = unicode(translate('SongUsagePlugin.SongUsageDetailForm',
'usage_detail_%s_%s.txt')) % (
self.fromDate.selectedDate().toString(u'ddMMyyyy'),
self.toDate.selectedDate().toString(u'ddMMyyyy'))
QtCore.QSettings().setValue(u'songusage/from date',
QtCore.QVariant(self.fromDate.selectedDate()))
QtCore.QSettings().setValue(u'songusage/to date',
QtCore.QVariant(self.toDate.selectedDate()))
usage = self.plugin.manager.get_all_objects(
SongUsageItem, and_(
SongUsageItem.usagedate >= self.fromDate.selectedDate().toPyDate(),
SongUsageItem.usagedate < self.toDate.selectedDate().toPyDate()),
[SongUsageItem.usagedate, SongUsageItem.usagetime])
outname = os.path.join(unicode(self.fileLineEdit.text()), filename)
file = None
outname = os.path.join(path, filename)
fileHandle = None
try:
file = open(outname, u'w')
fileHandle = open(outname, u'w')
for instance in usage:
record = u'\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\"\n' % (
instance.usagedate, instance.usagetime, instance.title,
instance.copyright, instance.ccl_number, instance.authors)
file.write(record)
fileHandle.write(record.encode(u'utf-8'))
except IOError:
log.exception(u'Failed to write out song usage records')
finally:
if file:
file.close()
if fileHandle:
fileHandle.close()
self.close()