]> git.zarvox.org Git - wp3.git/commitdiff
Implement SQLite database backend via QtSql.
authorDrew Fisher <drew.m.fisher@gmail.com>
Sun, 5 Jul 2009 06:30:51 +0000 (01:30 -0500)
committerDrew Fisher <drew.m.fisher@gmail.com>
Sun, 5 Jul 2009 06:30:51 +0000 (01:30 -0500)
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.

chooseaction.py
editperson.py
mainapp.py
newpersonwizard.py
pagecommit.py
pagenetid.py
pagenewuserdata.py

index 2009419f2bf6a436ad598a6fcc94db26d79e7cf6..b45d47c37d3b66825b5a01f347c85dc9eee3ca5a 100644 (file)
@@ -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("<center><h1>Select a task:<h1></center>")
-               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("<h1>Select a task:<h1>")
+               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
index d2936aa14dbc11b3ebd73abbaf5fc142bce77e26..53808e5d11cfebf4fe43e5feb1afae1b78ed814e 100644 (file)
@@ -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("<center>Photo goes here</center>")
+               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 )
index 328210b372d35c05aa4059154e8d869025d94d16..85c78a6b3fd194fd71790ed5e976abd9e3e64a24 100644 (file)
@@ -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
index bff756382bc9746bfd4e49fa1f2fbcc5461ac7d8..820d47e21b86a799466889de046a2f13bfd52b37 100644 (file)
@@ -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()
 
index 547b2e346398eaa152985c0259f84ee0e4b0fdca..31a6f3379b0aca780d1ace64ce3957cb4def76b6 100644 (file)
@@ -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([""])
index a44f08f9ade175275f6af19fb88a6b1dc28bed64..38f593ccbc3f6b38b689abad6d3c4f769f56da7f 100644 (file)
@@ -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)
index bb11bbe6995f8c8ff4cfa21915bc6939687d2223..37ad4c92737f652f0ab72dc1ce7294137fc5523d 100644 (file)
@@ -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"):