Explorar o código

feat: 单词显示时使用单词数而非词条数

SongZihuan %!s(int64=3) %!d(string=hai) anos
pai
achega
b04ba2b1d3
Modificáronse 3 ficheiros con 16 adicións e 13 borrados
  1. 3 3
      app/test.py
  2. 7 4
      app/user.py
  3. 6 6
      templates/test.html

+ 3 - 3
app/test.py

@@ -36,7 +36,7 @@ class UploadFile(FlaskForm):
 
 def __load_word(word):
     user: UserWordDataBase = current_user
-    box, box_sum = user.get_box_count()
+    box, box_distinct, box_sum, box_sum_distinct = user.get_box_count()
     search_from = SearchForm()
     reset_delete_form = ResetDeleteForm()
     upload_form = UploadFile()
@@ -52,8 +52,8 @@ def __load_word(word):
         have_job = False
 
     template_var = dict(word=word, len=len,
-                        box=box, box_sum=box_sum, have_job=have_job,
-                        search=search_from, reset_delete=reset_delete_form, upload=upload_form)
+                        box=box, box_distinct=box_distinct, box_sum=box_sum, box_sum_distinct=box_sum_distinct,
+                        have_job=have_job, search=search_from, reset_delete=reset_delete_form, upload=upload_form)
     if word is None:
         return render_template("test.html", **template_var, have_word=False)
     serializer = URLSafeTimedSerializer(current_app.config["SECRET_KEY"])

+ 7 - 4
app/user.py

@@ -38,12 +38,15 @@ class UserWordDataBase(WordDatabase, UserMixin):
     def delete_user(self):
         self.delete_self()
 
-    def get_box_count(self) -> Tuple[list, int]:
-        res = self.search(columns=["COUNT(id)", "box"], table="Word", group_by=["box"])
+    def get_box_count(self) -> Tuple[list, list, int, int]:
+        res = self.search(columns=["COUNT(word)", "COUNT(DISTINCT word)", "box"], table="Word", group_by=["box"])
+        print(res)
         ret = [0, 0, 0, 0, 0]
+        ret_distinct = [0, 0, 0, 0, 0]
         for i in res:
-            ret[i[1] - 1] = i[0]
-        return ret, sum(ret)
+            ret[i[2] - 1] = i[0]
+            ret_distinct[i[2] - 1] = i[1]
+        return ret, ret_distinct, sum(ret), sum(ret_distinct)
 
     def reset(self):
         self.update(table="Word", kw={"box": "1"}, where="1")

+ 6 - 6
templates/test.html

@@ -147,19 +147,19 @@
 
                     <div class="col-12 col-lg-6 offset-lg-3 text-left">
                         <p class="h4 mb-2 col-12"> User: {{ current_user.user }} </p>
-                        <p class="col-12"> You have {{ box_sum }} word(s) in database. </p>
+                        <p class="col-12"> You have {{ box_sum_distinct }}({{ box_sum }}) word(s) in database. </p>
 
                         <div class="col-12 text-center mb-2">
                             <a class="btn btn-light mr-2 mb-2" href="{{ url_for("word_list.show_box_word", box=1) }}" >
-                                <span class="small"> Box1: {{ box[0] }} </span> </a>
+                                <span class="small"> Box1: {{ box_distinct[0] }} ({{ box[0] }}) </span> </a>
                             <a class="btn btn-light mr-2 mb-2" href="{{ url_for("word_list.show_box_word", box=2) }}">
-                                <span class="small"> Box2: {{ box[1] }} </span> </a>
+                                <span class="small"> Box2: {{ box_distinct[1] }} ({{ box[1] }}) </span> </a>
                             <a class="btn btn-light mr-2 mb-2" href="{{ url_for("word_list.show_box_word", box=3) }}">
-                                <span class="small"> Box3: {{ box[2] }} </span> </a>
+                                <span class="small"> Box3: {{ box_distinct[2] }} ({{ box[2] }}) </span> </a>
                             <a class="btn btn-light mr-2 mb-2" href="{{ url_for("word_list.show_box_word", box=4) }}">
-                                <span class="small"> Box4: {{ box[3] }} </span> </a>
+                                <span class="small"> Box4: {{ box_distinct[3] }} ({{ box[3] }}) </span> </a>
                             <a class="btn btn-light mb-2" href="{{ url_for("word_list.show_box_word", box=5) }}">
-                                <span class="small"> Box5: {{ box[4] }} </span> </a>
+                                <span class="small"> Box5: {{ box_distinct[4] }} ({{ box[4] }}) </span> </a>
                         </div>
 
                         <a class="col-12 btn btn-primary mb-2" href="{{ url_for("word_list.show_all_word") }}"> Word List </a>