Browse Source

feat & fix: 优化数据初始化程序

SongZihuan 3 years ago
parent
commit
b5fb072549
5 changed files with 55 additions and 42 deletions
  1. 1 1
      app/data/views.py
  2. 1 1
      app/web.py
  3. 12 11
      conf/args.py
  4. 29 26
      init.py
  5. 12 3
      main.py

+ 1 - 1
app/data/views.py

@@ -49,7 +49,7 @@ def count_by_days():
                       title_opts=pyecharts.options.TitleOpts(title="时段统计")))
 
     for i in res:
-        bar.add_yaxis(series_name=i, y_axis=res[i], color=random_color())
+        bar.add_yaxis(series_name=i, y_axis=res[i], color=random_color(), stack="count")
     bar.add_yaxis(series_name="合计", y_axis=count_data, color=random_color())
 
     return Markup(bar.render_embed())

+ 1 - 1
app/web.py

@@ -187,7 +187,7 @@ class DataWebsite(WebsiteBase):
                               where=["CheckResult is not null", "CheckResult=1"],
                               group_by=["GarbageType"],
                               order_by=[("GarbageType", "ASC")])
-        if cur is None or cur.rowcount == 0:
+        if cur is None:
             return None
         return cur.fetchall()
 

+ 12 - 11
conf/args.py

@@ -2,6 +2,7 @@ import argparse
 import os
 import sys
 from typing import Optional, Dict
