forked from openlp/openlp
Database refactor
This commit is contained in:
parent
bf71742308
commit
f71f27c890
@ -178,36 +178,24 @@ class Manager(object):
|
|||||||
"""
|
"""
|
||||||
return self.session.query(object_class).filter(filter_clause).first()
|
return self.session.query(object_class).filter(filter_clause).first()
|
||||||
|
|
||||||
def get_all_objects(self, object_class, order_by_ref=None):
|
def get_all_objects(self, object_class, filter_clause=None,
|
||||||
|
order_by_ref=None):
|
||||||
"""
|
"""
|
||||||
Returns all the objects from the database
|
Returns all the objects from the database
|
||||||
|
|
||||||
``object_class``
|
``object_class``
|
||||||
The type of objects to return
|
The type of objects to return
|
||||||
|
|
||||||
|
``filter_clause``
|
||||||
|
The filter governing selection of objects to return. Defaults to
|
||||||
|
None.
|
||||||
|
|
||||||
``order_by_ref``
|
``order_by_ref``
|
||||||
Any parameters to order the returned objects by. Defaults to None.
|
Any parameters to order the returned objects by. Defaults to None.
|
||||||
"""
|
"""
|
||||||
query = self.session.query(object_class)
|
query = self.session.query(object_class)
|
||||||
if order_by_ref is not None:
|
if filter_clause:
|
||||||
return query.order_by(order_by_ref).all()
|
query = query.filter(filter_clause)
|
||||||
return query.all()
|
|
||||||
|
|
||||||
def get_all_objects_filtered(self, object_class, filter_clause,
|
|
||||||
order_by_ref=None):
|
|
||||||
"""
|
|
||||||
Returns a selection of objects from the database
|
|
||||||
|
|
||||||
``object_class``
|
|
||||||
The type of objects to return
|
|
||||||
|
|
||||||
``filter_clause``
|
|
||||||
The filter governing selection of objects to return
|
|
||||||
|
|
||||||
``order_by_ref``
|
|
||||||
Any parameters to order the returned objects by. Defaults to None.
|
|
||||||
"""
|
|
||||||
query = self.session.query(object_class).filter(filter_clause)
|
|
||||||
if order_by_ref is not None:
|
if order_by_ref is not None:
|
||||||
return query.order_by(order_by_ref).all()
|
return query.order_by(order_by_ref).all()
|
||||||
return query.all()
|
return query.all()
|
||||||
@ -235,7 +223,7 @@ class Manager(object):
|
|||||||
else:
|
else:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def delete_all_objects(self, object_class):
|
def delete_all_objects(self, object_class, filter_clause=None):
|
||||||
"""
|
"""
|
||||||
Delete all object records
|
Delete all object records
|
||||||
|
|
||||||
@ -243,11 +231,13 @@ class Manager(object):
|
|||||||
The type of object to delete
|
The type of object to delete
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
self.session.query(object_class).delete(synchronize_session=False)
|
query = self.session.query(object_class)
|
||||||
|
if filter_clause:
|
||||||
|
query = query.filter(filter_clause)
|
||||||
|
query.delete(synchronize_session=False)
|
||||||
self.session.commit()
|
self.session.commit()
|
||||||
return True
|
return True
|
||||||
except InvalidRequestError:
|
except InvalidRequestError:
|
||||||
self.session.rollback()
|
self.session.rollback()
|
||||||
log.exception(u'Failed to delete all %s records',
|
log.exception(u'Failed to delete %s records', object_class.__name__)
|
||||||
object_class.__name__)
|
|
||||||
return False
|
return False
|
||||||
|
@ -62,7 +62,8 @@ class AlertForm(QtGui.QDialog, Ui_AlertDialog):
|
|||||||
|
|
||||||
def loadList(self):
|
def loadList(self):
|
||||||
self.AlertListWidget.clear()
|
self.AlertListWidget.clear()
|
||||||
alerts = self.manager.get_all_objects(AlertItem, AlertItem.text)
|
alerts = self.manager.get_all_objects(AlertItem,
|
||||||
|
order_by_ref=AlertItem.text)
|
||||||
for alert in alerts:
|
for alert in alerts:
|
||||||
item_name = QtGui.QListWidgetItem(alert.text)
|
item_name = QtGui.QListWidgetItem(alert.text)
|
||||||
item_name.setData(QtCore.Qt.UserRole, QtCore.QVariant(alert.id))
|
item_name.setData(QtCore.Qt.UserRole, QtCore.QVariant(alert.id))
|
||||||
|
@ -198,7 +198,8 @@ class BibleManager(object):
|
|||||||
u'name': book.name,
|
u'name': book.name,
|
||||||
u'chapters': self.db_cache[bible].get_chapter_count(book.name)
|
u'chapters': self.db_cache[bible].get_chapter_count(book.name)
|
||||||
}
|
}
|
||||||
for book in self.db_cache[bible].get_all_objects(Book, Book.id)
|
for book in self.db_cache[bible].get_all_objects(Book,
|
||||||
|
order_by_ref=Book.id)
|
||||||
]
|
]
|
||||||
|
|
||||||
def get_chapter_count(self, bible, book):
|
def get_chapter_count(self, bible, book):
|
||||||
|
@ -75,7 +75,7 @@ class CustomPlugin(Plugin):
|
|||||||
|
|
||||||
Returns True if the theme is being used, otherwise returns False.
|
Returns True if the theme is being used, otherwise returns False.
|
||||||
"""
|
"""
|
||||||
if self.custommanager.get_all_objects_filtered(CustomSlide,
|
if self.custommanager.get_all_objects(CustomSlide,
|
||||||
CustomSlide.theme_name == theme):
|
CustomSlide.theme_name == theme):
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
@ -91,8 +91,8 @@ class CustomPlugin(Plugin):
|
|||||||
``newTheme``
|
``newTheme``
|
||||||
The new name the plugin should now use.
|
The new name the plugin should now use.
|
||||||
"""
|
"""
|
||||||
customsUsingTheme = self.custommanager.get_all_objects_filtered(
|
customsUsingTheme = self.custommanager.get_all_objects(CustomSlide,
|
||||||
CustomSlide, CustomSlide.theme_name == oldTheme)
|
CustomSlide.theme_name == oldTheme)
|
||||||
for custom in customsUsingTheme:
|
for custom in customsUsingTheme:
|
||||||
custom.theme_name = newTheme
|
custom.theme_name = newTheme
|
||||||
self.custommanager.save_object(custom)
|
self.custommanager.save_object(custom)
|
||||||
|
@ -73,7 +73,7 @@ class CustomMediaItem(MediaManagerItem):
|
|||||||
|
|
||||||
def initialise(self):
|
def initialise(self):
|
||||||
self.loadCustomListView(self.parent.custommanager.get_all_objects(
|
self.loadCustomListView(self.parent.custommanager.get_all_objects(
|
||||||
CustomSlide, CustomSlide.title))
|
CustomSlide, order_by_ref=CustomSlide.title))
|
||||||
#Called to redisplay the song list screen edith from a search
|
#Called to redisplay the song list screen edith from a search
|
||||||
#or from the exit of the Song edit dialog. If remote editing is active
|
#or from the exit of the Song edit dialog. If remote editing is active
|
||||||
#Trigger it and clean up so it will not update again.
|
#Trigger it and clean up so it will not update again.
|
||||||
|
@ -118,7 +118,8 @@ class EditSongForm(QtGui.QDialog, Ui_EditSongDialog):
|
|||||||
self.TopicRemoveButton.setEnabled(False)
|
self.TopicRemoveButton.setEnabled(False)
|
||||||
|
|
||||||
def loadAuthors(self):
|
def loadAuthors(self):
|
||||||
authors = self.songmanager.get_all_objects(Author, Author.display_name)
|
authors = self.songmanager.get_all_objects(Author,
|
||||||
|
order_by_ref=Author.display_name)
|
||||||
self.AuthorsSelectionComboItem.clear()
|
self.AuthorsSelectionComboItem.clear()
|
||||||
self.AuthorsSelectionComboItem.addItem(u'')
|
self.AuthorsSelectionComboItem.addItem(u'')
|
||||||
for author in authors:
|
for author in authors:
|
||||||
@ -128,7 +129,8 @@ class EditSongForm(QtGui.QDialog, Ui_EditSongDialog):
|
|||||||
row, QtCore.QVariant(author.id))
|
row, QtCore.QVariant(author.id))
|
||||||
|
|
||||||
def loadTopics(self):
|
def loadTopics(self):
|
||||||
topics = self.songmanager.get_all_objects(Topic, Topic.name)
|
topics = self.songmanager.get_all_objects(Topic,
|
||||||
|
order_by_ref=Topic.name)
|
||||||
self.SongTopicCombo.clear()
|
self.SongTopicCombo.clear()
|
||||||
self.SongTopicCombo.addItem(u'')
|
self.SongTopicCombo.addItem(u'')
|
||||||
for topic in topics:
|
for topic in topics:
|
||||||
@ -137,7 +139,7 @@ class EditSongForm(QtGui.QDialog, Ui_EditSongDialog):
|
|||||||
self.SongTopicCombo.setItemData(row, QtCore.QVariant(topic.id))
|
self.SongTopicCombo.setItemData(row, QtCore.QVariant(topic.id))
|
||||||
|
|
||||||
def loadBooks(self):
|
def loadBooks(self):
|
||||||
books = self.songmanager.get_all_objects(Book, Book.name)
|
books = self.songmanager.get_all_objects(Book, order_by_ref=Book.name)
|
||||||
self.SongbookCombo.clear()
|
self.SongbookCombo.clear()
|
||||||
self.SongbookCombo.addItem(u'')
|
self.SongbookCombo.addItem(u'')
|
||||||
for book in books:
|
for book in books:
|
||||||
|
@ -102,7 +102,8 @@ class SongMaintenanceForm(QtGui.QDialog, Ui_SongMaintenanceDialog):
|
|||||||
Reloads the Authors list.
|
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,
|
||||||
|
order_by_ref=Author.display_name)
|
||||||
for author in authors:
|
for author in authors:
|
||||||
if author.display_name:
|
if author.display_name:
|
||||||
author_name = QtGui.QListWidgetItem(author.display_name)
|
author_name = QtGui.QListWidgetItem(author.display_name)
|
||||||
@ -117,7 +118,8 @@ class SongMaintenanceForm(QtGui.QDialog, Ui_SongMaintenanceDialog):
|
|||||||
Reloads the Topics list.
|
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,
|
||||||
|
order_by_ref=Topic.name)
|
||||||
for topic in topics:
|
for topic in topics:
|
||||||
topic_name = QtGui.QListWidgetItem(topic.name)
|
topic_name = QtGui.QListWidgetItem(topic.name)
|
||||||
topic_name.setData(QtCore.Qt.UserRole, QtCore.QVariant(topic.id))
|
topic_name.setData(QtCore.Qt.UserRole, QtCore.QVariant(topic.id))
|
||||||
@ -128,7 +130,7 @@ class SongMaintenanceForm(QtGui.QDialog, Ui_SongMaintenanceDialog):
|
|||||||
Reloads the Books list.
|
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, order_by_ref=Book.name)
|
||||||
for book in books:
|
for book in books:
|
||||||
book_name = QtGui.QListWidgetItem(u'%s (%s)' % (book.name,
|
book_name = QtGui.QListWidgetItem(u'%s (%s)' % (book.name,
|
||||||
book.publisher))
|
book.publisher))
|
||||||
@ -140,7 +142,7 @@ class SongMaintenanceForm(QtGui.QDialog, Ui_SongMaintenanceDialog):
|
|||||||
Returns False if the given Author is already in the list otherwise
|
Returns False if the given Author is already in the list otherwise
|
||||||
True.
|
True.
|
||||||
"""
|
"""
|
||||||
authors = self.songmanager.get_all_objects_filtered(Author,
|
authors = self.songmanager.get_all_objects(Author,
|
||||||
and_(
|
and_(
|
||||||
Author.first_name == new_author.first_name,
|
Author.first_name == new_author.first_name,
|
||||||
Author.last_name == new_author.last_name,
|
Author.last_name == new_author.last_name,
|
||||||
@ -165,7 +167,7 @@ class SongMaintenanceForm(QtGui.QDialog, Ui_SongMaintenanceDialog):
|
|||||||
"""
|
"""
|
||||||
Returns False if the given Topic is already in the list otherwise True.
|
Returns False if the given Topic is already in the list otherwise True.
|
||||||
"""
|
"""
|
||||||
topics = self.songmanager.get_all_objects_filtered(Topic,
|
topics = self.songmanager.get_all_objects(Topic,
|
||||||
Topic.name == new_topic.name)
|
Topic.name == new_topic.name)
|
||||||
if len(topics) > 0:
|
if len(topics) > 0:
|
||||||
# If we edit an existing Topic, we need to make sure that we do
|
# If we edit an existing Topic, we need to make sure that we do
|
||||||
@ -185,7 +187,7 @@ class SongMaintenanceForm(QtGui.QDialog, Ui_SongMaintenanceDialog):
|
|||||||
"""
|
"""
|
||||||
Returns False if the given Book is already in the list otherwise True.
|
Returns False if the given Book is already in the list otherwise True.
|
||||||
"""
|
"""
|
||||||
books = self.songmanager.get_all_objects_filtered(Book,
|
books = self.songmanager.get_all_objects(Book,
|
||||||
and_(Book.name == new_book.name,
|
and_(Book.name == new_book.name,
|
||||||
Book.publisher == new_book.publisher))
|
Book.publisher == new_book.publisher))
|
||||||
if len(books) > 0:
|
if len(books) > 0:
|
||||||
|
@ -164,20 +164,20 @@ class SongMediaItem(MediaManagerItem):
|
|||||||
search_type = self.SearchTypeComboBox.currentIndex()
|
search_type = self.SearchTypeComboBox.currentIndex()
|
||||||
if search_type == 0:
|
if search_type == 0:
|
||||||
log.debug(u'Titles Search')
|
log.debug(u'Titles Search')
|
||||||
search_results = self.parent.manager.get_all_objects_filtered(Song,
|
search_results = self.parent.manager.get_all_objects(Song,
|
||||||
Song.search_title.like(u'%' + search_keywords + u'%'),
|
Song.search_title.like(u'%' + search_keywords + u'%'),
|
||||||
Song.search_title.asc())
|
Song.search_title.asc())
|
||||||
self.displayResultsSong(search_results)
|
self.displayResultsSong(search_results)
|
||||||
elif search_type == 1:
|
elif search_type == 1:
|
||||||
log.debug(u'Lyrics Search')
|
log.debug(u'Lyrics Search')
|
||||||
search_results = self.parent.manager.get_all_objects_filtered(Song,
|
search_results = self.parent.manager.get_all_objects(Song,
|
||||||
Song.search_lyrics.like(u'%' + search_keywords + u'%'),
|
Song.search_lyrics.like(u'%' + search_keywords + u'%'),
|
||||||
Song.search_lyrics.asc())
|
Song.search_lyrics.asc())
|
||||||
self.displayResultsSong(search_results)
|
self.displayResultsSong(search_results)
|
||||||
elif search_type == 2:
|
elif search_type == 2:
|
||||||
log.debug(u'Authors Search')
|
log.debug(u'Authors Search')
|
||||||
search_results = self.parent.manager.get_all_objects_filtered(
|
search_results = self.parent.manager.get_all_objects(Author,
|
||||||
Author, Author.display_name.like(u'%' + search_keywords + u'%'),
|
Author.display_name.like(u'%' + search_keywords + u'%'),
|
||||||
Author.display_name.asc())
|
Author.display_name.asc())
|
||||||
self.displayResultsAuthor(search_results)
|
self.displayResultsAuthor(search_results)
|
||||||
#Called to redisplay the song list screen edith from a search
|
#Called to redisplay the song list screen edith from a search
|
||||||
|
@ -69,7 +69,7 @@ class SongsPlugin(Plugin):
|
|||||||
log.info(u'Songs Initialising')
|
log.info(u'Songs Initialising')
|
||||||
Plugin.initialise(self)
|
Plugin.initialise(self)
|
||||||
self.mediaItem.displayResultsSong(
|
self.mediaItem.displayResultsSong(
|
||||||
self.manager.get_all_objects(Song, Song.title))
|
self.manager.get_all_objects(Song, order_by_ref=Song.title))
|
||||||
|
|
||||||
def getMediaManagerItem(self):
|
def getMediaManagerItem(self):
|
||||||
"""
|
"""
|
||||||
@ -198,8 +198,7 @@ class SongsPlugin(Plugin):
|
|||||||
|
|
||||||
Returns True if the theme is being used, otherwise returns False.
|
Returns True if the theme is being used, otherwise returns False.
|
||||||
"""
|
"""
|
||||||
if self.manager.get_all_objects_filtered(Song,
|
if self.manager.get_all_objects(Song, Song.theme_name == theme):
|
||||||
Song.theme_name == theme):
|
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@ -214,7 +213,7 @@ class SongsPlugin(Plugin):
|
|||||||
``newTheme``
|
``newTheme``
|
||||||
The new name the plugin should now use.
|
The new name the plugin should now use.
|
||||||
"""
|
"""
|
||||||
songsUsingTheme = self.manager.get_all_objects_filtered(Song,
|
songsUsingTheme = self.manager.get_all_objects(Song,
|
||||||
Song.theme_name == oldTheme)
|
Song.theme_name == oldTheme)
|
||||||
for song in songsUsingTheme:
|
for song in songsUsingTheme:
|
||||||
song.theme_name = newTheme
|
song.theme_name = newTheme
|
||||||
|
@ -25,8 +25,9 @@
|
|||||||
|
|
||||||
from PyQt4 import QtGui
|
from PyQt4 import QtGui
|
||||||
|
|
||||||
from songusagedeletedialog import Ui_SongUsageDeleteDialog
|
|
||||||
from openlp.core.lib import translate
|
from openlp.core.lib import translate
|
||||||
|
from openlp.plugins.songusage.lib.db import SongUsageItem
|
||||||
|
from songusagedeletedialog import Ui_SongUsageDeleteDialog
|
||||||
|
|
||||||
class SongUsageDeleteForm(QtGui.QDialog, Ui_SongUsageDeleteDialog):
|
class SongUsageDeleteForm(QtGui.QDialog, Ui_SongUsageDeleteDialog):
|
||||||
"""
|
"""
|
||||||
@ -52,6 +53,6 @@ class SongUsageDeleteForm(QtGui.QDialog, Ui_SongUsageDeleteDialog):
|
|||||||
QtGui.QMessageBox.Cancel)
|
QtGui.QMessageBox.Cancel)
|
||||||
if ret == QtGui.QMessageBox.Ok:
|
if ret == QtGui.QMessageBox.Ok:
|
||||||
deleteDate = self.DeleteCalendar.selectedDate().toPyDate()
|
deleteDate = self.DeleteCalendar.selectedDate().toPyDate()
|
||||||
self.songusagemanager.delete_to_date(deleteDate)
|
self.songusagemanager.delete_all_objects(SongUsageItem,
|
||||||
|
SongUsageItem.usagedate <= deleteDate)
|
||||||
self.close()
|
self.close()
|
||||||
|
|
||||||
|
@ -27,9 +27,10 @@ import logging
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
from PyQt4 import QtCore, QtGui
|
from PyQt4 import QtCore, QtGui
|
||||||
|
from sqlalchemy.sql import and_
|
||||||
|
|
||||||
from openlp.core.lib import SettingsManager, translate
|
from openlp.core.lib import SettingsManager, translate
|
||||||
|
from openlp.plugins.songusage.lib.db import SongUsageItem
|
||||||
from songusagedetaildialog import Ui_SongUsageDetailDialog
|
from songusagedetaildialog import Ui_SongUsageDetailDialog
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
@ -74,8 +75,11 @@ class SongUsageDetailForm(QtGui.QDialog, Ui_SongUsageDetailDialog):
|
|||||||
filename = u'usage_detail_%s_%s.txt' % (
|
filename = u'usage_detail_%s_%s.txt' % (
|
||||||
self.FromDate.selectedDate().toString(u'ddMMyyyy'),
|
self.FromDate.selectedDate().toString(u'ddMMyyyy'),
|
||||||
self.ToDate.selectedDate().toString(u'ddMMyyyy'))
|
self.ToDate.selectedDate().toString(u'ddMMyyyy'))
|
||||||
usage = self.parent.songusagemanager.get_songusage_for_period(
|
usage = self.parent.songusagemanager.get_all_objects(
|
||||||
self.FromDate.selectedDate(), self.ToDate.selectedDate())
|
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)
|
outname = os.path.join(unicode(self.FileLineEdit.text()), filename)
|
||||||
file = None
|
file = None
|
||||||
try:
|
try:
|
||||||
|
@ -25,4 +25,3 @@
|
|||||||
"""
|
"""
|
||||||
The :mod:`lib` module contains the library functions for the songusage plugin.
|
The :mod:`lib` module contains the library functions for the songusage plugin.
|
||||||
"""
|
"""
|
||||||
from openlp.plugins.songusage.lib.manager import SongUsageManager
|
|
||||||
|
@ -1,81 +0,0 @@
|
|||||||
# -*- coding: utf-8 -*-
|
|
||||||
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
|
|
||||||
|
|
||||||
###############################################################################
|
|
||||||
# OpenLP - Open Source Lyrics Projection #
|
|
||||||
# --------------------------------------------------------------------------- #
|
|
||||||
# Copyright (c) 2008-2010 Raoul Snyman #
|
|
||||||
# Portions copyright (c) 2008-2010 Tim Bentley, Jonathan Corwin, Michael #
|
|
||||||
# Gorven, Scott Guerrieri, Christian Richter, Maikel Stuivenberg, Martin #
|
|
||||||
# Thompson, Jon Tibble, Carsten Tinggaard #
|
|
||||||
# --------------------------------------------------------------------------- #
|
|
||||||
# This program is free software; you can redistribute it and/or modify it #
|
|
||||||
# under the terms of the GNU General Public License as published by the Free #
|
|
||||||
# Software Foundation; version 2 of the License. #
|
|
||||||
# #
|
|
||||||
# This program is distributed in the hope that it will be useful, but WITHOUT #
|
|
||||||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
|
||||||
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
|
|
||||||
# more details. #
|
|
||||||
# #
|
|
||||||
# You should have received a copy of the GNU General Public License along #
|
|
||||||
# with this program; if not, write to the Free Software Foundation, Inc., 59 #
|
|
||||||
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
|
|
||||||
###############################################################################
|
|
||||||
"""
|
|
||||||
The :mod:`manager` module provides song usage specific database query code
|
|
||||||
"""
|
|
||||||
import logging
|
|
||||||
|
|
||||||
from sqlalchemy.exceptions import InvalidRequestError
|
|
||||||
|
|
||||||
from openlp.core.lib.db import Manager
|
|
||||||
from openlp.plugins.songusage.lib.db import init_schema, SongUsageItem
|
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
class SongUsageManager(Manager):
|
|
||||||
"""
|
|
||||||
The Song Manager provides a central location for all database code. This
|
|
||||||
class takes care of connecting to the database and running all the queries.
|
|
||||||
"""
|
|
||||||
log.info(u'SongUsage manager loaded')
|
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
"""
|
|
||||||
Creates the connection to the database, and creates the tables if they
|
|
||||||
don't exist.
|
|
||||||
"""
|
|
||||||
log.debug(u'SongUsage Initialising')
|
|
||||||
Manager.__init__(self, u'songusage', init_schema)
|
|
||||||
log.debug(u'SongUsage Initialised')
|
|
||||||
|
|
||||||
def get_songusage_for_period(self, start_date, end_date):
|
|
||||||
"""
|
|
||||||
Returns the details of SongUsage for a designated time period
|
|
||||||
|
|
||||||
``start_date``
|
|
||||||
The start of the period to return
|
|
||||||
|
|
||||||
``end_date``
|
|
||||||
The end of the period to return
|
|
||||||
"""
|
|
||||||
return self.session.query(SongUsageItem) \
|
|
||||||
.filter(SongUsageItem.usagedate >= start_date.toPyDate()) \
|
|
||||||
.filter(SongUsageItem.usagedate < end_date.toPyDate()) \
|
|
||||||
.order_by(SongUsageItem.usagedate, SongUsageItem.usagetime).all()
|
|
||||||
|
|
||||||
def delete_to_date(self, date):
|
|
||||||
"""
|
|
||||||
Delete SongUsage records before given date
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
self.session.query(SongUsageItem) \
|
|
||||||
.filter(SongUsageItem.usagedate <= date) \
|
|
||||||
.delete(synchronize_session=False)
|
|
||||||
self.session.commit()
|
|
||||||
return True
|
|
||||||
except InvalidRequestError:
|
|
||||||
self.session.rollback()
|
|
||||||
log.exception(u'Failed to delete all Song Usage items to %s' % date)
|
|
||||||
return False
|
|
@ -24,15 +24,15 @@
|
|||||||
###############################################################################
|
###############################################################################
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
from PyQt4 import QtCore, QtGui
|
from PyQt4 import QtCore, QtGui
|
||||||
|
|
||||||
from openlp.core.lib import Plugin, Receiver, build_icon, translate
|
from openlp.core.lib import Plugin, Receiver, build_icon, translate
|
||||||
from openlp.plugins.songusage.lib import SongUsageManager
|
from openlp.core.lib.db import Manager
|
||||||
from openlp.plugins.songusage.forms import SongUsageDetailForm, \
|
from openlp.plugins.songusage.forms import SongUsageDetailForm, \
|
||||||
SongUsageDeleteForm
|
SongUsageDeleteForm
|
||||||
from openlp.plugins.songusage.lib.db import SongUsageItem
|
from openlp.plugins.songusage.lib.db import init_schema, SongUsageItem
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -117,7 +117,7 @@ class SongUsagePlugin(Plugin):
|
|||||||
QtCore.QVariant(False)).toBool()
|
QtCore.QVariant(False)).toBool()
|
||||||
self.SongUsageStatus.setChecked(self.SongUsageActive)
|
self.SongUsageStatus.setChecked(self.SongUsageActive)
|
||||||
if self.songusagemanager is None:
|
if self.songusagemanager is None:
|
||||||
self.songusagemanager = SongUsageManager()
|
self.songusagemanager = Manager(u'songusage', init_schema)
|
||||||
self.SongUsagedeleteform = SongUsageDeleteForm(self.songusagemanager)
|
self.SongUsagedeleteform = SongUsageDeleteForm(self.songusagemanager)
|
||||||
self.SongUsagedetailform = SongUsageDetailForm(self)
|
self.SongUsagedetailform = SongUsageDetailForm(self)
|
||||||
self.SongUsageMenu.menuAction().setVisible(True)
|
self.SongUsageMenu.menuAction().setVisible(True)
|
||||||
|
Loading…
Reference in New Issue
Block a user