# Path: _helpers.py
# ONLINE_LAST_MINUTES = 5
# def force_int(value,default=1):
#     def block_code(self,text,lang):
# def fill_with_user(items):
# def fill_with_node(items):
# def fill_object(items,objects,*args,**kwargs):
# def _add_attr(item,objects_dict,arg,kwargs):
# def mark_online(user_id):
# def get_online_users():
# class BleepRenderer(HtmlRenderer,SmartyPants):
# class MaxinObject(object):
# 
# Path: lolipop/form.py
# class NodeForm(Form):
#     title = TextField(u'Node Name',validators = [Required(message=u'填写Node的名字')])
#     description = TextAreaField(u'Node 描述',validators = [Required(message=u'请填写描述')])
#     
#     def save(self):
#         node = Node(title=self.title.data,
#                 description = self.description.data,
#                 )
#         return node.save()
# 
# class CreateForm(Form):
#     subject = TextField(u'标题',validators=[DataRequired(message=u'标题')],description=u'这里填写标题哦')
#     content = TextAreaField(u'内容',validators=[DataRequired(u'内容')],description=u'这里填写内容的说')
# 
#     def save(self,node,user):
#         topic = Topic(title = self.subject.data)
#         post = Post(content = self.content.data)
#         return topic.save(node=node,user = user,post = post)
# 
# Path: lolipop/models.py
# class Node(db.Model):
#     __tablename__ = "nodes"
#     id = db.Column(db.Integer,primary_key = True)
#     title = db.Column(db.String(120),unique=True)
#     description = db.Column(db.String)
#     topics = db.relationship("Topic",backref="node",lazy="joined")
#     
#     @property
#     def post_count(self):
#         return Post.query.filter(Topic.node_id == self.id).filter(Post.topic_id == Topic.node_id).count()
# 
#     @property
#     def topic_count(self):
#         return Topic.query.filter(Topic.node_id == self.id).count()
# 
#     def save(self):
#         db.session.add(self)
#         db.session.commit()
#         return self
# 
#     def delete(self):
#         db.session.delete(self)
#         db.session.commit()
#         return self
# 
# class Topic(db.Model):
#     __tablename__ = 'topics'
#     id = db.Column(db.Integer,primary_key = True)
#     title = db.Column(db.String)
#     node_id = db.Column(db.Integer,db.ForeignKey("nodes.id",use_alter=True,name="fk_node_id"))
#     user_id = db.Column(db.Integer,db.ForeignKey("users.id"))
#     date_created = db.Column(db.DateTime,default=datetime.utcnow)
#     viewed = db.Column(db.Integer,default = 0)
#     post_count = db.Column(db.Integer,default = 0)
# 
#     # when two table reference each other and one-to-one
#     # set uselist = False
#     first_post_id = db.Column(db.Integer,db.ForeignKey("posts.id",ondelete="CASCADE"))
#     first_post = db.relationship("Post",backref="first_post",foreign_keys=[first_post_id],uselist=False)
# 
#     last_post_id = db.Column(db.Integer,db.ForeignKey("posts.id",ondelete="CASCADE",onupdate="CASCADE"))
#     last_post = db.relationship("Post",backref="last_post",foreign_keys=[last_post_id],uselist=False)
# 
#     posts = db.relationship("Post",backref="topic",lazy="joined",primaryjoin="Post.topic_id == Topic.id",cascade="all,delete-orphan",post_update=True)
# 
#     def __init__(self,title=None):
#         if title:
#             self.title = title
# 
#     def get_name(self):
#         return self.title
# 
#     def __str__(self):
#         return self.title
# 
#     def __repr__(self):
#         return '<Topic:%s>' % self.id
# 
#     @property
#     def second_last_post_id(self):
#         return self.posts[-2].id
# 
#     def save(self,node=None,user=None,post=None):
#         #def save(self,post=None):
#         # edit title
#         if self.id:
#             db.session.add(self)
#             db.session.commit()
# 
#         self.node_id = node.id
#         self.user_id = user.id
#         db.session.add(self)
#         db.session.commit()
# 
#         post.save(user,self)
#         self.first_post_id = post.id
#         db.session.commit()
#         return self
# 
#     def delete(self):
#         #count = Post.query.filter(topic_id == self.id).filter(user_id == self.user_id).count()
#         #self.user.score -= count
#         db.session.delete(self)
#         db.session.commit()
# 
# Path: views/account.py
# def admin_required(func):
#     @wraps(func)
#     def decorated_view(*args,**kwargs):
#         try:
#             if not current_user.id == 1:
#                 abort(403)
#             return func(*args,**kwargs)
#         except AttributeError:
#             abort(403)
#     return decorated_view


from flask import redirect,render_template,url_for,flash,abort,request,Blueprint
from _helpers import force_int,cache,fill_with_user,fill_with_node
from lolipop.form import NodeForm,CreateForm
from lolipop.models import Node,Topic
from flask.ext.login import current_user
from views.account import admin_required

bp = Blueprint("node",__name__)

@bp.route('/')
def nodes():
    nodes = Node.query.order_by(Node.id.desc()).all()
    return render_template('node/nodes.html',nodes = nodes)

@bp.route('/<urlname>',methods=('GET','POST'))
@cache.cached(timeout=50)
def view(urlname):
   node = Node.query.filter_by(title=urlname).first_or_404() 
   page = force_int(request.args.get('page',1),0)
   if not page:
       return abort(404)

