From d7b40ee699bdd1d0a2a561891ff30ad3cdba532f Mon Sep 17 00:00:00 2001 From: Blueotter Date: Sat, 18 Jun 2022 16:17:08 +0800 Subject: [PATCH 1/2] add wordCMD.py --- app/main.py | 2 ++ app/wordCMD.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 app/wordCMD.py diff --git a/app/main.py b/app/main.py index d903bf4..31b0e68 100644 --- a/app/main.py +++ b/app/main.py @@ -11,12 +11,14 @@ from Article import * import Yaml from user_service import userService from account_service import accountService +from wordCMD import show_bp app = Flask(__name__) app.secret_key = 'lunch.time!' # 将蓝图注册到Lab app app.register_blueprint(userService) app.register_blueprint(accountService) +app.register_blueprint(show_bp) path_prefix = '/var/www/wordfreq/wordfreq/' path_prefix = './' # comment this line in deployment diff --git a/app/wordCMD.py b/app/wordCMD.py new file mode 100644 index 0000000..f5cfdd6 --- /dev/null +++ b/app/wordCMD.py @@ -0,0 +1,42 @@ +from flask import Flask, request, Blueprint, render_template +from UseSqlite import InsertQuery, RecordQuery + +TKTK = 'token' + +show_bp = Blueprint( + 'site', + __name__, +) + + +def make_html_paragraph(s): + if s.strip() == '': + return '' + lst = s.split(',') + word = lst[1].strip() + result = '' + word + '' + return result + + +@show_bp.route('/show//') +def show(name): + token = request.args.get("token") + + if token != TKTK: + return "token is wrong, please try again" + + rq = RecordQuery('./static/wordfreqapp.db') + rq.instructions("SELECT * FROM words where user = \'" + name + "\'") + rq.do() + + record = '

' + f"Here are {name}'s words:" + '

' + record += '' + record += '' + record += '' + record += '' + # record += '' + for r in rq.format_results().split('\n\n'): + record += '%s' % (make_html_paragraph(r)) + # record += '' + record += '
WORDS
' + return record+'\n' -- 2.17.1 From fa44bc91264afd4aecd5eed2e41af451da8e80de Mon Sep 17 00:00:00 2001 From: Blueotter Date: Sat, 18 Jun 2022 22:16:18 +0800 Subject: [PATCH 2/2] add more comments and sql file --- app/static/words.sql | 18 ++++++++++++++++++ app/wordCMD.py | 17 ++++++++--------- 2 files changed, 26 insertions(+), 9 deletions(-) create mode 100644 app/static/words.sql diff --git a/app/static/words.sql b/app/static/words.sql new file mode 100644 index 0000000..7b0b44c --- /dev/null +++ b/app/static/words.sql @@ -0,0 +1,18 @@ +PRAGMA foreign_keys = OFF; + + +DROP TABLE IF EXISTS "main"."words"; +CREATE TABLE "words" ( +"user" TEXT NOT NULL, +"word" TEXT NOT NULL +); + + +INSERT INTO "main"."words" VALUES ('jack', 'numb'); +INSERT INTO "main"."words" VALUES ('jack', 'faint'); +INSERT INTO "main"."words" VALUES ('alice', 'tube'); +INSERT INTO "main"."words" VALUES ('alice', 'cake'); +INSERT INTO "main"."words" VALUES ('tom', 'kite'); +INSERT INTO "main"."words" VALUES ('tom', 'fly'); +INSERT INTO "main"."words" VALUES ('jack', 'be'); +INSERT INTO "main"."words" VALUES ('jack', 'fly'); diff --git a/app/wordCMD.py b/app/wordCMD.py index f5cfdd6..94546ce 100644 --- a/app/wordCMD.py +++ b/app/wordCMD.py @@ -1,7 +1,7 @@ from flask import Flask, request, Blueprint, render_template from UseSqlite import InsertQuery, RecordQuery -TKTK = 'token' +TKTK = 'token' # set token show_bp = Blueprint( 'site', @@ -9,7 +9,7 @@ show_bp = Blueprint( ) -def make_html_paragraph(s): +def make_html_paragraph(s): # build the word's table if s.strip() == '': return '' lst = s.split(',') @@ -18,25 +18,24 @@ def make_html_paragraph(s): return result -@show_bp.route('/show//') +@show_bp.route('/show//') # set route for show page means the var name to search def show(name): token = request.args.get("token") - + # when token is wrong if token != TKTK: return "token is wrong, please try again" rq = RecordQuery('./static/wordfreqapp.db') + # search the user's words in db rq.instructions("SELECT * FROM words where user = \'" + name + "\'") rq.do() - - record = '

' + f"Here are {name}'s words:" + '

' + # show the results + record = '

' + f"Here are {name}'s words:" + '

' record += '' record += '' record += '' record += '' - # record += '' for r in rq.format_results().split('\n\n'): record += '%s' % (make_html_paragraph(r)) - # record += '' record += '
WORDS
' - return record+'\n' + return record + '\n' -- 2.17.1