From: Drew Fisher Date: Sun, 5 Jul 2009 06:30:51 +0000 (-0500) Subject: Implement SQLite database backend via QtSql. X-Git-Url: http://git.zarvox.org/shortlog/year?a=commitdiff_plain;h=98e2739c9e0a395ee2a4d93762dbf0ca9de12aef;p=wp3.git Implement SQLite database backend via QtSql. Support new databases and opening existing databases. New person wizard actually adds data to the database. Other classes have access to the database when they want it. --- diff --git a/chooseaction.py b/chooseaction.py index 2009419..b45d47c 100644 --- a/chooseaction.py +++ b/chooseaction.py @@ -4,13 +4,15 @@ from newpersonwizard import NewPersonWizard from editperson import EditPerson class ChooseAction (QWidget): - def __init__(self, parent=None): + def __init__(self, parent=None, db=None): QWidget.__init__(self,parent) - self.l1 = QLabel("

Select a task:

") - self.pb1 = QCommandLinkButton("Add a person to the directory","If you haven't seen this program before, hit this button.") - self.pb2 = QCommandLinkButton("Edit a directory record","If you've already added your data, but want to edit it, hit this button.") - self.pb3 = QCommandLinkButton("Import photos","To match picture from a camera to entered data, hit this button.") - self.pb4 = QCommandLinkButton("Export as document","To export the directory as a document for printing, hit this button.") + self.db = db + self.l1 = QLabel("

Select a task:

