From 6210ca432aef89d7b09e2a628a3531913b4c4d5c Mon Sep 17 00:00:00 2001 From: hzz <849163445@qq.com> Date: Wed, 24 May 2023 18:31:52 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8ponyORM=E9=87=8D=E6=9E=84get=5Ftoday?= =?UTF-8?q?=5Farticle=E5=87=BD=E6=95=B0=E7=9A=84=E5=8E=9F=E7=94=9FSQL?= =?UTF-8?q?=E8=AF=AD=E5=8F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Article.py | 9 ++++----- app/MyPony.py | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 5 deletions(-) create mode 100644 app/MyPony.py diff --git a/app/Article.py b/app/Article.py index df9ac3a..ba9d7bc 100644 --- a/app/Article.py +++ b/app/Article.py @@ -8,6 +8,7 @@ import hashlib from datetime import datetime from flask import Flask, request, redirect, render_template, url_for, session, abort, flash, get_flashed_messages from difficulty import get_difficulty_level_for_user, text_difficulty_level, user_difficulty_level +from MyPony import * path_prefix = '/var/www/wordfreq/wordfreq/' @@ -32,22 +33,20 @@ def get_article_body(s): return '\n'.join(lst) +@db_session def get_today_article(user_word_list, visited_articles): - rq = RecordQuery(path_prefix + 'static/wordfreqapp.db') if visited_articles is None: visited_articles = { "index" : 0, # 为 article_ids 的索引 "article_ids": [] # 之前显示文章的id列表,越后越新 } if visited_articles["index"] > len(visited_articles["article_ids"])-1: # 生成新的文章,因此查找所有的文章 - rq.instructions("SELECT * FROM article") + result = list(selectAllArticles()) else: # 生成阅读过的文章,因此查询指定 article_id 的文章 if visited_articles["article_ids"][visited_articles["index"]] == 'null': # 可能因为直接刷新页面导致直接去查询了'null',因此当刷新的页面的时候,需要直接进行“上一篇”操作 visited_articles["index"] -= 1 visited_articles["article_ids"].pop() - rq.instructions('SELECT * FROM article WHERE article_id=%d' % (visited_articles["article_ids"][visited_articles["index"]])) - rq.do() - result = rq.get_results() + result = list(selectArticlesById(visited_articles["article_ids"][visited_articles["index"]])) random.shuffle(result) # Choose article according to reader's level diff --git a/app/MyPony.py b/app/MyPony.py new file mode 100644 index 0000000..71255a4 --- /dev/null +++ b/app/MyPony.py @@ -0,0 +1,55 @@ +from pony.orm import * + +db = Database() + + +# 数据库实体 +class Article(db.Entity): + _table_ = "article" # table name + article_id = PrimaryKey(int, auto=True) + text = Optional(str) + source = Optional(str) + date = Optional(str) + level = Optional(str) + question = Optional(str) + + +db.bind(provider='sqlite', filename='./static/wordfreqapp.db', create_db=True) +db.generate_mapping(create_tables=True) +# 打印SQL错误 +set_sql_debug(True) + + +@db_session +def selectAllArticles(): + """ + 获取全部文章 + """ + articles = select(a for a in Article) + articles_list = [] + for i in articles: + d = {"article_id": i.article_id, + "text": i.text, + "source": i.source, + "date": i.date, + "level": i.level, + "question": i.question} + articles_list.append(d) + return articles_list + + +@db_session +def selectArticlesById(id): + """ + 根据文章id搜索 + """ + article = Article.get(article_id=id) + li = [] + d = {"article_id": article.article_id, + "text": article.text, + "source": article.source, + "date": article.date, + "level": article.level, + "question": article.question} + li.append(d) + return li