#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import json

import riak
import memcache

import tornado.ioloop
import tornado.web
import tornado.gen
import tornado.autoreload

from pandas import read_json

from utils import pandas_to_dict
from admin.views import CubeHandler


class MainHandler(tornado.web.RequestHandler):
    @tornado.web.asynchronous
    def get(self):
        self.render('index.html')


class ProcessHandler(tornado.web.RequestHandler):
    @tornado.web.asynchronous
    def post(self):
        mc = memcache.Client(['127.0.0.1:11211'], debug=0)
        myClient = riak.RiakClient(protocol='http',
                                   http_port=8098,
                                   host='127.0.0.1')
        myBucket = myClient.bucket('openmining')

        columns = json.loads(myBucket.get('testando-columns').data)
        fields = columns
        try:
            if len(self.get_argument('fields')) >= 1:
                fields = self.get_argument('fields').split(',')
        except:
            pass

        fields_json = json.dumps(fields)
        if mc.get('testando') and mc.get('testando-columns') == fields_json:
            self.write(mc.get('testando'))
            self.finish()

        mc.set('testando-columns', fields_json)

        df = read_json(myBucket.get('testando').data)

        read = df[fields]
        convert = pandas_to_dict(read)

        write = json.dumps({'columns': fields, 'json': convert})
        mc.set('testando', write)
        self.write(write)


PROJECT_PATH = os.path.join(os.path.abspath(os.path.dirname(__file__)))
application = tornado.web.Application([
    (r'/assets/(.*)', tornado.web.StaticFileHandler,
        {'path': "{}/{}".format(PROJECT_PATH, "assets")}),
    (r"/process.json", ProcessHandler),
    (r"/admin/cube", CubeHandler),
    (r"/", MainHandler),
])


if __name__ == "__main__":
    print "openmining.io server starting..."

    def fn():
        print "openmining.io before reloading..."

    application.listen(8888)
    tornado.autoreload.add_reload_hook(fn)
    tornado.autoreload.start()
    tornado.ioloop.IOLoop.instance().start()

@@ -276,16 +276,35 @@
 eHandler
+, ConnectionHandler
 %0A%0A%0Aclass
@@ -1610,16 +1610,95 @@
 le__)))%0A
+settings = dict(%0A    template_path=%22%7B%7D/%7B%7D%22.format(PROJECT_PATH, 'templates'))%0A%0A
 applicat
@@ -1873,32 +1873,79 @@
 rocessHandler),%0A
+    (r%22/admin/connection%22, ConnectionHandler),%0A
     (r%22/admin/cu
@@ -1989,16 +1989,28 @@
 dler),%0A%5D
+, **settings
 )%0A%0A%0Aif _

Add admin on urls