index.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from flask import render_template, request, redirect, url_for, flash
  2. from flask_login import login_required, current_user
  3. from sqlalchemy.orm import joinedload
  4. from app import email_utils
  5. from app.dashboard.base import dashboard_bp
  6. from app.extensions import db
  7. from app.log import LOG
  8. from app.models import GenEmail, ClientUser
  9. @dashboard_bp.route("/", methods=["GET", "POST"])
  10. @login_required
  11. def index():
  12. # User generates a new email
  13. if request.method == "POST":
  14. if request.form.get("form-name") == "trigger-email":
  15. gen_email_id = request.form.get("gen-email-id")
  16. gen_email = GenEmail.get(gen_email_id)
  17. LOG.d("trigger an email to %s", gen_email)
  18. email_utils.send(
  19. gen_email.email,
  20. "A Test Email",
  21. f"""
  22. Hi {current_user.name} ! <br><br>
  23. This is a test email to make sure you receive email sent at {gen_email.email} <br><br>
  24. If you have any question, feel free to reply to this email :) <br><br>
  25. Have a nice day <br><br>
  26. SimpleLogin team.
  27. """,
  28. )
  29. flash(
  30. f"An email sent to {gen_email.email} is on its way, please check your inbox/spam folder",
  31. "success",
  32. )
  33. elif request.form.get("form-name") == "create-new-email":
  34. can_create_new_email = current_user.can_create_new_email()
  35. if can_create_new_email:
  36. gen_email = GenEmail.create_new_gen_email(user_id=current_user.id)
  37. db.session.commit()
  38. LOG.d("generate new email %s for user %s", gen_email, current_user)
  39. flash(f"Email {gen_email.email} has been created", "success")
  40. else:
  41. flash(f"You need to upgrade your plan to create new email.", "warning")
  42. elif request.form.get("form-name") == "switch-email-forwarding":
  43. gen_email_id = request.form.get("gen-email-id")
  44. gen_email: GenEmail = GenEmail.get(gen_email_id)
  45. LOG.d("switch email forwarding for %s", gen_email)
  46. gen_email.enabled = not gen_email.enabled
  47. if gen_email.enabled:
  48. flash(
  49. f"The email forwarding for {gen_email.email} has been enabled",
  50. "success",
  51. )
  52. else:
  53. flash(
  54. f"The email forwarding for {gen_email.email} has been disabled",
  55. "warning",
  56. )
  57. db.session.commit()
  58. return redirect(url_for("dashboard.index"))
  59. client_users = (
  60. ClientUser.filter_by(user_id=current_user.id)
  61. .options(joinedload(ClientUser.client))
  62. .options(joinedload(ClientUser.gen_email))
  63. .all()
  64. )
  65. sorted(client_users, key=lambda cu: cu.client.name)
  66. gen_emails = (
  67. GenEmail.filter_by(user_id=current_user.id)
  68. .order_by(GenEmail.email)
  69. .options(joinedload(GenEmail.client_users))
  70. .all()
  71. )
  72. return render_template(
  73. "dashboard/index.html", client_users=client_users, gen_emails=gen_emails
  74. )