From c2d772ded18dd40e3aa61fe4a9158b480d093a57 Mon Sep 17 00:00:00 2001 From: Son NK Date: Tue, 17 Mar 2020 11:51:40 +0100 Subject: [PATCH] rename GenEmail -> Alias, gen_email to alias whenever possible --- app/api/views/alias.py | 70 +++++----- app/api/views/alias_options.py | 18 +-- app/api/views/new_custom_alias.py | 10 +- app/api/views/new_random_alias.py | 8 +- app/api/views/user_info.py | 2 +- .../dashboard/alias_contact_manager.html | 6 +- app/dashboard/templates/dashboard/index.html | 36 ++--- .../templates/dashboard/refused_email.html | 6 +- app/dashboard/views/alias_contact_manager.py | 21 ++- app/dashboard/views/alias_log.py | 20 +-- app/dashboard/views/custom_alias.py | 14 +- app/dashboard/views/domain_detail.py | 4 +- app/dashboard/views/index.py | 113 +++++++-------- app/dashboard/views/mailbox_detail.py | 4 +- app/dashboard/views/setting.py | 8 +- app/dashboard/views/unsubscribe.py | 20 +-- app/email_utils.py | 12 +- app/models.py | 48 ++++--- app/oauth/views/authorize.py | 20 +-- cron.py | 24 ++-- email_handler.py | 130 +++++++++--------- server.py | 6 +- .../automatic-disable-alias.html | 4 +- .../transactional/automatic-disable-alias.txt | 4 +- .../emails/transactional/bounced-email.html | 4 +- .../emails/transactional/bounced-email.txt | 2 +- tests/api/test_alias.py | 38 ++--- tests/api/test_alias_options.py | 6 +- tests/api/test_new_custom_alias.py | 8 +- tests/api/test_new_random_alias.py | 6 +- tests/api/test_user_info.py | 2 +- tests/test_models.py | 14 +- 32 files changed, 333 insertions(+), 355 deletions(-) diff --git a/app/api/views/alias.py b/app/api/views/alias.py index 12b479c5..aa1ef7b8 100644 --- a/app/api/views/alias.py +++ b/app/api/views/alias.py @@ -13,7 +13,7 @@ from app.dashboard.views.index import get_alias_info, AliasInfo from app.extensions import db from app.log import LOG from app.models import EmailLog -from app.models import GenEmail, Contact +from app.models import Alias, Contact from app.utils import random_string @@ -43,23 +43,23 @@ def get_aliases(): except (ValueError, TypeError): return jsonify(error="page_id must be provided in request query"), 400 - aliases: [AliasInfo] = get_alias_info(user, page_id=page_id) + alias_infos: [AliasInfo] = get_alias_info(user, page_id=page_id) return ( jsonify( aliases=[ { - "id": alias.id, - "email": alias.gen_email.email, - "creation_date": alias.gen_email.created_at.format(), - "creation_timestamp": alias.gen_email.created_at.timestamp, - "nb_forward": alias.nb_forward, - "nb_block": alias.nb_blocked, - "nb_reply": alias.nb_reply, - "enabled": alias.gen_email.enabled, - "note": alias.note, + "id": alias_info.id, + "email": alias_info.alias.email, + "creation_date": alias_info.alias.created_at.format(), + "creation_timestamp": alias_info.alias.created_at.timestamp, + "nb_forward": alias_info.nb_forward, + "nb_block": alias_info.nb_blocked, + "nb_reply": alias_info.nb_reply, + "enabled": alias_info.alias.enabled, + "note": alias_info.note, } - for alias in aliases + for alias_info in alias_infos ] ), 200, @@ -79,12 +79,12 @@ def delete_alias(alias_id): """ user = g.user - gen_email = GenEmail.get(alias_id) + alias = Alias.get(alias_id) - if gen_email.user_id != user.id: + if alias.user_id != user.id: return jsonify(error="Forbidden"), 403 - GenEmail.delete(alias_id) + Alias.delete(alias_id) db.session.commit() return jsonify(deleted=True), 200 @@ -105,15 +105,15 @@ def toggle_alias(alias_id): """ user = g.user - gen_email: GenEmail = GenEmail.get(alias_id) + alias: Alias = Alias.get(alias_id) - if gen_email.user_id != user.id: + if alias.user_id != user.id: return jsonify(error="Forbidden"), 403 - gen_email.enabled = not gen_email.enabled + alias.enabled = not alias.enabled db.session.commit() - return jsonify(enabled=gen_email.enabled), 200 + return jsonify(enabled=alias.enabled), 200 @api_bp.route("/aliases//activities") @@ -138,12 +138,12 @@ def get_alias_activities(alias_id): except (ValueError, TypeError): return jsonify(error="page_id must be provided in request query"), 400 - gen_email: GenEmail = GenEmail.get(alias_id) + alias: Alias = Alias.get(alias_id) - if gen_email.user_id != user.id: + if alias.user_id != user.id: return jsonify(error="Forbidden"), 403 - alias_logs = get_alias_log(gen_email, page_id) + alias_logs = get_alias_log(alias, page_id) activities = [] for alias_log in alias_logs: @@ -187,13 +187,13 @@ def update_alias(alias_id): return jsonify(error="request body cannot be empty"), 400 user = g.user - gen_email: GenEmail = GenEmail.get(alias_id) + alias: Alias = Alias.get(alias_id) - if gen_email.user_id != user.id: + if alias.user_id != user.id: return jsonify(error="Forbidden"), 403 new_note = data.get("note") - gen_email.note = new_note + alias.note = new_note db.session.commit() return jsonify(note=new_note), 200 @@ -218,9 +218,9 @@ def serialize_contact(fe: Contact) -> dict: return res -def get_alias_contacts(gen_email, page_id: int) -> [dict]: +def get_alias_contacts(alias, page_id: int) -> [dict]: q = ( - Contact.query.filter_by(gen_email_id=gen_email.id) + Contact.query.filter_by(gen_email_id=alias.id) .order_by(Contact.id.desc()) .limit(PAGE_LIMIT) .offset(page_id * PAGE_LIMIT) @@ -257,12 +257,12 @@ def get_alias_contacts_route(alias_id): except (ValueError, TypeError): return jsonify(error="page_id must be provided in request query"), 400 - gen_email: GenEmail = GenEmail.get(alias_id) + alias: Alias = Alias.get(alias_id) - if gen_email.user_id != user.id: + if alias.user_id != user.id: return jsonify(error="Forbidden"), 403 - contacts = get_alias_contacts(gen_email, page_id) + contacts = get_alias_contacts(alias, page_id) return jsonify(contacts=contacts), 200 @@ -287,9 +287,9 @@ def create_contact_route(alias_id): return jsonify(error="request body cannot be empty"), 400 user = g.user - gen_email: GenEmail = GenEmail.get(alias_id) + alias: Alias = Alias.get(alias_id) - if gen_email.user_id != user.id: + if alias.user_id != user.id: return jsonify(error="Forbidden"), 403 contact_email = data.get("contact") @@ -305,17 +305,17 @@ def create_contact_route(alias_id): _, website_email = parseaddr(contact_email) # already been added - if Contact.get_by(gen_email_id=gen_email.id, website_email=website_email): + if Contact.get_by(gen_email_id=alias.id, website_email=website_email): return jsonify(error="Contact already added"), 409 contact = Contact.create( - gen_email_id=gen_email.id, + gen_email_id=alias.id, website_email=website_email, website_from=contact_email, reply_email=reply_email, ) - LOG.d("create reverse-alias for %s %s", contact_email, gen_email) + LOG.d("create reverse-alias for %s %s", contact_email, alias) db.session.commit() return jsonify(**serialize_contact(contact)), 201 diff --git a/app/api/views/alias_options.py b/app/api/views/alias_options.py index 4e47646e..4cad82a8 100644 --- a/app/api/views/alias_options.py +++ b/app/api/views/alias_options.py @@ -6,7 +6,7 @@ from app.api.base import api_bp, verify_api_key from app.config import ALIAS_DOMAINS, DISABLE_ALIAS_SUFFIX from app.extensions import db from app.log import LOG -from app.models import AliasUsedOn, GenEmail, User +from app.models import AliasUsedOn, Alias, User from app.utils import convert_to_id, random_word @@ -31,7 +31,7 @@ def options(): hostname = request.args.get("hostname") ret = { - "existing": [ge.email for ge in GenEmail.query.filter_by(user_id=user.id)], + "existing": [ge.email for ge in Alias.query.filter_by(user_id=user.id)], "can_create_custom": user.can_create_new_alias(), } @@ -39,10 +39,10 @@ def options(): if hostname: # put the latest used alias first q = ( - db.session.query(AliasUsedOn, GenEmail, User) + db.session.query(AliasUsedOn, Alias, User) .filter( - AliasUsedOn.gen_email_id == GenEmail.id, - GenEmail.user_id == user.id, + AliasUsedOn.gen_email_id == Alias.id, + Alias.user_id == user.id, AliasUsedOn.hostname == hostname, ) .order_by(desc(AliasUsedOn.created_at)) @@ -111,7 +111,7 @@ def options_v2(): ret = { "existing": [ - ge.email for ge in GenEmail.query.filter_by(user_id=user.id, enabled=True) + ge.email for ge in Alias.query.filter_by(user_id=user.id, enabled=True) ], "can_create": user.can_create_new_alias(), "suffixes": [], @@ -122,10 +122,10 @@ def options_v2(): if hostname: # put the latest used alias first q = ( - db.session.query(AliasUsedOn, GenEmail, User) + db.session.query(AliasUsedOn, Alias, User) .filter( - AliasUsedOn.gen_email_id == GenEmail.id, - GenEmail.user_id == user.id, + AliasUsedOn.gen_email_id == Alias.id, + Alias.user_id == user.id, AliasUsedOn.hostname == hostname, ) .order_by(desc(AliasUsedOn.created_at)) diff --git a/app/api/views/new_custom_alias.py b/app/api/views/new_custom_alias.py index e4c66ebe..a97a728a 100644 --- a/app/api/views/new_custom_alias.py +++ b/app/api/views/new_custom_alias.py @@ -7,7 +7,7 @@ from app.config import MAX_NB_EMAIL_FREE_PLAN, ALIAS_DOMAINS from app.dashboard.views.custom_alias import verify_prefix_suffix from app.extensions import db from app.log import LOG -from app.models import GenEmail, AliasUsedOn, User, CustomDomain +from app.models import Alias, AliasUsedOn, User, CustomDomain from app.utils import convert_to_id @@ -54,11 +54,11 @@ def new_custom_alias(): return jsonify(error="wrong alias prefix or suffix"), 400 full_alias = alias_prefix + alias_suffix - if GenEmail.get_by(email=full_alias): + if Alias.get_by(email=full_alias): LOG.d("full alias already used %s", full_alias) return jsonify(error=f"alias {full_alias} already exists"), 409 - gen_email = GenEmail.create( + alias = Alias.create( user_id=user.id, email=full_alias, mailbox_id=user.default_mailbox_id, note=note ) @@ -67,12 +67,12 @@ def new_custom_alias(): if alias_domain not in ALIAS_DOMAINS: domain = CustomDomain.get_by(domain=alias_domain) LOG.d("set alias %s to domain %s", full_alias, domain) - gen_email.custom_domain_id = domain.id + alias.custom_domain_id = domain.id db.session.commit() if hostname: - AliasUsedOn.create(gen_email_id=gen_email.id, hostname=hostname) + AliasUsedOn.create(gen_email_id=alias.id, hostname=hostname) db.session.commit() return jsonify(alias=full_alias), 201 diff --git a/app/api/views/new_random_alias.py b/app/api/views/new_random_alias.py index 978d9756..6d773dae 100644 --- a/app/api/views/new_random_alias.py +++ b/app/api/views/new_random_alias.py @@ -6,7 +6,7 @@ from app.api.base import api_bp, verify_api_key from app.config import MAX_NB_EMAIL_FREE_PLAN from app.extensions import db from app.log import LOG -from app.models import GenEmail, AliasUsedOn, AliasGeneratorEnum +from app.models import Alias, AliasUsedOn, AliasGeneratorEnum @api_bp.route("/alias/random/new", methods=["POST"]) @@ -47,12 +47,12 @@ def new_random_alias(): else: return jsonify(error=f"{mode} must be either word or alias"), 400 - gen_email = GenEmail.create_new_random(user=user, scheme=scheme, note=note) + alias = Alias.create_new_random(user=user, scheme=scheme, note=note) db.session.commit() hostname = request.args.get("hostname") if hostname: - AliasUsedOn.create(gen_email_id=gen_email.id, hostname=hostname) + AliasUsedOn.create(gen_email_id=alias.id, hostname=hostname) db.session.commit() - return jsonify(alias=gen_email.email), 201 + return jsonify(alias=alias.email), 201 diff --git a/app/api/views/user_info.py b/app/api/views/user_info.py index e8a2dd94..2b25745f 100644 --- a/app/api/views/user_info.py +++ b/app/api/views/user_info.py @@ -6,7 +6,7 @@ from app.api.base import api_bp, verify_api_key from app.config import EMAIL_DOMAIN from app.extensions import db from app.log import LOG -from app.models import AliasUsedOn, GenEmail, User +from app.models import AliasUsedOn, Alias, User from app.utils import convert_to_id, random_word diff --git a/app/dashboard/templates/dashboard/alias_contact_manager.html b/app/dashboard/templates/dashboard/alias_contact_manager.html index a868b6ce..7c8a9f25 100644 --- a/app/dashboard/templates/dashboard/alias_contact_manager.html +++ b/app/dashboard/templates/dashboard/alias_contact_manager.html @@ -9,7 +9,7 @@ {% block default_content %} @@ -26,8 +26,8 @@ This might sound complicated but trust us, only the first time is a bit awkward.

- {% if gen_email.mailbox_id %} - Make sure you send the email from the mailbox {{ gen_email.mailbox.email }}. + {% if alias.mailbox_id %} + Make sure you send the email from the mailbox {{ alias.mailbox.email }}. This is because only the mailbox that owns the alias can send emails from it. {% else %} Make sure you send the email from your personal email address ({{ current_user.email }}). diff --git a/app/dashboard/templates/dashboard/index.html b/app/dashboard/templates/dashboard/index.html index da483ca3..863fde16 100644 --- a/app/dashboard/templates/dashboard/index.html +++ b/app/dashboard/templates/dashboard/index.html @@ -72,7 +72,7 @@

{% for alias_info in aliases %} - {% set gen_email = alias_info.gen_email %} + {% set alias = alias_info.alias %}
@@ -86,14 +86,14 @@ use it whenever possible, for example when signing up for a newsletter or creating a new account on a suspicious website 😎" data-step="2" {% endif %} - {% if gen_email.enabled %} + {% if alias.enabled %} data-toggle="tooltip" title="Copy to clipboard" - data-clipboard-text="{{ gen_email.email }}" + data-clipboard-text="{{ alias.email }}" {% endif %} > - {{ gen_email.email }} - {% if gen_email.enabled %} + {{ alias.email }} + {% if alias.enabled %} Copy @@ -103,10 +103,10 @@
- + @@ -135,7 +135,7 @@

- Created {{ gen_email.created_at | dt }} + Created {{ alias.created_at | dt }} {% if alias_info.highlight %} - New {% endif %} @@ -145,7 +145,7 @@ {{ alias_info.nb_forward }} forwards, {{ alias_info.nb_blocked }} blocks, {{ alias_info.nb_reply }} replies - See All Activity  â†’ @@ -167,7 +167,7 @@

- +
- +