# Path: _helpers.py
# ONLINE_LAST_MINUTES = 5
# def force_int(value,default=1):
#     def block_code(self,text,lang):
# def fill_with_user(items):
# def fill_with_node(items):
# def fill_object(items,objects,*args,**kwargs):
# def _add_attr(item,objects_dict,arg,kwargs):
# def mark_online(user_id):
# def get_online_users():
# class BleepRenderer(HtmlRenderer,SmartyPants):
# class MaxinObject(object):
# 
# Path: lolipop/form.py
# class NodeForm(Form):
#     title = TextField(u'Node Name',validators = [Required(message=u'填写Node的名字')])
#     description = TextAreaField(u'Node 描述',validators = [Required(message=u'请填写描述')])
#     
#     def save(self):
#         node = Node(title=self.title.data,
#                 description = self.description.data,
#                 )
#         return node.save()
# 
# class CreateForm(Form):
#     subject = TextField(u'标题',validators=[DataRequired(message=u'标题')],description=u'这里填写标题哦')
#     content = TextAreaField(u'内容',validators=[DataRequired(u'内容')],description=u'这里填写内容的说')
# 
#     def save(self,node,user):
#         topic = Topic(title = self.subject.data)
#         post = Post(content = self.content.data)
#         return topic.save(node=node,user = user,post = post)
# 
# Path: lolipop/models.py
# class Node(db.Model):
#     __tablename__ = "nodes"
#     id = db.Column(db.Integer,primary_key = True)
#     title = db.Column(db.String(120),unique=True)
#     description = db.Column(db.String)
#     topics = db.relationship("Topic",backref="node",lazy="joined")
#     
#     @property
#     def post_count(self):
#         return Post.query.filter(Topic.node_id == self.id).filter(Post.topic_id == Topic.node_id).count()
# 
#     @property
#     def topic_count(self):
#         return Topic.query.filter(Topic.node_id == self.id).count()
# 
#     def save(self):
#         db.session.add(self)
#         db.session.commit()
#         return self
# 
#     def delete(self):
#         db.session.delete(self)
#         db.session.commit()
#         return self
# 
# class Topic(db.Model):
#     __tablename__ = 'topics'
#     id = db.Column(db.Integer,primary_key = True)
#     title = db.Column(db.String)
#     node_id = db.Column(db.Integer,db.ForeignKey("nodes.id",use_alter=True,name="fk_node_id"))
#     user_id = db.Column(db.Integer,db.ForeignKey("users.id"))
#     date_created = db.Column(db.DateTime,default=datetime.utcnow)
#     viewed = db.Column(db.Integer,default = 0)
#     post_count = db.Column(db.Integer,default = 0)
# 
#     # when two table reference each other and one-to-one
#     # set uselist = False
#     first_post_id = db.Column(db.Integer,db.ForeignKey("posts.id",ondelete="CASCADE"))
#     first_post = db.relationship("Post",backref="first_post",foreign_keys=[first_post_id],uselist=False)
# 
#     last_post_id = db.Column(db.Integer,db.ForeignKey("posts.id",ondelete="CASCADE",onupdate="CASCADE"))
#     last_post = db.relationship("Post",backref="last_post",foreign_keys=[last_post_id],uselist=False)
# 
#     posts = db.relationship("Post",backref="topic",lazy="joined",primaryjoin="Post.topic_id == Topic.id",cascade="all,delete-orphan",post_update=True)
# 
#     def __init__(self,title=None):
#         if title:
#             self.title = title
# 
#     def get_name(self):
#         return self.title
# 
#     def __str__(self):
#         return self.title
# 
#     def __repr__(self):
#         return '<Topic:%s>' % self.id
# 
#     @property
#     def second_last_post_id(self):
#         return self.posts[-2].id
# 
#     def save(self,node=None,user=None,post=None):
#         #def save(self,post=None):
#         # edit title
#         if self.id:
#             db.session.add(self)
#             db.session.commit()
# 
#         self.node_id = node.id
#         self.user_id = user.id
#         db.session.add(self)
#         db.session.commit()
# 
#         post.save(user,self)
#         self.first_post_id = post.id
#         db.session.commit()
#         return self
# 
#     def delete(self):
#         #count = Post.query.filter(topic_id == self.id).filter(user_id == self.user_id).count()
#         #self.user.score -= count
#         db.session.delete(self)
#         db.session.commit()
# 
# Path: views/account.py
# def admin_required(func):
#     @wraps(func)
#     def decorated_view(*args,**kwargs):
#         try:
#             if not current_user.id == 1:
#                 abort(403)
#             return func(*args,**kwargs)
#         except AttributeError:
#             abort(403)
#     return decorated_view


# Path: views/node.py
from flask import redirect,render_template,url_for,flash,abort,request,Blueprint
from _helpers import force_int,cache,fill_with_user,fill_with_node
from lolipop.form import NodeForm,CreateForm
from lolipop.models import Node,Topic
from flask.ext.login import current_user
from views.account import admin_required


bp = Blueprint("node",__name__)

@bp.route('/')
def nodes():
    nodes = Node.query.order_by(Node.id.desc()).all()
    return render_template('node/nodes.html',nodes = nodes)

@bp.route('/<urlname>',methods=('GET','POST'))
@cache.cached(timeout=50)
def view(urlname):
   node = Node.query.filter_by(title=urlname).first_or_404() 
   page = force_int(request.args.get('page',1),0)
   if not page:
       return abort(404)

   paginator = Topic.query.filter_by(node_id=node.id).order_by(Topic.id.desc()).paginate(page,10)