Esempio n. 1
0
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的类目不存在!'
Esempio n. 2
0
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'修改成功!'
Esempio n. 3
0
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