From b44b7e404b994ce1593307794bf65deb6859ab84 Mon Sep 17 00:00:00 2001 From: Tim Bentley Date: Tue, 22 Sep 2009 20:37:36 +0100 Subject: [PATCH] Audit database --- openlp/plugins/audit/lib/manager.py | 104 ++++++++++++++++++++++++++++ openlp/plugins/audit/lib/meta.py | 38 ++++++++++ openlp/plugins/audit/lib/models.py | 37 ++++++++++ openlp/plugins/audit/lib/tables.py | 40 +++++++++++ 4 files changed, 219 insertions(+) create mode 100644 openlp/plugins/audit/lib/manager.py create mode 100644 openlp/plugins/audit/lib/meta.py create mode 100644 openlp/plugins/audit/lib/models.py create mode 100644 openlp/plugins/audit/lib/tables.py diff --git a/openlp/plugins/audit/lib/manager.py b/openlp/plugins/audit/lib/manager.py new file mode 100644 index 000000000..63be37c5a --- /dev/null +++ b/openlp/plugins/audit/lib/manager.py @@ -0,0 +1,104 @@ +# -*- coding: utf-8 -*- +# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4 + +############################################################################### +# OpenLP - Open Source Lyrics Projection # +# --------------------------------------------------------------------------- # +# Copyright (c) 2008-2009 Raoul Snyman # +# Portions copyright (c) 2008-2009 Martin Thompson, Tim Bentley, Carsten # +# Tinggaard, Jon Tibble, Jonathan Corwin, Maikel Stuivenberg, Scott Guerrieri # +# --------------------------------------------------------------------------- # +# 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 # +############################################################################### + +import os, os.path +import sys + +from sqlalchemy import asc, desc +from openlp.plugins.audit.lib.models import init_models, metadata, session, \ + engine, audit_table, Audit + +import logging + +class AuditManager(): + """ + 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. + """ + + global log + log = logging.getLogger(u'AuditManager') + log.info(u'Audit manager loaded') + + def __init__(self, config): + """ + Creates the connection to the database, and creates the tables if they + don't exist. + """ + self.config = config + log.debug(u'Audit Initialising') + self.db_url = u'' + db_type = self.config.get_config(u'db type', u'sqlite') + if db_type == u'sqlite': + self.db_url = u'sqlite:///%s/Audit.sqlite' % \ + self.config.get_data_path() + else: + self.db_url = db_type + 'u://' + \ + self.config.get_config(u'db username') + u':' + \ + self.config.get_config(u'db password') + u'@' + \ + self.config.get_config(u'db hostname') + u'/' + \ + self.config.get_config(u'db database') + self.session = init_models(self.db_url) + metadata.create_all(checkfirst=True) + log.debug(u'AuditInitialised') + + def get_audits(self): + """ + Returns a list of all the audits + """ + return self.session.query(audit).order_by(audit.whensung).all() + + def get_audit(self, id): + """ + Details of the audit + """ + return self.session.query(audit).get(id) + + def save_audit(self, audit): + """ + Save the audit and refresh the cache + """ + try: + self.session.add(audit) + self.session.commit() + return True + except: + self.session.rollback() + log.exception(u'Could not save audit to song database') + return False + + def delete_audit(self, auditid): + """ + Delete the audit + """ + audit = self.get_audit(auditid) + try: + self.session.delete(audit) + self.session.commit() + return True + except: + self.session.rollback() + log.exception(u'Could not delete audit from song database') + return False + diff --git a/openlp/plugins/audit/lib/meta.py b/openlp/plugins/audit/lib/meta.py new file mode 100644 index 000000000..c8c602c97 --- /dev/null +++ b/openlp/plugins/audit/lib/meta.py @@ -0,0 +1,38 @@ +# -*- coding: utf-8 -*- +# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4 + +############################################################################### +# OpenLP - Open Source Lyrics Projection # +# --------------------------------------------------------------------------- # +# Copyright (c) 2008-2009 Raoul Snyman # +# Portions copyright (c) 2008-2009 Martin Thompson, Tim Bentley, Carsten # +# Tinggaard, Jon Tibble, Jonathan Corwin, Maikel Stuivenberg, Scott Guerrieri # +# --------------------------------------------------------------------------- # +# 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 # +############################################################################### + +from sqlalchemy import MetaData +from sqlalchemy.orm import scoped_session, sessionmaker + +__all__ = ['session', 'metadata', 'engine'] + +# SQLAlchemy database engine. Updated by model.init_model() +engine = None + +# SQLAlchemy session manager. Updated by model.init_model() +session = None + +# Global metadata. If you have multiple databases with overlapping table +# names, you'll need a metadata for each database +metadata = MetaData() diff --git a/openlp/plugins/audit/lib/models.py b/openlp/plugins/audit/lib/models.py new file mode 100644 index 000000000..70f228fe9 --- /dev/null +++ b/openlp/plugins/audit/lib/models.py @@ -0,0 +1,37 @@ +# -*- coding: utf-8 -*- +# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4 + +############################################################################### +# OpenLP - Open Source Lyrics Projection # +# --------------------------------------------------------------------------- # +# Copyright (c) 2008-2009 Raoul Snyman # +# Portions copyright (c) 2008-2009 Martin Thompson, Tim Bentley, Carsten # +# Tinggaard, Jon Tibble, Jonathan Corwin, Maikel Stuivenberg, Scott Guerrieri # +# --------------------------------------------------------------------------- # +# 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 # +############################################################################### + +from sqlalchemy import create_engine +from sqlalchemy.orm import scoped_session, sessionmaker, mapper, relation + +from openlp.plugins.audit.lib.meta import session, metadata, engine +from openlp.plugins.audit.lib.tables import * + +def init_models(url): + engine = create_engine(url) + metadata.bind = engine + session = scoped_session(sessionmaker(autoflush=False, + autocommit=False, bind=engine)) + mapper(Audit, audit_table) + return session diff --git a/openlp/plugins/audit/lib/tables.py b/openlp/plugins/audit/lib/tables.py new file mode 100644 index 000000000..550bd9a55 --- /dev/null +++ b/openlp/plugins/audit/lib/tables.py @@ -0,0 +1,40 @@ +# -*- coding: utf-8 -*- +# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4 + +############################################################################### +# OpenLP - Open Source Lyrics Projection # +# --------------------------------------------------------------------------- # +# Copyright (c) 2008-2009 Raoul Snyman # +# Portions copyright (c) 2008-2009 Martin Thompson, Tim Bentley, Carsten # +# Tinggaard, Jon Tibble, Jonathan Corwin, Maikel Stuivenberg, Scott Guerrieri # +# --------------------------------------------------------------------------- # +# 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 # +############################################################################### + +from datetime import now + +from sqlalchemy import * +from sqlalchemy import Column, Table, ForeignKey, types + +from openlp.plugins.audit.lib.meta import metadata + +# Definition of the "audits" table +audit_table = Table(u'audits', metadata, + Column(u'id', types.Integer, primary_key=True), + Column('whensung', types.DateTime, nullable=False, default=datetime.now()), + Column(u'authors', types.Unicode(255)), + Column(u'ccli_number', types.Unicode(64)), +) + +Index(u'audits_id',audit_table.c.timestamp, audit_table.c.id)