# 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: _helpers.py
# def force_int(value,default=1):
#     try:
#         return int(value)
#     except:
#         return default
# 
# def fill_object(items,objects,*args,**kwargs):
#     objects_dict = {}
#     for o in objects:
#        objects_dict[o.id] = o
#     for arg in args:
#         items = map(lambda o:_add_attr(o,objects_dict,arg,kwargs),items)
#     return items
# 
# Path: lolipop/models.py
# class User(db.Model,UserMixin):
#     '''User account'''
#     __tablename__ = "users"
#     id = db.Column(db.Integer,primary_key = True)
#     email = db.Column(db.String(120),unique=True)
#     username = db.Column(db.String(80),unique=True)
#     _password = db.Column('password',db.String(80),nullable=False)
#     date_joined = db.Column(db.DateTime,default=datetime.utcnow)
#     #care_nodes = db.Column(db.String,nullable = True)
#     posts = db.relationship("Post",backref="user",lazy="dynamic")
#     topics = db.relationship("Topic",backref="user",lazy="dynamic")
#     
#     # synonym method replace a column by another name
#     # descriptor is a parameter in sqlalchemy
#     # property is python build-in method
#     #def __init__(self):
#         # It is a list which record all nodes you care
# 
#     #def add_care_node(self,node_id):
#     #    self.care_nodes_list.append(node_id)
#     #    self.save_care_nodes()
# 
#    # def remove_care_node(self,node_id):
#    #     self.care_nodes_list.remove(node_id)
#    #     self.save_care_nodes()
# 
#    # def get_care_nodes(self):
#    #     return self.care_nodes_list
# 
#    # def save_care_nodes(self):
#    #     self.care_nodes = ','.join(self.care_nodes_list)
# 
#     def _set_password(self,password):
#         self._password = generate_password_hash(password)
# 
#     def _get_password(self):
#         return self._password
# 
#     def get_name(self):
#         return self.username
# 
# 
#     password = db.synonym('_password',descriptor = property(_get_password,_set_password))
# 
#     def __repr__(self):
#         return "%s" % self.username
# 
#     def __str__(self):
#         return "%s" % self.username
# 
#     def check_password(self,password):
#         if self.password is None:
#             return False
#         return check_password_hash(self.password,password)
# 
#     def get_all_posts(self):
#         return Post.query.filter(Post.user_id == self.id)
# 
#     def get_all_topics(self):
#         return Topic.query.filter(Topic.user_id == self.id)
# 
#     @classmethod
#     def authenticate(cls,login,password):
#         user = cls.query.filter(db.or_(User.username == login,User.email == login)).first()
#         if user:
#             authenticated = user.check_password(password)
#         else:
#             authenticated = False
#         return user,authenticated
# 
#     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 Profile(db.Model):
#     __tablename__ = "profiles"
#     id = db.Column(db.Integer,primary_key=True)
#     avatar = db.Column(db.String(200))
#     website = db.Column(db.String(400),nullable=True)
#     weibo = db.Column(db.String(100),nullable=True)
#     twitter = db.Column(db.String(100),nullable=True)
#     description = db.Column(db.Text)
#     significant = db.Column(db.Text,nullable = True)
#     rankPoint = db.Column(db.Integer,default = 0)
#     
#     @classmethod
#     def get_or_create(cls,uid):
#         item = cls.query.get(uid)
#         if item:
#             return item
#         item = cls(id=uid)
#         db.session.add(item)
#         db.session.commit()
#         return item
# 
#     def save(self):
#         db.session.add(self)
#         db.session.commit()
#         return self
# 
# 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()


from flask import Blueprint,request,render_template,flash,current_app,redirect,url_for
from views.account import admin_required
from _helpers import force_int,fill_object
from lolipop.models import User,Profile
from lolipop.form import NodeForm
import codecs,os
# coding:utf-8

bp = Blueprint('admin',__name__)

@bp.route('/',methods=['GET','POST'])
@admin_required
def dashboard():
    if request.method == 'POST':
        save_sidebar_notice(request.form.get('content',None))
        return redirect(url_for('.dashboard'))
    page = force_int(request.args.get('page', 1), 0)
    if not page:
        return abort(404)
    form = NodeForm()
    paginator = User.query.order_by(User.id.desc()).paginate(page)

