<s> from hashlib import md5 <EOL> import re <EOL> from app import db <EOL> from app import app <EOL> from config import WHOOSH_ENABLED <EOL> import sys <EOL> if sys . version_info >= ( <NUM_LIT:3> , <NUM_LIT:0> ) : <EOL> enable_search = False <EOL> else : <EOL> enable_search = WHOOSH_ENABLED <EOL> if enable_search : <EOL> import flask . ext . whooshalchemy as whooshalchemy <EOL> followers = db . Table ( <EOL> '<STR_LIT>' , <EOL> db . Column ( '<STR_LIT>' , db . Integer , db . ForeignKey ( '<STR_LIT>' ) ) , <EOL> db . Column ( '<STR_LIT>' , db . Integer , db . ForeignKey ( '<STR_LIT>' ) ) <EOL> ) <EOL> class User ( db . Model ) : <EOL> id = db . Column ( db . Integer , primary_key = True ) <EOL> nickname = db . Column ( db . String ( <NUM_LIT:64> ) , index = True , unique = True ) <EOL> email = db . Column ( db . String ( <NUM_LIT> ) , index = True , unique = True ) <EOL> posts = db . relationship ( '<STR_LIT>' , backref = '<STR_LIT>' , lazy = '<STR_LIT>' ) <EOL> about_me = db . Column ( db . String ( <NUM_LIT> ) ) <EOL> last_seen = db . Column ( db . DateTime ) <EOL> followed = db . relationship ( '<STR_LIT>' , <EOL> secondary = followers , <EOL> primaryjoin = ( followers . c . follower_id == id ) , <EOL> secondaryjoin = ( followers . c . followed_id == id ) , <EOL> backref = db . backref ( '<STR_LIT>' , lazy = '<STR_LIT>' ) , <EOL> lazy = '<STR_LIT>' ) <EOL> @ staticmethod <EOL> def make_valid_nickname ( nickname ) : <EOL> return re . sub ( '<STR_LIT>' , '<STR_LIT>' , nickname ) <EOL> @ staticmethod <EOL> def make_unique_nickname ( nickname ) : <EOL> if User . query . filter_by ( nickname = nickname ) . first ( ) is None : <EOL> return nickname <EOL> version = <NUM_LIT:2> <EOL> while True : <EOL> new_nickname = nickname + str ( version ) <EOL> if User . query . filter_by ( nickname = new_nickname ) . first ( ) is None : <EOL> break <EOL> version += <NUM_LIT:1> <EOL> return new_nickname <EOL> @ property <EOL> def is_authenticated ( self ) : <EOL> return True <EOL> @ property <EOL> def is_active ( self ) : <EOL> return True <EOL> @ property <EOL> def is_anonymous ( self ) : <EOL> return False <EOL> def get_id ( self ) : <EOL> try : <EOL> return unicode ( self . id ) <EOL> except NameError : <EOL> return str ( self . id ) <EOL> def avatar ( self , size ) : <EOL> return '<STR_LIT>' % ( md5 ( self . email . encode ( '<STR_LIT:utf-8>' ) ) . hexdigest ( ) , size ) <EOL> def follow ( self , user ) : <EOL> if not self . is_following ( user ) : <EOL> self . followed . append ( user ) <EOL> return self <EOL> def unfollow ( self , user ) : <EOL> if self . is_following ( user ) : <EOL> self . followed . remove ( user ) <EOL> return self <EOL> def is_following ( self , user ) : <EOL> return self . followed . filter ( <EOL> followers . c . followed_id == user . id ) . count ( ) > <NUM_LIT:0> <EOL> def followed_posts ( self ) : <EOL> return Post . query . join ( <EOL> followers , ( followers . c . followed_id == Post . user_id ) ) . filter ( <EOL> followers . c . follower_id == self . id ) . order_by ( <EOL> Post . timestamp . desc ( ) ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . nickname ) <EOL> class Post ( db . Model ) : <EOL> __searchable__ = [ '<STR_LIT:body>' ] <EOL> id = db . Column ( db . Integer , primary_key = True ) <EOL> body = db . Column ( db . String ( <NUM_LIT> ) ) <EOL> timestamp = db . Column ( db . DateTime ) <EOL> user_id = db . Column ( db . Integer , db . ForeignKey ( '<STR_LIT>' ) ) <EOL> language = db . Column ( db . String ( <NUM_LIT:5> ) ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . body ) <EOL> if enable_search : <EOL> whooshalchemy . whoosh_index ( app , Post ) </s>
