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

Commit

Permalink
HTTP server to handle lead scoring MongoDB
Browse files Browse the repository at this point in the history
  • Loading branch information
cuducos committed Jan 2, 2017
1 parent 325b5e8 commit 5cf49c0
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
.python-version
__pycache__/
htmlcov/
lead-scoring/.env

2 changes: 2 additions & 0 deletions lead-scoring/.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
MONGODB_SERVER=mongodb://username:password@server:port
MONGODB_DATABASE=database
63 changes: 59 additions & 4 deletions lead-scoring/index.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,68 @@
from flask import Flask
import json

from decouple import config
from flask import Flask, jsonify, request
from pymongo import MongoClient

from dataset import full_path, ranking

ranking().to_csv(full_path('ranking.csv'), index=False)
app = Flask(__name__)
mongodb = MongoClient(config('MONGODB_SERVER'))
db = getattr(mongodb, config('MONGODB_DATABASE'))


@app.route('/', methods=['GET'])
def get():
return jsonify([clean(document) for document in db.reports.find()])


@app.route('/', methods=['POST'])
def post():
data = clean(request.get_json())
if not all(data.get(key) for key in ('documents', 'email')):
error = {'error': 'Missing `documents` and `email` data.'}
return jsonify(error), 400

filter_ = {'documents': data['documents'], 'email': data['email']}
db.reports.update_one(filter_, {'$set': data}, upsert=True)
return jsonify(clean(db.reports.find(filter_)[0]))


def clean(document):
whitelist = (
'documents',
'email',
'illegal',
'refund',
'report_id',
'under_investigation'
)
cleaned = {key: val for key, val in document.items() if key in whitelist}

if isinstance(cleaned.get('documents'), str):
ids = cleaned['documents'].split(',')
cleaned['documents'] = list(filter(bool, map(to_int, ids)))

if isinstance(cleaned.get('refund'), str):
try:
cleaned['refund'] = float(cleaned['refund'])
except ValueError:
cleaned['refund'] = None

for key in ('illegal', 'under_investigation'):
if isinstance(cleaned.get(key), str):
cleaned[key] = cleaned[key].lower() in ('true', '1')

return cleaned


def to_int(value):
try:
return int(value)
except ValueError:
return None

@app.route('/')
def hello():
return 'Hello World!'

if __name__ == '__main__':
app.run()
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ git+https://github.com/datasciencebr/serenata-toolbox.git#egg=serenata-toolbox
Flask==0.11.1
geopy>=1.11.0
pymongo==3.4.0
python-decouple==3.0
scikit-learn>=0.17
scipy>=0.18

0 comments on commit 5cf49c0

Please sign in to comment.