Skip to content

Commit

Permalink
Cleaned up and commented JS code.
Browse files Browse the repository at this point in the history
  • Loading branch information
akavlie committed Oct 7, 2010
1 parent ff7decf commit 6b87687
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 47 deletions.
Binary file removed sms/__init__.pyc
Binary file not shown.
Binary file removed sms/config.pyc
Binary file not shown.
Binary file removed sms/model.pyc
Binary file not shown.
63 changes: 29 additions & 34 deletions sms/static/sms.js
@@ -1,42 +1,37 @@
var status_checker = 0; var status_checker = 0;


$(function() { $("#sms_form").submit(function() {
$('#test').click(function() { // Post form data via AJAX
$('.sent_message:first').slideToggle(1000); $.post($SCRIPT_ROOT + "/sms", $("#sms_form").serialize(),
}); function(data) {

// Insert returned HTML after the heading
$("#sms_form").submit(function() { $('#sent_message_list h2').after(data);
// Post form data via AJAX // Animate height expansion, then opacity
$.post($SCRIPT_ROOT + "/sms", $("#sms_form").serialize(), $('.new').animate({height: 'toggle'})
function(data) { .animate({opacity: 1})
// Insert returned HTML before the first message // Remove class so it doesn't catch animation again
$('#sent_message_list h2').after(data); .removeClass('new');
// Animate height expansion, then opacity // Check status of new messages at set interval
$('.new').animate({height: 'toggle'}) if(status_checker==0) {
.animate({opacity: 1}) status_checker = setInterval('checkStatus()', 5000);
// Remove class so it doesn't catch animation again }
.removeClass('new'); });
// Check status of new messages at set interval return false;
if(status_checker==0) {
status_checker = setInterval('checkStatus()', 5000);
}
});
return false;
});

}); });


function checkStatus() { function checkStatus() {
$('.queued, .sending, .fake').children('.status').each(function(i) { // Check every message on the page with status "queued" or "sending" --
if ($(this).text() == 'fake') { // Twilio's status possibilities for unsent messsages
$(this).html('TEST'); $('.queued, .sending').children('.status').each(function(i) {
} else { var sid = $(this).parent().find('.sid').html();
$(this).html('FAIL'); // Update status based on check with twilio
} $(this).load($SCRIPT_ROOT + '/sms/update/' + sid, function(status) {
$(this).parent().removeClass('queued sending fake'); // Add class based on current status; remove former status class
$(this).parent().addClass(status).removeClass('queued sending');
}); });
// Stop checking message status });
clearInterval(status_checker); // Stop checking message status
status_checker = 0; clearInterval(status_checker);
status_checker = 0;
} }


4 changes: 2 additions & 2 deletions sms/templates/index.html
Expand Up @@ -17,14 +17,14 @@
<div id="sent_message_list"> <div id="sent_message_list">
<h2>Message History</h2> <h2>Message History</h2>
{% for message in sent_messages %} {% for message in sent_messages %}
<div class="sent_message"> <div class="sent_message {{ message.status }}">
<div class="status">{{ message.status }}</div> <div class="status">{{ message.status }}</div>
<div class="sent_body"> <div class="sent_body">
<div class="phone">{{ message.phone }}</div> <div class="phone">{{ message.phone }}</div>
<div class="time">{{ message.date }}</div> <div class="time">{{ message.date }}</div>
<div class="clear"></div> <div class="clear"></div>
<div class="message_text"><p>{{ message.message }}</p></div> <div class="message_text"><p>{{ message.message }}</p></div>
<div class="hidden">{{ message.sid }}</div> <div class="sid hidden">{{ message.sid }}</div>
</div> </div>
</div> </div>
{% endfor %} {% endfor %}
Expand Down
2 changes: 1 addition & 1 deletion sms/templates/sent_message.html
Expand Up @@ -5,6 +5,6 @@
<div class="time">{{ sms.date_created }}</div> <div class="time">{{ sms.date_created }}</div>
<div class="clear"></div> <div class="clear"></div>
<div class="message_text"><p>{{ sms.body }}</p></div> <div class="message_text"><p>{{ sms.body }}</p></div>
<div class="hidden">{{ sms.sid }}</div> <div class="sid hidden">{{ sms.sid }}</div>
</div> </div>
</div> </div>
6 changes: 1 addition & 5 deletions sms/tw_send.py
Expand Up @@ -20,16 +20,12 @@ def twilio_send(phone_number, message):
def twilio_update(sid): def twilio_update(sid):
"""Update status for a given SMS. """ """Update status for a given SMS. """


data = {'From': app.config['CALLER_ID'],
'To': phone_number,
'Body': message}

account = twilio.Account(app.config['ACCOUNT_SID'], account = twilio.Account(app.config['ACCOUNT_SID'],
app.config['ACCOUNT_TOKEN']) app.config['ACCOUNT_TOKEN'])


tw_response = account.request('/%s/Accounts/%s/SMS/Messages/%s.json' % tw_response = account.request('/%s/Accounts/%s/SMS/Messages/%s.json' %
(app.config['API_VERSION'], (app.config['API_VERSION'],
app.config['ACCOUNT_SID'], sid), app.config['ACCOUNT_SID'], sid),
'GET', data) 'GET')


return tw_response return tw_response
Binary file removed sms/tw_send.pyc
Binary file not shown.
10 changes: 5 additions & 5 deletions sms/views.py
Expand Up @@ -5,7 +5,7 @@
from model import db, User, SentMessage, FrequentText from model import db, User, SentMessage, FrequentText
from datetime import datetime from datetime import datetime


from tw_send import twilio_send from tw_send import twilio_send, twilio_update


@app.route('/') @app.route('/')
def index(): def index():
Expand All @@ -32,17 +32,17 @@ def sms():
# flash('SMS sent to %s' % request.form['phone_number']) # flash('SMS sent to %s' % request.form['phone_number'])
return render_template('sent_message.html', sms=sms_data) return render_template('sent_message.html', sms=sms_data)


@app.route('/sms/update/<sid>', methods=['POST']) @app.route('/sms/update/<sid>')
def sms_update(sid): def sms_update(sid):
"""Update an SMS based on query from Twilio. """ """Update an SMS based on query from Twilio. """


tw_response = twilio_update(sid) tw_response = twilio_update(sid)
sms_data = json.loads(tw_response) sms_data = json.loads(tw_response)


sms = SentMessage.query.filter_by('sid=%s' % sid).one() sms = SentMessage.query.filter_by(sid=sid).one()
sms.status = sms_data.status sms.status = sms_data['status']
db.session.commit() db.session.commit()


return sms_data.status return sms.status




Binary file removed sms/views.pyc
Binary file not shown.

0 comments on commit 6b87687

Please sign in to comment.