Couple of changes to the Songs plugin classes - trying to get SQLAlchemy to work.

bzr-revno: 292
This commit is contained in:
Raoul Snyman 2009-02-01 21:12:18 +00:00
parent bd510467d5
commit 6ea70e92a1
10 changed files with 96 additions and 109 deletions

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE UserProject SYSTEM "UserProject-4.0.dtd"> <!DOCTYPE UserProject SYSTEM "UserProject-4.0.dtd">
<!-- eric4 user project file for project openlp.org 2.0 --> <!-- eric4 user project file for project openlp.org 2.0 -->
<!-- Saved: 2009-01-17, 13:06:08 --> <!-- Saved: 2009-01-27, 23:44:25 -->
<!-- Copyright (C) 2009 Raoul Snyman, raoulsnyman@openlp.org --> <!-- Copyright (C) 2009 Raoul Snyman, raoulsnyman@openlp.org -->
<UserProject version="4.0"> <UserProject version="4.0">
</UserProject> </UserProject>

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Tasks SYSTEM "Tasks-4.2.dtd"> <!DOCTYPE Tasks SYSTEM "Tasks-4.2.dtd">
<!-- eric4 tasks file for project openlp.org 2.0 --> <!-- eric4 tasks file for project openlp.org 2.0 -->
<!-- Saved: 2009-01-17, 13:06:08 --> <!-- Saved: 2009-01-27, 23:44:25 -->
<Tasks version="4.2"> <Tasks version="4.2">
<Task priority="1" completed="False" bugfix="False"> <Task priority="1" completed="False" bugfix="False">
<Summary>TODO: what is the tags for bridge, pre-chorus?</Summary> <Summary>TODO: what is the tags for bridge, pre-chorus?</Summary>

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Project SYSTEM "Project-4.4.dtd"> <!DOCTYPE Project SYSTEM "Project-4.4.dtd">
<!-- eric4 project file for project openlp.org 2.0 --> <!-- eric4 project file for project openlp.org 2.0 -->
<!-- Saved: 2009-01-17, 13:05:58 --> <!-- Saved: 2009-01-27, 23:44:25 -->
<!-- Copyright (C) 2009 Raoul Snyman, raoulsnyman@openlp.org --> <!-- Copyright (C) 2009 Raoul Snyman, raoulsnyman@openlp.org -->
<Project version="4.4"> <Project version="4.4">
<ProgLanguage mixed="0">Python</ProgLanguage> <ProgLanguage mixed="0">Python</ProgLanguage>
@ -66,7 +66,6 @@
<Source>openlp/plugins/bibles/bibleplugin.py</Source> <Source>openlp/plugins/bibles/bibleplugin.py</Source>
<Source>openlp/plugins/bibles/forms/bibleimportdialog.py</Source> <Source>openlp/plugins/bibles/forms/bibleimportdialog.py</Source>
<Source>openlp/plugins/bibles/forms/bibleimportform.py</Source> <Source>openlp/plugins/bibles/forms/bibleimportform.py</Source>
<Source>openlp/plugins/bibles/lib/biblemanager.py</Source>
<Source>openlp/plugins/bibles/lib/bibleDBimpl.py</Source> <Source>openlp/plugins/bibles/lib/bibleDBimpl.py</Source>
<Source>openlp/plugins/bibles/lib/bibleOSISimpl.py</Source> <Source>openlp/plugins/bibles/lib/bibleOSISimpl.py</Source>
<Source>openlp/plugins/bibles/lib/bibleHTTPimpl.py</Source> <Source>openlp/plugins/bibles/lib/bibleHTTPimpl.py</Source>
@ -74,7 +73,6 @@
<Source>openlp/plugins/bibles/__init__.py</Source> <Source>openlp/plugins/bibles/__init__.py</Source>
<Source>openlp/plugins/bibles/lib/__init__.py</Source> <Source>openlp/plugins/bibles/lib/__init__.py</Source>
<Source>openlp/plugins/bibles/forms/__init__.py</Source> <Source>openlp/plugins/bibles/forms/__init__.py</Source>
<Source>openlp/plugins/bibles/lib/biblecommon.py</Source>
<Source>openlp/plugins/songs/lib/__init__.py</Source> <Source>openlp/plugins/songs/lib/__init__.py</Source>
<Source>openlp/plugins/songs/lib/songxml.py</Source> <Source>openlp/plugins/songs/lib/songxml.py</Source>
<Source>openlp/plugins/songs/lib/models.py</Source> <Source>openlp/plugins/songs/lib/models.py</Source>
@ -93,12 +91,8 @@
<Source>openlp/plugins/videos/videoplugin.py</Source> <Source>openlp/plugins/videos/videoplugin.py</Source>
<Source>openlp/plugins/images/__init__.py</Source> <Source>openlp/plugins/images/__init__.py</Source>
<Source>openlp/plugins/images/imageplugin.py</Source> <Source>openlp/plugins/images/imageplugin.py</Source>
<Source>openlp/plugins/songs/lib/songinterface.py</Source>
<Source>openlp/plugins/songs/lib/songfile.py</Source>
<Source>openlp/plugins/songs/forms/editsongdialog.py</Source> <Source>openlp/plugins/songs/forms/editsongdialog.py</Source>
<Source>openlp/core/lib/pluginutils.py</Source> <Source>openlp/core/lib/pluginutils.py</Source>
<Source>openlp/plugins/songs/lib/songmanager.py</Source>
<Source>openlp/plugins/songs/lib/songDBimpl.py</Source>
<Source>openlpcnv.pyw</Source> <Source>openlpcnv.pyw</Source>
<Source>openlp/plugins/songs/forms/songbookdialog.py</Source> <Source>openlp/plugins/songs/forms/songbookdialog.py</Source>
<Source>openlp/plugins/songs/forms/topicsdialog.py</Source> <Source>openlp/plugins/songs/forms/topicsdialog.py</Source>
@ -111,8 +105,17 @@
<Source>openlp/migration/migratesongs.py</Source> <Source>openlp/migration/migratesongs.py</Source>
<Source>openlp/migration/display.py</Source> <Source>openlp/migration/display.py</Source>
<Source>openlp/migration/migratebibles.py</Source> <Source>openlp/migration/migratebibles.py</Source>
<Source>openlp/plugins/songs/lib/songclasses.py</Source> <Source>openlp/plugins/songs/lib/tables.py</Source>
<Source>openlp/plugins/songs/lib/songtables.py</Source> <Source>openlp/plugins/songs/lib/classes.py</Source>
<Source>openlp/plugins/songs/lib/manager.py</Source>
<Source>openlp/plugins/bibles/lib/classes.py</Source>
<Source>openlp/plugins/bibles/lib/tables.py</Source>
<Source>openlp/plugins/bibles/lib/manager.py</Source>
<Source>openlp/plugins/bibles/lib/common.py</Source>
<Source>openlp/plugins/songs/lib/meta.py</Source>
<Source>openlp/core/test/testplugins/deeper/__init__.py</Source>
<Source>openlp/core/test/testplugins/deeper/toodeep/__init__.py</Source>
<Source>openlp/core/test/testplugins/deeper/toodeep/plugin3toodeep.py</Source>
</Sources> </Sources>
<Forms> <Forms>
<Form>resources/forms/openlpexportform.ui</Form> <Form>resources/forms/openlpexportform.ui</Form>
@ -148,7 +151,7 @@
</Others> </Others>
<MainScript>openlp.pyw</MainScript> <MainScript>openlp.pyw</MainScript>
<Vcs> <Vcs>
<VcsType>PySvn</VcsType> <VcsType>Subversion</VcsType>
<VcsOptions> <VcsOptions>
<dict> <dict>
<key> <key>
@ -220,7 +223,7 @@
</key> </key>
<value> <value>
<list> <list>
<string></string> <string></string>
</list> </list>
</value> </value>
<key> <key>

