from flask import Flask, render_template, request, jsonify, session
import cf_deployment_tracker
import atexit
import os
import random
from services import load_credentials, get_database, get_watson_service, teardown_databases
import sys

# Emit Bluemix deployment event
cf_deployment_tracker.track()

app = Flask(__name__)

app.config['DATABASE'] = 'mydb'
app.secret_key = "\xfd4\xadtJ\x1a'\xed\xe9\x0e`{\xd4\x8a\x11.ah\x87j\t\xad\x9e\xac"

vcap = load_credentials()


def get_user():
    if 'username' not in session:
        user = str(random.randint(1, 10000))
        session['username'] = user
        print('User {0} created'.format(user))
    return session['username']


# On Bluemix, get the port number from the environment variable PORT
# When running this app on the local machine, default the port to 8080
port = int(os.getenv('PORT', 8080))


@app.route('/')
def home():
    return render_template('index.html')

# /* Endpoint to greet and add a new visitor to database.
# * Send a POST request to localhost:8080/api/visitors with body
# * {
# * 	"name": "Bob"
# * }
# */


@app.route('/api/visitors', methods=['GET'])
def get_visitor():
    user = get_user()

    db, client = get_database(app.config['DATABASE'])

    return jsonify([doc['name'] for doc in db if doc['user'] == user])

# /**
#  * Endpoint to get a JSON array of all the visitors in the database
#  * REST API example:
#  * <code>
#  * GET http://localhost:8080/api/visitors
#  * </code>
#  *
#  * Response:
#  * [ "Bob", "Jane" ]
#  * @return An array of all the visitor names
#  */


@app.route('/api/visitors', methods=['POST'])
def put_visitor():
    name = request.json['name']
    # Translate
    db, client = get_database(app.config['DATABASE'])
    translator = get_watson_service('language_translator')

    translated_name = translator.translate(name, source='es', target='en')

    user = get_user()

    data = {'name': translated_name, 'user': user}
    db.create_document(data)

    return 'La traduccion de {0} es {1}'.format(name, translated_name)


@atexit.register
def shutdown():
    teardown_databases()


if __name__ == '__main__':
    debug = False
    if len(sys.argv) > 1:
        if '--debug' in sys.argv:
            debug = True

    app.run(host='0.0.0.0', port=port, debug=debug)

@@ -230,16 +230,32 @@
 port sys
+%0Aimport datetime
 %0A%0A# Emit
@@ -1094,24 +1094,397 @@
  * %7D%0A# */%0A%0A%0A
+@app.route('/api/recent')%0Adef recent_searches():%0A    user = get_user()%0A    db, client = get_database(app.config%5B'DATABASE'%5D)%0A%0A    past_searches = %5B(doc%5B'name'%5D, doc%5B'timestamp'%5D) for doc in db if doc%5B'user'%5D == user%5D%5B::-1%5D%0A%0A    sorted_searches = %5Bname for (name, timestamp) in sorted(past_searches, key=lambda x: x%5B1%5D, reverse=True)%5D%0A%0A    return jsonify(sorted_searches)%0A%0A%0A
 @app.route('
@@ -2273,24 +2273,77 @@
 get_user()%0A%0A
+    timestamp = datetime.datetime.now().isoformat()%0A%0A
     data = %7B
@@ -2379,16 +2379,40 @@
 r': user
+, 'timestamp': timestamp
 %7D%0A    db

Add /api/recent for recent searches