# Path: websitemixer/models.py
# class User(db.Model):
#     __tablename__ = 'user'
#     id = db.Column(
#         'id',
#         db.Integer,
#         primary_key=True,
#         autoincrement=True, index=True)
#     username = db.Column(
#         'username',
#         db.String(255),
#         unique=True,
#         nullable=False,
#         index=True)
#     password = db.Column('password', db.String(255), nullable=False)
#     email = db.Column('email', db.String(50), nullable=False)
#     registered_on = db.Column('registered_on', db.DateTime)
#     admin = db.Column('admin', db.Integer, default=0)
#     name = db.Column('name', db.String(255))
#     image = db.Column('image', db.String(255))
#     facebook = db.Column('facebook', db.String(255))
#     twitter = db.Column('twitter', db.String(255))
#     google = db.Column('google', db.String(255))
# 
#     #roles = relationship(
#     #    'Role',
#     #    secondary=user_roles,
#     #    backref=backref('user', lazy='dynamic'),
#     #)
# 
#     #posts = relationship(
#     #    'Post',
#     #    secondary=user_posts,
#     #    backref=backref('user', lazy='dynamic')
#     #)
# 
#     #comments = relationship(
#     #    'Comment',
#     #    secondary=user_comments,
#     #    backref=backref('user', lazy='dynamic')
#     #)
# 
#     #preferences = relationship(
#     #    'Preference',
#     #    secondary=user_preferences,
#     #    backref=backref('user', lazy='dynamic')
#     #)
# 
#     def __init__(self, username, password, email):
#         self.username = username
#         self.password = self.set_password(password)
#         self.email = email
#         self.registered_on = datetime.utcnow()
#         self.is_admin = 0
# 
#     @classmethod
#     def get(kls, username):
#         """Returns User object by email.
# 
#         :param email: User's email address.
#         :type email: str
#         :returns: User() object of the corresponding
#         :user if found, otherwise None.
#         :rtype: instance or None
#         """
#         return kls.query.filter(kls.username == username.lower()).first()
#     
#     @classmethod
#     def get_by_id(kls, id):
#         return kls.query.filter(kls.id == id).first()
# 
#     def delete_by_email(kls, email):
#         """Delete artifacts of a user account then the user account itself.
# 
#         :param email: User's email address.
#         :type email: str
#         """
#         user = kls.query.filter(kls.email == email.lower()).first()
#         if user:
#             kls.query.filter(kls.email == email.lower()).first()
# 
#     @classmethod
#     def validate(kls, username, password):
#         """Validates user without returning a User() object.
# 
#         :param email: User's email address.
#         :type email: str
#         :param password: User's password.
#         :type password: str
#         :returns: True if email/password combo is valid, False if not.
#         :rtype: bool
#         """
#         user = kls.get(username)
#         if user is None:
#             return False
#         else:
#             return user.check_password(str(password))
# 
#     def set_password(self, password):
#         """Sets an encrypted password.
# 
#         :param password: User's password.
#         :type password: str
#         :returns: Password hash.
#         :rtype: str
#         """
#         return passlib.hash.sha512_crypt.encrypt(password)
# 
#     def check_password(self, password):
#         """Checks password against stored password for the User() instance.
# 
#         :param password: User's password.
#         :type password: str
#         :returns: True if supplied password matches instance
#         :password, False if not.
#         :rtype: bool
#         """
#         return passlib.hash.sha512_crypt.verify(password, self.password)
# 
#     def is_authenticated(self):
#         return True
# 
#     def is_active(self):
#         return True
# 
#     def is_anonymous(self):
#         return False
# 
#     def get_id(self):
#         return str(self.id)
# 
#     def is_admin(self):
#         if self.admin == 1:
#             return True
#         else:
#             return False
# 
#     def __repr__(self):
#         return "<User email={0}>".format(self.email)


from bs4 import BeautifulSoup
from websitemixer.models import User


def first_paragraph(content=""):
    # take content and return just the first <p></p> content,
    # used in blog loop template
    soup = BeautifulSoup(content, "html.parser")
    thespan = soup.find('p')
    if thespan is None:
        return ''
    else:
        return thespan.string

def process_tags(tags):
    rettags = ''
    taglist = [x.strip() for x in tags.split(',')]
    for t in taglist:
        rettags = rettags + '<a href="/tag/'+t+'/">'+t+'</a> '
    return rettags

def is_admin():
    check = User.query.filter_by(id=session['user_id']).first()
    return check.is_admin()

def author_name(username):