View File

@ -27,8 +27,6 @@ from songbookform import SongBookForm
from editsongdialog import Ui_EditSongDialog from editsongdialog import Ui_EditSongDialog
from openlp.plugins.songs.lib.models import Session, Song, Author, Topic
class EditSongForm(QWidget, Ui_EditSongDialog): class EditSongForm(QWidget, Ui_EditSongDialog):
""" """
Class documentation goes here. Class documentation goes here.
@ -61,11 +59,7 @@ class EditSongForm(QWidget, Ui_EditSongDialog):
self.AuthorsSelectionComboItem.addItem( i.display_name) self.AuthorsSelectionComboItem.addItem( i.display_name)
def load_song(self, songid): def load_song(self, songid):
if songid == None: # We have a new Song self.song = self.songmanager.get_song(songid)
self.song = Song()
else:
self.songid = songid
self.song = self.songmanager.get_song(songid)
self.TitleEditItem.setText(self.song.title) self.TitleEditItem.setText(self.song.title)
self.LyricsTextEdit.setText(self.song.lyrics) self.LyricsTextEdit.setText(self.song.lyrics)
self.CopyrightEditItem.setText(self.song.copyright) self.CopyrightEditItem.setText(self.song.copyright)

View File

@ -18,8 +18,8 @@ this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA Place, Suite 330, Boston, MA 02111-1307 USA
""" """
from songmanager import SongManager from manager import SongManager
from songtable import Author #from xml import SongOpenXml
__all__ = ['SongManager', 'Author'] __all__ = ['SongManager']

