Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions manage.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from csv import DictReader
from datetime import datetime
import gzip
import logging
from logging.handlers import RotatingFileHandler
from pprint import pprint

from flask.ext.script import Manager
Expand All @@ -9,6 +11,9 @@
from taarifa_waterpoints import app
from taarifa_waterpoints.schemas import facility_schema, service_schema

handler = RotatingFileHandler('taarifawaterpoints.log', maxBytes=10000, backupCount=1)
handler.setLevel(logging.INFO)
app.logger.addHandler(handler)
manager = Manager(app)


Expand Down
25 changes: 25 additions & 0 deletions taarifa_waterpoints/taarifa_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""
Very, VERY basic python client lib for Taarifa

by the way, this is not how we'd want an actual client lib to be...
should be built on the requests lib, or standard lib stuff if we don't use any
sort of authentication. However, for now, this is fast to use taarifa_api + eve
for posts.
"""

from taarifa_api import add_document


def create_request(wp, status, first_name=None, last_name=None, email=None):
"""Create an example request reporting a broken waterpoint"""
r = {'service_code': 'wps001',
'attribute': {
'waterpoint_id': wp,
'status': status
},
'first_name': first_name,
'last_name': last_name,
'email': email
}
response = add_document('requests', r)
return response
60 changes: 60 additions & 0 deletions taarifa_waterpoints/taarifa_email.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import json

from taarifa_client import create_request


def handle_email_webhook(webhook_data):
"""Main function to handle an email webhook!"""
parsed_webhook_data = parse_email_webhook(webhook_data)

if parsed_webhook_data['subject'] == 'status':
return create_status_request(parsed_webhook_data)


def parse_email_webhook(webhook_data):
data = json.loads(webhook_data)
parsed_data = {
'email': data['message_data']['addresses']['from']['email'].strip(),
'name': data['message_data']['addresses']['from']['name'].strip(),
'subject': data['message_data']['subject'].strip().lower(), # since this is like a command, to lower as well
'body': data['message_data']['body'][0]['content'].strip()
}
return parsed_data


def create_status_request(data):
"""Parses the email body and pulls out waterpoint id + status [up, down]
email body format is:

Subject: status
Body:
<waterpoint_id>
<1/0> - 1 = functional, 0 = not functional

Then creates a new request with that information
"""
# parse body
parsed_body = data['body'].split()
names = data['name'].split()

email = data['email']
first_name = names[0]
last_name = names[1]
wp = parsed_body[0]

# set proper status
if parsed_body[1]:
status = 'functional'
else:
status = 'not functional'

params = {
'email': email,
'first_name': first_name,
'last_name': last_name,
'wp': wp,
'status': status
}

# make call
return create_request(**params)
18 changes: 15 additions & 3 deletions taarifa_waterpoints/taarifa_waterpoints.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from flask import send_from_directory

from flask import request, send_from_directory
import json

from taarifa_api import api as app, main
import taarifa_email

app.name = 'TaarifaWaterpoints'

Expand All @@ -25,14 +28,23 @@ def views(filename):
return send_from_directory(app.root_path + '/dist/views/', filename)


@app.route("/")
@app.route('/')
def index():
return send_from_directory(app.root_path + '/dist/', 'index.html')


@app.route("/favicon.ico")
@app.route('/favicon.ico')
def favicon():
return send_from_directory(app.root_path + '/dist/', 'favicon.ico')


@app.route('/email', methods=['POST'])
def email():
response = taarifa_email.handle_email_webhook(request.data)
data, _, _, status = response
app.logger.info('response status: %s\nresponse data: %s' % (status, data))
return 'True'


if __name__ == '__main__':
main()