def get_article_list(index, type=''): try: index = int(index) except ValueError: index = 1 type = str(type) query = {} if type != '' and type != 'all': tt = article_dao.get_article_type_by_name(str(type)) if tt: query = {'type': str(tt['_id'])} else: return [], Page() item_limit = constants.item_limit * constants.item_limit_rate total_count = article_dao.get_article_count(query) page_count = int(math.ceil(total_count * 1.0 / item_limit)) if index > page_count or index < 1: offset = 0 index = 1 else: offset = (index - 1) * item_limit page = Page() page.index = index page.count = constants.page_count if page_count > constants.page_count else page_count if page.index <= page.count / 2: page.start = 1 else: page.start = page.index - int(page.count / 2) if page.start > page_count - page.count: page.start = page_count - page.count + 1 result = list(article_dao.get_article_list(offset, item_limit, query)) article_list = [] for art in result: model = Article() model._id = art['_id'] model.title = art['title'] model.tag = art['tag'] model.time = art['time'] model.content = art['content'] model.description = art['description'] model.visit_count = art['visit_count'] tt = article_dao.get_article_type_by_id(art['type']) if tt: model.type = tt['_id'] model.type_name = tt['type'] article_list.append(model) return article_list, page
def get_article(title): model = Article() if title != '': art = article_dao.get_article_by_title(str(title)) if art: model._id = art['_id'] model.title = art['title'] model.tag = art['tag'] model.time = art['time'] model.content = art['content'] model.description = art['description'] model.visit_count = art['visit_count'] tt = article_dao.get_article_type_by_id(art['type']) if tt: model.type = tt['_id'] model.type_name = tt['type'] return model
def get_latest_article(): result = list(article_dao.get_latest_article(constants.item_limit)) latest_articles = [] for art in result: model = Article() model._id = art['_id'] model.title = art['title'] model.tag = art['tag'] model.time = art['time'] model.content = art['content'] model.description = art['description'] model.visit_count = art['visit_count'] tt = article_dao.get_article_type_by_id(art['type']) if tt: model.type = tt['_id'] model.type_name = tt['type'] latest_articles.append(model) return latest_articles