View File

@ -16,8 +16,6 @@ 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 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA Place, Suite 330, Boston, MA 02111-1307 USA
""" """
from sqlalchemy.orm import mapper, relation
from openlp.plugins.songs.lib.tables import *
class BaseModel(object): class BaseModel(object):
""" """
@ -58,14 +56,3 @@ class Topic(BaseModel):
Topic model Topic model
""" """
pass pass
mapper(Author, authors_table)
mapper(Book, song_books_table)
mapper(Song, songs_table,
properties={'authors': relation(Author, backref='songs',
secondary=authors_songs_table),
'book': relation(Book, backref='songs'),
'topics': relation(Topic, backref='songs',
secondary=songs_topics_table)})
mapper(Topic, topics_table)

View File

@ -23,7 +23,7 @@ import sys
from sqlalchemy import asc, desc from sqlalchemy import asc, desc
from openlp.plugins.songs.lib.models import init_models, metadata, session, \ from openlp.plugins.songs.lib.models import init_models, metadata, session, \
songs_table, Song, Author, Topic engine, songs_table, Song, Author, Topic
import logging import logging
@ -47,15 +47,17 @@ class SongManager():
self.db_url = u'' self.db_url = u''
db_type = self.config.get_config(u'db type') db_type = self.config.get_config(u'db type')
if db_type == u'sqlite': if db_type == u'sqlite':
self.db_url = u'sqlite://' + self.config.get_data_path() + \ self.db_url = u'sqlite:///' + self.config.get_data_path() + \
u'/songs.sqlite' u'/songs.sqlite'
print self.db_url
else: else:
self.db_url = db_type + 'u://' + \ self.db_url = db_type + 'u://' + \
self.config.get_config(u'db username') + u':' + \ self.config.get_config(u'db username') + u':' + \
self.config.get_config(u'db password') + u'@' + \ self.config.get_config(u'db password') + u'@' + \
self.config.get_config(u'db hostname') + u'/' + \ self.config.get_config(u'db hostname') + u'/' + \
self.config.get_config(u'db database') self.config.get_config(u'db database')
init_models(self.db_url) #print self.db_url
self.session = init_models(self.db_url)
if not songs_table.exists(): if not songs_table.exists():
metadata.create_all() metadata.create_all()
log.debug( "Song Initialised") log.debug( "Song Initialised")
@ -67,19 +69,19 @@ class SongManager():
""" """
Returns the details of a song Returns the details of a song
""" """
return session.query(Song).order_by(title).all() return self.session.query(Song).order_by(title).all()
def search_song_title(self, keywords): def search_song_title(self, keywords):
""" """
Searches the song title for keywords. Searches the song title for keywords.
""" """
return session.query(Song).filter(search_title.like(u'%' + keywords + u'%')) return self.session.query(Song).filter(search_title.like(u'%' + keywords + u'%'))
def search_song_lyrics(self, keywords): def search_song_lyrics(self, keywords):
""" """
Searches the song lyrics for keywords. Searches the song lyrics for keywords.
""" """
return session.query(Song).filter(search_lyrics.like(u'%' + keywords + u'%')) return self.session.query(Song).filter(search_lyrics.like(u'%' + keywords + u'%'))
def get_song(self, id=None): def get_song(self, id=None):
""" """
@ -88,23 +90,23 @@ class SongManager():
if id is None: if id is None:
return Song() return Song()
else: else:
return session.query(Song).get(id) return self.session.query(Song).get(id)
def save_song(self, song): def save_song(self, song):
""" """
Saves a song to the database Saves a song to the database
""" """
try: try:
session.add(song) self.session.add(song)
session.commit() self.session.commit()
return True return True
except: except:
return False return False
def delete_song(self, song): def delete_song(self, song):
try: try:
session.delete(song) self.session.delete(song)
session.commit() self.session.commit()
return True return True
except: except:
return False return False
@ -113,21 +115,21 @@ class SongManager():
""" """
Returns a list of all the authors Returns a list of all the authors
""" """
return session.query(Author).order_by(display_name).all() return self.session.query(Author).order_by(Author.display_name).all()
def get_author(self, id): def get_author(self, id):
""" """
Details of the Author Details of the Author
""" """
return session.query(Author).get(id) return self.session.query(Author).get(id)
def save_author(self, author): def save_author(self, author):
""" """
Save the Author and refresh the cache Save the Author and refresh the cache
""" """
try: try:
session.add(author) self.session.add(author)
session.commit() self.session.commit()
return True return True
except: except:
return False return False
@ -137,8 +139,8 @@ class SongManager():
Delete the author and refresh the author cache Delete the author and refresh the author cache
""" """
try: try:
session.delete(author) self.session.delete(author)
session.commit() self.session.commit()
return True return True
except: except:
return False return False

