From: Drew Fisher <drew.m.fisher@gmail.com>
Date: Sat, 22 Aug 2009 05:35:33 +0000 (-0500)
Subject: Merge photos from a folder with files of the form <netid>.jpg
X-Git-Url: http://git.zarvox.org/%7Bwebsite%7D?a=commitdiff_plain;h=ba6e653c36bb9f6ca298b3f87cf7a0024f772ea6;p=wp3.git

Merge photos from a folder with files of the form <netid>.jpg

We support <netid>.jpg and <netid>.JPG, at the moment.  Additional
extensions should be easy enough to provide.  Additional (read:
time-based) merge techniques are still to come.
---

diff --git a/mergephotos.py b/mergephotos.py
index f9d3198..acd26b6 100644
--- a/mergephotos.py
+++ b/mergephotos.py
@@ -12,5 +12,33 @@ class MergePhotos(QWidget):
 		self.setLayout(self.layout)
 
 	def beginMerge(self):
-		print QFileDialog.getExistingDirectory( None, "Pick folder of photos", ".")
+		foldertext = QFileDialog.getExistingDirectory( None, "Pick folder of photos", ".")
+		if not foldertext.isEmpty():
+			print foldertext
+			dir = QDir(foldertext)
+			#dir.setNameFilters(["*.jpg", "*.JPG"])
+			q = QSqlQuery(self.db)
+			q.exec_("SELECT id, netid FROM people WHERE photo IS NULL")
+			rec = q.record()
+			col_id = rec.indexOf("id")
+			col_netid = rec.indexOf("netid")
+			while q.next():
+				# if netid.jpg exists, load it, write it to a QByteArray, bind it, and update that row
+				fileName = q.value(col_netid).toString() + QString(".jpg")
+				if not dir.exists(fileName):
+					fileName = q.value(col_netid).toString() + QString(".JPG")
+				if dir.exists(fileName):
+					image = QImage()
+					image.load(dir.absoluteFilePath(fileName))
+					ba = QByteArray()
+					buffer = QBuffer(ba)
+					buffer.open(QIODevice.WriteOnly)
+					image.save(buffer, "JPG")
+					buffer.close()
+					update_q = QSqlQuery(self.db)
+					update_q.prepare("UPDATE people SET photo=:photo WHERE id=:id")
+					update_q.bindValue(":photo", QVariant(ba))
+					update_q.bindValue(":id", q.value(col_id))
+					update_q.exec_()
+					print "Auto-imported photo for",q.value(col_netid).toString()