# Path: seoul_serenity/project/models.py
# class Project(SurrogatePK, Model):
#     __tablename__ = 'projects'
#     name = Column(db.String(80), unique=False, nullable=False)
#     start_date = Column(db.DateTime, nullable=True)
#     end_date = Column(db.DateTime, nullable=True)
#     description = Column(db.Text, nullable=True)
#     created_at = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow)
#     created_user_id = Column(db.Integer,nullable=True)
#     modified_at = Column(db.DateTime, nullable=True)
#     modified_user_id = Column(db.Integer,nullable=True)
#     display_yn = Column(db.Boolean, default=True)
# 
#     def __init__(self, name, **kwargs):
#         db.Model.__init__(self, name=name, **kwargs)
# 
#     def __repr__(self):
#         return '<Project({name})>'.format(name=self.name)
# 
#     def datetimeformat(value, format='%d-%m-%Y'):
#         return value.strftime(format,vlaue.gmtime())    
# 
# Path: seoul_serenity/user/models.py
# class User_project(SurrogatePK,Model):
#     __tablename__ = 'user_project'
#     
#     # TODO : next plan
#     # u_id = Column(db.Integer, db.ForeignKey('users.id'))
#     # p_id = Column(db.Integer, db.ForeignKey('projects.id'))
#     
#     u_id = Column(db.Integer, nullable=False);
#     p_id = Column(db.Integer, nullable=False);
# 
#     created_at = Column(db.DateTime, nullable=True) 
#     display_yn = Column(db.Boolean, default=True)
# 
#     # projects = db.relationship('Project',backref='user_project',lazy='dynamic')
#     
#     def __init__(self, **kwargs):
#         db.Model.__init__(self, **kwargs)
# 
#     def __repr__(self):
#         return 'todo'
# 
# class User(UserMixin, SurrogatePK, Model):
# 
#     __tablename__ = 'users'
#     username = Column(db.String(80), unique=True, nullable=False)
#     firstname = Column(db.String(30), nullable=True)
#     lastname = Column(db.String(30), nullable=True)
#     email = Column(db.String(80), unique=True, nullable=False)
#     #: The hashed password
#     password = Column(db.String(128), nullable=True)
#     created_at = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow)
#     active = Column(db.Boolean(), default=False)
#     is_admin = Column(db.Boolean(), default=False)
#     user_type = Column(db.Integer, default=0)
# 
#     # TODO : next plan
#     # user_projects = db.relationship('User_project', backref='users',lazy='dynamic')
#     
#     def __init__(self, username, email, password=None, **kwargs):
#         db.Model.__init__(self, username=username, email=email, **kwargs)
#         if password:
#             self.set_password(password)
#         else:
#             self.password = None
# 
#     def set_password(self, password):
#         self.password = bcrypt.generate_password_hash(password)
# 
#     def check_password(self, value):
#         return bcrypt.check_password_hash(self.password, value)
# 
#     def get_id(self):
#         return unicode(self.id)
#             
#     def __repr__(self):
#         return '<User({username!r})>'.format(username=self.username)


from flask import Blueprint, render_template, request, redirect, url_for
from flask.ext.login import login_required, current_user
from seoul_serenity.extensions import login_manager
from seoul_serenity.client.forms import LoginForm
from seoul_serenity.client_committee.models import Client
from seoul_serenity.project.models import Project
from seoul_serenity.user.models import User_project, User
            # flash_errors(form)
    return render_template("client/login.html", form=form)


@blueprint.route("/")
def home():
    projects = []
    if current_user and current_user.is_authenticated():
        user_projects = User_project.query.filter_by(u_id=current_user.id)
        for user_project in user_projects:
            project = Project.get_by_id(user_project.p_id)
            projects.append(project)
        return render_template("client/home.html", projects=projects)
    return redirect(url_for("client.login"))


# @blueprint.route("/view")
@blueprint.route("/view/<int:project_id>")
def view(project_id):
	project = Project.query.filter_by(id=project_id).first_or_404()	
	return render_template("client/view.html", project=project)

# @blueprint.route("/write")
@blueprint.route("/write/<int:project_id>")
def write(project_id):
	project = Project.query.filter_by(id=project_id).first_or_404()
	return render_template("client/write.html", project=project)

@blueprint.route("/list/<int:project_id>")
def list(project_id):

