# Path: core/web/forms.py
# class QuestionForm(Form):
#     name = StringField('Name', [Required()], description="The name you want to give this question.", )
#     prompt = TextAreaField('Prompt', [Optional()], description="The prompt for this question (optional).")
# 
#     def save(self):
#         from app import db
#         q = Question(user=current_user)
#         self.populate_obj(q)
#         db.session.add(q)
#         db.session.commit()
# 
# class EssayForm(Form):
#     text = TextAreaField('Text', [Required()], description="The text of the essay.")
#     actual_score = FloatField('Score (optional)', [Optional()],  description="The score the essay got, if it has a score.")
#     info = TextAreaField("Additional Info (optional)", [Optional()], description="Any additional info you want to store with this essay.")
# 
#     def save(self, question):
#         from app import db
#         e = Essay(question=question)
#         self.populate_obj(e)
#         db.session.add(e)
#         db.session.commit()
# 
# class EssayUploadForm(Form):
#     upload = FileField('File', [FileRequired(), FileAllowed(['csv'], 'CSV files only!')], description="The csv file with essays you want to upload.")
# 
#     def save(self, question, csvfile):
#         from app import db
#         csvfile.seek(0)
#         reader = csv.reader(csvfile, EssayDialect)
#         for i, row in enumerate(reader):
#             if i == 0:
#                 continue
# 
#             score = row[-1]
#             text = ",".join(row[:-1])
# 
#             if len(score) == 0:
#                 score = None
#             else:
#                 score = float(score)
#             e = Essay(question=question, text=text, actual_score=score)
#             db.session.add(e)
#         db.session.commit()
# 
# Path: scan/settings.py
# DB_URL = 'sqlite:///scan.db'
# DEBUG = True
# ROOT_PATH = path(__file__).dirname()
# REPO_PATH = ROOT_PATH.dirname()
# ENV_ROOT = REPO_PATH.dirname()
# DATA_PATH = os.path.join(REPO_PATH, "data")
# TEST_DATA_PATH = os.path.join(DATA_PATH, "test")
# MODEL_PATH = os.path.join(DATA_PATH, "models")
# SECRET_KEY = "30y^$e7henbp@#_w+z9)o9f8tovjrhoq(%vw=%#*(1-1esrh!_"
# USERNAME = "test"
# EMAIL = "test@test.com"
# PASSWORD = "test"
# SQLALCHEMY_DATABASE_URI = DB_URL
# SECURITY_REGISTERABLE = True
# SECURITY_RECOVERABLE = True
# SECURITY_SEND_REGISTER_EMAIL = False
# SECURITY_PASSWORD_HASH = "bcrypt"
# SECURITY_PASSWORD_SALT = SECRET_KEY
# BROKER_TRANSPORT_OPTIONS = {'visibility_timeout': 3600}
# BROKER_URL = 'redis://localhost:6379/3'
# CELERY_RESULT_BACKEND = 'redis://localhost:6379/3'
# CELERY_IMPORTS = ('core.tasks.tasks',)
# CACHE_TYPE = 'redis'
# CACHE_REDIS_URL = 'redis://localhost:6379/4'
# CACHE_REDIS_DB = 4
# MAX_FEATURES = 500
# MODEL_VERSION = 1
# 
# Path: core/database/models.py
# class Question(db.Model):
#     __tablename__ = 'questions'
# 
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(STRING_MAX))
#     description = db.Column(db.Text)
#     prompt = db.Column(db.Text)
#     user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
# 
#     user = db.relationship("User", backref=db.backref('questions', order_by=id))
# 
#     created = db.Column(db.DateTime(timezone=True), default=datetime.utcnow)
#     updated = db.Column(db.DateTime(timezone=True), onupdate=datetime.utcnow)
# 
# class Essay(db.Model):
#     __tablename__ = 'essays'
# 
#     id = db.Column(db.Integer, primary_key=True)
#     text = db.Column(db.Text)
#     actual_score = db.Column(db.Float)
#     info = db.Column(db.Text)
# 
#     predicted_score = db.Column(db.Float)
#     model_id = db.Column(db.Integer, db.ForeignKey('models.id'))
#     model = db.relationship("Model", backref=db.backref('essays', order_by=id))
# 
#     question_id = db.Column(db.Integer, db.ForeignKey('questions.id'))
#     question = db.relationship("Question", backref=db.backref('essays', order_by=id))
# 
#     created = db.Column(db.DateTime(timezone=True), default=datetime.utcnow)
#     updated = db.Column(db.DateTime(timezone=True), onupdate=datetime.utcnow)


from celery import states
from flask import Blueprint, render_template, abort, request, url_for, jsonify
from werkzeug.utils import redirect, secure_filename
from core.web.forms import QuestionForm, EssayForm, EssayUploadForm
from scan import settings
from flask.ext.security import login_required, auth_required
from flask.ext.login import current_user
from flask.views import MethodView
from core.database.models import Question, Essay
from celery.utils import get_full_cls_name
        from app import cache
        from app import cache
        from app import db
        from core.tasks.tasks import create_model
        from core.tasks.tasks import create_and_score
        from app import cache
        from app import cache
        from app import db
        from core.tasks.tasks import score_essay
        from app import celery
import os
import json

main_views = Blueprint('main_views', __name__, template_folder=os.path.join(settings.REPO_PATH, 'templates'))

class InvalidUser(Exception):
    pass

class InvalidAction(Exception):
    pass

@main_views.route('/')
def index():
    return render_template("index.html")

class BaseQuestionView(MethodView):
    decorators = [auth_required('session')]

    def get_model(self, id):

