<s> import cryptacular . bcrypt <EOL> from sqlalchemy import ( <EOL> Table , <EOL> Column , <EOL> ForeignKey , <EOL> ) <EOL> from sqlalchemy . orm import ( <EOL> scoped_session , <EOL> sessionmaker , <EOL> relation , <EOL> backref , <EOL> column_property , <EOL> synonym , <EOL> joinedload , <EOL> ) <EOL> from sqlalchemy . types import ( <EOL> Integer , <EOL> Unicode , <EOL> UnicodeText , <EOL> ) <EOL> from sqlalchemy . sql import func <EOL> from sqlalchemy . ext . declarative import declarative_base <EOL> from zope . sqlalchemy import ZopeTransactionExtension <EOL> from pyramid . security import ( <EOL> Everyone , <EOL> Authenticated , <EOL> Allow , <EOL> ) <EOL> DBSession = scoped_session ( sessionmaker ( extension = ZopeTransactionExtension ( ) ) ) <EOL> Base = declarative_base ( ) <EOL> crypt = cryptacular . bcrypt . BCRYPTPasswordManager ( ) <EOL> def hash_password ( password ) : <EOL> return unicode ( crypt . encode ( password ) ) <EOL> class User ( Base ) : <EOL> """<STR_LIT>""" <EOL> __tablename__ = '<STR_LIT>' <EOL> user_id = Column ( Integer , primary_key = True ) <EOL> username = Column ( Unicode ( <NUM_LIT:20> ) , unique = True ) <EOL> name = Column ( Unicode ( <NUM_LIT:50> ) ) <EOL> email = Column ( Unicode ( <NUM_LIT:50> ) ) <EOL> hits = Column ( Integer , default = <NUM_LIT:0> ) <EOL> misses = Column ( Integer , default = <NUM_LIT:0> ) <EOL> delivered_hits = Column ( Integer , default = <NUM_LIT:0> ) <EOL> delivered_misses = Column ( Integer , default = <NUM_LIT:0> ) <EOL> _password = Column ( '<STR_LIT:password>' , Unicode ( <NUM_LIT> ) ) <EOL> def _get_password ( self ) : <EOL> return self . _password <EOL> def _set_password ( self , password ) : <EOL> self . _password = hash_password ( password ) <EOL> password = property ( _get_password , _set_password ) <EOL> password = synonym ( '<STR_LIT>' , descriptor = password ) <EOL> def __init__ ( self , username , password , name , email ) : <EOL> self . username = username <EOL> self . name = name <EOL> self . email = email <EOL> self . password = password <EOL> @ classmethod <EOL> def get_by_username ( cls , username ) : <EOL> return DBSession . query ( cls ) . filter ( cls . username == username ) . first ( ) <EOL> @ classmethod <EOL> def check_password ( cls , username , password ) : <EOL> user = cls . get_by_username ( username ) <EOL> if not user : <EOL> return False <EOL> return crypt . check ( user . password , password ) <EOL> ideas_tags = Table ( '<STR_LIT>' , Base . metadata , <EOL> Column ( '<STR_LIT>' , Integer , ForeignKey ( '<STR_LIT>' ) ) , <EOL> Column ( '<STR_LIT>' , Integer , ForeignKey ( '<STR_LIT>' ) ) <EOL> ) <EOL> class Tag ( Base ) : <EOL> """<STR_LIT>""" <EOL> __tablename__ = '<STR_LIT>' <EOL> tag_id = Column ( Integer , primary_key = True ) <EOL> name = Column ( Unicode ( <NUM_LIT:50> ) , unique = True , index = True ) <EOL> def __init__ ( self , name ) : <EOL> self . name = name <EOL> @ staticmethod <EOL> def extract_tags ( tags_string ) : <EOL> tags = tags_string . replace ( '<STR_LIT:;>' , '<STR_LIT:U+0020>' ) . replace ( '<STR_LIT:U+002C>' , '<STR_LIT:U+0020>' ) <EOL> tags = [ tag . lower ( ) for tag in tags . split ( ) ] <EOL> tags = set ( tags ) <EOL> return tags <EOL> @ classmethod <EOL> def get_by_name ( cls , tag_name ) : <EOL> tag = DBSession . query ( cls ) . filter ( cls . name == tag_name ) <EOL> return tag . first ( ) <EOL> @ classmethod <EOL> def create_tags ( cls , tags_string ) : <EOL> tags_list = cls . extract_tags ( tags_string ) <EOL> tags = [ ] <EOL> for tag_name in tags_list : <EOL> tag = cls . get_by_name ( tag_name ) <EOL> if not tag : <EOL> tag = Tag ( name = tag_name ) <EOL> DBSession . add ( tag ) <EOL> tags . append ( tag ) <EOL> return tags <EOL> @ classmethod <EOL> def tag_counts ( cls ) : <EOL> query = DBSession . query ( Tag . name , func . count ( '<STR_LIT:*>' ) ) <EOL> return query . join ( '<STR_LIT>' ) . group_by ( Tag . name ) <EOL> voted_users = Table ( '<STR_LIT>' , Base . metadata , <EOL> Column ( '<STR_LIT>' , Integer , ForeignKey ( '<STR_LIT>' ) ) , <EOL> Column ( '<STR_LIT>' , Integer , ForeignKey ( '<STR_LIT>' ) ) <EOL> ) <EOL> class Idea ( Base ) : <EOL> __tablename__ = '<STR_LIT>' <EOL> idea_id = Column ( Integer , primary_key = True ) <EOL> target_id = Column ( Integer , ForeignKey ( '<STR_LIT>' ) ) <EOL> comments = relation ( '<STR_LIT>' , cascade = "<STR_LIT>" , <EOL> backref = backref ( '<STR_LIT:target>' , remote_side = idea_id ) ) <EOL> author_id = Column ( Integer , ForeignKey ( '<STR_LIT>' ) ) <EOL> author = relation ( User , cascade = "<STR_LIT>" , backref = '<STR_LIT>' ) <EOL> title = Column ( UnicodeText ) <EOL> text = Column ( UnicodeText ) <EOL> hits = Column ( Integer , default = <NUM_LIT:0> ) <EOL> misses = Column ( Integer , default = <NUM_LIT:0> ) <EOL> tags = relation ( Tag , secondary = ideas_tags , backref = '<STR_LIT>' ) <EOL> voted_users = relation ( User , secondary = voted_users , lazy = '<STR_LIT>' , <EOL> backref = '<STR_LIT>' ) <EOL> hit_percentage = func . coalesce ( hits / ( hits + misses ) * <NUM_LIT:100> , <NUM_LIT:0> ) <EOL> hit_percentage = column_property ( hit_percentage . label ( '<STR_LIT>' ) ) <EOL> total_votes = column_property ( ( hits + misses ) . label ( '<STR_LIT>' ) ) <EOL> vote_differential = column_property ( <EOL> ( hits - misses ) . label ( '<STR_LIT>' ) <EOL> ) <EOL> @ classmethod <EOL> def get_query ( cls , with_joinedload = True ) : <EOL> query = DBSession . query ( cls ) <EOL> if with_joinedload : <EOL> query = query . options ( joinedload ( '<STR_LIT>' ) , joinedload ( '<STR_LIT>' ) ) <EOL> return query <EOL> @ classmethod <EOL> def get_by_id ( cls , idea_id , with_joinedload = True ) : <EOL> query = cls . get_query ( with_joinedload ) <EOL> return query . filter ( cls . idea_id == idea_id ) . first ( ) <EOL> @ classmethod <EOL> def get_by_tagname ( cls , tag_name , with_joinedload = True ) : <EOL> query = cls . get_query ( with_joinedload ) <EOL> return query . filter ( Idea . tags . any ( name = tag_name ) ) <EOL> @ classmethod <EOL> def ideas_bunch ( cls , order_by , how_many = <NUM_LIT:10> , with_joinedload = True ) : <EOL> query = cls . get_query ( with_joinedload ) . join ( '<STR_LIT>' ) <EOL> query = query . filter ( cls . target == None ) . order_by ( order_by ) <EOL> return query . limit ( how_many ) . all ( ) <EOL> def user_voted ( self , username ) : <EOL> return bool ( self . voted_users . filter_by ( username = username ) . first ( ) ) <EOL> def vote ( self , user , positive ) : <EOL> if positive : <EOL> self . hits += <NUM_LIT:1> <EOL> self . author . hits += <NUM_LIT:1> <EOL> user . delivered_hits += <NUM_LIT:1> <EOL> else : <EOL> self . misses += <NUM_LIT:1> <EOL> self . author . misses += <NUM_LIT:1> <EOL> user . delivered_misses += <NUM_LIT:1> <EOL> self . voted_users . append ( user ) <EOL> class RootFactory ( object ) : <EOL> __acl__ = [ <EOL> ( Allow , Everyone , '<STR_LIT>' ) , <EOL> ( Allow , Authenticated , '<STR_LIT>' ) <EOL> ] <EOL> def __init__ ( self , request ) : <EOL> pass </s>
