117 | | self.cardbalance = '' |
118 | | self.username = '' |
119 | | self.addbalance = '' |
120 | | self.pykotauid = '' |
121 | | self.pykotabalance = 0.0 |
122 | | |
123 | | # TODO put try except around here |
124 | | #connect([dbname], [host], [port], [opt], [tty], [user], [passwd]) |
125 | | try: |
126 | | self.sql = pg.connect(dbname=DBNAME, host=HOST, port=PORT, user=USER, passwd=PASS) |
127 | | except: |
128 | | pass |
129 | | |
130 | | # query = self.sql.query("""SELECT printername FROM printers WHERE printername='cc200-LaserJet' """) |
131 | | #query = db.get(printers, "cc200-laserjet") |
132 | | # if len(query.getresult()) > 0: |
133 | | # d2 = query.dictresult() |
134 | | # print d2 #['username'] |
135 | | |
136 | | self.sc = smartcard(self.sql) |
| 137 | self.cardbalance = 0.0 |
| 138 | self.validuser = False |
| 139 | self.addbalance = 0.0 |
| 140 | |
| 141 | if not TESTMODE : |
| 142 | print "We are in test mode...." |
| 143 | |
| 144 | self.db = pgsql() |
| 145 | self.sc = smartcard(self.db) |
154 | | print "Text is now ->", self.utext.get_text() |
155 | | |
| 181 | if SEARCH : |
| 182 | if len(self.utext.get_text()) == SEARCHLENGTH: |
| 183 | if not self.db.alreadyopen: |
| 184 | if not self.db.pgopen(): |
| 185 | result = gtk.RESPONSE_CANCEL |
| 186 | dlg = gtk.MessageDialog(None,gtk.DIALOG_MODAL | |
| 187 | gtk.DIALOG_DESTROY_WITH_PARENT,gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE, |
| 188 | "Cannot connect or open the database.\nPlease contact technical suppport...", ) |
| 189 | result = dlg.run() |
| 190 | dlg.destroy() |
| 191 | return |
| 192 | if self.db.get_userslist(self.utext.get_text(), self.liststore): |
| 193 | pass |
| 194 | else: |
| 195 | return |
| 196 | |
| 197 | #self.username_entered(None, None) |
| 198 | |
| 199 | def username_found(self, completion, model, iter): |
| 200 | self.username = model[iter][0], 'was selected' |
| 201 | self.utext.set_text(model[iter][0]) |
| 202 | self.username_entered(self, None) |
| 203 | |
161 | | try: |
162 | | query = self.sql.query("SELECT id FROM users WHERE username='%s'" % (self.username)) |
163 | | self.pykotauid = (query.dictresult()[0])['id'] |
164 | | print "User ID is ->", self.pykotauid |
165 | | except: |
166 | | print "Username is invalid" |
| 209 | |
| 210 | if not self.db.alreadyopen: |
| 211 | if not self.db.pgopen(): |
| 212 | result = gtk.RESPONSE_CANCEL |
| 213 | dlg = gtk.MessageDialog(None,gtk.DIALOG_MODAL | |
| 214 | gtk.DIALOG_DESTROY_WITH_PARENT,gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE, |
| 215 | "Cannot connect or open the database.\nPlease contact technical suppport...", ) |
| 216 | result = dlg.run() |
| 217 | dlg.destroy() |
| 218 | return |
| 219 | |
| 220 | if self.db.get_pykotaid(uname): |
| 221 | self.validuser = True |
| 222 | else: |
173 | | try: |
174 | | query = self.sql.query("SELECT balance FROM users WHERE id='%s'" % (self.pykotauid)) |
175 | | self.pykotabalance = float((query.dictresult()[0])['balance']) |
176 | | self.printlabel.set_label("%s%.2f%s" % ("<big><b>$",self.pykotabalance,"</b></big>")) |
177 | | except: |
178 | | print "balance sql error..." |
179 | | try: |
180 | | query = self.sql.query("SELECT lifetimepaid FROM users WHERE id='%s'" % (self.pykotauid)) |
181 | | self.pykotalifebalance = float((query.dictresult()[0])['lifetimepaid']) |
182 | | print "%s%.2f" % ("$", self.pykotalifebalance) |
183 | | except: |
184 | | print "lifetimepaid sql error..." |
185 | | |
186 | | |
187 | | |
| 229 | return |
| 230 | |
| 231 | #self.liststore.append(['string text']) |
| 232 | |
| 233 | balance = self.db.get_pykotabalance() |
| 234 | if balance : |
| 235 | self.printlabel.set_label("%s%.2f%s" % ("<big><b>$",balance,"</b></big>")) |
| 236 | else: |
| 237 | result = gtk.RESPONSE_CANCEL |
| 238 | dlg = gtk.MessageDialog(None,gtk.DIALOG_MODAL | |
| 239 | gtk.DIALOG_DESTROY_WITH_PARENT,gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE, |
| 240 | "Cannot retrieve your printing balance.\nPlease contact technical suppport...", ) |
| 241 | result = dlg.run() |
| 242 | dlg.destroy() |
| 243 | self.validuser = False |
| 244 | return |
| 245 | |
| 246 | if not self.db.get_pykotalifebalance(): |
| 247 | result = gtk.RESPONSE_CANCEL |
| 248 | dlg = gtk.MessageDialog(None,gtk.DIALOG_MODAL | |
| 249 | gtk.DIALOG_DESTROY_WITH_PARENT,gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE, |
| 250 | "Cannot retrieve your life time printing balance.\nPlease contact technical suppport...", ) |
| 251 | result = dlg.run() |
| 252 | dlg.destroy() |
| 253 | self.validuser = False |
| 254 | return |
| 255 | |
| 256 | # Only set transfer button if both card balance and username valid |
| 257 | if self.cardbalance > 0.1 and self.validuser: |
| 258 | self.spinbutton.set_sensitive(True) |
| 259 | |
| 260 | |
190 | | self.addbalance = self.spinbutton.get_value() |
191 | | newbalance = self.addbalance + self.pykotabalance |
192 | | lifetimebalance = self.pykotalifebalance + self.addbalance |
193 | | self.sc.set_balance(newbalance, lifetimebalance, self.pykotauid) |
| 263 | addbalance = self.spinbutton.get_value() |
| 264 | |
| 265 | if not self.db.set_pykotabalances(addbalance): |
| 266 | result = gtk.RESPONSE_CANCEL |
| 267 | dlg = gtk.MessageDialog(None,gtk.DIALOG_MODAL | |
| 268 | gtk.DIALOG_DESTROY_WITH_PARENT,gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE, |
| 269 | "An error was encountered while updating your record.\nPlease contact technical support.") |
| 270 | result = dlg.run() |
| 271 | dlg.destroy() |
| 272 | return |
| 273 | |
| 274 | self.sc.set_balance(addbalance, self.cardbalance) |
| 275 | time.sleep(3) |
280 | | def set_balance(self, new, life, uid): |
281 | | #self.ser.write(binascii.a2b_hex("0200012400000000000010040103003F0C")) |
282 | | #s2 = self.ser.read(8) |
283 | | #print binascii.b2a_hex(s2) |
284 | | |
285 | | try: |
286 | | query = self.scsql.query("UPDATE users SET balance=%s, lifetimepaid=%s WHERE id='%s'" % |
287 | | (new, life, uid)) |
288 | | except: |
289 | | print "sql error..." |
290 | | result = gtk.RESPONSE_CANCEL |
291 | | dlg = gtk.MessageDialog(None,gtk.DIALOG_MODAL | |
292 | | gtk.DIALOG_DESTROY_WITH_PARENT,gtk.MESSAGE_INFO, gtk.BUTTONS_CLOSE, |
293 | | "An error was encountered while updating your record.\nPlease contact technical support.") |
294 | | result = dlg.run() |
295 | | dlg.destroy() |
296 | | |
297 | | """ |
298 | | def writeUserAccountBalance(self, user, newbalance, newlifetimepaid=None) : |
299 | | #Sets the new account balance and eventually new lifetime paid. |
300 | | if newlifetimepaid is not None : |
301 | | self.doModify("UPDATE users SET balance=%s, lifetimepaid=%s WHERE id=%s" % (self.doQuote(newbalance), self.doQuote(newlifetimepaid), self.doQuote(user.ident))) |
302 | | else : |
303 | | self.doModify("UPDATE users SET balance=%s WHERE id=%s" % (self.doQuote(newbalance), self.doQuote(user.ident))) |
304 | | |
305 | | def writeNewPayment(self, user, amount, comment="") : |
306 | | #Adds a new payment to the payments history. |
307 | | self.doModify("INSERT INTO payments (userid, amount, description) VALUES (%s, %s, %s)" % (self.doQuote(user.ident), self.doQuote(amount), self.doQuote(comment))) |
308 | | |
309 | | """ |
310 | | |
| 380 | def set_balance(self, subvalue, cardbalance): |
| 381 | newbalance = cardbalance - subvalue |
| 382 | a = (str(newbalance)).split('.') |
| 383 | b = a[0] + string.ljust(a[1],3,'0') |
| 384 | c = "%X" % (string.atoi(b)) |
| 385 | d = string.zfill(c,16) |
| 386 | chksum = self.checksum(d) |
| 387 | decrementvalue = "02000124" + d + "0103" + chksum + "0C" |
| 388 | |
| 389 | if TESTMODE: |
| 390 | print "Current card balance -> ", cardbalance |
| 391 | print "Amount to subtract from card -> ", subvalue |
| 392 | print "New card balance -> ", newbalance |
| 393 | print "Checksum -> ", chksum |
| 394 | print "Sent to card -> ",decrementvalue |
| 395 | return |
| 396 | |
| 397 | print "Sent to card -> ",decrementvalue |
| 398 | self.ser.write(binascii.a2b_hex(decrementvalue)) |
| 399 | s2 = self.ser.read(8) |
| 400 | print "Result -> ", binascii.b2a_hex(s2) |
| 401 | |
| 425 | |
| 426 | |
| 427 | class pgsql: |
| 428 | def __init__(self): |
| 429 | self.sql = None |
| 430 | self.username = '' |
| 431 | self.pykotauid = '' |
| 432 | self.balance = 0 |
| 433 | self.lifebalance = 0 |
| 434 | self.alreadyopen = False |
| 435 | |
| 436 | def pgopen(self): |
| 437 | try: |
| 438 | self.sql = pg.connect(dbname=DBNAME, host=HOST, port=PORT, user=USER, passwd=PASS) |
| 439 | self.alreadyopen = True |
| 440 | return True |
| 441 | except: |
| 442 | print "Problem opening database on server " + HOST + "...." |
| 443 | return False |
| 444 | |
| 445 | def pgclose(self): |
| 446 | self.username = '' |
| 447 | self.pykotauid = '' |
| 448 | self.balance = 0 |
| 449 | self.lifebalance = 0 |
| 450 | self.alreadyopen = False |
| 451 | self.sql.close() |
| 452 | |
| 453 | def get_userslist(self, uname,ls): |
| 454 | try: |
| 455 | query = self.sql.query("SELECT username FROM users WHERE username LIKE '%s'" % (uname+'%')) |
| 456 | users = query.getresult() |
| 457 | print "Users are ->", users |
| 458 | ls.clear() |
| 459 | for i in users: |
| 460 | ls.append([i[0]]) |
| 461 | #self.username = uname |
| 462 | return True |
| 463 | except: |
| 464 | #print "Username is invalid" |
| 465 | return False |
| 466 | |
| 467 | |
| 468 | def get_pykotaid(self, uname): |
| 469 | try: |
| 470 | query = self.sql.query("SELECT id FROM users WHERE username='%s'" % (uname)) |
| 471 | self.pykotauid = (query.dictresult()[0])['id'] |
| 472 | print "User ID is ->", self.pykotauid |
| 473 | self.username = uname |
| 474 | return True |
| 475 | except: |
| 476 | print "Username is invalid" |
| 477 | return False |
| 478 | |
| 479 | def get_pykotabalance(self): |
| 480 | try: |
| 481 | query = self.sql.query("SELECT balance FROM users WHERE id='%s'" % (self.pykotauid)) |
| 482 | self.balance = float((query.dictresult()[0])['balance']) |
| 483 | return self.balance |
| 484 | except: |
| 485 | print "balance sql error..." |
| 486 | return None |
| 487 | |
| 488 | def get_pykotalifebalance(self): |
| 489 | try: |
| 490 | query = self.sql.query("SELECT lifetimepaid FROM users WHERE id='%s'" % (self.pykotauid)) |
| 491 | self.lifebalance = float((query.dictresult()[0])['lifetimepaid']) |
| 492 | print "%s%.2f" % ("pykotalifebalance -> $", self.lifebalance) |
| 493 | return True |
| 494 | except: |
| 495 | print "lifetimepaid sql error..." |
| 496 | return False |
| 497 | |
| 498 | def set_pykotabalances(self, addbalance): |
| 499 | newbalance = addbalance + self.balance |
| 500 | newlifebalance = self.lifebalance + addbalance |
| 501 | try: |
| 502 | query = self.sql.query("UPDATE users SET balance=%s, lifetimepaid=%s WHERE id='%s'" % |
| 503 | (newbalance, newlifebalance, self.pykotauid)) |
| 504 | return True |
| 505 | except: |
| 506 | print "sql update error..." |
| 507 | return False |