Browse Source

feat: 选择文件夹

SongZihuan 2 years ago
parent
commit
cac62f90ed
3 changed files with 26 additions and 2 deletions
  1. 1 0
      templates/mailbox/mailbox.html
  2. 16 2
      web/mailbox.py
  3. 9 0
      web/user.py

+ 1 - 0
templates/mailbox/mailbox.html

@@ -10,6 +10,7 @@
     <form method="get" action="{{ url_for("mailbox.mail_list_page") }}" class="was-validated">
         {# 不需要隐藏字段 #}
         {{ render_field(to_mail.date) }}
+        {{ render_select_field(to_mail.select) }}
         <div class="text-end">
             {{ to_mail.submit(class='btn btn-success me-2') }}
         </div>

+ 16 - 2
web/mailbox.py

@@ -1,7 +1,7 @@
 from flask import Blueprint, render_template, request, flash
 from flask_login import login_required, current_user
 from flask_wtf import FlaskForm
-from wtforms import DateField, SubmitField
+from wtforms import DateField, SelectField, SubmitField
 from wtforms.validators import DataRequired
 from time import strftime, strptime, mktime, localtime
 from typing import List
@@ -16,13 +16,27 @@ mailbox = Blueprint("mailbox", __name__)
 
 class ToMailboxForm(FlaskForm):
     date = DateField("发信时间", description="信件发送时间", validators=[DataRequired("必须选择时间")])
+    select = SelectField("文件夹", description="INBOX或其他文件夹", validators=[DataRequired("必须选择文件夹")])
     submit = SubmitField("查询")
 
+    def __init__(self, select: list):
+        super(ToMailboxForm, self).__init__()
+
+        try:
+            del select[select.index("INBOX")]
+            select = ["INBOX"] + select
+        except ValueError:
+            pass
+
+
+        self.select.choices = select
+        self.select.coerce = str
+        self.select.default = "INBOX"
 
 
 def __load_mailbox_page(mail_list, page, to_mail=None, date=None, select=None, next_date=None, last_date=None):
     if not to_mail:
-        to_mail = ToMailboxForm()
+        to_mail = ToMailboxForm(current_user.get_inbox_list())
 
     max_page = get_max_page(len(mail_list), 10)
     page_list = get_page("mailbox.mail_list_page", page, max_page, date=date, select=select)

+ 9 - 0
web/user.py

@@ -98,3 +98,12 @@ class User(UserMixin):
             th.start()
             return imap.mailbox, True
         return imap.mailbox, False
+
+    def get_inbox_list(self):
+        imap = Imap(user=conf["IMAP_USERNAME"].format(self.username),
+                    passwd=conf["IMAP_PASSWD"].format(self.passwd),
+                    host=conf["IMAP_HOST"],
+                    port=conf["IMAP_PORT"],
+                    ssl=conf["IMAP_SSL"],
+                    start_ssl=conf["IMAP_START_SSL"])
+        return imap.list()