# -*- coding: utf-8 -*-
import flask

from flask import Flask,jsonify,send_from_directory, render_template
from config import Config
import yaml
import os

from storage import MailThread,db_session

app = Flask(__name__, template_folder="templates", static_folder="static")
package_directory = os.path.dirname(os.path.abspath(__file__))

cfg = Config(file(os.path.join(package_directory, 'config.cfg')))


def render_index(mths,code=200):
    return render_template("index.html",mths=mths,
                           title=cfg.title.decode("utf8"),
                           ), code    
from classifier import get_pipe
mail_threads=db_session.query(MailThread).all()
pipe1,le=get_pipe("pipe1",b"answered")
pipe2,le2=get_pipe("pipe2b", b"maintopic")
answered=le.inverse_transform(pipe1.predict(mail_threads))
maintopic=le2.inverse_transform(pipe2.predict(mail_threads))
for i, t in enumerate(mail_threads):
    t.answered=answered[i]
    t.maintopic=maintopic[i]
@app.route("/")
def hello():
    mth=mail_threads
    return render_index(mth) 

