Skip to content
This repository has been archived by the owner on Oct 1, 2020. It is now read-only.

Commit

Permalink
fix twilio logs update
Browse files Browse the repository at this point in the history
  • Loading branch information
monty5811 committed Feb 1, 2017
1 parent 6e47d13 commit 416e086
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 89 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

## [v1.16.2]

### Fixed

- Fix fetching logs from Twilio

## [v1.16.1]

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.16.1
1.16.2
2 changes: 1 addition & 1 deletion ansible/env_vars/base.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---

git_repo: https://github.com/monty5811/apostello.git
git_version: v1.16.1
git_version: v1.16.2

project_name: apostello
application_name: apostello
Expand Down
64 changes: 9 additions & 55 deletions apostello/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,71 +70,25 @@ def fetch_generator(direction):
return []


def fetch_list(direction, page_id):
"""Fetch list from twilio."""
if direction == 'in':
return twilio_client.messages.list(
page=page_id, page_size=50, to=settings.TWILIO_FROM_NUM
)
if direction == 'out':
return twilio_client.messages.list(
page=page_id, page_size=50, from_=settings.TWILIO_FROM_NUM
)
return []


def check_log(direction, page_id, fetch_all):
def check_log(direction):
"""Abstract check log function."""
if direction == 'in':
sms_handler = handle_incoming_sms
elif direction == 'out':
sms_handler = handle_outgoing_sms

check_next_page = False
if fetch_all:
# we want to iterate over all the incoming messages
sms_page = fetch_generator(direction)
else:
# we only want to iterate over the most recent messages to begin with
try:
sms_page = fetch_list(direction, page_id)
except TwilioRestException as e:
if e.msg == "Page number out of range":
# last page
return []
else:
raise e
# we want to iterate over all the incoming messages
sms_page = fetch_generator(direction)

for msg in sms_page:
check_next_page = sms_handler(msg) or check_next_page

if fetch_all:
# have looped over all messages and we are done
return

if check_next_page:
check_log(direction, page_id + 1, fetch_all)


def check_incoming_log(page_id=0, fetch_all=False):
"""
Check Twilio's logs for messages that have been sent to our number.
page_id: Twilio log page to start with.
fetch_all: If set to True, all messages on Twilio will be checked. If False,
only the first 50 messages will be checked. If a missing message is found in
these 50, the next 50 will also be checked.
"""
check_log('in', page_id, fetch_all)

def check_incoming_log():
"""Check Twilio's logs for messages that have been sent to our number."""
check_log('in')

def check_outgoing_log(page_id=0, fetch_all=False):
"""
Check Twilio's logs for messages that we have sent.

page_id: Twilio log page to start with.
fetch_all: If set to True, all messages on Twilio will be checked. If False,
only the first 50 messages will be checked. If a missing message is found in
these 50, the next 50 will also be checked.
"""
check_log('out', page_id, fetch_all)
def check_outgoing_log():
"""Check Twilio's logs for messages that we have sent."""
check_log('out')
2 changes: 1 addition & 1 deletion apostello/management/commands/import_incoming_sms.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ class Command(BaseCommand):

def handle(self, *args, **options):
"""Handle the command."""
check_incoming_log(fetch_all=True)
check_incoming_log()
2 changes: 1 addition & 1 deletion apostello/management/commands/import_outgoing_sms.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ class Command(BaseCommand):

def handle(self, *args, **options):
"""Handle the command."""
check_outgoing_log(fetch_all=True)
check_outgoing_log()
8 changes: 4 additions & 4 deletions apostello/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,16 @@ def ask_for_name(person_from_pk, sms_body, ask_for_name):
# SMS logging and consistency checks


def check_incoming_log(page_id=0, fetch_all=False):
def check_incoming_log():
"""Update incoming log."""
from apostello.logs import check_incoming_log
check_incoming_log(page_id=page_id, fetch_all=fetch_all)
check_incoming_log()


def check_outgoing_log(page_id=0, fetch_all=False):
def check_outgoing_log():
"""Update outgoing log."""
from apostello.logs import check_outgoing_log
check_outgoing_log(page_id=page_id, fetch_all=fetch_all)
check_outgoing_log()


def log_msg_in(p, t, from_pk):
Expand Down
2 changes: 1 addition & 1 deletion scripts/ansible_install.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash
set -e
# setup some constants
AP_VER=v1.16.1
AP_VER=v1.16.2
REPO_URL=https://github.com/monty5811/apostello.git
HOME_DIR=/home/apostello
CUSTOM_VARS_FILE=$HOME_DIR/custom_vars.yml
Expand Down
23 changes: 2 additions & 21 deletions tests/test_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,12 @@ class TestImportLogs:

@twilio_vcr
def test_all_incoming(self):
logs.check_incoming_log(fetch_all=True)
logs.check_incoming_log()

@twilio_vcr
def test_incoming_consistent(self):
logs.check_incoming_log(fetch_all=False)

@twilio_vcr
def test_all_outgoing(self):
logs.check_outgoing_log(fetch_all=True)

@twilio_vcr
def test_outgoing_consistent(self):
logs.check_outgoing_log(fetch_all=False)
logs.check_outgoing_log()


@pytest.mark.django_db
Expand Down Expand Up @@ -85,15 +78,3 @@ def test_fetch_all_out(self):
@twilio_vcr
def test_fetch_all_bad(self):
assert isinstance(logs.fetch_generator('nope'), list)

@twilio_vcr
def test_fetch_page_in(self):
assert isinstance(logs.fetch_list('in', 0), list)

@twilio_vcr
def test_fetch_page_out(self):
assert isinstance(logs.fetch_list('out', 0), list)

@twilio_vcr
def test_fetch_page_bad(self):
assert isinstance(logs.fetch_list('nope', 0), list)
6 changes: 2 additions & 4 deletions tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,11 @@ def test_send_group(self):

@twilio_vcr
def test_check_log_consistent(self):
check_incoming_log(page_id=0, fetch_all=False)
check_incoming_log(fetch_all=True)
check_incoming_log()

@twilio_vcr
def test_check_outgoing_log_consistent(self):
check_outgoing_log(page_id=0, fetch_all=False)
check_outgoing_log(fetch_all=True)
check_outgoing_log()

def test_send_keyword_digest(self, keywords, smsin, users):
send_keyword_digest()
Expand Down

0 comments on commit 416e086

Please sign in to comment.