From ebadcfb175ffab027f00ed7540bd2322225c5e91 Mon Sep 17 00:00:00 2001 From: Son NK Date: Wed, 5 Feb 2020 15:45:29 +0700 Subject: [PATCH 1/5] add GenEmail.note column --- app/models.py | 6 ++-- .../versions/2020_020515_b9f849432543_.py | 29 +++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 migrations/versions/2020_020515_b9f849432543_.py diff --git a/app/models.py b/app/models.py index bc36729e..53efb870 100644 --- a/app/models.py +++ b/app/models.py @@ -476,10 +476,12 @@ class GenEmail(db.Model, ModelMixin): db.ForeignKey("directory.id", ondelete="cascade"), nullable=True ) + note = db.Column(db.Text, default=None, nullable=True) + user = db.relationship(User) @classmethod - def create_new(cls, user_id, prefix): + def create_new(cls, user_id, prefix, note=None): if not prefix: raise Exception("alias prefix cannot be empty") @@ -491,7 +493,7 @@ class GenEmail(db.Model, ModelMixin): if not cls.get_by(email=email): break - return GenEmail.create(user_id=user_id, email=email) + return GenEmail.create(user_id=user_id, email=email, note=note) @classmethod def create_new_random( diff --git a/migrations/versions/2020_020515_b9f849432543_.py b/migrations/versions/2020_020515_b9f849432543_.py new file mode 100644 index 00000000..735a7550 --- /dev/null +++ b/migrations/versions/2020_020515_b9f849432543_.py @@ -0,0 +1,29 @@ +"""empty message + +Revision ID: b9f849432543 +Revises: 9c976df9b9c4 +Create Date: 2020-02-05 15:16:16.912369 + +""" +import sqlalchemy_utils +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'b9f849432543' +down_revision = '9c976df9b9c4' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('gen_email', sa.Column('note', sa.Text(), nullable=True)) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column('gen_email', 'note') + # ### end Alembic commands ### From d8229102a6be5b868d5c7a48bcfdec31126bc4c6 Mon Sep 17 00:00:00 2001 From: Son NK Date: Wed, 5 Feb 2020 17:35:38 +0700 Subject: [PATCH 2/5] add test for custom alias page --- tests/dashboard/test_custom_alias.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 tests/dashboard/test_custom_alias.py diff --git a/tests/dashboard/test_custom_alias.py b/tests/dashboard/test_custom_alias.py new file mode 100644 index 00000000..cb3ad09f --- /dev/null +++ b/tests/dashboard/test_custom_alias.py @@ -0,0 +1,22 @@ +from flask import url_for + +from app.config import EMAIL_DOMAIN +from app.extensions import db +from app.utils import random_word +from tests.utils import login + + +def test_add_alias_success(flask_client): + login(flask_client) + db.session.commit() + + word = random_word() + + r = flask_client.post( + url_for("dashboard.custom_alias"), + data={"prefix": "prefix", "suffix": f".{word}@{EMAIL_DOMAIN}"}, + follow_redirects=True, + ) + + assert r.status_code == 200 + assert f"Alias prefix.{word}@{EMAIL_DOMAIN} has been created" in str(r.data) From 145bed893f084b8a50342c3283b18e47c0085e39 Mon Sep 17 00:00:00 2001 From: Son NK Date: Wed, 5 Feb 2020 17:36:06 +0700 Subject: [PATCH 3/5] user can set note when creating custom alias --- .../templates/dashboard/custom_alias.html | 16 ++++++++++++++-- app/dashboard/views/custom_alias.py | 5 ++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/app/dashboard/templates/dashboard/custom_alias.html b/app/dashboard/templates/dashboard/custom_alias.html index d4bae6fb..984cdd0e 100644 --- a/app/dashboard/templates/dashboard/custom_alias.html +++ b/app/dashboard/templates/dashboard/custom_alias.html @@ -48,8 +48,20 @@ -
- +
+
+ +
+
+ + +
+
+ +
diff --git a/app/dashboard/views/custom_alias.py b/app/dashboard/views/custom_alias.py index f1b5d7f2..6b7cb0d2 100644 --- a/app/dashboard/views/custom_alias.py +++ b/app/dashboard/views/custom_alias.py @@ -46,6 +46,7 @@ def custom_alias(): if request.method == "POST": alias_prefix = request.form.get("prefix") alias_suffix = request.form.get("suffix") + alias_note = request.form.get("note") if verify_prefix_suffix( current_user, alias_prefix, alias_suffix, user_custom_domains @@ -61,7 +62,9 @@ def custom_alias(): "warning", ) else: - gen_email = GenEmail.create(user_id=current_user.id, email=full_alias) + gen_email = GenEmail.create( + user_id=current_user.id, email=full_alias, note=alias_note + ) # get the custom_domain_id if alias is created with a custom domain alias_domain = get_email_domain_part(full_alias) From 11b04d5e4a7870afae16251f92641c3a34060359 Mon Sep 17 00:00:00 2001 From: Son NK Date: Wed, 5 Feb 2020 17:38:02 +0700 Subject: [PATCH 4/5] user can update note in alias list page --- app/dashboard/templates/dashboard/index.html | 22 ++++++++++++++++++++ app/dashboard/views/index.py | 15 +++++++++++++ 2 files changed, 37 insertions(+) diff --git a/app/dashboard/templates/dashboard/index.html b/app/dashboard/templates/dashboard/index.html index 57842753..2a266561 100644 --- a/app/dashboard/templates/dashboard/index.html +++ b/app/dashboard/templates/dashboard/index.html @@ -151,6 +151,28 @@ +
+
+ +
+ +
+ +
+ + + + +
+
+
+
{% if gen_email.enabled %} diff --git a/app/dashboard/views/index.py b/app/dashboard/views/index.py index e16a6909..8d4ba3fe 100644 --- a/app/dashboard/views/index.py +++ b/app/dashboard/views/index.py @@ -124,6 +124,21 @@ def index(): LOG.error("alias %s has been added before to DeletedAlias", email) db.session.rollback() + elif request.form.get("form-name") == "set-note": + gen_email_id = request.form.get("gen-email-id") + gen_email: GenEmail = GenEmail.get(gen_email_id) + note = request.form.get("note") + + gen_email.note = note + db.session.commit() + + flash(f"Update note for alias {gen_email.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 = ( From 98487aba10fc52a70a7eb19348af9dc98c4e9874 Mon Sep 17 00:00:00 2001 From: Son NK Date: Wed, 5 Feb 2020 17:38:23 +0700 Subject: [PATCH 5/5] take into account "note" in search --- app/dashboard/views/index.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/dashboard/views/index.py b/app/dashboard/views/index.py index 8d4ba3fe..06b1ab98 100644 --- a/app/dashboard/views/index.py +++ b/app/dashboard/views/index.py @@ -1,5 +1,6 @@ from flask import render_template, request, redirect, url_for, flash from flask_login import login_required, current_user +from sqlalchemy import or_ from sqlalchemy.exc import IntegrityError from sqlalchemy.orm import joinedload @@ -179,7 +180,7 @@ def get_alias_info( ) if query: - q = q.filter(GenEmail.email.contains(query)) + q = q.filter(or_(GenEmail.email.contains(query), GenEmail.note.contains(query))) # pagination activated if page_id is not None: