optimize amount of data sent by get_updates and introduce a basic addpeople page
This commit is contained in:
parent
87ace86369
commit
04c9110a9c
32
dbmsgs.py
32
dbmsgs.py
|
@ -16,12 +16,9 @@ def logmsg(msg, to, sentby):
|
|||
print "um error"
|
||||
db.rollback()
|
||||
|
||||
def getloggedmsgs(howmany):
|
||||
def getloggedmsgs(lastOut):
|
||||
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""")
|
||||
c.execute("""SELECT sendlog.*, users.name AS sent_by_name FROM sendlog INNER JOIN users ON sendlog.sent_by = users.uid WHERE sendlog.id > %s ORDER BY id DESC""", (lastOut,))
|
||||
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'],))
|
||||
|
@ -29,3 +26,28 @@ def getloggedmsgs(howmany):
|
|||
db.commit()
|
||||
return rows
|
||||
|
||||
def loginboundmsg(inmsg):
|
||||
c = db.cursor()
|
||||
try:
|
||||
# check if this sender is known to us.
|
||||
shortfrom = inmsg['From'][2:] # also check for another form of number where +1 is omitted
|
||||
c.execute("""SELECT id FROM people WHERE people.phonenum = %s or people.phonenum = %s LIMIT 1""", (inmsg['From'], shortfrom))
|
||||
row = c.fetchone()
|
||||
sender_id = -1 # default to this if unknown...
|
||||
if row: # found? great, update sender_id.
|
||||
sender_id = row[0]
|
||||
|
||||
c.execute("""INSERT INTO inbound_msgs (`From`, SmsMessageSid, AccountSid, Body, SmsStatus, from_known_person) VALUES (%s, %s, %s, %s, %s, %s)""", (inmsg['From'], inmsg['SmsMessageSid'], inmsg['AccountSid'], inmsg['Body'], inmsg['SmsStatus'], sender_id))
|
||||
db.commit()
|
||||
except:
|
||||
print "um error"
|
||||
print c._last_executed
|
||||
db.rollback()
|
||||
|
||||
def getinboundmsgs(lastIn):
|
||||
c = db.cursor(MySQLdb.cursors.DictCursor)
|
||||
c.execute("""SELECT inbound_msgs.id, inbound_msgs.from, inbound_msgs.body, people.id as pid, people.name FROM `inbound_msgs` LEFT JOIN people ON inbound_msgs.from_known_person = people.id WHERE inbound_msgs.id > %s ORDER BY inbound_msgs.id DESC""", (lastIn,))
|
||||
print c._last_executed
|
||||
rows = c.fetchall()
|
||||
db.commit()
|
||||
return rows
|
||||
|
|
|
@ -55,3 +55,14 @@ class LibAddrBook:
|
|||
self.db.commit()
|
||||
ids = [r[0] for r in c.fetchall()]
|
||||
return ids
|
||||
|
||||
def add_person(self, name, num):
|
||||
c = self.db.cursor()
|
||||
try:
|
||||
c.execute("""INSERT INTO people (name, phonenum) VALUES (%s, %s)""", (name, num))
|
||||
self.db.commit()
|
||||
return True
|
||||
except:
|
||||
print "error adding user"
|
||||
self.db.rollback()
|
||||
return False
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
from flask import Flask, jsonify, render_template, request, json, redirect, url_for
|
||||
#from libsmscast import LibSMSCast
|
||||
from mocksmscast import LibSMSCast
|
||||
from libaddrbook import LibAddrBook
|
||||
from flask.ext.login import login_user, logout_user, current_user, login_required, LoginManager
|
||||
|
@ -34,7 +35,11 @@ def send():
|
|||
@app.route('/_get_updates')
|
||||
@login_required
|
||||
def get_updates():
|
||||
return jsonify(msgs=dbmsgs.getloggedmsgs(None))
|
||||
lastOut = request.args.get('lastOut')
|
||||
if not lastOut: lastOut = 0
|
||||
lastIn = request.args.get('lastIn')
|
||||
if not lastIn: lastIn = 0
|
||||
return jsonify(msgs=dbmsgs.getloggedmsgs(lastOut), inbound=dbmsgs.getinboundmsgs(lastIn))
|
||||
|
||||
@app.route('/_get_people_in_groups')
|
||||
@login_required
|
||||
|
@ -59,11 +64,36 @@ def get_groups():
|
|||
groups = addr.get_groups()
|
||||
return jsonify(groups=groups)
|
||||
|
||||
@app.route('/addperson', methods=["GET","POST"])
|
||||
@login_required
|
||||
def add_person():
|
||||
statustext = ""
|
||||
if request.method == "POST":
|
||||
addr = LibAddrBook()
|
||||
name = request.form['name']
|
||||
num = request.form['num']
|
||||
state = addr.add_person(name, num)
|
||||
if not state: # failure
|
||||
statustext = "Failed to add person {}".format(name)
|
||||
else: # success
|
||||
statustext = "Successfully added person {}".format(name)
|
||||
return render_template('addperson.html', statustext=statustext)
|
||||
|
||||
|
||||
@app.route('/')
|
||||
@login_required
|
||||
def index():
|
||||
return render_template('index.html')
|
||||
|
||||
@app.route("/_twilio_new_sms", methods=["POST"])
|
||||
def twilio_new_sms():
|
||||
if request.method == "POST":
|
||||
dbmsgs.loginboundmsg(request.form)
|
||||
return "(y)"
|
||||
#print request.form;
|
||||
return "(n)"
|
||||
|
||||
|
||||
@app.route("/login", methods=["GET", "POST"])
|
||||
def login():
|
||||
if current_user.is_authenticated() and current_user.is_active():
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
<p>add person</p>
|
||||
<p style="font-weight:bold">{{ statustext }}</p>
|
||||
<form name="addperson" action="/addperson" method="post">
|
||||
<p>name: <input type="text" name="name" autofocus /></p>
|
||||
<p>phone #: <input type="text" name="num" /></p>
|
||||
<p><input type="submit" value="Add" /></p>
|
||||
</form>
|
||||
|
|
@ -3,6 +3,7 @@
|
|||
<script type="text/javascript">
|
||||
window.SMSCAST = {};
|
||||
SMSCAST.largestMsgIdSeen = 0;
|
||||
SMSCAST.largestReceivedMsgIdSeen = 0;
|
||||
$(function() {
|
||||
$( document ).ready(function() {
|
||||
// doc ready. load users
|
||||
|
@ -112,11 +113,15 @@
|
|||
}
|
||||
|
||||
function loadUpdates(){
|
||||
$.getJSON($SCRIPT_ROOT + '/_get_updates', {},
|
||||
$.getJSON($SCRIPT_ROOT + '/_get_updates', {
|
||||
lastOut:SMSCAST.largestMsgIdSeen,
|
||||
lastIn:SMSCAST.largestReceivedMsgIdSeen
|
||||
},
|
||||
function(data) {
|
||||
updateSentMessages(data.msgs);
|
||||
updateReceivedMessages(data.inbound);
|
||||
});
|
||||
setTimeout(loadUpdates, 10000);
|
||||
setTimeout(loadUpdates, 5000);
|
||||
}
|
||||
function updateSentMessages(msgs){
|
||||
for(var i=msgs.length-1; i>=0; --i){
|
||||
|
@ -124,7 +129,9 @@
|
|||
$( "#msgs" ).prepend(getMessageHtml(msgs[i]));
|
||||
}
|
||||
}
|
||||
SMSCAST.largestMsgIdSeen = msgs[0].id;
|
||||
if(msgs.length > 0){ // if this payload had any messages, keep track of the largest seen id.
|
||||
SMSCAST.largestMsgIdSeen = msgs[0].id;
|
||||
}
|
||||
|
||||
}
|
||||
function getMessageHtml(msg){
|
||||
|
@ -137,12 +144,33 @@
|
|||
}
|
||||
}
|
||||
|
||||
return "<div class='msg' id='" + msg.id + "'>" +
|
||||
return "<div class='msg outbound' id='" + msg.id + "'>" +
|
||||
"<div class='msg_text'>" + msg.msg + "</div>" +
|
||||
"<div class='msg_info'>sent by " + msg.sent_by_name + " at " + msg.when + "</div>" +
|
||||
"<div class='msg_info'>to " + tolist + "</div>" +
|
||||
"</div>";
|
||||
}
|
||||
function updateReceivedMessages(msgs){
|
||||
for(var i=msgs.length-1; i>=0; --i){
|
||||
if(msgs[i].id > SMSCAST.largestReceivedMsgIdSeen){ // only add messages more recent
|
||||
$( "#inmsgs" ).prepend(getReceivedMessageHtml(msgs[i]));
|
||||
}
|
||||
}
|
||||
if(msgs.length > 0){ // if this payload had any messages, keep track of the largest seen id.
|
||||
SMSCAST.largestReceivedMsgIdSeen = msgs[0].id;
|
||||
}
|
||||
}
|
||||
function getReceivedMessageHtml(msg){
|
||||
var str = "<div class='msg inbound' id='" + msg.id + "'>" +
|
||||
"<div class='msg_text'>" + msg.body + "</div>";
|
||||
if(msg.name){
|
||||
str = str + "<div class='msg_info'>from " + msg.name + "</div>";
|
||||
}
|
||||
else {
|
||||
str = str + "<div class='msg_info'>from " + msg.from + "</div>";
|
||||
}
|
||||
return str + "</div>";
|
||||
}
|
||||
|
||||
});
|
||||
</script>
|
||||
|
@ -153,6 +181,7 @@
|
|||
font-family: "Myriad Pro", Arial;
|
||||
}
|
||||
.person, .group{
|
||||
display: inline-block;
|
||||
color: #222222;
|
||||
border: 2px solid #47aef5;
|
||||
border-radius: 10px;
|
||||
|
@ -254,7 +283,7 @@
|
|||
</div>
|
||||
<div class="column">
|
||||
<h2>received messages</h2>
|
||||
<div id="inmsgs">to be implemented ;)</div>
|
||||
<div id="inmsgs"></div>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
|
|
Loading…
Reference in New Issue