<s> from chartnet import db <EOL> from chartnet . setting import EACH_PAGE_POST_NUM <EOL> from datetime import datetime <EOL> from time import time <EOL> import logging <EOL> from chartnet import requests <EOL> import json <EOL> class User ( db . Model ) : <EOL> __tablename__ = '<STR_LIT>' <EOL> id = db . Column ( db . Integer , primary_key = True ) <EOL> name = db . Column ( db . String ( <NUM_LIT> ) , nullable = False ) <EOL> password = db . Column ( db . String ( <NUM_LIT> ) , nullable = False ) <EOL> def __init__ ( self , name = None , password = None ) : <EOL> self . name = name <EOL> self . password = password <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . name <EOL> class AboutReply ( db . Model ) : <EOL> __tablename__ = '<STR_LIT>' <EOL> id = db . Column ( db . Integer , primary_key = True ) <EOL> author = db . Column ( db . String ( <NUM_LIT> ) , nullable = False ) <EOL> email = db . Column ( db . String ( <NUM_LIT:100> ) , nullable = False ) <EOL> url = db . Column ( db . String ( <NUM_LIT:200> ) , nullable = False ) <EOL> comment = db . Column ( db . String ( <NUM_LIT> ) , nullable = False ) <EOL> reply_date = db . Column ( db . DateTime , nullable = False ) <EOL> so_good = db . Column ( db . Integer , nullable = False ) <EOL> so_bad = db . Column ( db . Integer , nullable = False ) <EOL> parent_id = db . Column ( db . Integer ) <EOL> def __init__ ( self , author , email , url , comment , so_good = <NUM_LIT:0> , so_bad = <NUM_LIT:0> , parent_id = None ) : <EOL> self . author = author <EOL> self . email = email <EOL> self . url = url <EOL> self . comment = comment <EOL> self . parent_id = parent_id <EOL> self . so_good = so_good <EOL> self . so_bad = so_bad <EOL> self . reply_date = datetime . now ( ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . author <EOL> class Article ( db . Model ) : <EOL> __tablename__ = '<STR_LIT>' <EOL> _id = db . Column ( '<STR_LIT:id>' , db . Integer , primary_key = True ) <EOL> _category = db . Column ( '<STR_LIT>' , db . String ( <NUM_LIT> ) , nullable = False ) <EOL> _title = db . Column ( '<STR_LIT:title>' , db . String ( <NUM_LIT:100> ) , nullable = False ) <EOL> _content = db . Column ( '<STR_LIT:content>' , db . Text , nullable = False ) <EOL> _comment_num = db . Column ( '<STR_LIT>' , db . Integer , nullable = False ) <EOL> _closecomment = db . Column ( '<STR_LIT>' , db . Integer , nullable = False ) <EOL> _tags = db . Column ( '<STR_LIT>' , db . String ( <NUM_LIT:100> ) , nullable = True ) <EOL> _password = db . Column ( '<STR_LIT:password>' , db . String ( <NUM_LIT:8> ) , nullable = True ) <EOL> _add_time = db . Column ( '<STR_LIT>' , db . Integer , nullable = False ) <EOL> _edit_time = db . Column ( '<STR_LIT>' , db . Integer , nullable = False ) <EOL> _shorten_content = db . Column ( '<STR_LIT>' , db . String ( <NUM_LIT:200> ) , nullable = True ) <EOL> _imgthumbnail = db . Column ( '<STR_LIT>' , db . String ( <NUM_LIT:200> ) , nullable = True ) <EOL> _post_type = db . Column ( '<STR_LIT>' , db . Integer , nullable = True ) <EOL> _click_num = db . Column ( '<STR_LIT>' , db . Integer , nullable = True ) <EOL> _editor_title = db . Column ( '<STR_LIT>' , db . String ( <NUM_LIT:100> ) , nullable = True ) <EOL> def __init__ ( self , _category = '<STR_LIT>' , _title = '<STR_LIT>' , _content = '<STR_LIT>' , _comment_num = <NUM_LIT:0> , <EOL> _closecomment = <NUM_LIT:0> , _tags = None , _password = None , _shorten_content = '<STR_LIT>' , <EOL> _imgthumbnail = '<STR_LIT>' , _post_type = <NUM_LIT:0> , _add_time = <NUM_LIT:0> , <EOL> _edit_time = <NUM_LIT:0> , _click_num = <NUM_LIT:0> , _editor_title = '<STR_LIT>' ) : <EOL> self . _category = _category <EOL> self . _title = _title <EOL> self . _content = _content <EOL> self . _comment_num = _comment_num <EOL> self . _closecomment = _closecomment <EOL> self . _tags = _tags <EOL> self . _password = _password <EOL> self . _add_time = _add_time <EOL> self . _edit_time = _edit_time <EOL> self . _shorten_content = _shorten_content <EOL> self . _imgthumbnail = _imgthumbnail <EOL> self . _post_type = _post_type <EOL> self . _click_num = _click_num <EOL> self . _editor_title = _editor_title <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . _title <EOL> class Timeline ( db . Model ) : <EOL> __tablename__ = '<STR_LIT>' <EOL> id = db . Column ( '<STR_LIT:id>' , db . Integer , primary_key = True ) <EOL> timeline_data = db . Column ( '<STR_LIT>' , db . Text , nullable = False ) <EOL> def __init__ ( self , timeline_data = '<STR_LIT>' ) : <EOL> self . timeline_data = timeline_data <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . timeline_data <EOL> class Link ( db . Model ) : <EOL> __tablename__ = '<STR_LIT>' <EOL> id = db . Column ( '<STR_LIT:id>' , db . Integer , primary_key = True ) <EOL> displayorder = db . Column ( '<STR_LIT>' , db . Integer , nullable = False ) <EOL> name = db . Column ( '<STR_LIT:name>' , db . String ( <NUM_LIT:100> ) , nullable = False ) <EOL> url = db . Column ( '<STR_LIT:url>' , db . String ( <NUM_LIT:100> ) , nullable = False ) <EOL> def __init__ ( self , name = None , displayorder = None , url = None ) : <EOL> self . name = name <EOL> self . displayorder = displayorder <EOL> self . url = url <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . name <EOL> class Category ( db . Model ) : <EOL> __tablename__ = '<STR_LIT>' <EOL> id = db . Column ( '<STR_LIT:id>' , db . Integer , primary_key = True ) <EOL> name = db . Column ( '<STR_LIT:name>' , db . String ( <NUM_LIT> ) , nullable = False ) <EOL> id_num = db . Column ( '<STR_LIT>' , db . Integer , nullable = False ) <EOL> content = db . Column ( '<STR_LIT:content>' , db . Text , nullable = False ) <EOL> def __init__ ( self , name = None , id_num = None , content = None ) : <EOL> self . name = name <EOL> self . id_num = id_num <EOL> self . content = content <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . name <EOL> class Tag ( db . Model ) : <EOL> __tablename__ = '<STR_LIT>' <EOL> id = db . Column ( '<STR_LIT:id>' , db . Integer , primary_key = True ) <EOL> name = db . Column ( '<STR_LIT:name>' , db . String ( <NUM_LIT> ) , nullable = False ) <EOL> id_num = db . Column ( '<STR_LIT>' , db . Integer , nullable = False ) <EOL> content = db . Column ( '<STR_LIT:content>' , db . Text , nullable = False ) <EOL> def __init__ ( self , name = None , id_num = None , content = None ) : <EOL> self . name = name <EOL> self . id_num = id_num <EOL> self . content = content <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . name <EOL> class Comment ( db . Model ) : <EOL> __tablename__ = '<STR_LIT>' <EOL> id = db . Column ( '<STR_LIT:id>' , db . Integer , primary_key = True ) <EOL> postid = db . Column ( '<STR_LIT>' , db . Integer , primary_key = True ) <EOL> author = db . Column ( '<STR_LIT>' , db . String ( <NUM_LIT:20> ) , nullable = True ) <EOL> email = db . Column ( '<STR_LIT:email>' , db . String ( <NUM_LIT:30> ) , nullable = True ) <EOL> url = db . Column ( '<STR_LIT:url>' , db . String ( <NUM_LIT> ) , nullable = True ) <EOL> visible = db . Column ( '<STR_LIT>' , db . Integer , nullable = True ) <EOL> add_time = db . Column ( '<STR_LIT>' , db . Integer , nullable = False ) <EOL> content = db . Column ( '<STR_LIT:content>' , db . Text , nullable = False ) <EOL> def __init__ ( self , postid = None , author = None , email = None , url = None , visible = None , content = None , ) : <EOL> self . postid = postid <EOL> self . author = author <EOL> self . email = email <EOL> self . url = url <EOL> self . visible = visible <EOL> self . add_time = int ( time ( ) ) <EOL> self . content = content <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . content <EOL> class OperatorDB : <EOL> def getAboutReplyAll ( self ) : <EOL> return AboutReply . query . order_by ( AboutReply . reply_date . desc ( ) ) . all ( ) <EOL> def getAboutPost ( self ) : <EOL> post = Article . query . filter_by ( _post_type = <NUM_LIT:2> ) . order_by ( Article . _add_time . desc ( ) ) . first ( ) <EOL> return post <EOL> def getContactPost ( self ) : <EOL> post = Article . query . filter_by ( _post_type = <NUM_LIT:3> ) . order_by ( Article . _add_time . desc ( ) ) . first ( ) <EOL> return post <EOL> def insertAboutReply ( self , author , email , url , comment , parent_id = None ) : <EOL> aboutReply = AboutReply ( author , email , url , comment ) <EOL> db . session . add ( aboutReply ) <EOL> db . session . commit ( ) <EOL> def addSogood ( self , _id ) : <EOL> updateSql = '<STR_LIT>' % _id <EOL> db . session . execute ( updateSql ) <EOL> db . session . commit ( ) <EOL> def addSobad ( self , _id ) : <EOL> updateSql = '<STR_LIT>' % _id <EOL> db . session . execute ( updateSql ) <EOL> db . session . commit ( ) <EOL> def updateDuoshuoCommentsNum ( self , page ) : <EOL> try : <EOL> posts = Article . query . order_by ( Article . _add_time . desc ( ) ) [ EACH_PAGE_POST_NUM * ( page - <NUM_LIT:1> ) : EACH_PAGE_POST_NUM * page ] <EOL> for post in posts : <EOL> r = requests . get ( '<STR_LIT>' % post . _id ) <EOL> coments_num = - <NUM_LIT:1> <EOL> if r . status_code == <NUM_LIT:200> : <EOL> json_r = json . loads ( r . content ) <EOL> if json_r [ "<STR_LIT:code>" ] == <NUM_LIT:0> and json_r [ '<STR_LIT>' ] != [ ] : <EOL> coments_num = json_r [ '<STR_LIT>' ] [ str ( post . _id ) ] [ '<STR_LIT>' ] <EOL> if coments_num != - <NUM_LIT:1> : <EOL> post . _comment_num = coments_num <EOL> db . session . commit ( ) <EOL> except Exception , e : <EOL> logging . error ( "<STR_LIT>" % e ) <EOL> finally : <EOL> db . session . close ( ) <EOL> def get_post_page ( self , page ) : <EOL> self . updateDuoshuoCommentsNum ( page ) <EOL> posts = Article . query . filter ( Article . _post_type . in_ ( [ <NUM_LIT:0> , <NUM_LIT:1> ] ) ) . order_by ( Article . _add_time . desc ( ) ) . paginate ( page , EACH_PAGE_POST_NUM , False ) <EOL> db . session . close ( ) <EOL> return posts <EOL> def get_weixin_articles ( self , num = <NUM_LIT:3> ) : <EOL> posts = Article . query . filter ( Article . _post_type . in_ ( [ <NUM_LIT:0> , <NUM_LIT:1> ] ) ) . order_by ( Article . _add_time . desc ( ) ) [ : num ] <EOL> db . session . close ( ) <EOL> return posts <EOL> def get_editor_post ( self , _start = <NUM_LIT:0> , _end = <NUM_LIT:3> ) : <EOL> return Article . query . filter_by ( _post_type = <NUM_LIT:1> ) . order_by ( Article . _add_time . desc ( ) ) . all ( ) [ _start : _end ] <EOL> def get_post_page_category ( self , cat ) : <EOL> return Article . query . order_by ( Article . _add_time . desc ( ) ) . filter_by ( _category = cat ) . all ( ) <EOL> def get_post_page_tags ( self , tags ) : <EOL> return Article . query . order_by ( Article . _add_time . desc ( ) ) . filter ( Article . _tags . contains ( tags ) ) . all ( ) <EOL> def get_post_older_newer ( self , postid ) : <EOL> _older = Article . query . order_by ( Article . _id . desc ( ) ) . filter ( Article . _id < postid ) . first ( ) <EOL> _newer = Article . query . order_by ( Article . _id . asc ( ) ) . filter ( Article . _id > postid ) . first ( ) <EOL> return _older , _newer <EOL> def get_max_id ( self ) : <EOL> result = db . session . execute ( "<STR_LIT>" ) <EOL> maxid = <NUM_LIT:0> <EOL> for row in result : <EOL> if row [ '<STR_LIT>' ] : <EOL> maxid = row [ '<STR_LIT>' ] <EOL> result . close ( ) <EOL> return maxid <EOL> def get_last_post_add_time ( self ) : <EOL> result = db . session . execute ( "<STR_LIT>" ) <EOL> add_time = <NUM_LIT:0> <EOL> for row in result : <EOL> if row [ '<STR_LIT>' ] : <EOL> add_time = row [ '<STR_LIT>' ] <EOL> result . close ( ) <EOL> return add_time <EOL> def get_last_post_edit_time ( self ) : <EOL> result = db . session . execute ( "<STR_LIT>" ) <EOL> edit_time = <NUM_LIT:0> <EOL> for row in result : <EOL> if row [ '<STR_LIT>' ] : <EOL> edit_time = row [ '<STR_LIT>' ] <EOL> result . close ( ) <EOL> return edit_time <EOL> def add_new_article ( self , category , title , content , tags , password , <EOL> shorten_content , imgthumbnail , _post_type , _editor_title , _click_num = <NUM_LIT:0> ) : <EOL> timestamp = int ( time ( ) ) <EOL> _article = Article ( category , title , content , <NUM_LIT:0> , <NUM_LIT:0> , tags , <EOL> password , shorten_content , imgthumbnail , <EOL> _post_type , timestamp , timestamp , _click_num , _editor_title ) <EOL> db . session . add ( _article ) <EOL> db . session . commit ( ) <EOL> return _article <EOL> def update_article ( self , id , category , title , content , tags , password , <EOL> shorten_content , imgthumbnail , post_type , editor_title ) : <EOL> timestamp = int ( time ( ) ) <EOL> _article = Article . query . filter_by ( _id = id ) . first ( ) <EOL> _article . _category = category <EOL> _article . _title = title <EOL> _article . _content = content <EOL> _article . _tags = tags <EOL> _article . _password = password <EOL> _article . _shorten_content = shorten_content <EOL> _article . _edit_time = timestamp <EOL> _article . _imgthumbnail = imgthumbnail <EOL> _article . _post_type = post_type <EOL> _article . _editor_title = editor_title <EOL> db . session . commit ( ) <EOL> def add_new_comment ( self , postid , author , email , url , visible , content ) : <EOL> comment = Comment ( postid , author , email , url , visible , content ) <EOL> db . session . add ( comment ) <EOL> _article = Article . query . filter_by ( _id = postid ) . first ( ) <EOL> _article . _comment_num = _article . _comment_num + <NUM_LIT:1> <EOL> db . session . commit ( ) <EOL> return comment . id <EOL> def getArticleAllForTimeline ( self ) : <EOL> return Article . query . order_by ( Article . _add_time . asc ( ) ) . all ( ) <EOL> def isHasData ( self ) : <EOL> if <NUM_LIT:0> == len ( Timeline . query . all ( ) ) : <EOL> return False <EOL> return True <EOL> def getTimelineData ( self ) : <EOL> return Timeline . query . first ( ) <EOL> def saveTimelineData ( self , _data = '<STR_LIT>' , _id = '<STR_LIT>' ) : <EOL> if _id == '<STR_LIT>' : <EOL> timeline = Timeline ( _data ) <EOL> db . session . add ( timeline ) <EOL> else : <EOL> timelinedata = Timeline . query . filter_by ( id = _id ) . first ( ) <EOL> timelinedata . timeline_data = _data <EOL> db . session . commit ( ) <EOL> def get_post_comments ( self , postid ) : <EOL> return Comment . query . order_by ( Comment . add_time . desc ( ) ) . filter_by ( postid = postid ) . all ( ) <EOL> def get_comments_new ( self , count = <NUM_LIT:5> ) : <EOL> return Comment . query . order_by ( Comment . add_time . desc ( ) ) . all ( ) [ : count ] <EOL> def get_comment_by_id ( self , id ) : <EOL> return Comment . query . fiter_by ( id = id ) . first ( ) <EOL> def del_comment_by_id ( self , id ) : <EOL> db . session . execute ( '<STR_LIT>' % id ) <EOL> db . session . commit ( ) <EOL> def detail_post_by_id ( self , _id = '<STR_LIT>' ) : <EOL> if _id : <EOL> article = Article . query . filter_by ( _id = _id ) . first ( ) <EOL> article . _click_num += <NUM_LIT:1> <EOL> logging . error ( "<STR_LIT>" % article . _click_num ) <EOL> db . session . commit ( ) <EOL> return article <EOL> def del_post_by_id ( self , _id = '<STR_LIT>' ) : <EOL> _article = Article . query . filter_by ( _id = _id ) . first ( ) <EOL> if _article : <EOL> db . session . execute ( '<STR_LIT>' % _id ) <EOL> db . session . execute ( '<STR_LIT>' % ( _id , _article . _comment_num ) ) <EOL> _category = Category . query . filter_by ( name = _article . _category ) . first ( ) <EOL> if _category : <EOL> if _category . id_num > <NUM_LIT:1> : <EOL> _category . id_num = _category . id_num - <NUM_LIT:1> <EOL> else : <EOL> db . session . delete ( _category ) <EOL> db . session . commit ( ) <EOL> def get_post_for_homepage ( self ) : <EOL> return Article . query . order_by ( Article . _id . desc ( ) ) . all ( ) [ : EACH_PAGE_POST_NUM ] <EOL> def login_user ( self , name = '<STR_LIT>' , password = '<STR_LIT>' ) : <EOL> result = db . session . execute ( "<STR_LIT>" % ( name , password ) ) <EOL> for row in result : <EOL> if row [ '<STR_LIT:id>' ] : <EOL> return True <EOL> return False <EOL> def has_user ( self ) : <EOL> result = db . session . execute ( "<STR_LIT>" ) <EOL> for row in result : <EOL> if row [ '<STR_LIT:id>' ] : <EOL> return True <EOL> return False <EOL> def add_user ( self , name = '<STR_LIT>' , password = '<STR_LIT>' ) : <EOL> _user = User ( name , password ) <EOL> db . session . add ( _user ) <EOL> db . session . commit ( ) <EOL> def get_all_links ( self ) : <EOL> return Link . query . order_by ( Link . displayorder . asc ( ) ) . all ( ) <EOL> def add_new_link ( self , name , displayorder , url ) : <EOL> _link = Link ( name , displayorder , url ) <EOL> db . session . add ( _link ) <EOL> db . session . commit ( ) <EOL> def update_link_edit ( self , id , name , displayorder , url ) : <EOL> _link = Link . query . filter_by ( id = id ) . first ( ) <EOL> _link . name = name <EOL> _link . displayorder = displayorder <EOL> _link . url = url <EOL> db . session . commit ( ) <EOL> def get_link_by_id ( self , id ) : <EOL> return Link . query . filter_by ( id = id ) . first ( ) <EOL> def del_link_by_id ( self , id ) : <EOL> db . session . execute ( "<STR_LIT>" % id ) <EOL> db . session . commit ( ) <EOL> def get_all_cat_name ( self ) : <EOL> return Category . query . order_by ( Category . name . asc ( ) ) . all ( ) <EOL> def add_postid_to_cat ( self , name = '<STR_LIT>' , postid = '<STR_LIT>' ) : <EOL> _category = Category . query . filter_by ( name = name ) . first ( ) <EOL> if _category : <EOL> _category . id_num = _category . id_num + <NUM_LIT:1> <EOL> _category . content = postid + '<STR_LIT:U+002C>' + _category . content <EOL> else : <EOL> _category = Category ( name , <NUM_LIT:1> , postid ) <EOL> db . session . add ( _category ) <EOL> db . session . commit ( ) <EOL> def get_all_tag_name ( self ) : <EOL> return Tag . query . order_by ( Tag . id_num . asc ( ) ) . all ( ) [ : <NUM_LIT:10> ] <EOL> def add_postid_to_tags ( self , tags = [ ] , postid = '<STR_LIT>' ) : <EOL> for tag_name in tags : <EOL> _tag = Tag . query . filter_by ( name = tag_name ) . first ( ) <EOL> if _tag : <EOL> _tag . id_num = _tag . id_num + <NUM_LIT:1> <EOL> _tag . content = postid + '<STR_LIT:U+002C>' + _tag . content <EOL> else : <EOL> _tag = Tag ( tag_name , <NUM_LIT:1> , postid ) <EOL> db . session . add ( _tag ) <EOL> db . session . commit ( ) <EOL> operatorDB = OperatorDB ( ) </s>
