def getPageByIndex(Index): '''根据文章索引号获取显示页,每页显示3篇文章,所以0、1、2其实返回数据是一样的。 使用@cache.cached()装饰器装饰,根据索引进行缓存,提高显示效率。 flask-cache扩展依据【request.path】作为缓存Key,所以调用本函数前要对request.path进行key值处理,必须包含Index信息''' app.logger.debug("index begin") articles = [] postDir = app.config["POST_DIR"] fileList = [] files = os.listdir(postDir) p = int(Index) for f in files: fileList.append(postDir + os.sep + f) fileList = sortArticleListByTime(fileList, reverse = True) for singleFile in fileList[p:p + 3]: article = articleFileRender(singleFile, False) if article: articles.append(article) if p > 2: prev = True else: prev = False if p + 4 <= len(fileList): pnext = True else: pnext = False tmprender = render_template("index.html", title=app.config['TITLE'], url=app.config['URL'], articles=articles, prev=prev, pnext=pnext, prevnum=p - 3, nextnum=p + 3) app.logger.debug("index end") return tmprender
def RSSMaker(): articles = [] postDir = app.config["POST_DIR"] fileList = [] files = os.listdir(postDir) for f in files: fileList.append(postDir + os.sep + f) fileList = sortArticleListByTime(fileList, reverse = True) for singleFile in fileList: article = articleFileRender(singleFile, True) if article: articles.append(article) rssItems = [] for article in articles: link = app.config["URL"] + "/article/" + article["name"] rssItem = PyRSS2Gen.RSSItem( title=article["title"], link=link, description=article["content"], guid=PyRSS2Gen.Guid(link), pubDate=datetime.datetime(int(article["date"][0:4]), int(article["date"][5:7]), int(article["date"][8:10]), int(article["date"][11:13]), int(article["date"][14:16]))) rssItems.append(rssItem) rss = PyRSS2Gen.RSS2( title=app.config["TITLE"], link=app.config["URL"], description="", lastBuildDate=datetime.datetime.utcnow(), items=rssItems) rss.write_xml(open(app.config['RSS_PATH'], "w"))