smscast/dbmsgs.py

32 lines
1.1 KiB
Python

import config
import MySQLdb
db = MySQLdb.connect(user=config.MYSQL_USER, passwd=config.MYSQL_PASSWD, db=config.MYSQL_DB)
def logmsg(msg, to, sentby):
c = db.cursor()
try:
c.execute("""INSERT INTO sendlog (msg, sent_by) VALUES (%s, %s)""", (msg, sentby))
sendlogid = c.lastrowid
for recipient in to:
c.execute("""INSERT INTO sendlog_to (id, recipient) VALUES (%s, %s)""", (sendlogid, recipient))
db.commit()
except:
print "um error"
db.rollback()
def getloggedmsgs(howmany):
c = db.cursor(MySQLdb.cursors.DictCursor)
if howmany:
c.execute("""SELECT sendlog.*, users.name AS sent_by_name FROM sendlog INNER JOIN users ON sendlog.sent_by = users.uid ORDER BY id DESC LIMIT %s""", (howmany,))
else:
c.execute("""SELECT sendlog.*, users.name AS sent_by_name FROM sendlog INNER JOIN users ON sendlog.sent_by = users.uid ORDER BY id DESC""")
rows = c.fetchall()
for r in rows:
c.execute("""SELECT people.id, people.name FROM sendlog_to INNER JOIN people ON sendlog_to.recipient = people.id WHERE sendlog_to.id = %s """, (r['id'],))
r['to'] = c.fetchall()
db.commit()
return rows