View File

@ -26,16 +26,16 @@ from openlp.plugins.songs.lib.classes import *
def init_models(url): def init_models(url):
engine = create_engine(url) engine = create_engine(url)
metadata.bind = engine
session = scoped_session(sessionmaker(autoflush=True, autocommit=False, session = scoped_session(sessionmaker(autoflush=True, autocommit=False,
bind=engine)) bind=engine))
metadata.bind = engine mapper(Author, authors_table)
mapper(Book, song_books_table)
mapper(Author, authors_table) mapper(Song, songs_table,
mapper(Book, song_books_table)
mapper(Song, songs_table,
properties={'authors': relation(Author, backref='songs', properties={'authors': relation(Author, backref='songs',
secondary=authors_songs_table), secondary=authors_songs_table),
'book': relation(Book, backref='songs'), 'book': relation(Book, backref='songs'),
'topics': relation(Topic, backref='songs', 'topics': relation(Topic, backref='songs',
secondary=songs_topics_table)}) secondary=songs_topics_table)})
mapper(Topic, topics_table) mapper(Topic, topics_table)
return session

View File

@ -22,11 +22,10 @@ import logging
from PyQt4 import QtCore, QtGui from PyQt4 import QtCore, QtGui
from openlp.core.resources import * from openlp.core.resources import *
from openlp.core.lib import Plugin,PluginUtils, MediaManagerItem from openlp.core.lib import Plugin, PluginUtils, MediaManagerItem
from forms import EditSongForm, OpenLPImportForm, OpenSongImportForm, \ from forms import EditSongForm, OpenLPImportForm, OpenSongImportForm, \
OpenLPExportForm, OpenSongExportForm OpenLPExportForm, OpenSongExportForm
from openlp.plugins.songs.lib import SongManager from openlp.plugins.songs.lib import SongManager
from openlp.plugins.songs.lib.songclasses import *
class SongsPlugin(Plugin, PluginUtils): class SongsPlugin(Plugin, PluginUtils):
global log global log
@ -123,9 +122,9 @@ class SongsPlugin(Plugin, PluginUtils):
self.SongListView.setObjectName("listView") self.SongListView.setObjectName("listView")
self.MediaManagerItem.PageLayout.addWidget(self.SongListView) self.MediaManagerItem.PageLayout.addWidget(self.SongListView)
QtCore.QObject.connect(self.SearchTextButton, QtCore.SIGNAL("pressed()"), self.onSearchTextButton) QtCore.QObject.connect(self.SearchTextButton, QtCore.SIGNAL("pressed()"), self.onSearchTextButtonClick)
QtCore.QObject.connect(self.ClearTextButton, QtCore.SIGNAL("pressed()"), self.onClearTextButton) QtCore.QObject.connect(self.ClearTextButton, QtCore.SIGNAL("pressed()"), self.onClearTextButtonClick)
QtCore.QObject.connect(self.SearchTextEdit, QtCore.SIGNAL("textChanged(const QString&)"), self.onSearchTextEdit) QtCore.QObject.connect(self.SearchTextEdit, QtCore.SIGNAL("textChanged(const QString&)"), self.onSearchTextEditChanged)
QtCore.QObject.connect(self.SongListView, QtCore.SIGNAL("itemPressed(QTableWidgetItem * item)"), self.onSongSelected) QtCore.QObject.connect(self.SongListView, QtCore.SIGNAL("itemPressed(QTableWidgetItem * item)"), self.onSongSelected)
@ -194,35 +193,37 @@ class SongsPlugin(Plugin, PluginUtils):
QtCore.QObject.connect(self.ExportOpenSongItem, QtCore.SIGNAL("triggered()"), self.onExportOpenSongItemClicked) QtCore.QObject.connect(self.ExportOpenSongItem, QtCore.SIGNAL("triggered()"), self.onExportOpenSongItemClicked)
def initialise(self): def initialise(self):
self.SearchTypeComboBox.addItem("Titles") self.SearchTypeComboBox.addItem(u'Titles')
self.SearchTypeComboBox.addItem("Lyrics") self.SearchTypeComboBox.addItem(u'Lyrics')
self.SearchTypeComboBox.addItem("Authors") self.SearchTypeComboBox.addItem(u'Authors')
def onClearTextButton(self): def onClearTextButtonClick(self):
"""
Clear the search text.
"""
self.SearchTextEdit.clear() self.SearchTextEdit.clear()
def onSearchTextEdit(self): def onSearchTextEditChanged(self, text):
sl = 3 #sl = 3
if self.SearchTypeComboBox.currentText()=="Lyrics": #if self.SearchTypeComboBox.currentText() == u'Lyrics':
sl = 7 # sl = 7
if len(self.SearchTextEdit.displayText()) > sl: # only search if > 3 characters #if len(text) > sl: # only search if > 3 characters
self.onSearchTextButton() self.onSearchTextButtonClick()
def onSearchTextButton(self): def onSearchTextButtonClick(self):
searchtext = str(self.SearchTextEdit.displayText() ) search_keywords = str(self.SearchTextEdit.displayText())
searchresults = [] search_results = []
ct = self.SearchTypeComboBox.currentText() search_type = self.SearchTypeComboBox.currentText()
if self.SearchTypeComboBox.currentText()=="Titles": if search_type == u'Titles':
log.debug("Titles Search") log.debug("Titles Search")
searchresults = self.songmanager.get_song_from_title(searchtext) search_results = self.songmanager.search_song_title(search_keywords)
elif self.SearchTypeComboBox.currentText()=="Lyrics": elif search_type == u'Lyrics':
log.debug("Lyrics Search") log.debug("Lyrics Search")
searchresults = self.songmanager.get_song_from_lyrics(searchtext) searchresults = self.songmanager.search_song_lyrics(search_keywords)
elif self.SearchTypeComboBox.currentText()=="Authors": elif search_type == u'Authors':
log.debug("Authors Search") log.debug("Authors Search")
searchresults = self.songmanager.get_song_from_author(searchtext) #searchresults = self.songmanager.get_song_from_author(searchtext)
self._display_results(search_results)
self._display_results(searchresults)
def onSongSelected(self, item): def onSongSelected(self, item):
print item print item