# 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
# 
# 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


from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.login import LoginManager
from flask.ext.cache import Cache
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
from misaka import HtmlRenderer,SmartyPants
from redis import Redis
from datetime import datetime
from lolipop.models import User,Profile,Node
import misaka as m
import time
#from flaskext.mail import Mail

ONLINE_LAST_MINUTES = 5
db = SQLAlchemy()
cache = Cache()
redis = Redis()
#mail = Mail()
login_manager = LoginManager()

def force_int(value,default=1):
    try:
        return int(value)
    except:
        return default

class BleepRenderer(HtmlRenderer,SmartyPants):
    ''' code highlight '''
    def block_code(self,text,lang):
        if not lang:
            return '\n<pre><code>%s</code></pre>\n' % \
                    h.escape_html(text.encode("utf8").strip())
        lexer = get_lexer_by_name(lang,stripall=True)
        formatter = HtmlFormatter()
        return highlight(text,lexer,formatter)

class MaxinObject(object):
    pass

def fill_with_user(items):
    uids = set([item.user_id for item in 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
# 
# 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


# Path: _helpers.py
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.login import LoginManager
from flask.ext.cache import Cache
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
from misaka import HtmlRenderer,SmartyPants
from redis import Redis
from datetime import datetime
from lolipop.models import User,Profile,Node
import misaka as m
import time

#from flaskext.mail import Mail

ONLINE_LAST_MINUTES = 5
db = SQLAlchemy()
cache = Cache()
redis = Redis()
#mail = Mail()
login_manager = LoginManager()

def force_int(value,default=1):
    try:
        return int(value)
    except:
        return default

class BleepRenderer(HtmlRenderer,SmartyPants):
    ''' code highlight '''
    def block_code(self,text,lang):
        if not lang:
            return '\n<pre><code>%s</code></pre>\n' % \
                    h.escape_html(text.encode("utf8").strip())
        lexer = get_lexer_by_name(lang,stripall=True)
        formatter = HtmlFormatter()
        return highlight(text,lexer,formatter)

class MaxinObject(object):
    pass

def fill_with_user(items):
    uids = set([item.user_id for item in items])

    users = User.query.filter(User.id.in_(uids)).all()