+import conf
 
 res = os.environ.get('HGSSystem_NA')
 p_args: "Dict[str: Optional[str]]" = {"mysql_url": None,
@@ -74,15 +75,15 @@ if p_args.get('mysql_url') is None or p_args.get('mysql_name') is None or p_args
     if res is not None:
         res = res.split(';')
         if len(res) == 4:
-            p_args['mysql_url'] = res[0]
-            p_args['mysql_name'] = res[1]
-            p_args['mysql_passwd'] = res[2]
-            p_args['mysql_port'] = res[3]
+            p_args['mysql_url'] = str(conf.conf_args.get("mysql_url", res[0]))
+            p_args['mysql_name'] = str(conf.conf_args.get("mysql_name", res[1]))
+            p_args['mysql_passwd'] = str(conf.conf_args.get("mysql_passwd", res[2]))
+            p_args['mysql_port'] = str(conf.conf_args.get("mysql_port", res[3]))
         elif len(res) == 3:
-            p_args['mysql_url'] = res[0]
-            p_args['mysql_name'] = res[1]
-            p_args['mysql_passwd'] = res[2]
-            p_args['mysql_port'] = "3306"
+            p_args['mysql_url'] = str(conf.conf_args.get("mysql_url", res[0]))
+            p_args['mysql_name'] = str(conf.conf_args.get("mysql_name", res[1]))
+            p_args['mysql_passwd'] = str(conf.conf_args.get("mysql_passwd", res[2]))
+            p_args['mysql_port'] = str(conf.conf_args.get("mysql_port", 3306))
         else:
             print("MYSQL地址错误", file=sys.stderr)
             exit(1)
@@ -92,14 +93,14 @@ if p_args.get('aliyun_key') is None or p_args.get('aliyun_secret') is None:
     if res is not None:
         res = res.split(';')
         if len(res) == 2:
-            p_args["aliyun_key"] = res[0]
-            p_args["aliyun_secret"] = res[1]
+            p_args['aliyun_key'] = str(conf.conf_args.get("aliyun_key", res[0]))
+            p_args['aliyun_secret'] = str(conf.conf_args.get("aliyun_secret", res[1]))
         else:
             print("阿里云认证错误", file=sys.stderr)
             exit(1)
 
 if p_args.get('app_secret') is None:
-    res = os.environ.get('HGSSystem_AppSecret')
+    res = conf.conf_args.get("AppSecret", os.environ.get('HGSSystem_AppSecret'))
     if res is not None:
         p_args['app_secret'] = res
     else:

+ 29 - 26
init.py

@@ -88,6 +88,11 @@ try:
 except pymysql.err.Error:
     print("请提供正确的MySQL信息", file=sys.stderr)
     sys.exit(1)
+else:
+    cursor.execute("USE hgssystem")
+
+from tool.login import create_uid
+from tool.time import mysql_time
 
 print("是否执行数据库初始化程序?\n执行初始化程序会令你丢失所有数据.")
 res = input("[Y/n]")
@@ -105,16 +110,12 @@ if res == 'Y' or res == 'y':
     while len(admin_phone) != 11:
         admin_phone = input("输入 'admin' 管理员的电话[长度=11]: ")
 
-    from tool.login import create_uid
-    from tool.time import mysql_time
-
     # 生成基本 admin 用户
     uid = create_uid("admin", admin_passwd)
     cursor.execute(f"INSERT INTO user(UserID, Name, IsManager, Phone, Score, Reputation, CreateTime) "
                    f"VALUES ('{uid}', 'admin', 1, '{admin_phone}', 10, 300, {mysql_time()});")
     sql.commit()
 
-
 print("是否伪造数据?")
 if input("[Y/n]") != "Y":
     cursor.close()
@@ -201,7 +202,7 @@ def random_garbage_c(r_time, r_time2, cur):
 
 def random_garbage_u(r_time, r_time2, cur):
     user = random.choice(random_normal)
-    random_garbage_c_to_user(user, r_time, r_time2, cur)
+    random_garbage_u_to_user(user, r_time, r_time2, cur)
 
 
 def random_news(c_time, cur):
@@ -272,12 +273,12 @@ def make_fake():
         if phone == 'x':
             phone = fake.phone_number()
         w_garbage = int(w_garbage)
-        c_garbage = int(w_garbage)
+        c_garbage = int(c_garbage)
         random_user(name, passwd, phone, c_time, 0, cursor, w_garbage, c_garbage)
 
     count = int(input("步骤3, 注册随机管理员账户[输入个数]:"))
     while count > 0:
-        name = randomPassword()[:5]
+        name = fake.name()
         passwd = randomPassword()
         phone = fake.phone_number()
         c_time = random_time()
@@ -286,36 +287,38 @@ def make_fake():
 
     count = int(input("步骤3, 注册随机普通账户[输入个数]:"))
     while count > 0:
-        name = randomPassword()[:5]
+        name = fake.name()
         passwd = randomPassword()
         phone = fake.phone_number()
         c_time = random_time()
         random_user(name, passwd, phone, c_time, 0, cursor)
         count -= 1
 
-    count = int(input("步骤4, 注册随机已检查垃圾袋[输入个数]:"))
-    while count > 0:
-        count -= 1
-        c_time2, c_time1 = random_time_double()
-        random_garbage_u(c_time1, c_time2, cursor)
-
-    count = int(input("步骤5, 注册随机待检查垃圾袋[输入个数]:"))
-    while count > 0:
-        count -= 1
-        c_time2, c_time1 = random_time_double()
-        random_garbage_c(c_time1, c_time2, cursor)
-
-    count = int(input("步骤6, 注册随机未使用垃圾袋[输入个数]:"))
+    count = int(input("步骤4, 注册随机未使用垃圾袋[输入个数]:"))
     while count > 0:
         count -= 1
         c_time = random_time()
         random_garbage_n(c_time, cursor)
 
-    count = int(input("步骤7, 注册随机新闻内容:"))
-    while count > 0:
-        count -= 1
-        c_time = random_time()
-        random_news(c_time, cursor)
+    if len(random_normal) > 0:
+        count = int(input("步骤5, 注册随机待检查垃圾袋[输入个数]:"))
+        while count > 0:
+            count -= 1
+            c_time2, c_time1 = random_time_double()
+            random_garbage_c(c_time1, c_time2, cursor)
+
+        if len(random_manager) > 0:
+            count = int(input("步骤6, 注册随机已检查垃圾袋[输入个数]:"))
+            while count > 0:
+                count -= 1
+                c_time2, c_time1 = random_time_double()
+                random_garbage_u(c_time1, c_time2, cursor)
+
+        count = int(input("步骤7, 注册随机新闻内容:"))
+        while count > 0:
+            count -= 1
+            c_time = random_time()
+            random_news(c_time, cursor)
 
     count = int(input("步骤8, 注册随机商城内容:"))
     while count > 0:

+ 12 - 3
main.py

@@ -8,6 +8,8 @@
 import sys
 import os
 
+import pymysql
+
 from conf import Config
 
 app = None
@@ -35,7 +37,10 @@ def main():
     if program_name == "setup":  # setup程序不需要数据库链接等操作
         __main = os.path.dirname(os.path.abspath(__file__))
 
-        res = os.system(f"{sys.executable} {os.path.join(__main, 'init.py')} "
+        exe = list(os.path.split(sys.executable))
+        exe[-1] = exe[-1].replace("pythonw", "python")
+        exe = os.path.join(*exe)
+        res = os.system(f"{exe} {os.path.join(__main, 'init.py')} "
                         f"--mysql_url={Config.mysql_url} "
                         f"--mysql_name={Config.mysql_name} "
                         f"--mysql_passwd={Config.mysql_passwd} "
@@ -46,8 +51,12 @@ def main():
             sys.exit(1)
         sys.exit(0)
 
-    from sql.db import DB
-    mysql = DB()
+    try:
+        from sql.db import DB
+        mysql = DB()
+    except pymysql.Error:
+        print("无法连接到 MySQL")
+        sys.exit(1)
 
     if program_name == "garbage":
         from equipment.aliyun import Aliyun