Ver código fonte

feat: 调整web文件结构

SongZihuan 3 anos atrás
pai
commit
209e693f32
5 arquivos alterados com 82 adições e 77 exclusões
  1. 1 0
      app/__init__.py
  2. 45 0
      app/rank_views.py
  3. 34 0
      app/rank_web.py
  4. 0 75
      app/ranking.py
  5. 2 2
      main.py

+ 1 - 0
app/__init__.py

@@ -0,0 +1 @@
+from . import rank_views as rank

+ 45 - 0
app/rank_views.py

@@ -0,0 +1,45 @@
+import conf
+from flask import render_template, Blueprint, Flask
+from .rank_web import RankWebsite
+from sql.db import DB
+from tool.type_ import Optional
+
+main = Blueprint("main", __name__)
+rank_website: Optional[RankWebsite] = None
+rank_app: Optional[Flask] = None
+
+
+@main.route('/')
+def index():
+    return render_template("index.html", loc=conf.base_location)
+
+
+@main.route('/rank_up')
+def rank_up():
+    global rank_website
+    head = ["#", "UserName", "UserID", "Reputation", "Score"]
+    data = rank_website.get_rank("DESC")
+    return render_template("ranking.html", rank_head=head, rank_info=data, ranking_name="高分榜")
+
+
+@main.route('/rank_down')
+def rank_down():
+    global rank_website
+    head = ["#", "UserName", "UserID", "Reputation", "Score"]
+    data = rank_website.get_rank("ASC")
+    return render_template("ranking.html", rank_head=head, rank_info=data, ranking_name="警示榜")
+
+
+@main.app_errorhandler(404)
+def error_404(e):
+    return render_template("error.html", error_code="404", error_info=e), 404
+
+
+def creat_ranking_website(db: DB):
+    global rank_website, rank_app
+    if rank_website is not None:
+        return rank_website, rank_website.app
+    rank_app = Flask(__name__)
+    rank_app.register_blueprint(main)
+    rank_website = RankWebsite(db, rank_app)
+    return rank_website, rank_app

+ 34 - 0
app/rank_web.py

@@ -0,0 +1,34 @@
+from flask import Flask
+
+import conf
+from sql.db import DB
+from tool.type_ import Optional, List, Tuple
+
+
+class RankWebsite:
+    def __init__(self, db: DB, app: Flask):
+        self._db = db
+        self.app = app
+
+    def get_rank(self, order_by: str = "DESC") -> Optional[List[Tuple]]:
+        cur = self._db.search((f"SELECT UserID, Name, Score, Reputation "
+                               f"FROM user "
+                               f"WHERE IsManager = 0 "
+                               f"ORDER BY Reputation {order_by}, Score {order_by}, UserID {order_by} "
+                               f"LIMIT 200;"))
+        if cur is None:
+            return None
+        res = []
+        for index in range(cur.rowcount):
+            i = cur.fetchone()
+            res.append((f"NO.{index + 1}", i[1], i[0][:conf.tk_show_uid_len], str(i[3]), str(i[2])))
+        return res
+
+    def run(self,
+            host: Optional[str] = None,
+            port: Optional[int] = None,
+            debug: Optional[bool] = None,
+            load_dotenv: bool = True,
+            **options,
+            ):
+        self.app.run(host, port, debug, load_dotenv, **options)

+ 0 - 75
app/ranking.py

@@ -1,75 +0,0 @@
-from flask import Flask, render_template, Blueprint
-
-import conf
-from sql.db import DB
-from tool.type_ import Optional, List, Tuple
-
-
-class RankWebsite:
-    main = Blueprint("main", __name__)
-
-    def __init__(self, db: DB):
-        self._db = db
-        self.app: Flask = Flask(__name__)
-        self.app.register_blueprint(self.main)
-
-    def get_rank(self, order_by: str = "DESC") -> Optional[List[Tuple]]:
-        cur = self._db.search((f"SELECT UserID, Name, Score, Reputation "
-                               f"FROM user "
-                               f"WHERE IsManager = 0 "
-                               f"ORDER BY Reputation {order_by}, Score {order_by}, UserID {order_by} "
-                               f"LIMIT 200;"))
-        if cur is None:
-            return None
-        res = []
-        for index in range(cur.rowcount):
-            i = cur.fetchone()
-            res.append((f"NO.{index + 1}", i[1], i[0][:conf.tk_show_uid_len], str(i[3]), str(i[2])))
-        return res
-
-    @staticmethod
-    @main.route('/')
-    def index():
-        global website
-        return render_template("index.html", loc=conf.base_location)
-
-    @staticmethod
-    @main.route('/rank_up')
-    def rank_up():
-        global website
-        head = ["#", "UserName", "UserID", "Reputation", "Score"]
-        data = website.get_rank("DESC")
-        return render_template("ranking.html", rank_head=head, rank_info=data, ranking_name="高分榜")
-
-    @staticmethod
-    @main.route('/rank_down')
-    def rank_down():
-        global website
-        head = ["#", "UserName", "UserID", "Reputation", "Score"]
-        data = website.get_rank("ASC")
-        return render_template("ranking.html", rank_head=head, rank_info=data, ranking_name="警示榜")
-
-    @staticmethod
-    @main.app_errorhandler(404)
-    def error_404(e):
-        return render_template("error.html", error_code="404", error_info=e), 404
-
-    def run(self):
-        self.app.run()
-
-
-website: Optional[RankWebsite] = None
-
-
-def creat_ranking_website(db: DB):
-    global website
-    if website is not None:
-        return website, website.app
-    website = RankWebsite(db)
-    return website, website.app
-
-
-if __name__ == '__main__':
-    mysql_db = DB()
-    web, app = creat_ranking_website(mysql_db)
-    web.run()

+ 2 - 2
main.py

@@ -67,12 +67,12 @@ elif program_name == "manager":
     station.mainloop()
 elif program_name == "ranking_website":
     try:
-        import app.ranking as ranking_website
+        from app import rank as rank_web
     except ImportError:
         can_not_load("在线排行榜服务")
         sys.exit(1)
 
-    web, app = ranking_website.creat_ranking_website(mysql)
+    web, app = rank_web.creat_ranking_website(mysql)
     web.run()
 else:
     can_not_load(program_name)