From e4bb85ac87d011fcaf5ea417c51b0ed96cc7fb5c Mon Sep 17 00:00:00 2001 From: Son NK Date: Sun, 23 Feb 2020 13:58:09 +0700 Subject: [PATCH] Full-mailbox User can change alias mailbox --- app/dashboard/templates/dashboard/index.html | 44 ++++++++++++++++---- app/dashboard/views/index.py | 28 +++++++++++++ app/models.py | 13 +++++- server.py | 15 +++---- 4 files changed, 82 insertions(+), 18 deletions(-) diff --git a/app/dashboard/templates/dashboard/index.html b/app/dashboard/templates/dashboard/index.html index 8a185921..4396580f 100644 --- a/app/dashboard/templates/dashboard/index.html +++ b/app/dashboard/templates/dashboard/index.html @@ -134,12 +134,6 @@
- {% if alias_info.mailbox != None %} -
- Owned by {{ alias_info.mailbox.email }} mailbox -
- {% endif %} -

Created {{ gen_email.created_at | dt }} {% if alias_info.highlight %} @@ -157,10 +151,42 @@ + {% if current_user.full_mailbox and mailboxes|length > 1 %} +

+
Current mailbox
+
+
+ +
+ +
+ + + + +
+ +
+
+ {% elif alias_info.mailbox != None %} +
+ Owned by {{ alias_info.mailbox.email }} mailbox +
+ {% endif %} + +
-
+
-
+
-
diff --git a/app/dashboard/views/index.py b/app/dashboard/views/index.py index f8a1b076..f55ad639 100644 --- a/app/dashboard/views/index.py +++ b/app/dashboard/views/index.py @@ -142,6 +142,31 @@ def index(): ) ) + elif request.form.get("form-name") == "set-mailbox": + gen_email_id = request.form.get("gen-email-id") + gen_email: GenEmail = GenEmail.get(gen_email_id) + mailbox_email = request.form.get("mailbox") + + mailbox = Mailbox.get_by(email=mailbox_email) + if not mailbox or mailbox.user_id != current_user.id: + flash("Something went wrong, please retry", "warning") + else: + gen_email.mailbox_id = mailbox.id + db.session.commit() + LOG.d("Set alias %s mailbox to %s", gen_email, mailbox) + + flash( + f"Update mailbox for {gen_email.email} to {mailbox_email}", + "success", + ) + return redirect( + url_for( + "dashboard.index", + highlight_gen_email_id=gen_email.id, + query=query, + ) + ) + return redirect(url_for("dashboard.index", query=query)) client_users = ( @@ -153,6 +178,8 @@ def index(): sorted(client_users, key=lambda cu: cu.client.name) + mailboxes = current_user.mailboxes() + return render_template( "dashboard/index.html", client_users=client_users, @@ -160,6 +187,7 @@ def index(): highlight_gen_email_id=highlight_gen_email_id, query=query, AliasGeneratorEnum=AliasGeneratorEnum, + mailboxes=mailboxes, ) diff --git a/app/models.py b/app/models.py index 1de69fa1..62b380df 100644 --- a/app/models.py +++ b/app/models.py @@ -162,6 +162,13 @@ class User(db.Model, ModelMixin, UserMixin): GenEmail.create_new(user.id, prefix="my-first-alias") db.session.flush() + # todo: uncomment when all existing users are full_mailbox + # to run just after migrating all existing user to full mailbox + # so new users are automatically full-mailbox + # Mailbox.create(user_id=user.id, email=user.email, verified=True) + # user.full_mailbox = True + # db.session.flush() + # Schedule onboarding emails Job.create( name=JOB_ONBOARDING_1, @@ -511,7 +518,7 @@ class GenEmail(db.Model, ModelMixin): mailbox = db.relationship("Mailbox") @classmethod - def create_new(cls, user_id, prefix, note=None): + def create_new(cls, user_id, prefix, note=None, mailbox_id=None): if not prefix: raise Exception("alias prefix cannot be empty") @@ -523,7 +530,9 @@ class GenEmail(db.Model, ModelMixin): if not cls.get_by(email=email): break - return GenEmail.create(user_id=user_id, email=email, note=note) + return GenEmail.create( + user_id=user_id, email=email, note=note, mailbox_id=mailbox_id + ) @classmethod def create_new_random( diff --git a/server.py b/server.py index 02790473..9098ec5f 100644 --- a/server.py +++ b/server.py @@ -132,6 +132,7 @@ def fake_data(): is_admin=True, otp_secret="base32secret3232", can_use_multiple_mailbox=True, + full_mailbox=True, ) db.session.commit() @@ -156,9 +157,13 @@ def fake_data(): api_key = ApiKey.create(user_id=user.id, name="Firefox") api_key.code = "codeFF" - GenEmail.create_new(user.id, "e1@") - GenEmail.create_new(user.id, "e2@") - GenEmail.create_new(user.id, "e3@") + m1 = Mailbox.create(user_id=user.id, email="m1@cd.ef", verified=True) + m2 = Mailbox.create(user_id=user.id, email="m2@zt.com", verified=False) + m3 = Mailbox.create(user_id=user.id, email="m3@cd.ef", verified=True) + db.session.commit() + + GenEmail.create_new(user.id, "e1@", mailbox_id=m1.id) + GenEmail.create_new(user.id, "e2@", mailbox_id=m3.id) CustomDomain.create(user_id=user.id, domain="ab.cd", verified=True) CustomDomain.create( @@ -185,10 +190,6 @@ def fake_data(): client2.published = True db.session.commit() - Mailbox.create(user_id=user.id, email="ab@cd.ef", verified=True) - Mailbox.create(user_id=user.id, email="xy@zt.com", verified=False) - db.session.commit() - DeletedAlias.create(user_id=user.id, email="d1@ab.cd") DeletedAlias.create(user_id=user.id, email="d2@ab.cd") db.session.commit()