# Path: websitemixer/models.py
# class User(db.Model):
#     __tablename__ = 'user'
#     id = db.Column(
#         'id',
#         db.Integer,
#         primary_key=True,
#         autoincrement=True, index=True)
#     username = db.Column(
#         'username',
#         db.String(255),
#         unique=True,
#         nullable=False,
#         index=True)
#     password = db.Column('password', db.String(255), nullable=False)
#     email = db.Column('email', db.String(50), nullable=False)
#     registered_on = db.Column('registered_on', db.DateTime)
#     admin = db.Column('admin', db.Integer, default=0)
#     name = db.Column('name', db.String(255))
#     image = db.Column('image', db.String(255))
#     facebook = db.Column('facebook', db.String(255))
#     twitter = db.Column('twitter', db.String(255))
#     google = db.Column('google', db.String(255))
# 
#     #roles = relationship(
#     #    'Role',
#     #    secondary=user_roles,
#     #    backref=backref('user', lazy='dynamic'),
#     #)
# 
#     #posts = relationship(
#     #    'Post',
#     #    secondary=user_posts,
#     #    backref=backref('user', lazy='dynamic')
#     #)
# 
#     #comments = relationship(
#     #    'Comment',
#     #    secondary=user_comments,
#     #    backref=backref('user', lazy='dynamic')
#     #)
# 
#     #preferences = relationship(
#     #    'Preference',
#     #    secondary=user_preferences,
#     #    backref=backref('user', lazy='dynamic')
#     #)
# 
#     def __init__(self, username, password, email):
#         self.username = username
#         self.password = self.set_password(password)
#         self.email = email
#         self.registered_on = datetime.utcnow()
#         self.is_admin = 0
# 
#     @classmethod
#     def get(kls, username):
#         """Returns User object by email.
# 
#         :param email: User's email address.
#         :type email: str
#         :returns: User() object of the corresponding
#         :user if found, otherwise None.
#         :rtype: instance or None
#         """
#         return kls.query.filter(kls.username == username.lower()).first()
#     
#     @classmethod
#     def get_by_id(kls, id):
#         return kls.query.filter(kls.id == id).first()
# 
#     def delete_by_email(kls, email):
#         """Delete artifacts of a user account then the user account itself.
# 
#         :param email: User's email address.
#         :type email: str
#         """
#         user = kls.query.filter(kls.email == email.lower()).first()
#         if user:
#             kls.query.filter(kls.email == email.lower()).first()
# 
#     @classmethod
#     def validate(kls, username, password):
#         """Validates user without returning a User() object.
# 
#         :param email: User's email address.
#         :type email: str
#         :param password: User's password.
#         :type password: str
#         :returns: True if email/password combo is valid, False if not.
#         :rtype: bool
#         """
#         user = kls.get(username)
#         if user is None:
#             return False
#         else:
#             return user.check_password(str(password))
# 
#     def set_password(self, password):
#         """Sets an encrypted password.
# 
#         :param password: User's password.
#         :type password: str
#         :returns: Password hash.
#         :rtype: str
#         """
#         return passlib.hash.sha512_crypt.encrypt(password)
# 
#     def check_password(self, password):
#         """Checks password against stored password for the User() instance.
# 
#         :param password: User's password.
#         :type password: str
#         :returns: True if supplied password matches instance
#         :password, False if not.
#         :rtype: bool
#         """
#         return passlib.hash.sha512_crypt.verify(password, self.password)
# 
#     def is_authenticated(self):
#         return True
# 
#     def is_active(self):
#         return True
# 
#     def is_anonymous(self):
#         return False
# 
#     def get_id(self):
#         return str(self.id)
# 
#     def is_admin(self):
#         if self.admin == 1:
#             return True
#         else:
#             return False
# 
#     def __repr__(self):
#         return "<User email={0}>".format(self.email)


# Path: websitemixer/context.py
from bs4 import BeautifulSoup
from websitemixer.models import User



def first_paragraph(content=""):
    # take content and return just the first <p></p> content,
    # used in blog loop template
    soup = BeautifulSoup(content, "html.parser")
    thespan = soup.find('p')
    if thespan is None:
        return ''
    else:
        return thespan.string

def process_tags(tags):
    rettags = ''
    taglist = [x.strip() for x in tags.split(',')]
    for t in taglist:
        rettags = rettags + '<a href="/tag/'+t+'/">'+t+'</a> '
    return rettags

def is_admin():
    check = User.query.filter_by(id=session['user_id']).first()
    return check.is_admin()

def author_name(username):

    author = User.query.filter_by(username=username).first()