# Path: seoul_serenity/project/models.py
# class Project(SurrogatePK, Model):
#     __tablename__ = 'projects'
#     name = Column(db.String(80), unique=False, nullable=False)
#     start_date = Column(db.DateTime, nullable=True)
#     end_date = Column(db.DateTime, nullable=True)
#     description = Column(db.Text, nullable=True)
#     created_at = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow)
#     created_user_id = Column(db.Integer,nullable=True)
#     modified_at = Column(db.DateTime, nullable=True)
#     modified_user_id = Column(db.Integer,nullable=True)
#     display_yn = Column(db.Boolean, default=True)
# 
#     def __init__(self, name, **kwargs):
#         db.Model.__init__(self, name=name, **kwargs)
# 
#     def __repr__(self):
#         return '<Project({name})>'.format(name=self.name)
# 
#     def datetimeformat(value, format='%d-%m-%Y'):
#         return value.strftime(format,vlaue.gmtime())    
# 
# Path: seoul_serenity/user/models.py
# class User_project(SurrogatePK,Model):
#     __tablename__ = 'user_project'
#     
#     # TODO : next plan
#     # u_id = Column(db.Integer, db.ForeignKey('users.id'))
#     # p_id = Column(db.Integer, db.ForeignKey('projects.id'))
#     
#     u_id = Column(db.Integer, nullable=False);
#     p_id = Column(db.Integer, nullable=False);
# 
#     created_at = Column(db.DateTime, nullable=True) 
#     display_yn = Column(db.Boolean, default=True)
# 
#     # projects = db.relationship('Project',backref='user_project',lazy='dynamic')
#     
#     def __init__(self, **kwargs):
#         db.Model.__init__(self, **kwargs)
# 
#     def __repr__(self):
#         return 'todo'
# 
# class User(UserMixin, SurrogatePK, Model):
# 
#     __tablename__ = 'users'
#     username = Column(db.String(80), unique=True, nullable=False)
#     firstname = Column(db.String(30), nullable=True)
#     lastname = Column(db.String(30), nullable=True)
#     email = Column(db.String(80), unique=True, nullable=False)
#     #: The hashed password
#     password = Column(db.String(128), nullable=True)
#     created_at = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow)
#     active = Column(db.Boolean(), default=False)
#     is_admin = Column(db.Boolean(), default=False)
#     user_type = Column(db.Integer, default=0)
# 
#     # TODO : next plan
#     # user_projects = db.relationship('User_project', backref='users',lazy='dynamic')
#     
#     def __init__(self, username, email, password=None, **kwargs):
#         db.Model.__init__(self, username=username, email=email, **kwargs)
#         if password:
#             self.set_password(password)
#         else:
#             self.password = None
# 
#     def set_password(self, password):
#         self.password = bcrypt.generate_password_hash(password)
# 
#     def check_password(self, value):
#         return bcrypt.check_password_hash(self.password, value)
# 
#     def get_id(self):
#         return unicode(self.id)
#             
#     def __repr__(self):
#         return '<User({username!r})>'.format(username=self.username)


# Path: seoul_serenity/client/views.py
from flask import Blueprint, render_template, request, redirect, url_for
from flask.ext.login import login_required, current_user
from seoul_serenity.extensions import login_manager
from seoul_serenity.client.forms import LoginForm
from seoul_serenity.client_committee.models import Client
from seoul_serenity.project.models import Project
from seoul_serenity.user.models import User_project, User

            # flash_errors(form)
    return render_template("client/login.html", form=form)


@blueprint.route("/")
def home():
    projects = []
    if current_user and current_user.is_authenticated():
        user_projects = User_project.query.filter_by(u_id=current_user.id)
        for user_project in user_projects:
            project = Project.get_by_id(user_project.p_id)
            projects.append(project)
        return render_template("client/home.html", projects=projects)
    return redirect(url_for("client.login"))


# @blueprint.route("/view")
@blueprint.route("/view/<int:project_id>")
def view(project_id):
	project = Project.query.filter_by(id=project_id).first_or_404()	
	return render_template("client/view.html", project=project)

# @blueprint.route("/write")
@blueprint.route("/write/<int:project_id>")
def write(project_id):
	project = Project.query.filter_by(id=project_id).first_or_404()
	return render_template("client/write.html", project=project)

@blueprint.route("/list/<int:project_id>")
def list(project_id):

    project = Project.query.filter_by(id=project_id).first_or_404()