mailbox.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. from flask import Blueprint, render_template, request, flash, abort
  2. from flask_login import login_required, current_user
  3. from flask_wtf import FlaskForm
  4. from wtforms import DateField, SelectField, SubmitField
  5. from wtforms.validators import DataRequired
  6. from time import strftime, strptime, mktime, localtime
  7. from typing import List
  8. from mailbox.email import Mail, HTML, PLAIN
  9. from .page import get_page, get_max_page
  10. from .logger import Logger
  11. mailbox = Blueprint("mailbox", __name__)
  12. class ToMailboxForm(FlaskForm):
  13. date = DateField("发信时间", description="信件发送时间", validators=[DataRequired("必须选择时间")])
  14. select = SelectField("文件夹", description="INBOX或其他文件夹", validators=[DataRequired("必须选择文件夹")])
  15. submit = SubmitField("查询")
  16. def __init__(self, select: list):
  17. super(ToMailboxForm, self).__init__()
  18. try:
  19. del select[select.index("INBOX")]
  20. select = ["INBOX"] + select
  21. except ValueError:
  22. pass
  23. self.select.choices = select
  24. self.select.coerce = str
  25. self.select.default = "INBOX"
  26. def __load_mailbox_page(mail_list, page, to_mail=None, date=None, select=None, next_date=None, last_date=None):
  27. if not to_mail:
  28. to_mail = ToMailboxForm(current_user.get_inbox_list())
  29. max_page = get_max_page(len(mail_list), 10)
  30. page_list = get_page("mailbox.mail_list_page", page, max_page, date=date, select=select)
  31. page_mail_list: List[Mail] = mail_list[(page - 1) * 10: page * 10]
  32. return render_template("mailbox/mailbox.html",
  33. to_mail=to_mail,
  34. date=date,
  35. select=select,
  36. page_list=page_list,
  37. page=page,
  38. mail_list=page_mail_list,
  39. max_page=max_page,
  40. empty=(len(page_mail_list) == 0),
  41. next_date=next_date,
  42. last_date=last_date)
  43. @mailbox.route("/")
  44. @login_required
  45. def mail_list_page():
  46. date = request.args.get("date", None, type=str)
  47. select = request.args.get("select", "INBOX", type=str)
  48. page = request.args.get("page", 1, type=int)
  49. if date:
  50. date_obj = strptime(date, "%Y-%m-%d")
  51. mail_list, download = current_user.get_mail_list(select, strftime('%d-%b-%Y', date_obj))
  52. if mail_list is None and not download:
  53. flash("旧任务未完成,正在下载邮件,请稍后")
  54. mail_list = []
  55. elif download:
  56. flash("启动新任务下载邮件,请稍后")
  57. mail_list = []
  58. next_date = strftime("%Y-%m-%d", localtime(mktime(date_obj) + 24 * 60 * 60))
  59. last_date = strftime("%Y-%m-%d", localtime(mktime(date_obj) - 24 * 60 * 60))
  60. else:
  61. mail_list = []
  62. next_date = None
  63. last_date = None
  64. Logger.print_load_page_log("mail list")
  65. return __load_mailbox_page(mail_list,
  66. page,
  67. date=date,
  68. select=select,
  69. next_date=next_date,
  70. last_date=last_date)
  71. def __get_mail() -> (Mail, str, str, int):
  72. mail_id = request.args.get("mail", None, type=int)
  73. date = request.args.get("date", None, type=str)
  74. select = request.args.get("select", None, type=str)
  75. if not mail_id or not date or not select:
  76. abort(404)
  77. date_obj = strftime('%d-%b-%Y', strptime(date, "%Y-%m-%d"))
  78. mail: Mail = current_user.get_mail(date_obj, select, mail_id)
  79. if not mail:
  80. abort(404)
  81. return mail, date, select, mail_id
  82. @mailbox.route("/html")
  83. @login_required
  84. def html_page():
  85. html_id = request.args.get("id", 1, type=int)
  86. mail, *_ = __get_mail()
  87. count = 0
  88. for i in mail.body_list:
  89. count += 1
  90. if type(i) is HTML:
  91. if count == html_id:
  92. return i.body
  93. return abort(404)
  94. @mailbox.route("/mail")
  95. @login_required
  96. def mail_page():
  97. mail, date, select, mail_id = __get_mail()
  98. count = 0
  99. html_id = []
  100. plain = []
  101. for i in mail.body_list:
  102. count += 1
  103. if type(i) is HTML:
  104. html_id.append(count)
  105. elif type(i) is PLAIN:
  106. plain.append(i)
  107. return render_template("mailbox/mail.html",
  108. date=date,
  109. select=select,
  110. mail_id=mail_id,
  111. mail=mail,
  112. html_id=html_id,
  113. plain=plain)