# 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: _helpers.py
# def force_int(value,default=1):
#     try:
#         return int(value)
#     except:
#         return default
# 
# def fill_object(items,objects,*args,**kwargs):
#     objects_dict = {}
#     for o in objects:
#        objects_dict[o.id] = o
#     for arg in args:
#         items = map(lambda o:_add_attr(o,objects_dict,arg,kwargs),items)
#     return items
# 
# Path: lolipop/models.py
# class User(db.Model,UserMixin):
#     '''User account'''
#     __tablename__ = "users"
#     id = db.Column(db.Integer,primary_key = True)
#     email = db.Column(db.String(120),unique=True)
#     username = db.Column(db.String(80),unique=True)
#     _password = db.Column('password',db.String(80),nullable=False)
#     date_joined = db.Column(db.DateTime,default=datetime.utcnow)
#     #care_nodes = db.Column(db.String,nullable = True)
#     posts = db.relationship("Post",backref="user",lazy="dynamic")
#     topics = db.relationship("Topic",backref="user",lazy="dynamic")
#     
#     # synonym method replace a column by another name
#     # descriptor is a parameter in sqlalchemy
#     # property is python build-in method
#     #def __init__(self):
#         # It is a list which record all nodes you care
# 
#     #def add_care_node(self,node_id):
#     #    self.care_nodes_list.append(node_id)
#     #    self.save_care_nodes()
# 
#    # def remove_care_node(self,node_id):
#    #     self.care_nodes_list.remove(node_id)
#    #     self.save_care_nodes()
# 
#    # def get_care_nodes(self):
#    #     return self.care_nodes_list
# 
#    # def save_care_nodes(self):
#    #     self.care_nodes = ','.join(self.care_nodes_list)
# 
#     def _set_password(self,password):
#         self._password = generate_password_hash(password)
# 
#     def _get_password(self):
#         return self._password
# 
#     def get_name(self):
#         return self.username
# 
# 
#     password = db.synonym('_password',descriptor = property(_get_password,_set_password))
# 
#     def __repr__(self):
#         return "%s" % self.username
# 
#     def __str__(self):
#         return "%s" % self.username
# 
#     def check_password(self,password):
#         if self.password is None:
#             return False
#         return check_password_hash(self.password,password)
# 
#     def get_all_posts(self):
#         return Post.query.filter(Post.user_id == self.id)
# 
#     def get_all_topics(self):
#         return Topic.query.filter(Topic.user_id == self.id)
# 
#     @classmethod
#     def authenticate(cls,login,password):
#         user = cls.query.filter(db.or_(User.username == login,User.email == login)).first()
#         if user:
#             authenticated = user.check_password(password)
#         else:
#             authenticated = False
#         return user,authenticated
# 
#     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 Profile(db.Model):
#     __tablename__ = "profiles"
#     id = db.Column(db.Integer,primary_key=True)
#     avatar = db.Column(db.String(200))
#     website = db.Column(db.String(400),nullable=True)
#     weibo = db.Column(db.String(100),nullable=True)
#     twitter = db.Column(db.String(100),nullable=True)
#     description = db.Column(db.Text)
#     significant = db.Column(db.Text,nullable = True)
#     rankPoint = db.Column(db.Integer,default = 0)
#     
#     @classmethod
#     def get_or_create(cls,uid):
#         item = cls.query.get(uid)
#         if item:
#             return item
#         item = cls(id=uid)
#         db.session.add(item)
#         db.session.commit()
#         return item
# 
#     def save(self):
#         db.session.add(self)
#         db.session.commit()
#         return self
# 
# 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()


# Path: views/admin.py
from flask import Blueprint,request,render_template,flash,current_app,redirect,url_for
from views.account import admin_required
from _helpers import force_int,fill_object
from lolipop.models import User,Profile
from lolipop.form import NodeForm
import codecs,os

# coding:utf-8

bp = Blueprint('admin',__name__)

@bp.route('/',methods=['GET','POST'])
@admin_required
def dashboard():
    if request.method == 'POST':
        save_sidebar_notice(request.form.get('content',None))
        return redirect(url_for('.dashboard'))
    page = force_int(request.args.get('page', 1), 0)
    if not page:
        return abort(404)
    form = NodeForm()
    paginator = User.query.order_by(User.id.desc()).paginate(page)

    profiles = Profile.query.order_by(Profile.id).paginate(page).items