def getUser(self, uid = None, ck = None, email = None): if uid is None and ck is None and email is None: raise BadArgs("At least one of uid, ck, and email must be" + " given to getUser") try: cursor = self.__conn.cursor(MySQLdb.cursors.DictCursor) if uid is None and email is None: cursor.execute(""" SELECT _id, _email, _openid, _firstName, _lastName, _cellPhone, _memberSince, _lastSeen, _type, _cookie FROM """ + DbWorker.dbCfg['table.user'] + """ WHERE _cookie = %s """, (ck,)) elif ck is None and email is None: cursor.execute(""" SELECT _id, _email, _openid, _firstName, _lastName, _cellPhone, _memberSince, _lastSeen, _type, _cookie FROM """ + DbWorker.dbCfg['table.user'] + """ WHERE _id = %s """ , (uid,)) elif ck is None and uid is None: cursor.execute(""" SELECT _id, _email, _openid, _firstName, _lastName, _cellPhone, _memberSince, _lastSeen, _type, _cookie FROM """ + DbWorker.dbCfg['table.user'] + """ WHERE _email = %s """ , (email,)) else: cursor.execute(""" SELECT _id, _email, _openid, _firstName, _lastName, _cellPhone, _memberSince, _lastSeen, _type, _cookie FROM """ + DbWorker.dbCfg['table.user'] + """ WHERE _id = %s AND _cookie = %s """ , (uid, ck)) if cursor.rowcount == 0: raise RecordNotFound(""" Record not found for user in table %s with _id = %s and _cookie = %s """ % (DbWorker.dbCfg['table.user'], utils.nullForNone(uid), utils.nullForNone(ck))) if cursor.rowcount > 1: raise UniqueViolation(""" More than one record in table %s for _id = %d and ck = %d """ % (DbWorker.dbCfg['table.user'],uid,ck)) user = cursor.fetchone() cursor.close() utils.dbgMsg("User = %s" % json.dumps(user), DbWorker.globalCfg['debug'], DbWorker.globalCfg['debug.info'], __name__, DbWorker.currFrame.f_lineno, DbWorker.currFrame.f_code.co_filename) return user except: raise
def newBill(self, userCookie, amount, category, date, reportedBy, reportedAt = utils.timestamp(False), billType = 'individual', participants = [], tags = [], description = None, userAmounts = [], emails = []): cursor = self.__conn.cursor() try: uid = self.getUser(ck = userCookie)['_id'] ppl = participants if len(ppl) == 0: ppl.append(reportedBy) bill_id = utils.genIdentifier(["%d" % utils.genRandom(), "%d" % date, "%d" % reportedBy, category]) cursor.execute(""" INSERT INTO """ + DbWorker.dbCfg['table.bill'] + """ VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s) """ , (bill_id, amount, category, date, reportedAt, reportedBy, billType, ",".join([str(x) for x in ppl]), utils.joinOrNone(tags), description)) utils.dbgMsg("Inserted %d rows to table %s" % (cursor.rowcount, DbWorker.dbCfg['table.bill']), DbWorker.globalCfg['debug'], DbWorker.globalCfg['debug.info'], __name__, DbWorker.currFrame.f_lineno, DbWorker.currFrame.f_code.co_filename) amnts = userAmounts if (len(amnts) == 0): amnts.append(amount) if (len(ppl) != len(amnts) and len(ppl) != len(emails)): raise LogicalError("""#ppl and #amount #emails should be same. ppl = [%s], amnts = [%s], emails = [%s] """ % (",".join(["%d" % x for x in ppl]), ",".join(["%f" % x for x in amnts])), ",".join(emails)) idAmntComb = map(lambda x, y: (x, y), ppl, amnts) idAMntEmailComb = map(lambda (x, y), z: (x, y, z), idAmntComb, emails) for uid, amt, email in idAMntEmailComb: uid = int(uid) amt = float(amt) try: self.getUser(uid = uid) except (BadArgs, RecordNotFound): tstmp = utils.timestamp(False) tmpCookie = utils.genIdentifier(["%d" % utils.genRandom(), "%d" % uid, "%d" % tstmp]) self.userLogin(userId = uid, userCookie = tmpCookie, lastSeen = tstmp, email = email, openid = None, firstName = None, lastName = None, cellPhone = None, memberSince = tstmp, uType = 'auto') cursor.execute(""" INSERT INTO """ + DbWorker.dbCfg['table.expense'] +""" VALUES(%s, %s, %s, %s, %s, %s) """ , (bill_id, uid, amt, date, category, utils.joinOrNone(tags))) except: raise finally: self.__conn.commit() cursor.close()