") + self.l1.setAlignment(QtCore.Qt.AlignCenter) + self.pb1 = QCommandLinkButton("&Add a person to the directory","If you haven't seen this program before, hit this button.") + self.pb2 = QCommandLinkButton("&Edit a directory record","If you've already added your data, but want to edit it, hit this button.") + self.pb3 = QCommandLinkButton("&Import photos","To match picture from a camera to entered data, hit this button.") + self.pb4 = QCommandLinkButton("E&xport as document","To export the directory as a document for printing, hit this button.") self.layout = QGridLayout() self.layout.addWidget(self.l1,0,0,1,3) self.layout.addWidget(self.pb1,1,1,1,1) @@ -23,14 +25,15 @@ class ChooseAction (QWidget): QtCore.QObject.connect(self.pb4, QtCore.SIGNAL("clicked()"), self.exportDocument) self.setLayout(self.layout) def addPerson(self): - wiz = NewPersonWizard() + wiz = NewPersonWizard(self,self.db) wiz.exec_() wiz = None def editPerson(self): - ep = EditPerson() + ep = EditPerson(self,self.db) ep.exec_() ep = None def importPhotos(self): pass def exportDocument(self): - pass + fileName = QFileDialog.getSaveFileName(self, "Save new file as:", ".", "OpenDocument Text Documents (*.odt)") + print "Will export db as",fileName diff --git a/editperson.py b/editperson.py index d2936aa..53808e5 100644 --- a/editperson.py +++ b/editperson.py @@ -2,8 +2,9 @@ from PyQt4.QtCore import * from PyQt4.QtGui import * class EditPerson(QDialog): - def __init__(self, parent=None): + def __init__(self, parent=None, db=None): QDialog.__init__(self,parent) + self.db = db self.setWindowTitle("Edit records") self.toplayout = QHBoxLayout() # Master layout @@ -50,9 +51,14 @@ class EditPerson(QDialog): self.room_lab = QLabel("Room number:") self.bday_lab = QLabel("Birthday:") self.dorm_lab = QLabel("Dorm:") - self.photo = QLabel("
Photo goes here
") + self.photo = QLabel("Photo goes here") self.photo.setScaledContents(True) + self.currentimage = QImage() + self.displayimage = QImage() + self.displaypixmap = QPixmap() + self.photo.setAlignment(Qt.AlignCenter) self.pb_changephoto = QPushButton("Select &photo") + self.pb_changephoto.setEnabled(True) # Personal info self.surname = QLineEdit() self.firstname = QLineEdit() @@ -117,7 +123,6 @@ class EditPerson(QDialog): self.buttonlayout.addWidget(self.pb_cancel) self.buttonlayout.addWidget(self.pb_save) self.buttonlayout.addWidget(self.pb_saveclose) - self.pb_changephoto.setEnabled(False) def setupActions(self): # Update filtered table QObject.connect(self.search_bar, SIGNAL("textChanged(QString)"), self.updateTable ) diff --git a/mainapp.py b/mainapp.py index 328210b..85c78a6 100644 --- a/mainapp.py +++ b/mainapp.py @@ -1,14 +1,22 @@ from PyQt4 import QtCore from PyQt4.QtGui import * +from PyQt4.QtSql import * from chooseaction import ChooseAction class MainApp (QMainWindow): + schema = "CREATE TABLE people ( id INTEGER PRIMARY KEY AUTOINCREMENT, netid TEXT NOT NULL, surname TEXT NOT NULL, forename TEXT NOT NULL, email TEXT, birthday TEXT, phone TEXT, major TEXT, dorm TEXT, room INTEGER, photo BLOB, createtime TEXT NOT NULL, mtime TEXT NOT NULL);" def __init__(self, Parent=None): QMainWindow.__init__(self, Parent) self.setWindowTitle("Whitepages V3") - self.center = ChooseAction(self) + self.db = QSqlDatabase.addDatabase("QSQLITE") + self.db.setDatabaseName("families.db") + self.db.open() + q = QSqlQuery(self.db) + q.exec_(MainApp.schema) + + self.center = ChooseAction(self,self.db) self.setCentralWidget(self.center) self.createActions() @@ -40,10 +48,22 @@ class MainApp (QMainWindow): def newFile(self): - print "Close the existing database, create a new one." + self.db.close() + fileName = QFileDialog.getSaveFileName(self, "Save new file as:", ".", "Databases (*.db)") + self.db.setDatabaseName(fileName) + self.db.open() + q = QSqlQuery(self.db) + q.exec_(MainApp.schema) + print "Closed the existing database, created",fileName def openFile(self): fileName = QFileDialog.getOpenFileName(self, "Open dataset", ".", "Databases (*.db)") - print "and then we'd open the db",fileName + if not fileName.isEmpty(): + self.db.close() + self.db.setDatabaseName(fileName) + self.db.open() + print "Loaded database",fileName + else: + print "Canceled loading database" def mergeWizard(self): fileName = QFileDialog.getOpenFileName(self, "Import which dataset?", ".", "Databases (*.db)" ) print "and then we'd import data from",fileName diff --git a/newpersonwizard.py b/newpersonwizard.py index bff7563..820d47e 100644 --- a/newpersonwizard.py +++ b/newpersonwizard.py @@ -6,11 +6,12 @@ from pagenetid import PageNetID from pagecommit import PageCommit class NewPersonWizard(QWizard): - def __init__(self, parent=None): + def __init__(self, parent=None,db=None): QWizard.__init__(self, parent) + self.db = db self.addPage(PageNetID()) self.addPage(PageNewUserData()) - self.addPage(PageCommit()) + self.addPage(PageCommit(self,self.db)) self.resize(400,300) self.show() diff --git a/pagecommit.py b/pagecommit.py index 547b2e3..31a6f33 100644 --- a/pagecommit.py +++ b/pagecommit.py @@ -1,9 +1,11 @@ from PyQt4.QtCore import * from PyQt4.QtGui import * +from PyQt4.QtSql import * class PageCommit(QWizardPage): - def __init__(self, parent=None): + def __init__(self, parent=None, db=None): QWizardPage.__init__(self,parent) + self.db = db self.instructions = QLabel("You're done with the computer. Go have your picture taken.\n\nThe SAs/PAs will add the photo to your data.") self.instructions.setWordWrap(True) self.grid = QGridLayout() @@ -11,8 +13,28 @@ class PageCommit(QWizardPage): self.setLayout(self.grid) self.setTitle("Move-in Wizard") self.setSubTitle("Photo") + def initializePage(self): - pass # handle committing the data from the previous pages to the DB + q = QSqlQuery(self.db) + q.prepare("INSERT INTO people (netid, forename, surname, email, birthday, phone, major, dorm, room, createtime, mtime )" + "VALUES (:netid, :forename, :surname, :email, :birthday, :phone, :major, :dorm, :room, :createtime, :mtime )" ); + q.bindValue(":netid", self.field("netid") ) + q.bindValue(":forename", self.field("forename") ) + q.bindValue(":surname", self.field("surname") ) + q.bindValue(":email", self.field("email") ) + y = self.field("year").toInt()[0] + 1987 + m = self.field("month").toInt()[0] + 1 + d = self.field("day").toInt()[0] + 1 + bday = QDate(y,m,d) + q.bindValue(":birthday", QVariant(bday.toString(Qt.ISODate)) ) + q.bindValue(":phone", self.field("phone") ) + q.bindValue(":major", self.field("major") ) + q.bindValue(":dorm", self.field("dorm") ) + q.bindValue(":room", self.field("room") ) + q.bindValue(":createtime", QVariant(QDateTime.currentDateTime().toString(Qt.ISODate)) ) + q.bindValue(":mtime", QVariant(QDateTime.currentDateTime().toString(Qt.ISODate)) ) + q.exec_() + print "Inserted into database" if __name__ == "__main__": a = QApplication([""]) diff --git a/pagenetid.py b/pagenetid.py index a44f08f..38f593c 100644 --- a/pagenetid.py +++ b/pagenetid.py @@ -7,7 +7,7 @@ class PageNetID(QWizardPage): self.instructions = QLabel("&Enter your NetID:") self.netid = QLineEdit() self.instructions.setBuddy(self.netid) - self.registerField("netid*", self.netid, "text", SIGNAL("textChanged(QString)")) + self.registerField("netid*", self.netid) self.grid = QGridLayout() self.grid.addWidget(self.instructions,0,0) self.grid.addWidget(self.netid,0,1) diff --git a/pagenewuserdata.py b/pagenewuserdata.py index bb11bbe..37ad4c9 100644 --- a/pagenewuserdata.py +++ b/pagenewuserdata.py @@ -6,12 +6,12 @@ from ldapsearcher import LDAPSearcher class PageNewUserData(QWizardPage): def __init__(self, parent=None): QWizardPage.__init__(self,parent) - self.completed = False # Is the page done being filled out? + self.setCommitPage(True) # Create all the subwidgets self.year_lab = QLabel("Year:") self.month_lab = QLabel("Month:") self.day_lab = QLabel("Day:") - self.firstname_lab = QLabel("Preferred name:") + self.forename_lab = QLabel("Preferred name:") self.surname_lab = QLabel("Last name:") self.email_lab = QLabel("Email:") self.phone_lab = QLabel("Phone:") @@ -22,7 +22,7 @@ class PageNewUserData(QWizardPage): self.dorm_lab = QLabel("Dorm:") # Personal info self.surname = QLineEdit() - self.firstname = QLineEdit() + self.forename = QLineEdit() self.phone = QLineEdit() self.email = QLineEdit() self.major = QLineEdit() @@ -48,7 +48,7 @@ class PageNewUserData(QWizardPage): # Register fields with QWizard self.registerField("surname*", self.surname) - self.registerField("firstname*", self.firstname) + self.registerField("forename*", self.forename) self.registerField("phone", self.phone) self.registerField("email*", self.email) self.registerField("major", self.major) @@ -65,8 +65,8 @@ class PageNewUserData(QWizardPage): self.bday_layout.addWidget(self.year) self.bday_layout.addWidget(self.month) self.bday_layout.addWidget(self.day) - self.layout.addWidget(self.firstname_lab, 1, 0) - self.layout.addWidget(self.firstname, 1, 1) + self.layout.addWidget(self.forename_lab, 1, 0) + self.layout.addWidget(self.forename, 1, 1) self.layout.addWidget(self.surname_lab, 2, 0) self.layout.addWidget(self.surname, 2, 1) self.layout.addWidget(self.email_lab, 3, 0) @@ -93,7 +93,7 @@ class PageNewUserData(QWizardPage): if(key == "sn"): self.surname.setText(value) elif(key == "givenName"): - self.firstname.setText(value) + self.forename.setText(value) elif(key == "mail"): self.email.setText(value) elif(key == "major"):