fbone / fbone / models /

# -*- coding: utf-8 -*-

from werkzeug import (generate_password_hash, check_password_hash,
from flaskext.login import UserMixin

from fbone.extensions import db
from fbone.models import DenormalizedText
from fbone.utils import get_current_time, VARCHAR_LEN_128

class User(db.Model, UserMixin):

    __tablename__ = 'users'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(VARCHAR_LEN_128), nullable=False, unique=True)
    email = db.Column(db.String(VARCHAR_LEN_128), nullable=False, unique=True)
    _password = db.Column('password', db.String(VARCHAR_LEN_128), nullable=False)
    activation_key = db.Column(db.String(VARCHAR_LEN_128))
    followers = db.Column(DenormalizedText)
    following = db.Column(DenormalizedText)
    created_time = db.Column(db.DateTime, default=get_current_time)

    def __repr__(self):
        return '<User %r>' %

    def _get_password(self):
        return self._password

    def _set_password(self, password):
        self._password = generate_password_hash(password)

    # Hide password encryption by exposing password field only.
    password = db.synonym('_password',

    def check_password(self, password):
        if self.password is None:
            return False
        return check_password_hash(self.password, password)

    def num_followers(self):
        if self.followers:
            return len(self.followers)
        return 0

    def num_following(self):
        return len(self.following)

    def follow(self, user):

    def unfollow(self, user):
        if in user.followers:

        if in self.following:

    def get_following_query(self):
        return User.query.filter( or set()))

    def get_followers_query(self):
        return User.query.filter( or set()))

    def authenticate(cls, login, password):
        user = cls.query.filter(db.or_(,

        if user:
            authenticated = user.check_password(password)
            authenticated = False

        return user, authenticated

    def search(cls, keywords):
        criteria = []
        for keyword in keywords.split():
            keyword = '%' + keyword + '%'
        q = reduce(db.and_, criteria)
        return cls.query.filter(q)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.