Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Cleaned up and commented JS code.

  • Loading branch information...
commit 6b876872a5049308edb96d3665a747ea805c268b 1 parent ff7decf
@akavlie authored
View
BIN  sms/__init__.pyc
Binary file not shown
View
BIN  sms/config.pyc
Binary file not shown
View
BIN  sms/model.pyc
Binary file not shown
View
63 sms/static/sms.js
@@ -1,42 +1,37 @@
var status_checker = 0;
-$(function() {
- $('#test').click(function() {
- $('.sent_message:first').slideToggle(1000);
- });
-
- $("#sms_form").submit(function() {
- // Post form data via AJAX
- $.post($SCRIPT_ROOT + "/sms", $("#sms_form").serialize(),
- function(data) {
- // Insert returned HTML before the first message
- $('#sent_message_list h2').after(data);
- // Animate height expansion, then opacity
- $('.new').animate({height: 'toggle'})
- .animate({opacity: 1})
- // Remove class so it doesn't catch animation again
- .removeClass('new');
- // Check status of new messages at set interval
- if(status_checker==0) {
- status_checker = setInterval('checkStatus()', 5000);
- }
- });
- return false;
- });
-
+$("#sms_form").submit(function() {
+ // Post form data via AJAX
+ $.post($SCRIPT_ROOT + "/sms", $("#sms_form").serialize(),
+ function(data) {
+ // Insert returned HTML after the heading
+ $('#sent_message_list h2').after(data);
+ // Animate height expansion, then opacity
+ $('.new').animate({height: 'toggle'})
+ .animate({opacity: 1})
+ // Remove class so it doesn't catch animation again
+ .removeClass('new');
+ // Check status of new messages at set interval
+ if(status_checker==0) {
+ status_checker = setInterval('checkStatus()', 5000);
+ }
+ });
+ return false;
});
function checkStatus() {
- $('.queued, .sending, .fake').children('.status').each(function(i) {
- if ($(this).text() == 'fake') {
- $(this).html('TEST');
- } else {
- $(this).html('FAIL');
- }
- $(this).parent().removeClass('queued sending fake');
+ // Check every message on the page with status "queued" or "sending" --
+ // Twilio's status possibilities for unsent messsages
+ $('.queued, .sending').children('.status').each(function(i) {
+ var sid = $(this).parent().find('.sid').html();
+ // Update status based on check with twilio
+ $(this).load($SCRIPT_ROOT + '/sms/update/' + sid, function(status) {
+ // Add class based on current status; remove former status class
+ $(this).parent().addClass(status).removeClass('queued sending');
});
- // Stop checking message status
- clearInterval(status_checker);
- status_checker = 0;
+ });
+ // Stop checking message status
+ clearInterval(status_checker);
+ status_checker = 0;
}
View
4 sms/templates/index.html
@@ -17,14 +17,14 @@
<div id="sent_message_list">
<h2>Message History</h2>
{% for message in sent_messages %}
- <div class="sent_message">
+ <div class="sent_message {{ message.status }}">
<div class="status">{{ message.status }}</div>
<div class="sent_body">
<div class="phone">{{ message.phone }}</div>
<div class="time">{{ message.date }}</div>
<div class="clear"></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>
{% endfor %}
View
2  sms/templates/sent_message.html
@@ -5,6 +5,6 @@
<div class="time">{{ sms.date_created }}</div>
<div class="clear"></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>
View
6 sms/tw_send.py
@@ -20,16 +20,12 @@ def twilio_send(phone_number, message):
def twilio_update(sid):
"""Update status for a given SMS. """
- data = {'From': app.config['CALLER_ID'],
- 'To': phone_number,
- 'Body': message}
-
account = twilio.Account(app.config['ACCOUNT_SID'],
app.config['ACCOUNT_TOKEN'])
tw_response = account.request('/%s/Accounts/%s/SMS/Messages/%s.json' %
(app.config['API_VERSION'],
app.config['ACCOUNT_SID'], sid),
- 'GET', data)
+ 'GET')
return tw_response
View
BIN  sms/tw_send.pyc
Binary file not shown
View
10 sms/views.py
@@ -5,7 +5,7 @@
from model import db, User, SentMessage, FrequentText
from datetime import datetime
-from tw_send import twilio_send
+from tw_send import twilio_send, twilio_update
@app.route('/')
def index():
@@ -32,17 +32,17 @@ def sms():
# flash('SMS sent to %s' % request.form['phone_number'])
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):
"""Update an SMS based on query from Twilio. """
tw_response = twilio_update(sid)
sms_data = json.loads(tw_response)
- sms = SentMessage.query.filter_by('sid=%s' % sid).one()
- sms.status = sms_data.status
+ sms = SentMessage.query.filter_by(sid=sid).one()
+ sms.status = sms_data['status']
db.session.commit()
- return sms_data.status
+ return sms.status
View
BIN  sms/views.pyc
Binary file not shown
Please sign in to comment.
Something went wrong with that request. Please try again.