# Path: core/web/forms.py
# class QuestionForm(Form):
#     name = StringField('Name', [Required()], description="The name you want to give this question.", )
#     prompt = TextAreaField('Prompt', [Optional()], description="The prompt for this question (optional).")
# 
#     def save(self):
#         from app import db
#         q = Question(user=current_user)
#         self.populate_obj(q)
#         db.session.add(q)
#         db.session.commit()
# 
# class EssayForm(Form):
#     text = TextAreaField('Text', [Required()], description="The text of the essay.")
#     actual_score = FloatField('Score (optional)', [Optional()],  description="The score the essay got, if it has a score.")
#     info = TextAreaField("Additional Info (optional)", [Optional()], description="Any additional info you want to store with this essay.")
# 
#     def save(self, question):
#         from app import db
#         e = Essay(question=question)
#         self.populate_obj(e)
#         db.session.add(e)
#         db.session.commit()
# 
# class EssayUploadForm(Form):
#     upload = FileField('File', [FileRequired(), FileAllowed(['csv'], 'CSV files only!')], description="The csv file with essays you want to upload.")
# 
#     def save(self, question, csvfile):
#         from app import db
#         csvfile.seek(0)
#         reader = csv.reader(csvfile, EssayDialect)
#         for i, row in enumerate(reader):
#             if i == 0:
#                 continue
# 
#             score = row[-1]
#             text = ",".join(row[:-1])
# 
#             if len(score) == 0:
#                 score = None
#             else:
#                 score = float(score)
#             e = Essay(question=question, text=text, actual_score=score)
#             db.session.add(e)
#         db.session.commit()
# 
# Path: scan/settings.py
# DB_URL = 'sqlite:///scan.db'
# DEBUG = True
# ROOT_PATH = path(__file__).dirname()
# REPO_PATH = ROOT_PATH.dirname()
# ENV_ROOT = REPO_PATH.dirname()
# DATA_PATH = os.path.join(REPO_PATH, "data")
# TEST_DATA_PATH = os.path.join(DATA_PATH, "test")
# MODEL_PATH = os.path.join(DATA_PATH, "models")
# SECRET_KEY = "30y^$e7henbp@#_w+z9)o9f8tovjrhoq(%vw=%#*(1-1esrh!_"
# USERNAME = "test"
# EMAIL = "test@test.com"
# PASSWORD = "test"
# SQLALCHEMY_DATABASE_URI = DB_URL
# SECURITY_REGISTERABLE = True
# SECURITY_RECOVERABLE = True
# SECURITY_SEND_REGISTER_EMAIL = False
# SECURITY_PASSWORD_HASH = "bcrypt"
# SECURITY_PASSWORD_SALT = SECRET_KEY
# BROKER_TRANSPORT_OPTIONS = {'visibility_timeout': 3600}
# BROKER_URL = 'redis://localhost:6379/3'
# CELERY_RESULT_BACKEND = 'redis://localhost:6379/3'
# CELERY_IMPORTS = ('core.tasks.tasks',)
# CACHE_TYPE = 'redis'
# CACHE_REDIS_URL = 'redis://localhost:6379/4'
# CACHE_REDIS_DB = 4
# MAX_FEATURES = 500
# MODEL_VERSION = 1
# 
# Path: core/database/models.py
# class Question(db.Model):
#     __tablename__ = 'questions'
# 
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(STRING_MAX))
#     description = db.Column(db.Text)
#     prompt = db.Column(db.Text)
#     user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
# 
#     user = db.relationship("User", backref=db.backref('questions', order_by=id))
# 
#     created = db.Column(db.DateTime(timezone=True), default=datetime.utcnow)
#     updated = db.Column(db.DateTime(timezone=True), onupdate=datetime.utcnow)
# 
# class Essay(db.Model):
#     __tablename__ = 'essays'
# 
#     id = db.Column(db.Integer, primary_key=True)
#     text = db.Column(db.Text)
#     actual_score = db.Column(db.Float)
#     info = db.Column(db.Text)
# 
#     predicted_score = db.Column(db.Float)
#     model_id = db.Column(db.Integer, db.ForeignKey('models.id'))
#     model = db.relationship("Model", backref=db.backref('essays', order_by=id))
# 
#     question_id = db.Column(db.Integer, db.ForeignKey('questions.id'))
#     question = db.relationship("Question", backref=db.backref('essays', order_by=id))
# 
#     created = db.Column(db.DateTime(timezone=True), default=datetime.utcnow)
#     updated = db.Column(db.DateTime(timezone=True), onupdate=datetime.utcnow)


# Path: core/web/main_views.py
from celery import states
from flask import Blueprint, render_template, abort, request, url_for, jsonify
from werkzeug.utils import redirect, secure_filename
from core.web.forms import QuestionForm, EssayForm, EssayUploadForm
from scan import settings
from flask.ext.security import login_required, auth_required
from flask.ext.login import current_user
from flask.views import MethodView
from core.database.models import Question, Essay
from celery.utils import get_full_cls_name
        from app import cache
        from app import cache
        from app import db
        from core.tasks.tasks import create_model
        from core.tasks.tasks import create_and_score
        from app import cache
        from app import cache
        from app import db
        from core.tasks.tasks import score_essay
        from app import celery
import os
import json


main_views = Blueprint('main_views', __name__, template_folder=os.path.join(settings.REPO_PATH, 'templates'))

class InvalidUser(Exception):
    pass

class InvalidAction(Exception):
    pass

@main_views.route('/')
def index():
    return render_template("index.html")

class BaseQuestionView(MethodView):
    decorators = [auth_required('session')]

    def get_model(self, id):

        question = Question.query.filter_by(id=id).first()