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()
|
||||
|
||||
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
|
||||
|
||||
``object_class``
|
||||
The type of objects to return
|
||||
|
||||
``filter_clause``
|
||||
The filter governing selection of objects to return. Defaults to
|
||||
None.
|
||||
|
||||
``order_by_ref``
|
||||
Any parameters to order the returned objects by. Defaults to None.
|
||||
"""
|
||||
query = self.session.query(object_class)
|
||||
if order_by_ref is not None:
|
||||
return query.order_by(order_by_ref).all()
|
||||
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 filter_clause:
|
||||
query = query.filter(filter_clause)
|
||||
if order_by_ref is not None:
|
||||
return query.order_by(order_by_ref).all()
|
||||
return query.all()
|
||||
@ -235,7 +223,7 @@ class Manager(object):
|
||||
else:
|
||||
return True
|
||||
|
||||
def delete_all_objects(self, object_class):
|
||||
def delete_all_objects(self, object_class, filter_clause=None):
|
||||
"""
|
||||
Delete all object records
|
||||
|
||||
@ -243,11 +231,13 @@ class Manager(object):
|
||||
The type of object to delete
|
||||
"""
|
||||
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()
|
||||
return True
|
||||
except InvalidRequestError:
|
||||
self.session.rollback()
|
||||
log.exception(u'Failed to delete all %s records',
|
||||
object_class.__name__)
|
||||
log.exception(u'Failed to delete %s records', object_class.__name__)
|
||||
return False
|
||||
|
@ -62,7 +62,8 @@ class AlertForm(QtGui.QDialog, Ui_AlertDialog):
|
||||
|
||||
def loadList(self):
|
||||
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:
|
||||
item_name = QtGui.QListWidgetItem(alert.text)
|
||||
item_name.setData(QtCore.Qt.UserRole, QtCore.QVariant(alert.id))
|
||||
|
@ -198,7 +198,8 @@ class BibleManager(object):
|
||||
u'name': 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):
|
||||
|
@ -75,7 +75,7 @@ class CustomPlugin(Plugin):
|
||||
|
||||
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):
|
||||
return True
|
||||
return False
|
||||
@ -91,8 +91,8 @@ class CustomPlugin(Plugin):
|
||||
``newTheme``
|
||||
The new name the plugin should now use.
|
||||
"""
|
||||
customsUsingTheme = self.custommanager.get_all_objects_filtered(
|
||||
CustomSlide, CustomSlide.theme_name == oldTheme)
|
||||
customsUsingTheme = self.custommanager.get_all_objects(CustomSlide,
|
||||
CustomSlide.theme_name == oldTheme)
|
||||
for custom in customsUsingTheme:
|
||||
custom.theme_name = newTheme
|
||||
self.custommanager.save_object(custom)
|
||||
|
@ -73,7 +73,7 @@ class CustomMediaItem(MediaManagerItem):
|
||||
|
||||
def initialise(self):
|
||||
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
|
||||
#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.
|
||||
|
@ -118,7 +118,8 @@ class EditSongForm(QtGui.QDialog, Ui_EditSongDialog):
|
||||
self.TopicRemoveButton.setEnabled(False)
|
||||
|
||||
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.addItem(u'')
|
||||
for author in authors:
|
||||
@ -128,7 +129,8 @@ class EditSongForm(QtGui.QDialog, Ui_EditSongDialog):
|
||||
row, QtCore.QVariant(author.id))
|
||||
|
||||
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.addItem(u'')
|
||||
for topic in topics:
|
||||
@ -137,7 +139,7 @@ class EditSongForm(QtGui.QDialog, Ui_EditSongDialog):
|
||||
self.SongTopicCombo.setItemData(row, QtCore.QVariant(topic.id))
|
||||
|
||||
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.addItem(u'')
|
||||
for book in books:
|
||||
|
@ -102,7 +102,8 @@ class SongMaintenanceForm(QtGui.QDialog, Ui_SongMaintenanceDialog):
|
||||
Reloads the Authors list.
|
||||
"""
|
||||
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:
|
||||
if author.display_name:
|
||||
author_name = QtGui.QListWidgetItem(author.display_name)
|
||||
@ -117,7 +118,8 @@ class SongMaintenanceForm(QtGui.QDialog, Ui_SongMaintenanceDialog):
|
||||
Reloads the Topics list.
|
||||
"""
|
||||
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:
|
||||
topic_name = QtGui.QListWidgetItem(topic.name)
|
||||
topic_name.setData(QtCore.Qt.UserRole, QtCore.QVariant(topic.id))
|
||||
@ -128,7 +130,7 @@ class SongMaintenanceForm(QtGui.QDialog, Ui_SongMaintenanceDialog):
|
||||
Reloads the Books list.
|
||||
"""
|
||||
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:
|
||||
book_name = QtGui.QListWidgetItem(u'%s (%s)' % (book.name,
|
||||
book.publisher))
|
||||
@ -140,7 +142,7 @@ class SongMaintenanceForm(QtGui.QDialog, Ui_SongMaintenanceDialog):
|
||||
Returns False if the given Author is already in the list otherwise
|
||||
True.
|
||||
"""
|
||||
authors = self.songmanager.get_all_objects_filtered(Author,
|
||||
authors = self.songmanager.get_all_objects(Author,
|
||||
and_(
|
||||
Author.first_name == new_author.first_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.
|
||||
"""
|
||||
topics = self.songmanager.get_all_objects_filtered(Topic,
|
||||
topics = self.songmanager.get_all_objects(Topic,
|
||||
Topic.name == new_topic.name)
|
||||
if len(topics) > 0:
|
||||
# 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.
|
||||
"""
|
||||
books = self.songmanager.get_all_objects_filtered(Book,
|
||||
books = self.songmanager.get_all_objects(Book,
|
||||
and_(Book.name == new_book.name,
|
||||
Book.publisher == new_book.publisher))
|
||||
if len(books) > 0:
|
||||
|
@ -164,20 +164,20 @@ class SongMediaItem(MediaManagerItem):
|
||||
search_type = self.SearchTypeComboBox.currentIndex()
|
||||
if search_type == 0:
|
||||
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.asc())
|
||||
self.displayResultsSong(search_results)
|
||||
elif search_type == 1:
|
||||
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.asc())
|
||||
self.displayResultsSong(search_results)
|
||||
elif search_type == 2:
|
||||
log.debug(u'Authors Search')
|
||||
search_results = self.parent.manager.get_all_objects_filtered(
|
||||
Author, Author.display_name.like(u'%' + search_keywords + u'%'),
|
||||
search_results = self.parent.manager.get_all_objects(Author,
|
||||
Author.display_name.like(u'%' + search_keywords + u'%'),
|
||||
Author.display_name.asc())
|
||||
self.displayResultsAuthor(search_results)
|
||||
#Called to redisplay the song list screen edith from a search
|
||||
|
@ -69,7 +69,7 @@ class SongsPlugin(Plugin):
|
||||
log.info(u'Songs Initialising')
|
||||
Plugin.initialise(self)
|
||||
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):
|
||||
"""
|
||||
@ -198,8 +198,7 @@ class SongsPlugin(Plugin):
|
||||
|
||||
Returns True if the theme is being used, otherwise returns False.
|
||||
"""
|
||||
if self.manager.get_all_objects_filtered(Song,
|
||||
Song.theme_name == theme):
|
||||
if self.manager.get_all_objects(Song, Song.theme_name == theme):
|
||||
return True
|
||||
return False
|
||||
|
||||
@ -214,7 +213,7 @@ class SongsPlugin(Plugin):
|
||||
``newTheme``
|
||||
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)
|
||||
for song in songsUsingTheme:
|
||||
song.theme_name = newTheme
|
||||
|
@ -25,8 +25,9 @@
|
||||
|
||||
from PyQt4 import QtGui
|
||||
|
||||
from songusagedeletedialog import Ui_SongUsageDeleteDialog
|
||||
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):
|
||||
"""
|
||||
@ -52,6 +53,6 @@ class SongUsageDeleteForm(QtGui.QDialog, Ui_SongUsageDeleteDialog):
|
||||
QtGui.QMessageBox.Cancel)
|
||||
if ret == QtGui.QMessageBox.Ok:
|
||||
deleteDate = self.DeleteCalendar.selectedDate().toPyDate()
|
||||
self.songusagemanager.delete_to_date(deleteDate)
|
||||
self.songusagemanager.delete_all_objects(SongUsageItem,
|
||||
SongUsageItem.usagedate <= deleteDate)
|
||||
self.close()
|
||||
|
||||
|
@ -27,9 +27,10 @@ import logging
|
||||
import os
|
||||
|
||||
from PyQt4 import QtCore, QtGui
|
||||
from sqlalchemy.sql import and_
|
||||
|
||||
from openlp.core.lib import SettingsManager, translate
|
||||
|
||||
from openlp.plugins.songusage.lib.db import SongUsageItem
|
||||
from songusagedetaildialog import Ui_SongUsageDetailDialog
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
@ -74,8 +75,11 @@ class SongUsageDetailForm(QtGui.QDialog, Ui_SongUsageDetailDialog):
|
||||
filename = u'usage_detail_%s_%s.txt' % (
|
||||
self.FromDate.selectedDate().toString(u'ddMMyyyy'),
|
||||
self.ToDate.selectedDate().toString(u'ddMMyyyy'))
|
||||
usage = self.parent.songusagemanager.get_songusage_for_period(
|
||||
self.FromDate.selectedDate(), self.ToDate.selectedDate())
|
||||
usage = self.parent.songusagemanager.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
|
||||
try:
|
||||
|
@ -25,4 +25,3 @@
|
||||
"""
|
||||
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
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
from PyQt4 import QtCore, QtGui
|
||||
|
||||
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, \
|
||||
SongUsageDeleteForm
|
||||
from openlp.plugins.songusage.lib.db import SongUsageItem
|
||||
from openlp.plugins.songusage.lib.db import init_schema, SongUsageItem
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
@ -117,7 +117,7 @@ class SongUsagePlugin(Plugin):
|
||||
QtCore.QVariant(False)).toBool()
|
||||
self.SongUsageStatus.setChecked(self.SongUsageActive)
|
||||
if self.songusagemanager is None:
|
||||
self.songusagemanager = SongUsageManager()
|
||||
self.songusagemanager = Manager(u'songusage', init_schema)
|
||||
self.SongUsagedeleteform = SongUsageDeleteForm(self.songusagemanager)
|
||||
self.SongUsagedetailform = SongUsageDetailForm(self)
|
||||
self.SongUsageMenu.menuAction().setVisible(True)
|
||||
|
Loading…
Reference in New Issue
Block a user