class CommentDAO: def __init__(self): self.session = MysqlUtil().get_session() def new_comment(self, art_id, content): if ArticleDao().get_article_by_id(art_id) is None: return False, u'评论的文章不存在' else: comment = Comment() comment.article_id = art_id comment.comment = content comment.user = current_user.username self.session.add(comment) self.session.commit() return True, u'评论成功!' def get_article_comments_num(self, art_id): return self.session.query(Comment) \ .filter_by(article_id=art_id).count() def get_comments_by_article(self, article_id, page=0, size=20): article_num = self.get_article_comments_num(article_id) if page * size > article_num: return None, u'分页数超出最大值' info = u'more' if (page + 1) * size < article_num else u'nomore' return self.session.query(Comment)\ .filter_by(article_id=article_id)\ .order_by(desc(Comment.modified_time)).offset(size * page).limit(size).all(), info
class CategoryDAO: def __init__(self): self.session = MysqlUtil().get_session() def add_category(self, cate_name): if self.get_cate_by_name(cate_name) is None: cate = Category(cate_name=cate_name) self.session.add(cate) self.session.commit() return True, u'成功添加新分类' else: return False, u'类目已经存在' def get_cate_by_name(self, cate_name): cate = self.session.query(Category).filter_by( cate_name=cate_name).first() if cate is not None: return cate else: return None def get_cate_by_id(self, cate_id): cate = self.session.query(Category).filter_by(id=cate_id).first() if cate is not None: return cate else: return None def get_categories(self): return self.session.query(Category).all() def delete_category(self, cate_id): cate = self.get_cate_by_id(cate_id) if cate is not None: self.session.delete(cate) self.session.commit() return True, u'删除成功' else: return False, u'删除失败,删除的类目不存在!' def update_category(self, cate_id, cate_name): cate = self.get_cate_by_id(cate_id) if cate is not None: if self.get_cate_by_id(cate_name) is None: cate.cate_name = cate_name cate.modify_time = datetime.datetime.utcnow() self.session.commit() return True, u'修改成功!' else: return False, u'修改的类目名称已经存在!' else: return False, u'指定ID的类目不存在!'
class AuthorDAO(): def __init__(self): self.session = MysqlUtil().get_session() def get_authors(self): return self.session.query(Author).all() def get_author_by_name(self, auth_name): return self.session.query(Author).filter_by(name=auth_name).first() def get_author_by_id(self, auth_id): return self.session.query(Author).filter_by(id=auth_id).first() def add_author(self, auth_name): if self.get_author_by_name(auth_name) is None: auth = Author(name=auth_name) self.session.add(auth) self.session.commit() return True, u'添加成功' else: return False, u'作者已经存在!' def delete_author(self, auth_name): auth = self.get_author_by_name(auth_name) if auth is None: return False, u'作者不存在!' self.session.delete(auth) self.session.commit() return True, u'删除作者成功!' def update_author(self, auth_id, auth_name): auth = self.get_author_by_name(auth_name) if auth is not None: return False, u'作者名已经存在!' else: auth = self.get_author_by_id(auth_id) auth.name = auth_name self.session.commit() return True, u'修改成功!'
def init_db(): conf = ConfigUtil() host = conf.get_config(conf='host', section='db_info') port = conf.get_config(conf='port', section='db_info') username = conf.get_config(conf='username', section='db_info') password = conf.get_config(conf='password', section='db_info') database = conf.get_config(conf='database', section='db_info') conn = MySQLdb.connect(host=host, user=username, passwd=password, port=int(port)) cur = conn.cursor() sql = 'CREATE DATABASE IF NOT EXISTS `{db}` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;'.format( db=database) cur.execute(sql) conn.commit() cur.close() conn.close() session = MysqlUtil().get_session() BaseModel.metadata.create_all(MysqlUtil().get_engine()) email = conf.get_config('email', 'admin_account') if session.query(User).filter_by(email=email).first() is None: password = conf.get_config('password', 'admin_account') username = conf.get_config('username', 'admin_account') user = User(username=username, password=generate_password_hash(password), email=email, status=1, is_admin=1, register_time=datetime.datetime.now(), last_login_time=datetime.datetime.now()) session.add(user) session.commit()
class ArticleDao: def __init__(self): self.session = MysqlUtil().get_session() def get_articles(self, page=0, size=20): article_num = self.get_article_num() if page * size > article_num: return None, u'分页数超出最大值' info = u'more' if (page + 1) * size < article_num else u'nomore' return self.session.query(Article).order_by(desc( Article.modified_time)).offset(size * page).limit(size).all(), info def get_article_num(self): return self.session.query(Article).count() def get_article_by_title(self, title): return self.session.query(Article).filter_by(title=title).first() def get_article_by_id(self, id): return self.session.query(Article).filter_by(id=id).first() def new_article(self, title, author_id, author_name, cate_id, cate_name, intro, filepath, tags): article = Article(title=title, intro=intro, is_public=1, auth_id=author_id, auth_name=author_name, cate_id=cate_id, cate_name=cate_name, file_path=filepath, tags=tags, create_time=datetime.datetime.now(), modified_time=datetime.datetime.now()) if self.get_article_by_title(title) is None: self.session.add(article) self.session.commit() return True, u'成功添加新文章!' else: return False, u'文章标题已经存在!' def delete_article(self, article): self.session.delete(article) self.session.commit() return True, u'删除成功!' def get_articles_by_cate(self, cate, page=0, size=20): article_num = self.get_article_num() if page * size > article_num: return None, u'分页数超出最大值' info = u'more' if (page + 1) * size < article_num else u'nomore' return self.session.query(Article).filter_by(cate_id=cate).order_by( desc(Article.modified_time)).offset(size * page).limit(size).all(), info def get_article_by_tag(self, tag, page=0, size=20): article_num = self.get_article_num() if page * size > article_num: return None, u'分页数超出最大值' info = u'more' if (page + 1) * size < article_num else u'nomore' return self.session.query(Article)\ .filter(Article.tags.like(u'%{tag}%'.format(tag=tag)))\ .order_by(desc(Article.modified_time)).offset(size * page).limit(size).all(), info