diff --git a/dbmsgs.py b/dbmsgs.py new file mode 100644 index 0000000..658c723 --- /dev/null +++ b/dbmsgs.py @@ -0,0 +1,31 @@ +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 + diff --git a/libaddrbook.py b/libaddrbook.py index 3acc7ca..a564ed5 100644 --- a/libaddrbook.py +++ b/libaddrbook.py @@ -8,7 +8,8 @@ class LibAddrBook: self.db = MySQLdb.connect(user=config.MYSQL_USER, passwd=config.MYSQL_PASSWD, db=config.MYSQL_DB) def get_everyone(self): c = self.db.cursor() - c.execute("""SELECT * from people""") + c.execute("""SELECT * from people ORDER BY people.name ASC""") + self.db.commit() ppl = [] nums = [] for row in c.fetchall(): @@ -21,6 +22,7 @@ class LibAddrBook: def get_groups(self): c = self.db.cursor() c.execute("""SELECT * from groups""") + self.db.commit() groups = [] for row in c.fetchall(): groups.append({'gid': row[0], 'name': row[1]}) @@ -28,10 +30,12 @@ class LibAddrBook: # todo: make sure ids is actually a list of numbers? def get_nums_with_ids(self, ids): + if len(ids) == 0: return [] c = self.db.cursor() idlist = ', '.join('{}'.format(x) for x in ids) print idlist c.execute("""SELECT * from people WHERE id IN ({})""".format(idlist)) + self.db.commit() nums = [] for row in c.fetchall(): nums.append(row[PHONE_NUM_COL]) @@ -48,5 +52,6 @@ class LibAddrBook: grouplist = ', '.join('{}'.format(x) for x in groups) print grouplist c.execute("""SELECT people.id from people INNER JOIN memberships ON people.id = memberships.person_id WHERE memberships.group_id IN ({})""".format(grouplist)) + self.db.commit() ids = [r[0] for r in c.fetchall()] return ids diff --git a/smscastwebapp.py b/smscastwebapp.py index d0bb084..f06e678 100644 --- a/smscastwebapp.py +++ b/smscastwebapp.py @@ -3,6 +3,7 @@ from mocksmscast import LibSMSCast from libaddrbook import LibAddrBook from flask.ext.login import login_user, logout_user, current_user, login_required, LoginManager import user +import dbmsgs app = Flask(__name__) app.secret_key = 'abcdgfkfdhgslkjh' # lol @@ -22,12 +23,19 @@ def send(): to = [int(x) for x in json.loads(tostr)] print to + dbmsgs.logmsg(msg, to, current_user.uid) + dest = addr.get_nums_with_ids(to) print dest howmany = len(dest) client.send(dest, msg) return jsonify(result="sent {} to {} numbers(s)".format(msg, howmany)) +@app.route('/_get_updates') +@login_required +def get_updates(): + return jsonify(msgs=dbmsgs.getloggedmsgs(None)) + @app.route('/_get_people_in_groups') @login_required def get_people_in_groups(): @@ -68,7 +76,7 @@ def login(): # do login because username and password have been specified uid = user.authenticate(username, password) if uid != None: - login_user(user.get(uid)) + login_user(user.get(uid), remember=True) return redirect(url_for('index')) return render_template('login.html') diff --git a/templates/index.html b/templates/index.html index bbac998..6150185 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,6 +1,8 @@ {% extends "layout.html" %} {% block body %} + +

logged in as {{ current_user.name }} log out

smscast

-

send

-

res

+
+
+

Send!

+

Select recipients below and enter a message to broadcast in the box above.

groups

people

+
+
+

sent messages

+
+
+
+

received messages

+
to be implemented ;)
+
+ + {% endblock %} diff --git a/templates/login.html b/templates/login.html index f8772b1..1c1855e 100644 --- a/templates/login.html +++ b/templates/login.html @@ -1,3 +1,4 @@ +

log in

Username:

Password:

diff --git a/user.py b/user.py index 1641b65..9bb2d6c 100644 --- a/user.py +++ b/user.py @@ -10,6 +10,7 @@ class User: c = self.db.cursor() c.execute("""SELECT name FROM users WHERE uid = %s""", (uid,)) self.name = c.fetchall()[0][0] + self.db.commit() def is_authenticated(self): return True # stub @@ -33,6 +34,7 @@ def authenticate(username, password): # get salt for the user c.execute("""SELECT salt, password, uid FROM users WHERE name = %s """ , (username,)) + db.commit() print "getting salt for {}".format(username) if(c.rowcount == 0): return None row = c.fetchall()[0]